Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
v0.7.3 (unreleased):

- 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):

Expand Down
26 changes: 26 additions & 0 deletions src/mwparserfromhell/parser/ctokenizer/tok_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -2746,6 +2746,28 @@ Tokenizer_handle_end(Tokenizer *self, uint64_t context)
return Tokenizer_pop(self);
}

/*
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
everything is safe, or -1 if the route must be failed.
Expand Down Expand Up @@ -2785,6 +2807,10 @@ Tokenizer_verify_safe(Tokenizer *self, uint64_t context, Py_UCS4 data)
self->topstack->context |= LC_FAIL_NEXT;
return 0;
}
if ((data == '<' || data == '>') &&
Tokenizer_template_name_has_colon(self)) {
return 0;
}
if (data == '[' || data == ']' || data == '<' || data == '>') {
return -1;
}
Expand Down
16 changes: 16 additions & 0 deletions src/mwparserfromhell/parser/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,20 @@ def _handle_end(self):
self._fail_route()
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):
"""Make sure we are not trying to write an invalid character."""
context = self._context
Expand All @@ -1366,6 +1380,8 @@ def _verify_safe(self, this):
if this == "}" or (this == "<" and self._read(1) == "!"):
self._context |= contexts.FAIL_NEXT
return True
if this in ("<", ">") and self._template_name_has_colon():
return True
if this in ("[", "]", "<", ">"):
return False
if this == "|":
Expand Down
21 changes: 21 additions & 0 deletions tests/tokenizer/templates.mwtest
Original file line number Diff line number Diff line change
Expand Up @@ -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
label: nested template as an unnamed parameter
input: "{{foo|{{bar}}}}"
Expand Down