-
-
Notifications
You must be signed in to change notification settings - Fork 158
fix(view)!: fix inconsistent fallthrough attributes and introduce apply attribute for full control #2092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iamdadmin
wants to merge
12
commits into
tempestphp:3.x
Choose a base branch
from
iamdadmin:3.x-applyFallthroughAttributes
base: 3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix(view)!: fix inconsistent fallthrough attributes and introduce apply attribute for full control #2092
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0272ea3
test(view): add additional FallthroughAttribute tests
iamdadmin 4eaf13d
feat(view): resolve issue with fallthroughattributes from #2040, and …
iamdadmin 303bed8
tests(view): updated and expanded FallthroughAttributes tests
iamdadmin fdb3a05
feat(view): removed extra space, tweaked stringify rules
iamdadmin 93eae83
feat(view): remove unnecessary if and collapse into default condition
iamdadmin 5cf0a89
tests(view): integration test updates
iamdadmin dfbb2f9
docs(view): docs update for fallthrough attributes and ApplyAttribute
iamdadmin df1d4d4
chore(view): mago fmt
iamdadmin cbefb7a
docs(view): update for with and without methods introduced in #2094
iamdadmin fe7132c
refactor(view): remove unused import
iamdadmin 4573df8
refactor(view): rector rule performed refactor
iamdadmin 5b85298
refactor(view): moved from str_contains to ast search
iamdadmin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tempest\View\Attributes; | ||
|
|
||
| use Tempest\Support\Arr\ImmutableArray; | ||
| use Tempest\View\Attribute; | ||
| use Tempest\View\Element; | ||
| use Tempest\View\Elements\ViewComponentElement; | ||
|
|
||
| use function Tempest\Support\str; | ||
|
|
||
| final readonly class ApplyAttribute implements Attribute | ||
| { | ||
| public function apply(Element $element): Element | ||
| { | ||
| $value = $element->consumeAttribute(':apply'); | ||
|
|
||
| if ($value === null || trim($value) === '') { | ||
| return $element; | ||
| } | ||
|
|
||
| if ($element instanceof ViewComponentElement) { | ||
| $element->setApplyExpression($value); | ||
|
|
||
| return $element; | ||
| } | ||
|
|
||
| $element->addRawAttribute(sprintf( | ||
| '<?= \%s::renderAll(%s) ?>', | ||
| self::class, | ||
| $value, | ||
| )); | ||
|
|
||
| return $element; | ||
| } | ||
|
|
||
| /** | ||
| * Stringifies an ImmutableArray or plain array of attributes into an HTML attribute string. | ||
| * | ||
| * Rules: | ||
| * - boolean true → bare attribute name (e.g. `disabled`) | ||
| * - boolean false → omitted | ||
| * - null → omitted | ||
| * - empty string → omitted | ||
| * - int / float → name="value" (cast to string — HTML attributes are always strings) | ||
| * - string → name="value" | ||
| * - array → name="space-joined values" (via ExpressionAttribute::resolveValue) | ||
| * | ||
| * Returns 'key="val" key2="val2"' with NO leading space. GenericElement's compile() | ||
| * inserts one space before the raw attribute block at compile time, so adding a leading | ||
| * space here would produce a double space in the rendered output. | ||
| * | ||
| * Note: when this returns '' (all attributes omitted), GenericElement's compile-time | ||
| * space still appears, producing e.g. `<button >`. This is pre-existing framework | ||
| * behaviour shared with ExpressionAttribute and cannot be fixed in this class alone. | ||
| */ | ||
| public static function renderAll(ImmutableArray|array $attributes): string | ||
| { | ||
| if (is_array($attributes)) { | ||
| $attributes = new ImmutableArray($attributes); | ||
| } | ||
|
|
||
| $parts = []; | ||
|
|
||
| foreach ($attributes as $name => $value) { | ||
| $attrName = str($name)->kebab()->toString(); | ||
|
|
||
| if ($value === true) { | ||
| $parts[] = $attrName; | ||
| continue; | ||
| } | ||
| if ($value === false) { | ||
| continue; | ||
| } | ||
| if ($value === null) { | ||
| continue; | ||
| } | ||
| if ($value === '') { | ||
| continue; | ||
| } | ||
|
|
||
| $resolved = ExpressionAttribute::resolveValue($value); | ||
|
|
||
| if ($resolved !== '') { | ||
| $parts[] = sprintf('%s="%s"', $attrName, $resolved); | ||
| } | ||
| } | ||
|
|
||
| return implode(' ', $parts); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.