Skip to content

[TwigComponent] Html syntax bug test case + fix #824

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 1 commit into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions src/TwigComponent/src/Twig/TwigPreLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public function __construct(int $startingLine = 1)
$this->line = $startingLine;
}

public function preLexComponents(string $input): string
/**
* @param bool $insideOfBlock Are we sub-parsing the content inside a block?
*/
public function preLexComponents(string $input, bool $insideOfBlock = false): string
{
$this->input = $input;
$this->length = \strlen($input);
Expand All @@ -53,6 +56,15 @@ public function preLexComponents(string $input): string
continue;
}

// if we're already inside a component, and we're not inside a block,
// *and* we've just found a new component, then we should try to
// open the default block
if (!$insideOfBlock
&& !empty($this->currentComponents)
&& !$this->currentComponents[\count($this->currentComponents) - 1]['hasDefaultBlock']) {
$output .= $this->addDefaultBlock();
}

$attributes = $this->consumeAttributes($componentName);
$isSelfClosing = $this->consume('/>');
if (!$isSelfClosing) {
Expand Down Expand Up @@ -100,13 +112,12 @@ public function preLexComponents(string $input): string
++$this->line;
}

// handle adding a default block if needed
// handle adding a default block if we find non-whitespace outside of a block
if (!empty($this->currentComponents)
&& !$this->currentComponents[\count($this->currentComponents) - 1]['hasDefaultBlock']
&& preg_match('/\S/', $char)
) {
$this->currentComponents[\count($this->currentComponents) - 1]['hasDefaultBlock'] = true;
$output .= '{% block content %}';
$output .= $this->addDefaultBlock();
}

$output .= $char;
Expand Down Expand Up @@ -202,10 +213,15 @@ private function consumeAttributes(string $componentName): string
return implode(', ', $attributes);
}

/**
* If the next character(s) exactly matches the given string, then
* consume it (move forward) and return true.
*/
private function consume(string $string): bool
{
if (substr($this->input, $this->position, \strlen($string)) === $string) {
$this->position += \strlen($string);
$stringLength = \strlen($string);
if (substr($this->input, $this->position, $stringLength) === $string) {
$this->position += $stringLength;

return true;
}
Expand Down Expand Up @@ -317,7 +333,7 @@ private function consumeBlock(string $componentName): string
$blockContents = $this->consumeUntilEndBlock();

$subLexer = new self($this->line);
$output .= $subLexer->preLexComponents($blockContents);
$output .= $subLexer->preLexComponents($blockContents, true);

$this->consume($closingTag);
$output .= '{% endblock %}';
Expand Down Expand Up @@ -395,4 +411,11 @@ private function doesStringEventuallyExist(string $needle): bool

return str_contains($remainingString, $needle);
}

private function addDefaultBlock(): string
{
$this->currentComponents[\count($this->currentComponents) - 1]['hasDefaultBlock'] = true;

return '{% block content %}';
}
}
30 changes: 29 additions & 1 deletion src/TwigComponent/tests/Unit/TwigPreLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,37 @@ public function getLexTests(): iterable
'<twig:foo><twig:block name="child"><twig:bar><twig:block name="message">Hello World!</twig:block></twig:bar></twig:block></twig:foo>',
'{% component \'foo\' %}{% block child %}{% component \'bar\' %}{% block message %}Hello World!{% endblock %}{% endcomponent %}{% endblock %}{% endcomponent %}',
];
yield 'component_with_html_syntaxt_in_argument' => [
yield 'component_with_mixture_of_string_and_twig_in_argument' => [
'<twig:foo text="Hello {{ name }}!"/>',
"{{ component('foo', { text: 'Hello '~(name)~'!' }) }}",
];
yield 'component_with_mixture_of_string_and_twig_with_quote_in_argument' => [
'<twig:foo text="Hello {{ name }}, I\'m Theo!"/>',
"{{ component('foo', { text: 'Hello '~(name)~', I\'m Theo!' }) }}",
];
yield 'component_where_entire_default_block_is_embedded_component' => [
<<<EOF
<twig:foo>
<twig:bar>bar content</twig:bar>
</twig:foo>
EOF,
<<<EOF
{% component 'foo' %}
{% block content %}{% component 'bar' %}{% block content %}bar content{% endblock %}{% endcomponent %}
{% endblock %}{% endcomponent %}
EOF
];
yield 'component_where_entire_default_block_is_embedded_component_self_closing' => [
<<<EOF
<twig:foo>
<twig:bar />
</twig:foo>
EOF,
<<<EOF
{% component 'foo' %}
{% block content %}{{ component('bar') }}
{% endblock %}{% endcomponent %}
EOF
];
}
}