phug-php / phug

Phug - The Pug Template Engine for PHP
https://phug.selfbuild.fr
MIT License
62 stars 3 forks source link

Add attribute_precedence option #97

Closed kylekatarnls closed 1 year ago

kylekatarnls commented 1 year ago

attribute_precedence "assignment" (default value), "attribute", "left", "right", callable

Allow to customize precedence between attributes and &attribute assignment.

Phug::display('
| Last assignment
- $data = ["href" => "/c"]
a(href="/a")&attributes(["href" => "/b"])&attributes($data)(href="/d")
', [], [
    'attribute_precedence' => 'assignment',
]);

Phug::display('
| Last attribute
- $data = ["href" => "/c"]
a(href="/a")&attributes(["href" => "/b"])&attributes($data)(href="/d")
', [], [
    'attribute_precedence' => 'attribute',
]);

Phug::display('
| First token (either it’s attribute or assignment)
- $data = ["href" => "/c"]
a(href="/a")&attributes(["href" => "/b"])&attributes($data)(href="/d")
', [], [
    'attribute_precedence' => 'left',
]);

Phug::display('
| Last token (either it’s attribute or assignment)
- $data = ["href" => "/c"]
a(href="/a")&attributes(["href" => "/b"])&attributes($data)(href="/d")
', [], [
    'attribute_precedence' => 'right',
]);

Phug::display('
| First assignment
- $data = ["href" => "/c"]
a(href="/a")&attributes(["href" => "/b"])&attributes($data)(href="/d")
', [], [
    'attribute_precedence' => static function (array $assignments, array $attributes) {
        // Sort in reverse-order (right-most tokens to left-most tokens)
        usort($assignments, static function (\Phug\Util\OrderedValue $a, \Phug\Util\OrderedValue $b) {
            return $b->getOrder() - $a->getOrder();
        });
        usort($attributes, static function (\Phug\Util\OrderedValue $a, \Phug\Util\OrderedValue $b) {
            return $b->getOrder() - $a->getOrder();
        });

        // Return order in which values will merge
        // So from lowest to highest precedence
        return array_merge($attributes, $assignments);
    },
]);

Phug::display('
| First attribute
- $data = ["href" => "/c"]
a(href="/a")&attributes(["href" => "/b"])&attributes($data)(href="/d")
', [], [
    'attribute_precedence' => static function (array $assignments, array $attributes) {
        // Sort in reverse-order (right-most tokens to left-most tokens)
        usort($assignments, static function (\Phug\Util\OrderedValue $a, \Phug\Util\OrderedValue $b) {
            return $b->getOrder() - $a->getOrder();
        });
        usort($attributes, static function (\Phug\Util\OrderedValue $a, \Phug\Util\OrderedValue $b) {
            return $b->getOrder() - $a->getOrder();
        });

        // Return order in which values will merge
        // So from lowest to highest precedence
        return array_merge($assignments, $attributes);
    },
]);

Output:

Last assignment
<a href="/c"></a>

Last attribute
<a href="/d"></a>

First token (either it’s attribute or assignment)
<a href="/a"></a>

Last token (either it’s attribute or assignment)
<a href="/d"></a>

First assignment
<a href="/b"></a>

First attribute
<a href="/a"></a>

Fix #88

codecov[bot] commented 1 year ago

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (9a03e8e) 100.00% compared to head (c64b5fd) 100.00%.

:exclamation: Current head c64b5fd differs from pull request most recent head 9b9dcc0. Consider uploading reports for the commit 9b9dcc0 to get more accurate results

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #97 +/- ## ========================================= Coverage 100.00% 100.00% ========================================= Files 255 259 +4 Lines 7395 7481 +86 ========================================= + Hits 7395 7481 +86 ``` | [Files Changed](https://app.codecov.io/gh/phug-php/phug/pull/97?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=phug-php) | Coverage Δ | | |---|---|---| | [src/Phug/Ast/Ast/Node.php](https://app.codecov.io/gh/phug-php/phug/pull/97?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=phug-php#diff-c3JjL1BodWcvQXN0L0FzdC9Ob2RlLnBocA==) | `100.00% <ø> (ø)` | | | [.../Formatter/Formatter/Element/AssignmentElement.php](https://app.codecov.io/gh/phug-php/phug/pull/97?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=phug-php#diff-c3JjL1BodWcvRm9ybWF0dGVyL0Zvcm1hdHRlci9FbGVtZW50L0Fzc2lnbm1lbnRFbGVtZW50LnBocA==) | `100.00% <ø> (ø)` | | | [...g/Formatter/Formatter/Element/AttributeElement.php](https://app.codecov.io/gh/phug-php/phug/pull/97?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=phug-php#diff-c3JjL1BodWcvRm9ybWF0dGVyL0Zvcm1hdHRlci9FbGVtZW50L0F0dHJpYnV0ZUVsZW1lbnQucGhw) | `100.00% <ø> (ø)` | | | [...Phug/Formatter/Formatter/Element/MarkupElement.php](https://app.codecov.io/gh/phug-php/phug/pull/97?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=phug-php#diff-c3JjL1BodWcvRm9ybWF0dGVyL0Zvcm1hdHRlci9FbGVtZW50L01hcmt1cEVsZW1lbnQucGhw) | `100.00% <ø> (ø)` | | | [src/Phug/Lexer/Lexer/State.php](https://app.codecov.io/gh/phug-php/phug/pull/97?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=phug-php#diff-c3JjL1BodWcvTGV4ZXIvTGV4ZXIvU3RhdGUucGhw) | `100.00% <ø> (ø)` | | | [src/Phug/Parser/Parser/Event/NodeEvent.php](https://app.codecov.io/gh/phug-php/phug/pull/97?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=phug-php#diff-c3JjL1BodWcvUGFyc2VyL1BhcnNlci9FdmVudC9Ob2RlRXZlbnQucGhw) | `100.00% <ø> (ø)` | | | [src/Phug/Parser/Parser/Node/ElementNode.php](https://app.codecov.io/gh/phug-php/phug/pull/97?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=phug-php#diff-c3JjL1BodWcvUGFyc2VyL1BhcnNlci9Ob2RlL0VsZW1lbnROb2RlLnBocA==) | `100.00% <ø> (ø)` | | | [src/Phug/Parser/Parser/Node/MixinCallNode.php](https://app.codecov.io/gh/phug-php/phug/pull/97?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=phug-php#diff-c3JjL1BodWcvUGFyc2VyL1BhcnNlci9Ob2RlL01peGluQ2FsbE5vZGUucGhw) | `100.00% <ø> (ø)` | | | [src/Phug/Reader/Reader.php](https://app.codecov.io/gh/phug-php/phug/pull/97?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=phug-php#diff-c3JjL1BodWcvUmVhZGVyL1JlYWRlci5waHA=) | `100.00% <ø> (ø)` | | | [...Phug/Renderer/Renderer/Adapter/Stream/Template.php](https://app.codecov.io/gh/phug-php/phug/pull/97?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=phug-php#diff-c3JjL1BodWcvUmVuZGVyZXIvUmVuZGVyZXIvQWRhcHRlci9TdHJlYW0vVGVtcGxhdGUucGhw) | `100.00% <ø> (ø)` | | | ... and [20 more](https://app.codecov.io/gh/phug-php/phug/pull/97?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=phug-php) | | ... and [9 files with indirect coverage changes](https://app.codecov.io/gh/phug-php/phug/pull/97/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=phug-php)

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.