Skip to content

Commit

Permalink
Add attribute_precedence option
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Aug 15, 2023
1 parent 9a03e8e commit 22db58a
Show file tree
Hide file tree
Showing 28 changed files with 678 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Phug\Parser\Node\AssignmentNode;
use Phug\Parser\Node\AttributeNode;
use Phug\Parser\NodeInterface;
use Phug\Util\AttributesStorage;
use Phug\Util\OrderableInterface;
use SplObjectStorage;

class AssignmentNodeCompiler extends AbstractNodeCompiler
Expand All @@ -25,9 +27,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 @@ -9,6 +9,7 @@
use Phug\Formatter\ElementInterface;
use Phug\Parser\Node\AttributeNode;
use Phug\Parser\NodeInterface;
use Phug\Util\OrderableInterface;

class AttributeNodeCompiler extends AbstractNodeCompiler
{
Expand Down Expand Up @@ -71,6 +72,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
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 22db58a

Please sign in to comment.