Skip to content

[TwigComponent] Fix PreLexer confusing {# inside output expressions with comments#3518

Draft
Amoifr wants to merge 1 commit into
symfony:2.xfrom
Amoifr:fix/twig-component-prelexer-icu-plural
Draft

[TwigComponent] Fix PreLexer confusing {# inside output expressions with comments#3518
Amoifr wants to merge 1 commit into
symfony:2.xfrom
Amoifr:fix/twig-component-prelexer-icu-plural

Conversation

@Amoifr

@Amoifr Amoifr commented May 4, 2026

Copy link
Copy Markdown
Contributor
Q A
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix #3514
License MIT

The TwigPreLexer was treating any {# it encountered as the opening of a Twig comment, even when that {# actually lived inside a Twig output expression (e.g. inside a string literal passed to |trans). When the matching #} was missing — typically the case for ICU MessageFormat plural strings like {count, plural, one {# item} other {# items}} — the lexer consumed the rest of the template as a fake comment, swallowing any <twig:…> tags after it.

This PR adds a top-level handler for {{ … }} in the PreLexer's main loop. The new helper consumeTwigExpressionContent() walks the expression body with string-literal awareness (single quotes, double quotes, \\-escapes), so that }} or {# appearing inside a string don't end the expression early or trigger comment mode. The implicit {% block content %} injection still kicks in when the expression appears as content inside a component.

5 regression tests added covering:

  • the original ICU plural example from the issue
  • }} inside a string literal in {{ … }}
  • {#…#} inside a double-quoted string
  • escaped quotes inside the expression
  • {{ … }} inside a component (default-block injection)

Marking as draft pending @Kocal's input on the scope question raised in #3514: should this same protection be extended to {% … %} (where {% set foo = '{# bar #}' %} has the same latent issue), or kept focused on {{ … }} ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant