From aa39fcc6c596e68a6814d24205f14bc792ab912c Mon Sep 17 00:00:00 2001 From: Ishu Patel <258577714+ishu7w@users.noreply.github.com> Date: Fri, 27 Nov 2026 13:55:45 +0530 Subject: [PATCH] Reject wikilinks with explicitly empty titles --- .../parser/ctokenizer/tok_parse.c | 13 +++++++++++- src/mwparserfromhell/parser/tokenizer.py | 8 ++++++++ tests/test_wikicode.py | 15 ++++++++++++++ tests/tokenizer/wikilinks.mwtest | 20 ++++++++++++++++--- 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/mwparserfromhell/parser/ctokenizer/tok_parse.c b/src/mwparserfromhell/parser/ctokenizer/tok_parse.c index 72c4beda..0fbc2188 100644 --- a/src/mwparserfromhell/parser/ctokenizer/tok_parse.c +++ b/src/mwparserfromhell/parser/ctokenizer/tok_parse.c @@ -462,6 +462,11 @@ Tokenizer_parse_wikilink(Tokenizer *self) static int Tokenizer_handle_wikilink_separator(Tokenizer *self) { + if (!PyList_GET_SIZE(self->topstack->stack) && + !self->topstack->textbuffer->length) { + Tokenizer_fail_route(self); + return -1; + } self->topstack->context ^= LC_WIKILINK_TITLE; self->topstack->context |= LC_WIKILINK_TEXT; if (Tokenizer_emit(self, WikilinkSeparator)) { @@ -476,7 +481,13 @@ Tokenizer_handle_wikilink_separator(Tokenizer *self) static PyObject * Tokenizer_handle_wikilink_end(Tokenizer *self) { - PyObject *stack = Tokenizer_pop(self); + PyObject *stack; + if ((self->topstack->context & LC_WIKILINK_TITLE) && + !PyList_GET_SIZE(self->topstack->stack) && + !self->topstack->textbuffer->length) { + return Tokenizer_fail_route(self); + } + stack = Tokenizer_pop(self); self->head += 1; return stack; } diff --git a/src/mwparserfromhell/parser/tokenizer.py b/src/mwparserfromhell/parser/tokenizer.py index 92310807..c19bb17b 100644 --- a/src/mwparserfromhell/parser/tokenizer.py +++ b/src/mwparserfromhell/parser/tokenizer.py @@ -408,12 +408,20 @@ def _parse_wikilink(self): def _handle_wikilink_separator(self): """Handle the separator between a wikilink's title and its text.""" + if not self._stack and not self._textbuffer: + self._fail_route() self._context ^= contexts.WIKILINK_TITLE self._context |= contexts.WIKILINK_TEXT self._emit(tokens.WikilinkSeparator()) def _handle_wikilink_end(self): """Handle the end of a wikilink at the head of the string.""" + if ( + self._context & contexts.WIKILINK_TITLE + and not self._stack + and not self._textbuffer + ): + self._fail_route() self._head += 1 return self._pop() diff --git a/tests/test_wikicode.py b/tests/test_wikicode.py index 2ba23ace..077461ef 100644 --- a/tests/test_wikicode.py +++ b/tests/test_wikicode.py @@ -641,3 +641,18 @@ def test_get_tree(): + "{{\n\t\t\tbaz\n\t }}\n\t| spam\n\t= eggs\n}}" ) assert expected.expandtabs(4) == code.get_tree() + + +@pytest.mark.parametrize("text", ("[[]]", "[[|]]", "[[|||]]", "before [[]] after")) +def test_empty_wikilink_titles_are_text(text): + code = parse(text) + assert str(code) == text + assert code.filter_wikilinks() == [] + assert code.strip_code() == text + + +@pytest.mark.parametrize("text", ("[[{{echo|}}]]", "[[{{!}}]]", "[[{{{title|}}}]]")) +def test_dynamic_wikilink_titles_are_preserved(text): + code = parse(text) + assert str(code) == text + assert len(code.filter_wikilinks()) == 1 diff --git a/tests/tokenizer/wikilinks.mwtest b/tests/tokenizer/wikilinks.mwtest index 34651d7b..20b9b2b9 100644 --- a/tests/tokenizer/wikilinks.mwtest +++ b/tests/tokenizer/wikilinks.mwtest @@ -1,14 +1,14 @@ name: blank label: wikilink with no content input: "[[]]" -output: [WikilinkOpen(), WikilinkClose()] +output: [Text(text="[[]]")] --- name: blank_with_text label: wikilink with no content but a pipe input: "[[|]]" -output: [WikilinkOpen(), WikilinkSeparator(), WikilinkClose()] +output: [Text(text="[[|]]")] --- @@ -29,7 +29,7 @@ output: [WikilinkOpen(), Text(text="foo"), WikilinkSeparator(), Text(text="bar") name: blank_with_multiple_texts label: no content, multiple pipes input: "[[|||]]" -output: [WikilinkOpen(), WikilinkSeparator(), Text(text="||"), WikilinkClose()] +output: [Text(text="[[|||]]")] --- @@ -205,3 +205,17 @@ name: incomplete_preserve_previous label: incomplete wikilinks: a valid wikilink followed by an invalid one input: "[[foo]] [[bar" output: [WikilinkOpen(), Text(text="foo"), WikilinkClose(), Text(text=" [[bar")] + +--- + +name: empty_title_followed_by_link +label: rejecting an empty title does not prevent a subsequent valid link +input: "[[]][[valid]]" +output: [Text(text="[[]]"), WikilinkOpen(), Text(text="valid"), WikilinkClose()] + +--- + +name: template_title_may_expand_empty +label: templates in titles must be preserved without attempting expansion +input: "[[{{echo|}}]]" +output: [WikilinkOpen(), TemplateOpen(), Text(text="echo"), TemplateParamSeparator(), TemplateClose(), WikilinkClose()]