Skip to content

Commit

Permalink
Merge pull request #97 from phug-php/feature/issue-88-precedence-option
Browse files Browse the repository at this point in the history
Add attribute_precedence option
  • Loading branch information
kylekatarnls committed Aug 15, 2023
2 parents 9a03e8e + 9b9dcc0 commit d0a9793
Show file tree
Hide file tree
Showing 144 changed files with 1,165 additions and 189 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1']
php: ['5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3']
setup: ['lowest', 'stable', 'next']

name: PHP ${{ matrix.php }} - ${{ matrix.setup }}
Expand Down
1 change: 0 additions & 1 deletion src/Phug/Ast/Ast/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@ public function findChildren(callable $callback, $depth = null, $level = null)
$level = $level ?: 0;

foreach ($this->children as $child) {

/** @var NodeInterface $child */
if ($child->is($callback)) {
yield $child;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Phug\Parser\Node\AssignmentNode;
use Phug\Parser\Node\AttributeNode;
use Phug\Parser\NodeInterface;
use Phug\Util\OrderableInterface;
use SplObjectStorage;

class AssignmentNodeCompiler extends AbstractNodeCompiler
Expand All @@ -25,9 +26,16 @@ public function compileNode(NodeInterface $node, ElementInterface $parent = null
*/
$name = $node->getName();
$attributes = new SplObjectStorage();

foreach ($node->getAttributes() as $attribute) {
/* @var AttributeNode $attribute */
$attributes->attach($this->getCompiler()->compileNode($attribute, $parent));
$attributeElement = $this->getCompiler()->compileNode($attribute, $parent);

if ($attribute instanceof OrderableInterface && $attributeElement instanceof OrderableInterface) {
$attributeElement->setOrder($attribute->getOrder());
}

$attributes->attach($attributeElement);
}

return new AssignmentElement($name, $attributes, null, $node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function compileNode(NodeInterface $node, ElementInterface $parent = null
$name = $this->compileName($node);
$value = $this->compileValue($node);
$attribute = new AttributeElement($name, $value, $node);
$attribute->setOrder($node->getOrder());
$attribute->setIsVariadic($node->isVariadic());

return $attribute;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Phug\Parser\Node\ElementNode;
use Phug\Parser\Node\ExpressionNode;
use Phug\Parser\NodeInterface;
use Phug\Util\OrderableInterface;
use SplObjectStorage;

class ElementNodeCompiler extends AbstractNodeCompiler
Expand All @@ -35,6 +36,11 @@ public function compileNode(NodeInterface $node, ElementInterface $parent = null
$markup = new MarkupElement($name, $node->isAutoClosed(), $attributes, $node);
foreach ($node->getAssignments() as $assignment) {
$compiledAssignment = $compiler->compileNode($assignment, $parent);

if ($compiledAssignment instanceof OrderableInterface && $assignment instanceof OrderableInterface) {
$compiledAssignment->setOrder($assignment->getOrder());
}

if ($compiledAssignment instanceof AssignmentElement) {
$markup->addAssignment($compiledAssignment);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Phug/Compiler/Compiler/NodeCompilerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
interface NodeCompilerInterface
{
/**
* @param $nodeList
* @param $nodeList
* @param ElementInterface $parent
*
* @return array
Expand Down
13 changes: 8 additions & 5 deletions src/Phug/Formatter/Formatter/AbstractFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ protected function getDebugInfo($element)
/**
* @param string|ElementInterface $element
* @param bool $noDebug
* @param $element
* @param $element
*
* @return string
*/
Expand Down Expand Up @@ -460,13 +460,16 @@ protected function formatDynamicValue($formattedName, $value)
return 'null';
}

if ($value instanceof ExpressionElement &&
in_array(($code = strtolower($value->getValue())), ['true', 'false', 'null', 'undefined'])
) {
return $code;
if ($value instanceof ExpressionElement) {
$code = strtolower($value->getValue());

if (in_array($code, ['true', 'false', 'null', 'undefined'])) {
return $code;
}
}

$code = $this->formatAssignmentValue($value);

if ($value instanceof ExpressionElement && $value->isEscaped()) {
return $this->exportHelper('array_escape', [$formattedName, $code]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Phug\Formatter;

use Phug\Formatter\Element\AssignmentElement;
use Phug\Util\AttributesOrderInterface;

interface AssignmentContainerInterface extends ElementInterface
interface AssignmentContainerInterface extends ElementInterface, AttributesOrderInterface
{
public function getName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@

use Phug\Formatter\AbstractElement;
use Phug\Formatter\AssignmentContainerInterface;
use Phug\Util\Partial\AttributesOrderTrait;
use SplObjectStorage;

abstract class AbstractAssignmentContainerElement extends AbstractElement implements AssignmentContainerInterface
{
use AttributesOrderTrait;

/**
* @var SplObjectStorage<AssignmentElement>
*/
private $assignments;

/**
Expand All @@ -19,6 +25,10 @@ abstract class AbstractAssignmentContainerElement extends AbstractElement implem
*/
public function addAssignment(AssignmentElement $element)
{
if ($element->getOrder() === null) {
$element->setOrder($this->getNextAttributeIndex());
}

$element->setContainer($this);
$this->getAssignments()->attach($element);

Expand All @@ -42,7 +52,7 @@ public function removedAssignment(AssignmentElement $element)
/**
* Return markup assignments list.
*
* @return SplObjectStorage[AssignmentElement]
* @return SplObjectStorage<AssignmentElement>
*/
public function getAssignments()
{
Expand Down
12 changes: 8 additions & 4 deletions src/Phug/Formatter/Formatter/Element/AssignmentElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,31 @@
use Phug\Formatter\AssignmentContainerInterface;
use Phug\Parser\NodeInterface as ParserNode;
use Phug\Util\AttributesInterface;
use Phug\Util\OrderableInterface;
use Phug\Util\Partial\AttributeTrait;
use Phug\Util\Partial\NameTrait;
use Phug\Util\Partial\OrderTrait;
use SplObjectStorage;

class AssignmentElement extends AbstractElement implements AttributesInterface
class AssignmentElement extends AbstractElement implements AttributesInterface, OrderableInterface
{
use AttributeTrait;
use NameTrait;
use OrderTrait;

/**
* AssignmentElement constructor.
*
* @param string $name
* @param \SplObjectStorage|null $attributes
* @param AssignmentContainerInterface|null $markup
* @param SplObjectStorage|null $attributes
* @param AssignmentContainerInterface|null $container
* @param ParserNode|null $originNode
* @param NodeInterface|null $parent
* @param array|null $children
*/
public function __construct(
$name,
\SplObjectStorage $attributes = null,
SplObjectStorage $attributes = null,
AssignmentContainerInterface $container = null,
ParserNode $originNode = null,
NodeInterface $parent = null,
Expand Down
5 changes: 4 additions & 1 deletion src/Phug/Formatter/Formatter/Element/AttributeElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

use Phug\Ast\NodeInterface;
use Phug\Parser\NodeInterface as ParserNode;
use Phug\Util\OrderableInterface;
use Phug\Util\Partial\NameTrait;
use Phug\Util\Partial\OrderTrait;
use Phug\Util\Partial\VariadicTrait;

class AttributeElement extends AbstractValueElement
class AttributeElement extends AbstractValueElement implements OrderableInterface
{
use NameTrait;
use OrderTrait;
use VariadicTrait;

/**
Expand Down
15 changes: 8 additions & 7 deletions src/Phug/Formatter/Formatter/Element/MarkupElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Phug\Util\AttributesInterface;
use Phug\Util\Partial\AttributeTrait;
use Phug\Util\Partial\NameTrait;
use SplObjectStorage;

class MarkupElement extends AbstractMarkupElement implements AttributesInterface
{
Expand All @@ -21,17 +22,17 @@ class MarkupElement extends AbstractMarkupElement implements AttributesInterface
/**
* MarkupElement constructor.
*
* @param string $name
* @param bool $autoClosed
* @param \SplObjectStorage|null $attributes
* @param ParserNode|null $originNode
* @param NodeInterface|null $parent
* @param array|null $children
* @param string $name
* @param bool $autoClosed
* @param SplObjectStorage|null $attributes
* @param ParserNode|null $originNode
* @param NodeInterface|null $parent
* @param array|null $children
*/
public function __construct(
$name,
$autoClosed = false,
\SplObjectStorage $attributes = null,
SplObjectStorage $attributes = null,
ParserNode $originNode = null,
NodeInterface $parent = null,
array $children = null
Expand Down
Loading

0 comments on commit d0a9793

Please sign in to comment.