瀏覽代碼

Keep parser functions containing < or > as templates so enclosing templates are not lost

pull/375/head
LE DUC MANH 5 天之前
父節點
當前提交
4e91dff28f
共有 4 個檔案被更改,包括 66 行新增 和 0 行删除
  1. +3
    -0
      CHANGELOG
  2. +26
    -0
      src/mwparserfromhell/parser/ctokenizer/tok_parse.c
  3. +16
    -0
      src/mwparserfromhell/parser/tokenizer.py
  4. +21
    -0
      tests/tokenizer/templates.mwtest

+ 3
- 0
CHANGELOG 查看文件

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



+ 26
- 0
src/mwparserfromhell/parser/ctokenizer/tok_parse.c 查看文件

@@ -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
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;
return 0;
}
if ((data == '<' || data == '>') &&
Tokenizer_template_name_has_colon(self)) {
return 0;
}
if (data == '[' || data == ']' || data == '<' || data == '>') {
return -1;
}


+ 16
- 0
src/mwparserfromhell/parser/tokenizer.py 查看文件

@@ -1341,6 +1341,20 @@ class Tokenizer:
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
@@ -1366,6 +1380,8 @@ class Tokenizer:
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 == "|":


+ 21
- 0
tests/tokenizer/templates.mwtest 查看文件

@@ -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}}}}"


Loading…
取消
儲存