Skip to content

fix: allow % and } in {%p %}/{%tr %}/{%tc %}/{%r %} expressions#645

Open
Noethix55555 wants to merge 1 commit into
elapouya:masterfrom
Noethix55555:fix/patch-xml-expr-modulo-brace
Open

fix: allow % and } in {%p %}/{%tr %}/{%tc %}/{%r %} expressions#645
Noethix55555 wants to merge 1 commit into
elapouya:masterfrom
Noethix55555:fix/patch-xml-expr-modulo-brace

Conversation

@Noethix55555

Copy link
Copy Markdown

Fixes #644.

patch_xml matched the expression body of these directive tags with [^}%]*, which stops at the first % or }. Tags whose expression contains a modulo (if i % 2 == 0) or a literal brace (set d = {"x":1}) were not stripped of their surrounding <w:p>/<w:tr>/<w:tc>/<w:r> wrapper, so the literal {%p ...%} reached Jinja and raised TemplateSyntaxError: Encountered unknown tag 'p'.

The fix matches up to the closing %}/}} instead of excluding those characters:

([^}%%]*(?:%%}|}}))  ->  ((?:(?!%%}|}}).)*(?:%%}|}}))

Verified with a real render: a template using {%p if i % 2 == 0 %} raises TemplateSyntaxError on the current code and renders correctly with the fix. The representative existing fixture tests (dynamic_table, nested_for, vertical_merge, horizontal_merge, less_cells_after_loop, comments, escape, merge_paragraph, richtext, order and others) still pass.

CI runs flake8 only and the existing tests are binary .docx fixtures with no assertions, so I did not add a fixture; the reproduction above is in the linked issue. Happy to add a fixture-based test if you prefer.

patch_xml matched the expression body of these directive tags with
[^}%]*, which stops at the first % or }. Tags whose expression contains a
modulo (if i % 2 == 0) or a literal brace (set d = {"x":1}) were not
stripped of their surrounding <w:p>/<w:tr>/<w:tc>/<w:r> wrapper, so the
literal {%p ...%} reached Jinja and raised TemplateSyntaxError:
Encountered unknown tag 'p'.

Match up to the closing %} or }} instead of excluding the characters.
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.

{%p %} and similar tags fail when the expression contains % or }

1 participant