Skip to content

Commit

Permalink
Merge pull request #87 from phug-php/fix/issue-86-attributes-precedence
Browse files Browse the repository at this point in the history
Align markup vs &attributes precedence with pug-js
  • Loading branch information
kylekatarnls authored Nov 11, 2021
2 parents 1e19e5b + b40122a commit 523139f
Show file tree
Hide file tree
Showing 49 changed files with 254 additions and 125 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ jobs:
${{ runner.os }}-${{ matrix.setup }}-v2-php-${{ matrix.php }}-
- name: Code Climate Test Reporter Preparation
if: matrix.php == '7.4' && matrix.setup == 'stable'
run: |
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
Expand All @@ -56,7 +55,7 @@ jobs:
run: vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml

- name: Code Climate Test Reporter
if: ${{ matrix.php == '7.4' && matrix.setup == 'stable' && env.CC_TEST_REPORTER_ID != '' }}
if: ${{ env.CC_TEST_REPORTER_ID != '' }}
run: |
cp coverage.xml clover.xml
bash <(curl -s https://codecov.io/bash)
Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
"homepage": "http://phug-lang.com",
"authors": [
{
"name": "KyleKatarn",
"email": "[email protected]",
"name": "kylekatarnls",
"homepage": "http://github.com/kylekatarnls"
},
{
"name": "Torben Koehn",
"email": "[email protected]"
}
],
"support": {
Expand Down
7 changes: 7 additions & 0 deletions src/Phug/Ast/Ast/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use InvalidArgumentException;
use Phug\AstException;
use ReturnTypeWillChange;

/**
* Represents a node in a tree-like data structure.
Expand Down Expand Up @@ -418,6 +419,7 @@ public function findArray(callable $callback, $depth = null)
/**
* {@inheritdoc}
*/
#[ReturnTypeWillChange]
public function getIterator()
{
foreach ($this->children as $child) {
Expand All @@ -428,6 +430,7 @@ public function getIterator()
/**
* {@inheritdoc}
*/
#[ReturnTypeWillChange]
public function count()
{
return $this->getChildCount();
Expand All @@ -436,6 +439,7 @@ public function count()
/**
* {@inheritdoc}
*/
#[ReturnTypeWillChange]
public function offsetExists($offset)
{
return $this->hasChildAt($offset);
Expand All @@ -444,6 +448,7 @@ public function offsetExists($offset)
/**
* {@inheritdoc}
*/
#[ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->getChildAt($offset);
Expand All @@ -452,6 +457,7 @@ public function offsetGet($offset)
/**
* {@inheritdoc}
*/
#[ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
if (!($value instanceof NodeInterface)) {
Expand All @@ -475,6 +481,7 @@ public function offsetSet($offset, $value)
/**
* {@inheritdoc}
*/
#[ReturnTypeWillChange]
public function offsetUnset($offset)
{
$this->removeChildAt($offset);
Expand Down
3 changes: 1 addition & 2 deletions src/Phug/Ast/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"email": "[email protected]"
},
{
"name": "KyleKatarn",
"email": "[email protected]",
"name": "kylekatarnls",
"homepage": "http://github.com/kylekatarnls"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ImportNodeCompiler extends AbstractNodeCompiler
protected function isPugImport($path)
{
$compiler = $this->getCompiler();
$extension = pathinfo($path, PATHINFO_EXTENSION) ?: '';
$extension = pathinfo((string) $path, PATHINFO_EXTENSION);
$extensions = $compiler->getOption('extensions');

if ($extension === '') {
Expand Down Expand Up @@ -72,7 +72,7 @@ public function compileNode(NodeInterface $node, ElementInterface $parent = null

$paths = $isAbsolutePath
? null
: [dirname($compiler->getPath()) ?: '.'];
: [dirname((string) $compiler->getPath()) ?: '.'];

$path = $compiler->resolve($node->getPath(), $paths);
$compiler->registerImportPath($path);
Expand Down
7 changes: 5 additions & 2 deletions src/Phug/Compiler/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
"homepage": "http://phug-lang.com",
"authors": [
{
"name": "KyleKatarn",
"email": "[email protected]",
"name": "kylekatarnls",
"homepage": "http://github.com/kylekatarnls"
},
{
"name": "Torben Koehn",
"email": "[email protected]"
}
],
"support": {
Expand Down
3 changes: 1 addition & 2 deletions src/Phug/DependencyInjection/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"email": "[email protected]"
},
{
"name": "KyleKatarn",
"email": "[email protected]",
"name": "kylekatarnls",
"homepage": "http://github.com/kylekatarnls"
}
],
Expand Down
3 changes: 3 additions & 0 deletions src/Phug/Event/Event/ListenerQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Phug\Event;

use ReturnTypeWillChange;
use SplPriorityQueue;

class ListenerQueue extends SplPriorityQueue
{
#[ReturnTypeWillChange]
public function compare($priority, $priorityToCompare)
{
if ($priority === $priorityToCompare) {
Expand All @@ -15,6 +17,7 @@ public function compare($priority, $priorityToCompare)
return $priority > $priorityToCompare ? -1 : 1;
}

#[ReturnTypeWillChange]
public function insert($value, $priority)
{
if (!is_callable($value)) {
Expand Down
3 changes: 1 addition & 2 deletions src/Phug/Event/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"email": "[email protected]"
},
{
"name": "KyleKatarn",
"email": "[email protected]",
"name": "kylekatarnls",
"homepage": "http://github.com/kylekatarnls"
}
],
Expand Down
2 changes: 1 addition & 1 deletion src/Phug/Formatter/Formatter/AbstractFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ abstract class AbstractFormat implements FormatInterface, OptionInterface
is_object($_pug_temp = %s) && method_exists($_pug_temp, "__toBoolean")
? $_pug_temp->__toBoolean()
: $_pug_temp';
const HTML_EXPRESSION_ESCAPE = 'htmlspecialchars(%s)';
const HTML_EXPRESSION_ESCAPE = 'htmlspecialchars((string) (%s))';
const HTML_TEXT_ESCAPE = 'htmlspecialchars';
const PAIR_TAG = '%s%s%s';
const TRANSFORM_EXPRESSION = '%s';
Expand Down
3 changes: 2 additions & 1 deletion src/Phug/Formatter/Formatter/Element/AssignmentElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
use Phug\Formatter\AbstractElement;
use Phug\Formatter\AssignmentContainerInterface;
use Phug\Parser\NodeInterface as ParserNode;
use Phug\Util\AttributesInterface;
use Phug\Util\Partial\AttributeTrait;
use Phug\Util\Partial\NameTrait;

class AssignmentElement extends AbstractElement
class AssignmentElement extends AbstractElement implements AttributesInterface
{
use AttributeTrait;
use NameTrait;
Expand Down
3 changes: 2 additions & 1 deletion src/Phug/Formatter/Formatter/Element/MarkupElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

use Phug\Ast\NodeInterface;
use Phug\Parser\NodeInterface as ParserNode;
use Phug\Util\AttributesInterface;
use Phug\Util\Partial\AttributeTrait;
use Phug\Util\Partial\NameTrait;

class MarkupElement extends AbstractMarkupElement
class MarkupElement extends AbstractMarkupElement implements AttributesInterface
{
use AttributeTrait;
use NameTrait;
Expand Down
3 changes: 2 additions & 1 deletion src/Phug/Formatter/Formatter/Element/MixinCallElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace Phug\Formatter\Element;

use Phug\Util\AttributesInterface;
use Phug\Util\Partial\AttributeTrait;
use Phug\Util\Partial\NameTrait;

class MixinCallElement extends AbstractAssignmentContainerElement
class MixinCallElement extends AbstractAssignmentContainerElement implements AttributesInterface
{
use AttributeTrait;
use NameTrait;
Expand Down
3 changes: 2 additions & 1 deletion src/Phug/Formatter/Formatter/Element/MixinElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace Phug\Formatter\Element;

use Phug\Formatter\AbstractElement;
use Phug\Util\AttributesInterface;
use Phug\Util\Partial\AttributeTrait;
use Phug\Util\Partial\NameTrait;

class MixinElement extends AbstractElement
class MixinElement extends AbstractElement implements AttributesInterface
{
use AttributeTrait;
use NameTrait;
Expand Down
86 changes: 66 additions & 20 deletions src/Phug/Formatter/Formatter/Format/XmlFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Phug\Formatter\Format;

use Generator;
use Phug\Formatter;
use Phug\Formatter\AbstractFormat;
use Phug\Formatter\AssignmentContainerInterface;
use Phug\Formatter\Element\AbstractValueElement;
use Phug\Formatter\Element\AssignmentElement;
use Phug\Formatter\Element\AttributeElement;
Expand All @@ -16,6 +18,7 @@
use Phug\Formatter\MarkupInterface;
use Phug\Formatter\Partial\AssignmentHelpersTrait;
use Phug\FormatterException;
use Phug\Util\AttributesInterface;
use Phug\Util\Joiner;
use SplObjectStorage;

Expand Down Expand Up @@ -163,7 +166,7 @@ protected function formatAttributeElement(AttributeElement $element)
$nonEmptyAttribute = ($name === 'class' || $name === 'id');
if ($nonEmptyAttribute && (
!$value ||
($value instanceof TextElement && ($value->getValue() ?: '') === '') ||
($value instanceof TextElement && ((string) $value->getValue()) === '') ||
(is_string($value) && in_array(trim($value), ['', '""', "''"]))
)) {
return '';
Expand Down Expand Up @@ -266,25 +269,78 @@ protected function yieldAssignmentElement(AssignmentElement $element)
/* @var MarkupElement $markup */
$markup = $element->getContainer();

$arguments = $markup instanceof AssignmentContainerInterface
? $this->formatAttributeAssignments($markup)
: [];

$arguments = array_merge(
$markup instanceof AttributesInterface
? $this->formatMarkupAttributes($markup)
: [],
$arguments
);

foreach ($markup->getAssignments() as $assignment) {
/* @var AssignmentElement $assignment */
$this->throwException(
'Unable to handle '.$assignment->getName().' assignment',
$assignment
);
}

if (count($arguments)) {
yield $this->attributesAssignmentsFromPairs($arguments);
}
}

/**
* @param AssignmentContainerInterface $markup
*
* @return array<string>
*/
protected function formatAttributeAssignments(AssignmentContainerInterface $markup)
{
$arguments = [];

foreach ($this->yieldAssignmentAttributes($markup) as $attribute) {
$checked = method_exists($attribute, 'isChecked') && $attribute->isChecked();

while (method_exists($attribute, 'getValue')) {
$attribute = $attribute->getValue();
}

$arguments[] = $this->formatCode($attribute, $checked);
}

return $arguments;
}

/**
* @param AssignmentContainerInterface $markup
*
* @return Generator|AbstractValueElement[]
*/
protected function yieldAssignmentAttributes(AssignmentContainerInterface $markup)
{
foreach ($markup->getAssignmentsByName('attributes') as $attributesAssignment) {
/* @var AssignmentElement $attributesAssignment */
foreach ($attributesAssignment->getAttributes() as $attribute) {
/* @var AbstractValueElement $attribute */
$value = $attribute;
$checked = method_exists($value, 'isChecked') && $value->isChecked();

while (method_exists($value, 'getValue')) {
$value = $value->getValue();
}

$arguments[] = $this->formatCode($value, $checked);
yield $attribute;
}

$markup->removedAssignment($attributesAssignment);
}
}

/**
* @param AttributesInterface $markup
*
* @return array<string>
*/
protected function formatMarkupAttributes(AttributesInterface $markup)
{
$arguments = [];
$attributes = $markup->getAttributes();

foreach ($attributes as $attribute) {
Expand All @@ -294,17 +350,7 @@ protected function yieldAssignmentElement(AssignmentElement $element)

$attributes->removeAll($attributes);

foreach ($markup->getAssignments() as $assignment) {
/* @var AssignmentElement $assignment */
$this->throwException(
'Unable to handle '.$assignment->getName().' assignment',
$assignment
);
}

if (count($arguments)) {
yield $this->attributesAssignmentsFromPairs($arguments);
}
return $arguments;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,14 @@ protected function provideStyleAttributeAssignment()
if (is_string($value) && mb_substr($value, 0, 7) === '{&quot;') {
$value = json_decode(htmlspecialchars_decode($value));
}

$styles = isset($attributes['style']) ? array_filter(explode(';', $attributes['style'])) : [];

foreach ((array) $value as $propertyName => $propertyValue) {
if (!is_int($propertyName)) {
$propertyValue = $propertyName.':'.$propertyValue;
}

$styles[] = $propertyValue;
}

Expand Down
3 changes: 1 addition & 2 deletions src/Phug/Formatter/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"homepage": "http://phug-lang.com",
"authors": [
{
"name": "KyleKatarn",
"email": "[email protected]",
"name": "kylekatarnls",
"homepage": "http://github.com/kylekatarnls"
},
{
Expand Down
Loading

0 comments on commit 523139f

Please sign in to comment.