diff --git a/mwparserfromhell/nodes/tag.py b/mwparserfromhell/nodes/tag.py index db60689..08d5204 100644 --- a/mwparserfromhell/nodes/tag.py +++ b/mwparserfromhell/nodes/tag.py @@ -37,7 +37,10 @@ class Tag(Node): closing_tag=None): super(Tag, self).__init__() self._tag = tag - self._contents = contents + if contents is None and not self_closing: + self._contents = parse_anything("") + else: + self._contents = contents self._attrs = attrs if attrs else [] self._showtag = showtag self._self_closing = self_closing @@ -51,7 +54,7 @@ class Tag(Node): def __unicode__(self): if not self.showtag: - open_, close = get_wikicode[self.tag] + open_, close = get_wikicode(self.tag) if self.self_closing: return open_ else: @@ -81,7 +84,7 @@ class Tag(Node): if self.contents: for child in getter(self.contents): yield self.contents, child - if not self.self_closing and self.closing_tag: + if not self.self_closing and self.showtag and self.closing_tag: for child in getter(self.closing_tag): yield self.closing_tag, child @@ -198,7 +201,13 @@ class Tag(Node): @padding.setter def padding(self, value): - self._padding = str(value) + if not value: + self._padding = "" + else: + value = str(value) + if not value.isspace(): + raise ValueError("padding must be entirely whitespace") + self._padding = value @closing_tag.setter def closing_tag(self, value): diff --git a/tests/test_tag.py b/tests/test_tag.py index e69de29..ed2416e 100644 --- a/tests/test_tag.py +++ b/tests/test_tag.py @@ -0,0 +1,134 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2012-2013 Ben Kurtovic +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from __future__ import unicode_literals +import unittest + +from mwparserfromhell.compat import str +from mwparserfromhell.nodes import Tag, Text +from mwparserfromhell.nodes.extras import Attribute +from ._test_tree_equality import TreeEqualityTestCase, getnodes, wrap, wraptext + +agen = lambda name, value: Attribute(wraptext(name), wraptext(value)) +agennv = lambda name: Attribute(wraptext(name)) +agennq = lambda name, value: Attribute(wraptext(name), wraptext(value), False) +agenp = lambda name, value, a, b, c: Attribute(wraptext(name), wraptext(value), + True, a, b, c) +agenpnv = lambda name, a, b, c: Attribute(wraptext(name), None, True, a, b, c) + +class TestTag(TreeEqualityTestCase): + """Test cases for the Tag node.""" + + def test_unicode(self): + """test Tag.__unicode__()""" + node1 = Tag(wraptext("ref")) + node2 = Tag(wraptext("span"), wraptext("foo"), + [agen("style", "color: red;")]) + node3 = Tag(wraptext("ref"), + attrs=[agennq("name", "foo"), + agenpnv("some_attr", " ", "", "")], + self_closing=True) + node4 = Tag(wraptext("br"), self_closing=True, padding=" ") + node5 = Tag(wraptext("br"), self_closing=True, implicit=True) + node6 = Tag(wraptext("br"), self_closing=True, invalid=True, + implicit=True) + node7 = Tag(wraptext("br"), self_closing=True, invalid=True, + padding=" ") + node8 = Tag(wraptext("hr"), showtag=False, self_closing=True) + node9 = Tag(wraptext("i"), wraptext("italics!"), showtag=False) + + self.assertEqual("", str(node1)) + self.assertEqual('foo', str(node2)) + self.assertEqual("", str(node3)) + self.assertEqual("
", str(node4)) + self.assertEqual("
", str(node5)) + self.assertEqual("
", str(node6)) + self.assertEqual("
", str(node7)) + self.assertEqual("----", str(node8)) + self.assertEqual("''italics!''", str(node9)) + + def test_iternodes(self): + """test Tag.__iternodes__()""" + node1n1, node1n2 = Text("ref"), Text("foobar") + node2n1, node3n1, node3n2 = Text("bold text"), Text("img"), Text("id") + node3n3, node3n4, node3n5 = Text("foo"), Text("class"), Text("bar") + + # foobar + node1 = Tag(wrap([node1n1]), wrap([node1n2])) + # '''bold text''' + node2 = Tag(wraptext("i"), wrap([node2n1]), showtag=False) + # + node3 = Tag(wrap([node3n1]), + attrs=[Attribute(wrap([node3n2]), wrap([node3n3])), + Attribute(wrap([node3n4]), wrap([node3n5]))], + self_closing=True, padding=" ") + + gen1 = node1.__iternodes__(getnodes) + gen2 = node2.__iternodes__(getnodes) + gen3 = node3.__iternodes__(getnodes) + self.assertEqual((None, node1), next(gen1)) + self.assertEqual((None, node2), next(gen2)) + self.assertEqual((None, node3), next(gen3)) + self.assertEqual((node1.tag, node1n1), next(gen1)) + self.assertEqual((node3.tag, node3n1), next(gen3)) + self.assertEqual((node3.attributes[0].name, node3n2), next(gen3)) + self.assertEqual((node3.attributes[0].value, node3n3), next(gen3)) + self.assertEqual((node3.attributes[1].name, node3n4), next(gen3)) + self.assertEqual((node3.attributes[1].value, node3n5), next(gen3)) + self.assertEqual((node1.contents, node1n2), next(gen1)) + self.assertEqual((node2.contents, node2n1), next(gen2)) + self.assertEqual((node1.closing_tag, node1n1), next(gen1)) + self.assertRaises(StopIteration, next, gen1) + self.assertRaises(StopIteration, next, gen2) + self.assertRaises(StopIteration, next, gen3) + + def test_strip(self): + """test Tag.__strip__()""" + node1 = Tag(wraptext("i"), wraptext("foobar")) + node2 = Tag(wraptext("math"), wraptext("foobar")) + for a in (True, False): + for b in (True, False): + self.assertEqual("foobar", node1.__strip__(a, b)) + self.assertEqual(None, node2.__strip__(a, b)) + + def test_showtree(self): + """test Tag.__showtree__()""" + output = [] + getter, marker = object(), object() + get = lambda code: output.append((getter, code)) + mark = lambda: output.append(marker) + node1 = Tag(wraptext("ref"), wraptext("text"), [agen("name", "foo")]) + node2 = Tag(wraptext("br"), self_closing=True, padding=" ") + node3 = Tag(wraptext("br"), self_closing=True, invalid=True, + implicit=True, padding=" ") + node1.__showtree__(output.append, get, mark) + node2.__showtree__(output.append, get, mark) + node3.__showtree__(output.append, get, mark) + valid = [ + "<", (getter, node1.tag), (getter, node1.attributes[0].name), + " = ", marker, (getter, node1.attributes[0].value), ">", + (getter, node1.contents), "", + "<", (getter, node2.tag), "/>", ""] + self.assertEqual(valid, output) + +if __name__ == "__main__": + unittest.main(verbosity=2)