Commit 7ea9cc7
committed
bug #3762 [TwigComponent] Fix twig:blockquote is not a block (smnandre)
This PR was merged into the 3.x branch.
Discussion
----------
[TwigComponent] Fix twig:blockquote is not a block
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Issues | -
| License | MIT
A component whose name starts with `block` breaks the block it sits in.
## Reproducer
```twig
<twig:Card>
<twig:block name="footer">
<twig:blockquote cite="Matt">He ho!</twig:blockquote>
</twig:block>
</twig:Card>
```
```
Twig\Error\SyntaxError: Expected closing tag "</twig:Card>" not found at line 5.
```
The error points at `Card`, which is correctly closed, and at the last line of the
template. Nothing mentions `blockquote`, so there is little to go on.
Same failure with `<twig:blockquote />`, `<twig:block-title />`, `<twig:blocked />`, and
inside a traditional `{% block %} … {% endblock %}`.
Not affected: `<twig:Blockquote />` (the comparison is case-sensitive), and any of these
outside a block.
## Cause
`TwigPreLexer::consumeUntilEndBlock()` tracks block nesting with a fixed-length prefix
comparison:
```php
if (!$inComment && '<twig:block' === substr($this->input, $this->position, 11)) {
++$depth;
}
```
`'<twig:block'` is also the first 11 characters of `<twig:blockquote`, so the depth is
incremented for a component that is not a block. It is never decremented back: the
closing comparison is `'</twig:block>'`, which ends with `>` and therefore does not
match `</twig:blockquote>`. The enclosing `<twig:block>` never terminates, scanning runs
to the end of the template, and the outer component is reported as unclosed.
## Fix
Anchor the match and require that `block` is not followed by another component-name
character, using the same character class as `consumeComponentName()`:
```php
if (!$inComment && preg_match('/\G<twig:block(?![A-Za-z0-9_:@\-.])/', $this->input, $matches, 0, $this->position)) {
++$depth;
}
```
The closing comparison needs no change.
Commits
-------
0d2b87f [TwigComponent] Fix twig:blockquote is not a block2 files changed
Lines changed: 54 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
455 | 455 | | |
456 | 456 | | |
457 | 457 | | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
458 | 463 | | |
459 | 464 | | |
460 | 465 | | |
| |||
542 | 547 | | |
543 | 548 | | |
544 | 549 | | |
545 | | - | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
546 | 553 | | |
547 | 554 | | |
548 | 555 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
113 | 159 | | |
114 | 160 | | |
115 | 161 | | |
| |||
0 commit comments