Sfoglia il codice sorgente

Reject wikilinks with explicitly empty titles

pull/373/head
Ishu Patel 1 settimana fa
parent
commit
aa39fcc6c5
4 ha cambiato i file con 52 aggiunte e 4 eliminazioni
  1. +12
    -1
      src/mwparserfromhell/parser/ctokenizer/tok_parse.c
  2. +8
    -0
      src/mwparserfromhell/parser/tokenizer.py
  3. +15
    -0
      tests/test_wikicode.py
  4. +17
    -3
      tests/tokenizer/wikilinks.mwtest

+ 12
- 1
src/mwparserfromhell/parser/ctokenizer/tok_parse.c Vedi File

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


+ 8
- 0
src/mwparserfromhell/parser/tokenizer.py Vedi File

@@ -408,12 +408,20 @@ class Tokenizer:

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



+ 15
- 0
tests/test_wikicode.py Vedi File

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

+ 17
- 3
tests/tokenizer/wikilinks.mwtest Vedi File

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

Caricamento…
Annulla
Salva