Skip to content

[langfun] Make Template.raw_str total over arbitrary text - #783

Open
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_1004690769
Open

copybara-service[bot] wants to merge 1 commit into
mainfrom
test_1004690769

Conversation

@copybara-service

@copybara-service copybara-service Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

[langfun] Make Template.raw_str total over arbitrary text

Template.raw_str(text) wrapped text in {% raw %} ... {% endraw %} to
preserve it verbatim. A raw block ends at the first endraw tag, so text that
itself contained one closed the block early: the remainder was parsed as Jinja,
which raised Bad template string on a {% ... %} sequence and registered an
undeclared variable on a {{ ... }} one. The function whose job is to quote
arbitrary text was not total over it, and neither failure was fixable by the
caller, since the escaping it would have to redo is the escaping that failed.

  • Each literal endraw tag is now emitted from outside the raw block -- its
    {% as a Jinja string expression, the remainder from a reopened raw block --
    so the template renders back to exactly the input and declares no variables.
    _ENDRAW_TAG matches every spelling that ends a block, including the
    whitespace-control forms, mirroring the TOKEN_RAW_BEGIN rule of
    jinja2.lexer. Everything else, {% raw %} itself included, is already
    literal inside a raw block, which does not nest.

  • Template.from_raw_str(text, **kwargs) forwards template attributes and
    variables instead of discarding them. It accepted only text, so a caller
    that needed any other attribute had to re-implement the escaping rather than
    reuse it. clean stays forced off, because reindenting the text would
    contradict preserving it as original.

`Template.raw_str(text)` wrapped `text` in `{% raw %}` ... `{% endraw %}` to
preserve it verbatim. A raw block ends at the first `endraw` tag, so text that
itself contained one closed the block early: the remainder was parsed as Jinja,
which raised `Bad template string` on a `{% ... %}` sequence and registered an
undeclared variable on a `{{ ... }}` one. The function whose job is to quote
arbitrary text was not total over it, and neither failure was fixable by the
caller, since the escaping it would have to redo is the escaping that failed.

* Each literal `endraw` tag is now emitted from outside the raw block -- its
  `{%` as a Jinja string expression, the remainder from a reopened raw block --
  so the template renders back to exactly the input and declares no variables.
  `_ENDRAW_TAG` matches every spelling that ends a block, including the
  whitespace-control forms, mirroring the `TOKEN_RAW_BEGIN` rule of
  `jinja2.lexer`. Everything else, `{% raw %}` itself included, is already
  literal inside a raw block, which does not nest.

* `Template.from_raw_str(text, **kwargs)` forwards template attributes and
  variables instead of discarding them. It accepted only `text`, so a caller
  that needed any other attribute had to re-implement the escaping rather than
  reuse it. `clean` stays forced off, because reindenting the text would
  contradict preserving it as original.

PiperOrigin-RevId: 1004690769
@copybara-service copybara-service Bot changed the title [langfun] Stop re-parsing already-rendered content as a Jinja template [langfun] Make Template.raw_str total over arbitrary text Oct 23, 2026
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