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
13 changes: 12 additions & 1 deletion src/mwparserfromhell/parser/ctokenizer/tok_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
}
Expand Down
8 changes: 8 additions & 0 deletions src/mwparserfromhell/parser/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
15 changes: 15 additions & 0 deletions tests/test_wikicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 17 additions & 3 deletions tests/tokenizer/wikilinks.mwtest
Original file line number Diff line number Diff line change
@@ -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="[[|]]")]

---

Expand All @@ -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="[[|||]]")]

---

Expand Down Expand Up @@ -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()]