-
-
Notifications
You must be signed in to change notification settings - Fork 383
Remove ComponentAttributeFactory and inject EscaperRuntime directly #2749
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
Merged
Merged
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -32,7 +32,8 @@ | |
use Symfony\UX\LiveComponent\Metadata\LivePropMetadata; | ||
use Symfony\UX\LiveComponent\Util\DehydratedProps; | ||
use Symfony\UX\TwigComponent\ComponentAttributes; | ||
use Symfony\UX\TwigComponent\ComponentAttributesFactory; | ||
use Twig\Environment; | ||
use Twig\Runtime\EscaperRuntime; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
|
@@ -53,7 +54,7 @@ public function __construct( | |
private LiveComponentMetadataFactory $liveComponentMetadataFactory, | ||
private NormalizerInterface|DenormalizerInterface|null $serializer, | ||
#[\SensitiveParameter] private string $secret, | ||
private readonly ComponentAttributesFactory $componentAttributesFactory, | ||
private readonly Environment $twig, | ||
) { | ||
if (!$secret) { | ||
throw new \InvalidArgumentException('A non-empty secret is required.'); | ||
|
@@ -146,7 +147,7 @@ public function hydrate(object $component, array $props, array $updatedProps, Li | |
$dehydratedOriginalProps = $this->combineAndValidateProps($props, $updatedPropsFromParent); | ||
$dehydratedUpdatedProps = DehydratedProps::createFromUpdatedArray($updatedProps); | ||
|
||
$attributes = $this->componentAttributesFactory->create($dehydratedOriginalProps->getPropValue(self::ATTRIBUTES_KEY, [])); | ||
$attributes = new ComponentAttributes($dehydratedOriginalProps->getPropValue(self::ATTRIBUTES_KEY, []), $this->twig->getRuntime(EscaperRuntime::class)); | ||
$dehydratedOriginalProps->removePropValue(self::ATTRIBUTES_KEY); | ||
|
||
$needProcessOnUpdatedHooks = []; | ||
|
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 |
---|---|---|
|
@@ -15,8 +15,10 @@ | |
use Symfony\Contracts\Service\ServiceSubscriberInterface; | ||
use Symfony\UX\LiveComponent\LiveComponentHydrator; | ||
use Symfony\UX\LiveComponent\Metadata\LiveComponentMetadataFactory; | ||
use Symfony\UX\TwigComponent\ComponentAttributesFactory; | ||
use Symfony\UX\TwigComponent\ComponentAttributes; | ||
use Symfony\UX\TwigComponent\ComponentFactory; | ||
use Twig\Environment; | ||
use Twig\Runtime\EscaperRuntime; | ||
|
||
/** | ||
* @author Ryan Weaver <[email protected]> | ||
|
@@ -28,7 +30,7 @@ class ChildComponentPartialRenderer implements ServiceSubscriberInterface | |
public function __construct( | ||
private FingerprintCalculator $fingerprintCalculator, | ||
private TwigAttributeHelperFactory $attributeHelperFactory, | ||
private ComponentAttributesFactory $componentAttributesFactory, | ||
private Environment $twig, | ||
private ContainerInterface $container, | ||
) { | ||
} | ||
|
@@ -85,7 +87,7 @@ public function renderChildComponent(string $deterministicId, string $currentPro | |
private function createHtml(array $attributes, string $childTag): string | ||
{ | ||
$attributes['data-live-preserve'] = true; | ||
$attributes = $this->componentAttributesFactory->create($attributes); | ||
$attributes = new ComponentAttributes($attributes, $this->twig->getRuntime(EscaperRuntime::class)); | ||
|
||
return \sprintf('<%s%s></%s>', $childTag, $attributes, $childTag); | ||
} | ||
|
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
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 |
---|---|---|
|
@@ -12,8 +12,8 @@ | |
namespace Symfony\UX\TwigComponent; | ||
|
||
use Symfony\UX\StimulusBundle\Dto\StimulusAttributes; | ||
use Symfony\UX\TwigComponent\Escaper\HtmlAttributeEscaperInterface; | ||
use Symfony\WebpackEncoreBundle\Dto\AbstractStimulusDto; | ||
use Twig\Runtime\EscaperRuntime; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
|
@@ -29,19 +29,13 @@ final class ComponentAttributes implements \Stringable, \IteratorAggregate, \Cou | |
/** @var array<string,true> */ | ||
private array $rendered = []; | ||
|
||
private readonly ?HtmlAttributeEscaperInterface $escaper; | ||
|
||
/** | ||
* @param array<string, string|bool> $attributes | ||
*/ | ||
public function __construct( | ||
private array $attributes, | ||
?HtmlAttributeEscaperInterface $escaper = null, | ||
private readonly EscaperRuntime $escaper, | ||
) { | ||
// Third argument used as internal flag to prevent multiple deprecations | ||
if ((null === $this->escaper = $escaper) && 3 > func_num_args()) { | ||
trigger_deprecation('symfony/ux-twig-component', '2.24', 'Not passing an "%s" to "%s" is deprecated and will throw in 3.0.', HtmlAttributeEscaperInterface::class, self::class); | ||
} | ||
} | ||
|
||
public function __toString(): string | ||
|
@@ -87,13 +81,17 @@ public function __toString(): string | |
// - special syntax names (Vue.js, Svelte, Alpine.js, ...) | ||
// v-*, x-*, @*, :* | ||
if (!ctype_alpha(str_replace(['-', '_', ':', '@', '.'], '', $key))) { | ||
$key = $this->escaper?->escapeName($key) ?? $key; | ||
$key = (string) $this->escaper->escape($key, 'html_attr'); | ||
} | ||
|
||
if (true === $value) { | ||
$attributes .= ' '.$key; | ||
} else { | ||
$attributes .= ' '.\sprintf('%s="%s"', $key, $this->escaper?->escapeValue($value) ?? $value); | ||
if (!ctype_alnum(str_replace(['-', '_'], '', $value))) { | ||
$value = $this->escaper->escape($value, 'html'); | ||
} | ||
|
||
$attributes .= ' '.\sprintf('%s="%s"', $key, $value); | ||
} | ||
} | ||
|
||
|
@@ -167,7 +165,7 @@ public function defaults(iterable $attributes): self | |
unset($attributes[$attribute]); | ||
} | ||
|
||
return new self($attributes, $this->escaper, true); | ||
return new self($attributes, $this->escaper); | ||
} | ||
|
||
/** | ||
|
@@ -183,7 +181,7 @@ public function only(string ...$keys): self | |
} | ||
} | ||
|
||
return new self($attributes, $this->escaper, true); | ||
return new self($attributes, $this->escaper); | ||
} | ||
|
||
/** | ||
|
@@ -221,7 +219,7 @@ public function add($stimulusDto): self | |
))); | ||
unset($controllersAttributes['data-controller']); | ||
|
||
$clone = new self($attributes, $this->escaper, true); | ||
$clone = new self($attributes, $this->escaper); | ||
|
||
// add the remaining attributes for values/classes | ||
return $clone->defaults($controllersAttributes); | ||
|
@@ -233,7 +231,7 @@ public function remove($key): self | |
|
||
unset($attributes[$key]); | ||
|
||
return new self($attributes, $this->escaper, true); | ||
return new self($attributes, $this->escaper); | ||
} | ||
|
||
public function nested(string $namespace): self | ||
|
@@ -249,7 +247,7 @@ public function nested(string $namespace): self | |
} | ||
} | ||
|
||
return new self($attributes, $this->escaper, true); | ||
return new self($attributes, $this->escaper); | ||
} | ||
|
||
public function getIterator(): \Traversable | ||
|
This file was deleted.
Oops, something went wrong.
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.