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
6 changes: 6 additions & 0 deletions src/mwparserfromhell/wikicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ def getter(i: int, node: Node) -> Generator[tuple[int, Node]]:
else:
inodes = enumerate(self.nodes)
for i, node in inodes:
if (
forcetype is Wikilink
and isinstance(node, Wikilink)
and not str(node.title)
):
continue
if (forcetype is None or isinstance(node, forcetype)) and match(
cast(N, node)
):
Expand Down
26 changes: 26 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,29 @@ def test_skip_style_tags(pyparser):
without_style = parser.Parser().parse(text, skip_style_tags=True)
assert_wikicode_equal(a, with_style)
assert_wikicode_equal(b, without_style)


@pytest.mark.parametrize(
"text",
[
"[[]]",
"[[|]]",
"[[|foo]]",
"[[|||]]",
],
)
def test_empty_title_wikilink_filter_wikilinks(text):
"""[[]] and [[|...]] have an empty title and should not be links.

MediaWiki does not render these as hyperlinks; filter_wikilinks() should
not return false positives, while the parsed Wikilink node remains
available to tools that want to detect and fix invalid wikilinks.
Regression test for https://github.com/earwig/mwparserfromhell/issues/292.
"""
parsed = parser.Parser().parse(text)
assert parsed.filter_wikilinks() == []
assert parsed.filter(forcetype=Wikilink) == []
assert list(parsed.ifilter(forcetype=Wikilink)) == []
assert isinstance(parsed.get(0), Wikilink)
assert parsed.get(0) in parsed.filter()
assert str(parsed) == text