A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

335 rader
15 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2014 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. from __future__ import unicode_literals
  23. try:
  24. import unittest2 as unittest
  25. except ImportError:
  26. import unittest
  27. from mwparserfromhell.compat import str
  28. from mwparserfromhell.nodes import Tag, Template, Text
  29. from mwparserfromhell.nodes.extras import Attribute
  30. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  31. agen = lambda name, value: Attribute(wraptext(name), wraptext(value))
  32. agennv = lambda name: Attribute(wraptext(name))
  33. agennq = lambda name, value: Attribute(wraptext(name), wraptext(value), None)
  34. agenp = lambda name, v, a, b, c: Attribute(wraptext(name), v, '"', a, b, c)
  35. agenpnv = lambda name, a, b, c: Attribute(wraptext(name), None, '"', a, b, c)
  36. class TestTag(TreeEqualityTestCase):
  37. """Test cases for the Tag node."""
  38. def test_unicode(self):
  39. """test Tag.__unicode__()"""
  40. node1 = Tag(wraptext("ref"))
  41. node2 = Tag(wraptext("span"), wraptext("foo"),
  42. [agen("style", "color: red;")])
  43. node3 = Tag(wraptext("ref"),
  44. attrs=[agennq("name", "foo"),
  45. agenpnv("some_attr", " ", "", "")],
  46. self_closing=True)
  47. node4 = Tag(wraptext("br"), self_closing=True, padding=" ")
  48. node5 = Tag(wraptext("br"), self_closing=True, implicit=True)
  49. node6 = Tag(wraptext("br"), self_closing=True, invalid=True,
  50. implicit=True)
  51. node7 = Tag(wraptext("br"), self_closing=True, invalid=True,
  52. padding=" ")
  53. node8 = Tag(wraptext("hr"), wiki_markup="----", self_closing=True)
  54. node9 = Tag(wraptext("i"), wraptext("italics!"), wiki_markup="''")
  55. self.assertEqual("<ref></ref>", str(node1))
  56. self.assertEqual('<span style="color: red;">foo</span>', str(node2))
  57. self.assertEqual("<ref name=foo some_attr/>", str(node3))
  58. self.assertEqual("<br />", str(node4))
  59. self.assertEqual("<br>", str(node5))
  60. self.assertEqual("</br>", str(node6))
  61. self.assertEqual("</br />", str(node7))
  62. self.assertEqual("----", str(node8))
  63. self.assertEqual("''italics!''", str(node9))
  64. def test_children(self):
  65. """test Tag.__children__()"""
  66. # <ref>foobar</ref>
  67. node1 = Tag(wraptext("ref"), wraptext("foobar"))
  68. # '''bold text'''
  69. node2 = Tag(wraptext("b"), wraptext("bold text"), wiki_markup="'''")
  70. # <img id="foo" class="bar" selected />
  71. node3 = Tag(wraptext("img"),
  72. attrs=[agen("id", "foo"), agen("class", "bar"),
  73. agennv("selected")],
  74. self_closing=True, padding=" ")
  75. gen1 = node1.__children__()
  76. gen2 = node2.__children__()
  77. gen3 = node3.__children__()
  78. self.assertEqual(node1.tag, next(gen1))
  79. self.assertEqual(node3.tag, next(gen3))
  80. self.assertEqual(node3.attributes[0].name, next(gen3))
  81. self.assertEqual(node3.attributes[0].value, next(gen3))
  82. self.assertEqual(node3.attributes[1].name, next(gen3))
  83. self.assertEqual(node3.attributes[1].value, next(gen3))
  84. self.assertEqual(node3.attributes[2].name, next(gen3))
  85. self.assertEqual(node1.contents, next(gen1))
  86. self.assertEqual(node2.contents, next(gen2))
  87. self.assertEqual(node1.closing_tag, next(gen1))
  88. self.assertRaises(StopIteration, next, gen1)
  89. self.assertRaises(StopIteration, next, gen2)
  90. self.assertRaises(StopIteration, next, gen3)
  91. def test_strip(self):
  92. """test Tag.__strip__()"""
  93. node1 = Tag(wraptext("i"), wraptext("foobar"))
  94. node2 = Tag(wraptext("math"), wraptext("foobar"))
  95. node3 = Tag(wraptext("br"), self_closing=True)
  96. for a in (True, False):
  97. for b in (True, False):
  98. self.assertEqual("foobar", node1.__strip__(a, b))
  99. self.assertEqual(None, node2.__strip__(a, b))
  100. self.assertEqual(None, node3.__strip__(a, b))
  101. def test_showtree(self):
  102. """test Tag.__showtree__()"""
  103. output = []
  104. getter, marker = object(), object()
  105. get = lambda code: output.append((getter, code))
  106. mark = lambda: output.append(marker)
  107. node1 = Tag(wraptext("ref"), wraptext("text"),
  108. [agen("name", "foo"), agennv("selected")])
  109. node2 = Tag(wraptext("br"), self_closing=True, padding=" ")
  110. node3 = Tag(wraptext("br"), self_closing=True, invalid=True,
  111. implicit=True, padding=" ")
  112. node1.__showtree__(output.append, get, mark)
  113. node2.__showtree__(output.append, get, mark)
  114. node3.__showtree__(output.append, get, mark)
  115. valid = [
  116. "<", (getter, node1.tag), (getter, node1.attributes[0].name),
  117. " = ", marker, (getter, node1.attributes[0].value),
  118. (getter, node1.attributes[1].name), ">", (getter, node1.contents),
  119. "</", (getter, node1.closing_tag), ">", "<", (getter, node2.tag),
  120. "/>", "</", (getter, node3.tag), ">"]
  121. self.assertEqual(valid, output)
  122. def test_tag(self):
  123. """test getter/setter for the tag attribute"""
  124. tag = wraptext("ref")
  125. node = Tag(tag, wraptext("text"))
  126. self.assertIs(tag, node.tag)
  127. self.assertIs(tag, node.closing_tag)
  128. node.tag = "span"
  129. self.assertWikicodeEqual(wraptext("span"), node.tag)
  130. self.assertWikicodeEqual(wraptext("span"), node.closing_tag)
  131. self.assertEqual("<span>text</span>", node)
  132. def test_contents(self):
  133. """test getter/setter for the contents attribute"""
  134. contents = wraptext("text")
  135. node = Tag(wraptext("ref"), contents)
  136. self.assertIs(contents, node.contents)
  137. node.contents = "text and a {{template}}"
  138. parsed = wrap([Text("text and a "), Template(wraptext("template"))])
  139. self.assertWikicodeEqual(parsed, node.contents)
  140. self.assertEqual("<ref>text and a {{template}}</ref>", node)
  141. def test_attributes(self):
  142. """test getter for the attributes attribute"""
  143. attrs = [agen("name", "bar")]
  144. node1 = Tag(wraptext("ref"), wraptext("foo"))
  145. node2 = Tag(wraptext("ref"), wraptext("foo"), attrs)
  146. self.assertEqual([], node1.attributes)
  147. self.assertIs(attrs, node2.attributes)
  148. def test_wiki_markup(self):
  149. """test getter/setter for the wiki_markup attribute"""
  150. node = Tag(wraptext("i"), wraptext("italic text"))
  151. self.assertIs(None, node.wiki_markup)
  152. node.wiki_markup = "''"
  153. self.assertEqual("''", node.wiki_markup)
  154. self.assertEqual("''italic text''", node)
  155. node.wiki_markup = False
  156. self.assertFalse(node.wiki_markup)
  157. self.assertEqual("<i>italic text</i>", node)
  158. def test_closing_wiki_markup(self):
  159. """test getter/setter behavior for closing_wiki_markup attribute"""
  160. node = Tag(wraptext("table"), wraptext("\n"))
  161. self.assertIs(None, node.closing_wiki_markup)
  162. node.wiki_markup = "{|"
  163. self.assertEqual("{|", node.closing_wiki_markup)
  164. node.closing_wiki_markup = "|}"
  165. self.assertEqual("|}", node.closing_wiki_markup)
  166. self.assertEqual("{|\n|}", node)
  167. node.wiki_markup = False
  168. self.assertFalse(node.closing_wiki_markup)
  169. self.assertEqual("<table>\n</table>", node)
  170. def test_self_closing(self):
  171. """test getter/setter for the self_closing attribute"""
  172. node = Tag(wraptext("ref"), wraptext("foobar"))
  173. self.assertFalse(node.self_closing)
  174. node.self_closing = True
  175. self.assertTrue(node.self_closing)
  176. self.assertEqual("<ref/>", node)
  177. node.self_closing = 0
  178. self.assertFalse(node.self_closing)
  179. self.assertEqual("<ref>foobar</ref>", node)
  180. def test_invalid(self):
  181. """test getter/setter for the invalid attribute"""
  182. node = Tag(wraptext("br"), self_closing=True, implicit=True)
  183. self.assertFalse(node.invalid)
  184. node.invalid = True
  185. self.assertTrue(node.invalid)
  186. self.assertEqual("</br>", node)
  187. node.invalid = 0
  188. self.assertFalse(node.invalid)
  189. self.assertEqual("<br>", node)
  190. def test_implicit(self):
  191. """test getter/setter for the implicit attribute"""
  192. node = Tag(wraptext("br"), self_closing=True)
  193. self.assertFalse(node.implicit)
  194. node.implicit = True
  195. self.assertTrue(node.implicit)
  196. self.assertEqual("<br>", node)
  197. node.implicit = 0
  198. self.assertFalse(node.implicit)
  199. self.assertEqual("<br/>", node)
  200. def test_padding(self):
  201. """test getter/setter for the padding attribute"""
  202. node = Tag(wraptext("ref"), wraptext("foobar"))
  203. self.assertEqual("", node.padding)
  204. node.padding = " "
  205. self.assertEqual(" ", node.padding)
  206. self.assertEqual("<ref >foobar</ref>", node)
  207. node.padding = None
  208. self.assertEqual("", node.padding)
  209. self.assertEqual("<ref>foobar</ref>", node)
  210. self.assertRaises(ValueError, setattr, node, "padding", True)
  211. def test_closing_tag(self):
  212. """test getter/setter for the closing_tag attribute"""
  213. tag = wraptext("ref")
  214. node = Tag(tag, wraptext("foobar"))
  215. self.assertIs(tag, node.closing_tag)
  216. node.closing_tag = "ref {{ignore me}}"
  217. parsed = wrap([Text("ref "), Template(wraptext("ignore me"))])
  218. self.assertWikicodeEqual(parsed, node.closing_tag)
  219. self.assertEqual("<ref>foobar</ref {{ignore me}}>", node)
  220. def test_has(self):
  221. """test Tag.has()"""
  222. node = Tag(wraptext("ref"), wraptext("cite"), [agen("name", "foo")])
  223. self.assertTrue(node.has("name"))
  224. self.assertTrue(node.has(" name "))
  225. self.assertTrue(node.has(wraptext("name")))
  226. self.assertFalse(node.has("Name"))
  227. self.assertFalse(node.has("foo"))
  228. attrs = [agen("id", "foo"), agenp("class", "bar", " ", "\n", "\n"),
  229. agen("foo", "bar"), agenpnv("foo", " ", " \n ", " \t")]
  230. node2 = Tag(wraptext("div"), attrs=attrs, self_closing=True)
  231. self.assertTrue(node2.has("id"))
  232. self.assertTrue(node2.has("class"))
  233. self.assertTrue(node2.has(attrs[1].pad_first + str(attrs[1].name) +
  234. attrs[1].pad_before_eq))
  235. self.assertTrue(node2.has(attrs[3]))
  236. self.assertTrue(node2.has(str(attrs[3])))
  237. self.assertFalse(node2.has("idclass"))
  238. self.assertFalse(node2.has("id class"))
  239. self.assertFalse(node2.has("id=foo"))
  240. def test_get(self):
  241. """test Tag.get()"""
  242. attrs = [agen("name", "foo")]
  243. node = Tag(wraptext("ref"), wraptext("cite"), attrs)
  244. self.assertIs(attrs[0], node.get("name"))
  245. self.assertIs(attrs[0], node.get(" name "))
  246. self.assertIs(attrs[0], node.get(wraptext("name")))
  247. self.assertRaises(ValueError, node.get, "Name")
  248. self.assertRaises(ValueError, node.get, "foo")
  249. attrs = [agen("id", "foo"), agenp("class", "bar", " ", "\n", "\n"),
  250. agen("foo", "bar"), agenpnv("foo", " ", " \n ", " \t")]
  251. node2 = Tag(wraptext("div"), attrs=attrs, self_closing=True)
  252. self.assertIs(attrs[0], node2.get("id"))
  253. self.assertIs(attrs[1], node2.get("class"))
  254. self.assertIs(attrs[1], node2.get(
  255. attrs[1].pad_first + str(attrs[1].name) + attrs[1].pad_before_eq))
  256. self.assertIs(attrs[3], node2.get(attrs[3]))
  257. self.assertIs(attrs[3], node2.get(str(attrs[3])))
  258. self.assertIs(attrs[3], node2.get(" foo"))
  259. self.assertRaises(ValueError, node2.get, "idclass")
  260. self.assertRaises(ValueError, node2.get, "id class")
  261. self.assertRaises(ValueError, node2.get, "id=foo")
  262. def test_add(self):
  263. """test Tag.add()"""
  264. node = Tag(wraptext("ref"), wraptext("cite"))
  265. node.add("name", "value")
  266. node.add("name", "value", quotes=None)
  267. node.add("name", "value", quotes="'")
  268. node.add("name")
  269. node.add(1, False)
  270. node.add("style", "{{foobar}}")
  271. node.add("name", "value", '"', "\n", " ", " ")
  272. attr1 = ' name="value"'
  273. attr2 = " name=value"
  274. attr3 = " name='value'"
  275. attr4 = " name"
  276. attr5 = ' 1="False"'
  277. attr6 = ' style="{{foobar}}"'
  278. attr7 = '\nname = "value"'
  279. self.assertEqual(attr1, node.attributes[0])
  280. self.assertEqual(attr2, node.attributes[1])
  281. self.assertEqual(attr3, node.attributes[2])
  282. self.assertEqual(attr4, node.attributes[3])
  283. self.assertEqual(attr5, node.attributes[4])
  284. self.assertEqual(attr6, node.attributes[5])
  285. self.assertEqual(attr7, node.attributes[6])
  286. self.assertEqual(attr7, node.get("name"))
  287. self.assertWikicodeEqual(wrap([Template(wraptext("foobar"))]),
  288. node.attributes[5].value)
  289. self.assertEqual("".join(("<ref", attr1, attr2, attr3, attr4, attr5,
  290. attr6, attr7, ">cite</ref>")), node)
  291. self.assertRaises(ValueError, node.add, "name", "foo", quotes="bar")
  292. self.assertRaises(ValueError, node.add, "name", "a bc d", quotes=None)
  293. def test_remove(self):
  294. """test Tag.remove()"""
  295. attrs = [agen("id", "foo"), agenp("class", "bar", " ", "\n", "\n"),
  296. agen("foo", "bar"), agenpnv("foo", " ", " \n ", " \t")]
  297. node = Tag(wraptext("div"), attrs=attrs, self_closing=True)
  298. node.remove("class")
  299. self.assertEqual('<div id="foo" foo="bar" foo \n />', node)
  300. node.remove("foo")
  301. self.assertEqual('<div id="foo"/>', node)
  302. self.assertRaises(ValueError, node.remove, "foo")
  303. node.remove("id")
  304. self.assertEqual('<div/>', node)
  305. if __name__ == "__main__":
  306. unittest.main(verbosity=2)