wmlparser3: Fix [+foo] without preceding [foo] in toplevel

This commit is contained in:
josteph 2018-11-12 02:51:11 +00:00
parent b94ccf9020
commit 711434d571

View file

@ -172,7 +172,7 @@ class TagNode:
s = b"[" + self.name + b"]\n"
for sub in self.data:
s += sub.wml() + b"\n"
s += b"[/" + self.name + b"]\n"
s += b"[/" + self.name.lstrip(b'+') + b"]\n"
return s
def debug(self):
@ -180,7 +180,7 @@ class TagNode:
for sub in self.data:
for subline in sub.debug().splitlines():
s += " %s\n" % subline
s += "[/%s]\n" % self.name.decode("utf8")
s += "[/%s]\n" % self.name.decode("utf8").lstrip('+')
return s
def get_all(self, **kw):
@ -506,7 +506,7 @@ class Parser:
self.in_tag = b""
if tag.startswith(b"/"):
self.parent_node = self.parent_node[:-1]
elif tag.startswith(b"+"):
elif tag.startswith(b"+") and self.parent_node and self.parent_node[-1].get_all(tag = tag[1:].decode()):
node_to_append_to = self.parent_node[-1].get_all(tag = tag[1:].decode())[-1]
self.parent_node.append(node_to_append_to)
else:
@ -723,6 +723,32 @@ a=1
test(
"""
[+foo]
a=1
[/foo]
""", """
[+foo]
a='1'
[/foo]
""", "+foo without foo in toplevel")
test(
"""
[foo]
[+bar]
a=1
[/bar]
[/foo]
""", """
[foo]
[+bar]
a='1'
[/bar]
[/foo]
""", "+foo without foo in child")
test(
"""
[test]
[foo]
a=1