Skip to content

Commit e7fc5b8

Browse files
committed
bug #820 Resolve twig syntax in twig component arguments (WebMamba)
This PR was merged into the 2.x branch. Discussion ---------- Resolve twig syntax in twig component arguments | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Tickets | Fix #812 | License | MIT Commits ------- 7d63e23 Resolve twig syntax in twig component arguments
2 parents 286a0d9 + 7d63e23 commit e7fc5b8

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

src/TwigComponent/src/Twig/TwigPreLexer.php

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,18 @@ private function consumeAttributes(string $componentName): string
184184
$this->expectAndConsumeChar('}');
185185
$this->expectAndConsumeChar('}');
186186
$this->consumeUntil($quote);
187-
$isAttributeDynamic = true;
188-
} else {
189-
$attributeValue = $this->consumeUntil($quote);
190-
}
191-
$this->expectAndConsumeChar($quote);
192187

193-
if ($isAttributeDynamic) {
194188
$attributes[] = sprintf('%s: %s', $key, $attributeValue);
195189
} else {
196-
$attributes[] = sprintf("%s: '%s'", $key, str_replace("'", "\'", $attributeValue));
197-
}
190+
$attributeValue = $this->consumeAttributeValue($quote);
198191

192+
if ($isAttributeDynamic) {
193+
$attributes[] = sprintf('%s: %s', $key, $attributeValue);
194+
} else {
195+
$attributes[] = sprintf("%s: '%s'", $key, $attributeValue);
196+
}
197+
}
198+
$this->expectAndConsumeChar($quote);
199199
$this->consumeWhitespace();
200200
}
201201

@@ -352,6 +352,43 @@ private function consumeUntilEndBlock(): string
352352
return substr($this->input, $start, $this->position - $start);
353353
}
354354

355+
private function consumeAttributeValue(string $quote): string
356+
{
357+
$attributeValue = '';
358+
while ($this->position < $this->length) {
359+
if (substr($this->input, $this->position, 1) === $quote) {
360+
break;
361+
}
362+
363+
if ("\n" === $this->input[$this->position]) {
364+
++$this->line;
365+
}
366+
367+
if ('\'' === $this->input[$this->position]) {
368+
$attributeValue .= "\'";
369+
++$this->position;
370+
371+
continue;
372+
}
373+
374+
if ('{{' === substr($this->input, $this->position, 2)) {
375+
$this->consume('{{');
376+
$attributeValue .= "'~(";
377+
$this->consumeWhitespace();
378+
$value = rtrim($this->consumeUntil('}}'));
379+
$this->expectAndConsumeChar('}');
380+
$this->expectAndConsumeChar('}');
381+
$attributeValue .= $value;
382+
$attributeValue .= ")~'";
383+
}
384+
385+
$attributeValue .= $this->input[$this->position];
386+
++$this->position;
387+
}
388+
389+
return $attributeValue;
390+
}
391+
355392
private function doesStringEventuallyExist(string $needle): bool
356393
{
357394
$remainingString = substr($this->input, $this->position);

src/TwigComponent/tests/Unit/TwigPreLexerTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,9 @@ public function getLexTests(): iterable
9696
'<twig:foo><twig:block name="child"><twig:bar><twig:block name="message">Hello World!</twig:block></twig:bar></twig:block></twig:foo>',
9797
'{% component \'foo\' %}{% block child %}{% component \'bar\' %}{% block message %}Hello World!{% endblock %}{% endcomponent %}{% endblock %}{% endcomponent %}',
9898
];
99+
yield 'component_with_html_syntaxt_in_argument' => [
100+
'<twig:foo text="Hello {{ name }}!"/>',
101+
"{{ component('foo', { text: 'Hello '~(name)~'!' }) }}",
102+
];
99103
}
100104
}

0 commit comments

Comments
 (0)