| @@ -1,6 +1,9 @@ | |||||
| v0.7.3 (unreleased): | v0.7.3 (unreleased): | ||||
| - Add support for Python 3.14 and drop end-of-life 3.9. | - Add support for Python 3.14 and drop end-of-life 3.9. | ||||
| - Fix parser functions containing '<' or '>' (like '{{#ifexpr: x <= 2 | ...}}') | |||||
| not being parsed as templates, which could cause enclosing templates to be | |||||
| lost. | |||||
| v0.7.2 (released July 1, 2025): | v0.7.2 (released July 1, 2025): | ||||
| @@ -2747,6 +2747,28 @@ Tokenizer_handle_end(Tokenizer *self, uint64_t context) | |||||
| } | } | ||||
| /* | /* | ||||
| Return whether the current template name has a top-level colon, as in | |||||
| parser functions like {{#ifexpr: ...}}. | |||||
| */ | |||||
| static int | |||||
| Tokenizer_template_name_has_colon(Tokenizer *self) | |||||
| { | |||||
| Py_ssize_t i; | |||||
| Py_UCS4 c; | |||||
| for (i = self->topstack->ident.head; i < self->head; i++) { | |||||
| c = PyUnicode_READ(self->text.kind, self->text.data, i); | |||||
| if (c == ':') { | |||||
| return 1; | |||||
| } | |||||
| if (c == '{') { | |||||
| return 0; | |||||
| } | |||||
| } | |||||
| return 0; | |||||
| } | |||||
| /* | |||||
| Make sure we are not trying to write an invalid character. Return 0 if | Make sure we are not trying to write an invalid character. Return 0 if | ||||
| everything is safe, or -1 if the route must be failed. | everything is safe, or -1 if the route must be failed. | ||||
| */ | */ | ||||
| @@ -2785,6 +2807,10 @@ Tokenizer_verify_safe(Tokenizer *self, uint64_t context, Py_UCS4 data) | |||||
| self->topstack->context |= LC_FAIL_NEXT; | self->topstack->context |= LC_FAIL_NEXT; | ||||
| return 0; | return 0; | ||||
| } | } | ||||
| if ((data == '<' || data == '>') && | |||||
| Tokenizer_template_name_has_colon(self)) { | |||||
| return 0; | |||||
| } | |||||
| if (data == '[' || data == ']' || data == '<' || data == '>') { | if (data == '[' || data == ']' || data == '<' || data == '>') { | ||||
| return -1; | return -1; | ||||
| } | } | ||||
| @@ -1341,6 +1341,20 @@ class Tokenizer: | |||||
| self._fail_route() | self._fail_route() | ||||
| return self._pop() | return self._pop() | ||||
| def _template_name_has_colon(self): | |||||
| """Return whether the current template name has a top-level colon. | |||||
| This is the case for parser functions like ``{{#ifexpr: ...}}``, where | |||||
| the text after the colon is really an argument and may contain | |||||
| characters (like ``<`` or ``>``) that are invalid in page titles. | |||||
| """ | |||||
| for segment in self._text[self._stack_ident[0] : self._head]: | |||||
| if segment == ":": | |||||
| return True | |||||
| if segment == "{": | |||||
| return False | |||||
| return False | |||||
| def _verify_safe(self, this): | def _verify_safe(self, this): | ||||
| """Make sure we are not trying to write an invalid character.""" | """Make sure we are not trying to write an invalid character.""" | ||||
| context = self._context | context = self._context | ||||
| @@ -1366,6 +1380,8 @@ class Tokenizer: | |||||
| if this == "}" or (this == "<" and self._read(1) == "!"): | if this == "}" or (this == "<" and self._read(1) == "!"): | ||||
| self._context |= contexts.FAIL_NEXT | self._context |= contexts.FAIL_NEXT | ||||
| return True | return True | ||||
| if this in ("<", ">") and self._template_name_has_colon(): | |||||
| return True | |||||
| if this in ("[", "]", "<", ">"): | if this in ("[", "]", "<", ">"): | ||||
| return False | return False | ||||
| if this == "|": | if this == "|": | ||||
| @@ -54,6 +54,27 @@ output: [TemplateOpen(), Text(text=","), TemplateParamSeparator(), TemplateParam | |||||
| --- | --- | ||||
| name: parser_function_less_equals | |||||
| label: parser function with <= in its first argument | |||||
| input: "{{#ifexpr: 1 <= 2 | y}}" | |||||
| output: [TemplateOpen(), Text(text="#ifexpr: 1 <= 2 "), TemplateParamSeparator(), Text(text=" y"), TemplateClose()] | |||||
| --- | |||||
| name: parser_function_less_equals_nested | |||||
| label: parser function with <= nested in templates (issue: outer template lost) | |||||
| input: "{{a|b={{s|{{#ifexpr: {{X}} <= 2 | {{Y}} | {{Z}} }}|02}}}}" | |||||
| output: [TemplateOpen(), Text(text="a"), TemplateParamSeparator(), Text(text="b"), TemplateParamEquals(), TemplateOpen(), Text(text="s"), TemplateParamSeparator(), TemplateOpen(), Text(text="#ifexpr: "), TemplateOpen(), Text(text="X"), TemplateClose(), Text(text=" <= 2 "), TemplateParamSeparator(), Text(text=" "), TemplateOpen(), Text(text="Y"), TemplateClose(), Text(text=" "), TemplateParamSeparator(), Text(text=" "), TemplateOpen(), Text(text="Z"), TemplateClose(), Text(text=" "), TemplateClose(), TemplateParamSeparator(), Text(text="02"), TemplateClose(), TemplateClose()] | |||||
| --- | |||||
| name: parser_function_greater_equals | |||||
| label: parser function with >= in its first argument | |||||
| input: "{{#ifexpr: 1 >= 2 | y}}" | |||||
| output: [TemplateOpen(), Text(text="#ifexpr: 1 >= 2 "), TemplateParamSeparator(), Text(text=" y"), TemplateClose()] | |||||
| --- | |||||
| name: nested_unnamed_param | name: nested_unnamed_param | ||||
| label: nested template as an unnamed parameter | label: nested template as an unnamed parameter | ||||
| input: "{{foo|{{bar}}}}" | input: "{{foo|{{bar}}}}" | ||||