Repository navigation
[langfun] Make Template.raw_str total over arbitrary text - #783
Open
copybara-service[bot] wants to merge 1 commit into
Open
copybara-service[bot] wants to merge 1 commit into
copybara-service[bot] wants to merge 1 commit into
Conversation
`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
Bot
force-pushed
the
test_1004690769
branch
from
October 23, 2026 13:04
178b9ba to
ab4fad8
Compare
Template.raw_str total over arbitrary text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[langfun] Make
Template.raw_strtotal over arbitrary textTemplate.raw_str(text)wrappedtextin{% raw %}...{% endraw %}topreserve it verbatim. A raw block ends at the first
endrawtag, so text thatitself contained one closed the block early: the remainder was parsed as Jinja,
which raised
Bad template stringon a{% ... %}sequence and registered anundeclared variable on a
{{ ... }}one. The function whose job is to quotearbitrary 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
endrawtag 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_TAGmatches every spelling that ends a block, including thewhitespace-control forms, mirroring the
TOKEN_RAW_BEGINrule ofjinja2.lexer. Everything else,{% raw %}itself included, is alreadyliteral inside a raw block, which does not nest.
Template.from_raw_str(text, **kwargs)forwards template attributes andvariables instead of discarding them. It accepted only
text, so a callerthat needed any other attribute had to re-implement the escaping rather than
reuse it.
cleanstays forced off, because reindenting the text wouldcontradict preserving it as original.