Skip to content
Merged
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
41 changes: 16 additions & 25 deletions src/wikitextprocessor/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1607,23 +1607,23 @@ def table_cell_fn(ctx: "Wtp", token: str) -> None:
# data cells
_parser_merge_str_children(ctx)
node = ctx.parser_stack[-1]
if (
not node.attrs
and len(node.children) == 1
and isinstance(attrs := node.children[0], str)
if node.kind in (
NodeKind.TABLE_CAPTION,
NodeKind.TABLE_HEADER_CELL,
NodeKind.TABLE_CELL,
):
if node.kind in (
NodeKind.TABLE_CAPTION,
NodeKind.TABLE_HEADER_CELL,
NodeKind.TABLE_CELL,
):
node.children.pop()
# Using the walrus operator and pop()ing without return
# is just to make the type-checker happy without using
# an assert that attrs is definitely a str...
parse_attrs(node, attrs)
if len(node.attrs) == 0:
if len(node.children) == 1 and isinstance(
attrs := node.children[0], str
):
node.children.pop()
# Using the walrus operator and pop()ing without return
# is just to make the type-checker happy without using
# an assert that attrs is definitely a str...
parse_attrs(node, attrs)
return
return text_fn(ctx, token)
else:
return text_fn(ctx, token)

while True:
node = ctx.parser_stack[-1]
Expand Down Expand Up @@ -1676,16 +1676,6 @@ def double_vbar_fn(ctx: "Wtp", token: str) -> None:
vbar_fn(ctx, "|")
return

# If it is at the beginning of a line, interpret it as starting a new
# cell, without any HTML attributes. We do this by emitting one vbar.
if ctx.beginning_of_line and ctx.begline_enabled:
if _parser_have(ctx, NodeKind.TABLE):
vbar_fn(ctx, "|")
else:
vbar_fn(ctx, "|")
vbar_fn(ctx, "|")
return

while True:
node = ctx.parser_stack[-1]
if node.kind == NodeKind.TABLE_ROW:
Expand Down Expand Up @@ -2147,6 +2137,7 @@ def magicword_fn(ctx: "Wtp", token: str) -> None:
r"!!",
r"\s*https?://[\w.-]+(/[^][{}<>|\s]*)?",
r"^[ \t]*!",
r"^\|",
r"\|\|",
r"\|",
r"^----+",
Expand Down
26 changes: 26 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2101,6 +2101,9 @@ def test_table_hdr4(self):
)
row = tree.children[0].children[0]
self.assertEqual(len(row.children), 3)
self.assertEqual(row.children[0].children, ["bar\n"])
self.assertEqual(row.children[1].children, ["baz\n"])
self.assertEqual(row.children[2].children, [" zap\n"])

def test_table_bang1(self):
# Testing that the single exclamation mark in the middle of a table
Expand All @@ -2115,6 +2118,29 @@ def test_table_bang1(self):
self.assertEqual(self.ctx.warnings, [])
self.assertEqual(self.ctx.debugs, [])

def test_table_triple_vbar(self):
# Testing parsing for an empty cell at the beginning of the row.
# en edition page "돌아가시다", Template:ko-conj/verb
tree = self.parse(
"test",
"""{|
|-
|||foo
|}""",
)
self.assertEqual(len(tree.children), 1)
t = tree.children[0]
self.assertEqual(t.kind, NodeKind.TABLE)
self.assertEqual(len(t.children), 1)
row = t.children[0]
self.assertEqual(row.kind, NodeKind.TABLE_ROW)
self.assertEqual(len(row.children), 2)
a, b = row.children
self.assertEqual(a.kind, NodeKind.TABLE_CELL)
self.assertEqual(a.children, [])
self.assertEqual(b.kind, NodeKind.TABLE_CELL)
self.assertEqual(b.children, ["foo\n"])

def test_error1(self):
self.parse("test", "'''")
self.assertEqual(self.ctx.errors, [])
Expand Down