PageRenderTime 28ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/test/test_greencloth.rb

https://github.com/zevarito/undress
Ruby | 385 lines | 311 code | 56 blank | 18 comment | 2 complexity | 5a1472468b6f732e45817a66fcfb5ca8 MD5 | raw file
Possible License(s): MIT
  1. require File.expand_path(File.dirname(__FILE__) + "/test_helper")
  2. class Undress::GreenClothTest < Test::Unit::TestCase
  3. def assert_renders_greencloth(greencloth, html)
  4. assert_equal greencloth, Undress(html).to_greencloth
  5. end
  6. context "parsing badly indented documents" do
  7. test "badly indent doc" do
  8. html = "<ul>
  9. <li>foo</li>
  10. <li>bar</li>
  11. <li>and x is also.</li>
  12. </ul>"
  13. greencloth = "* foo\n* bar\n* and x is also.\n"
  14. assert_renders_greencloth greencloth, html
  15. end
  16. end
  17. context "some troubles with empty tags" do
  18. test "with pre" do
  19. html = "<pre></pre>"
  20. greencloth = "<pre></pre>"
  21. assert_renders_greencloth greencloth, html
  22. end
  23. test "with p" do
  24. html = "<p></p>"
  25. greencloth = ""
  26. assert_renders_greencloth greencloth, html
  27. end
  28. end
  29. # TODO:
  30. # this is ok to ensure invalid html -> to greencloth but xhtmlize! must have
  31. # tests on test_undress or something too
  32. context "parsing not valid xhtml documents" do
  33. test "space between 2 spans with styles" do
  34. html = "<p><span style='font-weight: bold;'>bold</span> <span style='font-style: italic;'>italic</span></p>"
  35. greencloth = "*bold* _italic_\n"
  36. assert_renders_greencloth greencloth, html
  37. end
  38. test "a <span> bold, italic, underline, line-through at the same time" do
  39. html = "<p>some text <span style='font-weight:bold; font-style:italic; text-decoration:underline;'>bold</span> with style</p>"
  40. greencloth = "some text *+_bold_+* with style\n"
  41. assert_renders_greencloth greencloth, html
  42. end
  43. test "font-weight:bold styles in <span> elements should be <strong>" do
  44. html = "<p>some text <span style='font-weight:bold'>bold</span> with style</p>"
  45. greencloth = "some text *bold* with style\n"
  46. assert_renders_greencloth greencloth, html
  47. html = "<p style='font-weight:bold'>some text bold with style</p>"
  48. greencloth = "*some text bold with style*\n"
  49. assert_renders_greencloth greencloth, html
  50. end
  51. test "style 'line-through' should be converted to <del> in <span> elements" do
  52. html = "<p>with <span style='text-decoration: line-through;'>some</span> in the <span style='text-decoration: line-through;'>paragraph</span></p>"
  53. greencloth = "with -some- in the -paragraph-\n"
  54. assert_renders_greencloth greencloth, html
  55. html = "<p style='text-decoration: line-through;'>with some in the paragraph</p>"
  56. greencloth = "-with some in the paragraph-\n"
  57. assert_renders_greencloth greencloth, html
  58. end
  59. test "style 'underline' should be converted to <ins> in <span> elements" do
  60. html = "<p>with <span style='text-decoration: underline;'>some</span> in the <span style='text-decoration: underline;'>paragraph</span></p>"
  61. greencloth = "with +some+ in the +paragraph+\n"
  62. assert_renders_greencloth greencloth, html
  63. html = "<p style='text-decoration: underline;'>with some in the paragraph</p>"
  64. greencloth = "+with some in the paragraph+\n"
  65. assert_renders_greencloth greencloth, html
  66. end
  67. test "style 'italic' should be converted to <em> in <span> elements" do
  68. html = "<p>with <span style='font-style: italic;'>some</span> in the <span style='font-style: italic;'>paragraph</span></p>"
  69. greencloth = "with _some_ in the _paragraph_\n"
  70. assert_renders_greencloth greencloth, html
  71. html = "<p style='font-style: italic;'>with some in the paragraph</p>"
  72. greencloth = "_with some in the paragraph_\n"
  73. assert_renders_greencloth greencloth, html
  74. end
  75. test "a nested invalid unordered list" do
  76. html = "<ul><li>item 1</li><li>item 2</li><ul><li>nested 1</li><li>nested 2</li></ul><li>item 3</li></ul>"
  77. greencloth = "* item 1\n* item 2\n** nested 1\n** nested 2\n* item 3\n"
  78. assert_renders_greencloth greencloth, html
  79. end
  80. test "a nested invalid ordered list" do
  81. html = "<ol><li>item 1</li><li>item 2</li><ol><li>nested 1</li><li>nested 2</li></ol><li>item 3</li></ol>"
  82. greencloth = "# item 1\n# item 2\n## nested 1\n## nested 2\n# item 3\n"
  83. assert_renders_greencloth greencloth, html
  84. end
  85. test "a nested invalid mixed list with 3 levels" do
  86. html = "<ul><li>item 1</li><li>item 2</li><ol><li>nested 1</li><li>nested 2</li><ul><li>nested2 1</li><li>nested2 2</li></ul></ol><li>item 3</li></ul>"
  87. greencloth = "* item 1\n* item 2\n*# nested 1\n*# nested 2\n*#* nested2 1\n*#* nested2 2\n* item 3\n"
  88. assert_renders_greencloth greencloth, html
  89. end
  90. test "a nested invalid mixed list" do
  91. html = "<ul><li>item 1</li><li>item 2</li><ol><li>nested 1</li><li>nested 2</li></ol><li>item 3</li></ul>"
  92. greencloth = "* item 1\n* item 2\n*# nested 1\n*# nested 2\n* item 3\n"
  93. assert_renders_greencloth greencloth, html
  94. end
  95. test "2 badly nested list inside" do
  96. html = "<ul><li>item 1</li><li>item 2</li><ul><li>nested 1</li><ul><li>item 1x</li><li>item 2x</li></ul><li>nested 2</li></ul><li>item 3</li></ul>"
  97. greencloth = "* item 1\n* item 2\n** nested 1\n*** item 1x\n*** item 2x\n** nested 2\n* item 3\n"
  98. assert_renders_greencloth greencloth, html
  99. end
  100. end
  101. # unallowed tags
  102. context "remove unallowed tags" do
  103. test "remove a head tag" do
  104. html = "<html><head><title>Title</title></head>"
  105. greencloth = ""
  106. assert_renders_greencloth greencloth, html
  107. end
  108. test "remove a script tag" do
  109. html = "<div>Some script inside a<script type='text/javascript'>window.alert('alert')</script> paragraph</div>"
  110. greencloth = "Some script inside a paragraph"
  111. assert_renders_greencloth greencloth, html
  112. end
  113. end
  114. # code
  115. context "converting code tags" do
  116. test "a code inside a paragraph" do
  117. html = "<p>do you like my <code>function</code>?</p>"
  118. greencloth = "do you like my @function@?\n"
  119. assert_renders_greencloth greencloth, html
  120. end
  121. test "code tag inside pre tag" do
  122. html = "<pre><code>def say_hi\n\tputs 'hi'\nend</code></pre>"
  123. greencloth = "<pre><code>def say_hi\n\tputs 'hi'\nend</code></pre>"
  124. assert_renders_greencloth greencloth, html
  125. end
  126. test "code inside list items" do
  127. html = "<ul><li><code>foo</code></li><li><code>bar</code></li><li>and <code>x</code> is also.</li></ul>"
  128. greencloth = "* @foo@\n* @bar@\n* and @x@ is also.\n"
  129. assert_renders_greencloth greencloth, html
  130. end
  131. test "code tag not inside a pre and without new lines inside" do
  132. html = "<code>some code inside</code>"
  133. greencloth = "@some code inside@"
  134. assert_renders_greencloth greencloth, html
  135. end
  136. end
  137. # embed and object
  138. # the elements pass trough but the order of the attributes change
  139. context "embed and object" do
  140. test "embed" do
  141. html = "<p>do you like my embedded blip.tv <embed src='http://blip.tv/play/Ac3GfI+2HA' allowfullscreen='true' type='application/x-shockwave-flash' allowscriptaccess='always' height='510' width='720' />?</p>"
  142. greencloth = "do you like my embedded blip.tv <embed allowfullscreen=\"true\" src=\"http://blip.tv/play/Ac3GfI+2HA\" allowscriptaccess=\"always\" type=\"application/x-shockwave-flash\" height=\"510\" width=\"720\" />?\n"
  143. assert_renders_greencloth greencloth, html
  144. end
  145. test "object" do
  146. html = "<p>do you like my embedded youtube <object width='425' height='344'><param name='movie' value='http://www.youtube.com/v/suvDQoXA-TA&hl=en&fs=1' /><param name='allowFullScreen' value='true' /><embed src='http://www.youtube.com/v/suvDQoXA-TA&hl=en&fs=1' type='application/x-shockwave-flash' width='425' height='344' allowfullscreen='true' /></object>?</p>"
  147. greencloth = "do you like my embedded youtube <object height=\"344\" width=\"425\"><param name=\"movie\" value=\"http://www.youtube.com/v/suvDQoXA-TA&hl=en&fs=1\" /><param name=\"allowFullScreen\" value=\"true\" /><embed allowfullscreen=\"true\" src=\"http://www.youtube.com/v/suvDQoXA-TA&hl=en&fs=1\" type=\"application/x-shockwave-flash\" height=\"344\" width=\"425\" /></object>?\n"
  148. assert_renders_greencloth greencloth, html
  149. end
  150. end
  151. # outline
  152. # don't allow link to anchors or anchor defs inside hx, greencloth -> html
  153. # take cares of it, so we are only allowing links inside hx elements for now
  154. context "outline" do
  155. test "table of contents toc" do
  156. html = "<ul class='toc'><li class='toc1'><a href='#fruits'><span>1</span> Fruits</a></li><ul><li class='toc2'><a href='#tasty-apples'><span>1.1</span> Tasty Apples</a></li><ul><li class='toc3'><a href='green'><span>1.1.1</span> Green</a></li><li class='toc3'><a href='#red'><span>1.1.2</span> Red</a></li></ul>"
  157. greencloth = "[[toc]]\n"
  158. assert_renders_greencloth greencloth, html
  159. end
  160. test "headings with links, anchors and links to anchors" do
  161. html = "<h1 class='first'><a name='russian-anarchists'></a>Russian Anarchists<a class='anchor' href='#russian-anarchists'>&para;</a></h1><h2><a name='michel-bakunin'></a>Michel <a href='http://en.wikipedia.org/wiki/Mikhail_Bakunin'>Bakunin</a><a class='anchor' href='#michel-bakunin'>&para;</a></h2><h2><a name='peter-kropotkin'></a><a href='http://en.wikipedia.org/wiki/Peter_Kropotkin'>Peter</a> Kropotkin<a class='anchor' href='#peter-kropotkin'>&para;</a></h2><h1><a name='russian-american-anarchists'></a>Russian-American Anarchists<a class='anchor' href='#russian-american-anarchists'>&para;</a></h1><h2><a name='emma-goldman'></a><a href='http://en.wikipedia.org/wiki/Emma_Goldman'>Emma Goldman</a><a class='anchor' href='#emma-goldman'>&para;</a></h2><h2><a name='alexander-berkman'></a>Alexander <a href='http://en.wikipedia.org/wiki/Alexander_Berkman'>Berkman</a><a class='anchor' href='#alexander-berkman'>&para;</a></h2>"
  162. greencloth = "Russian Anarchists\n==================\n\nMichel [Bakunin -> http://en.wikipedia.org/wiki/Mikhail_Bakunin]\n--------------\n\n[Peter -> http://en.wikipedia.org/wiki/Peter_Kropotkin] Kropotkin\n---------------\n\nRussian-American Anarchists\n===========================\n\n[Emma Goldman -> http://en.wikipedia.org/wiki/Emma_Goldman]\n------------\n\nAlexander [Berkman -> http://en.wikipedia.org/wiki/Alexander_Berkman]\n-----------------\n"
  163. assert_renders_greencloth greencloth, html
  164. end
  165. test "double trouble" do
  166. html = "<h1 class='first'><a name='title'></a>Title<a class='anchor' href='#title'>&para;</a></h1><h3><a name='under-first'></a>Under first<a class='anchor' href='#under-first'>&para;</a></h3><h1><a name='title_2'></a>Title<a class='anchor' href='#title_2'>&para;</a></h1><h3><a name='under-second'></a>Under second<a class='anchor' href='#under-second'>&para;</a></h3>"
  167. greencloth = "Title\n=====\n\nh3. Under first\n\nTitle\n=====\n\nh3. Under second\n"
  168. assert_renders_greencloth greencloth, html
  169. end
  170. end
  171. # basics
  172. context "basics" do
  173. test "headers" do
  174. html = "<h1 class='first'>header one</h1>\n<h2>header two</h2>"
  175. greencloth = "header one\n==========\n\nheader two\n----------\n"
  176. assert_renders_greencloth greencloth, html
  177. end
  178. test "headers with paragraph" do
  179. html = "<p>la la la</p>\n<h1 class='first'>header one</h1>\n<h2>header two</h2>\n<p>la la la</p>"
  180. greencloth = "la la la\n\nheader one\n==========\n\nheader two\n----------\n\nla la la\n"
  181. assert_renders_greencloth greencloth, html
  182. end
  183. end
  184. # sections
  185. # allways we render h1 with ==== and h2 with ----
  186. context "Convert sections" do
  187. test "one section no heading" do
  188. html = "<div class='wiki_section' id='wiki_section-0'><p>start unheaded section</p><p>line line line</p></div>"
  189. greencloth = "start unheaded section\n\nline line line\n"
  190. assert_renders_greencloth greencloth, html
  191. end
  192. test "one section with heading" do
  193. html = "<div class='wiki_section' id='wiki_section-0'><h2 class='first'>are you ready?!!?</h2><p>here we go now!</p></div>"
  194. greencloth = "are you ready?!!?\n-----------------\n\nhere we go now!\n"
  195. assert_renders_greencloth greencloth, html
  196. end
  197. test "all headings" do
  198. html = "<h1>First</h1><h2>Second</h2><h3>Tres</h3><h4>Cuatro</h4><h5>Five</h5><h6>Six</h6>"
  199. greencloth = "First\n=====\n\nSecond\n------\n\nh3. Tres\n\nh4. Cuatro\n\nh5. Five\n\nh6. Six\n"
  200. assert_renders_greencloth greencloth, html
  201. end
  202. test "multiple sections with text" do
  203. html = "<div class='wiki_section' id='wiki_section-0'><h2 class='first'>Section One</h2><p>section one line one is here<br />section one line two is next</p><p>Here is section one still</p></div><div class='wiki_section' id='wiki_section-1'><h1>Section Two</h1><p>Section two first line<br />Section two another line</p></div><div class='wiki_section' id='wiki_section-2'><h2>Section 3 with h2</h2><p>One more line for section 3</p></div><div class='wiki_section' id='wiki_section-3'><h3>final section 4</h3><p>section 4 first non-blank line</p>\n</div>"
  204. greencloth = "Section One\n-----------\n\nsection one line one is here\nsection one line two is next\n\nHere is section one still\n\nSection Two\n===========\n\nSection two first line\nSection two another line\n\nSection 3 with h2\n-----------------\n\nOne more line for section 3\n\nh3. final section 4\n\nsection 4 first non-blank line\n"
  205. assert_renders_greencloth greencloth, html
  206. end
  207. end
  208. # lists
  209. # TODO: start attribute not implemented
  210. context "Converting html lists to greencloth" do
  211. test "hard break in list" do
  212. html = "<ul>\n\t<li>first line</li>\n\t<li>second<br />\n\tline</li>\n\t<li>third line</li>\n</ul>\n"
  213. greencloth = "* first line\n* second\nline\n* third line\n"
  214. assert_renders_greencloth greencloth, html
  215. end
  216. test "mixed nesting" do
  217. html = "<ul><li>bullet\n<ol>\n<li>number</li>\n<li>number\n<ul>\n\t<li>bullet</li>\n</ul></li>\n<li>number</li>\n<li>number with<br />a break</li>\n</ol></li>\n<li>bullet\n<ul><li>okay</li></ul></li></ul>"
  218. greencloth = "* bullet\n*# number\n*# number\n*#* bullet\n*# number\n*# number with\na break\n* bullet\n** okay\n"
  219. assert_renders_greencloth greencloth, html
  220. end
  221. test "list continuation" do # uses start
  222. html = "<ol><li>one</li><li>two</li><li>three</li></ol><ol><li>one</li><li>two</li><li>three</li></ol><ol start='4'><li>four</li><li>five</li><li>six</li></ol>"
  223. greencloth = "# one\n# two\n# three\n\n# one\n# two\n# three\n\n# four\n# five\n# six\n"
  224. assert_renders_greencloth greencloth, html
  225. end
  226. test "continue after break" do # uses start
  227. html = "<ol><li>one</li><li>two</li><li>three</li></ol><p>test</p><ol><li>one</li><li>two</li><li>three</li></ol><p>test</p><ol start='4'><li>four</li><li>five</li><li>six</li></ol>"
  228. greencloth = "# one\n# two\n# three\n\ntest\n\n# one\n# two\n# three\n\ntest\n\n# four\n# five\n# six\n"
  229. assert_renders_greencloth greencloth, html
  230. end
  231. test "continue list when prior list contained nested list" do # uses start
  232. greencloth = "# one\n# two\n# three\n\n# four\n# five\n## sub-note\n## another sub-note\n# six\n\n# seven\n# eight\n# nine\n"
  233. html = "<ol><li>one</li><li>two</li><li>three</li></ol><ol start='4'><li>four</li><li>five<ol><li>sub-note</li><li>another sub-note</li></ol></li><li>six</li></ol><ol start='7'><li>seven</li><li>eight</li><li>nine</li></ol>"
  234. assert_renders_greencloth greencloth, html
  235. end
  236. test "" do
  237. end
  238. end
  239. # links
  240. context "Converting html links to greencloth" do
  241. test "convert a link to a wiki page inside a paragraph" do
  242. html = "<p>this is a <a href='/page/plain-link'>plain link</a> in some text</p>"
  243. greencloth = "this is a [plain link] in some text\n"
  244. assert_renders_greencloth greencloth, html
  245. end
  246. test "convert a link to a wiki page with namespace" do
  247. html= "<p>this is a <a href='/namespaced/link'>link</a> in some text</p>"
  248. greencloth = "this is a [namespaced / link] in some text\n"
  249. assert_renders_greencloth greencloth, html
  250. end
  251. test "convert a link to a wiki page" do
  252. html= "<p>this is a <a href='/page/something-else'>link to</a> in some text</p>"
  253. greencloth = "this is a [link to -> something else] in some text\n"
  254. assert_renders_greencloth greencloth, html
  255. end
  256. test "convert a link to a wiki page with namespace and text different than link dest" do
  257. html= "<p>this is a <a href='/namespace/something-else'>link to</a> in some text</p>"
  258. greencloth = "this is a [link to -> namespace / something else] in some text\n"
  259. assert_renders_greencloth greencloth, html
  260. end
  261. test "convert a link to an absolute path" do
  262. html = "<p>this is a <a href='/an/absolute/path'>link to</a> in some text</p>"
  263. greencloth = "this is a [link to -> /an/absolute/path] in some text\n"
  264. assert_renders_greencloth greencloth, html
  265. end
  266. test "convert a link to an external domain" do
  267. html = "<p>this is a <a href='https://riseup.net'>link to</a> a url</p>"
  268. greencloth = "this is a [link to -> https://riseup.net] a url\n"
  269. assert_renders_greencloth greencloth, html
  270. end
  271. test "a link to an external domain with the same text as dest" do
  272. html = "<p>url in brackets <a href='https://riseup.net/'>riseup.net</a></p>"
  273. greencloth = "url in brackets [riseup.net -> https://riseup.net/]\n"
  274. assert_renders_greencloth greencloth, html
  275. end
  276. test "a link to a wiki page with the same name as dest" do
  277. html = "<p>a <a href='/page/name-link'>name link</a> in need of humanizing</p>"
  278. greencloth = "a [name link] in need of humanizing\n"
  279. assert_renders_greencloth greencloth, html
  280. end
  281. test "link to a user blue" do
  282. html = "<p>link to a user <a href='/blue'>blue</a></p>"
  283. greencloth = "link to a user [blue]\n"
  284. assert_renders_greencloth greencloth, html
  285. end
  286. test "link with dashes should keep the dashes" do
  287. html = "<p><a href='/-dashes/in/the/link-'>link to</a></p>"
  288. greencloth = "[link to -> /-dashes/in/the/link-]\n"
  289. assert_renders_greencloth greencloth, html
  290. end
  291. test "link with underscores should keep the underscores" do
  292. html = "<p>links <a href='/page/with_underscores'>with_underscores</a> should keep underscore</p>"
  293. greencloth = "links [with_underscores] should keep underscore\n"
  294. assert_renders_greencloth greencloth, html
  295. end
  296. test "a link inside a li element" do
  297. html ="<ul>\n<li>\n\t\t\n<a href='/page/this'>link to</a></li></ul>"
  298. greencloth = "* [link to -> this]\n"
  299. assert_renders_greencloth greencloth, html
  300. end
  301. test "an external link inside a li element" do
  302. html = "<ul>\n<li><a href='https://riseup.net/'>riseup.net</a></li>\n</ul>"
  303. greencloth = "* [riseup.net -> https://riseup.net/]\n"
  304. assert_renders_greencloth greencloth, html
  305. end
  306. test "many anchors inside a paragraph" do
  307. html = "<p>make anchors <a name='here'>here</a> or <a name='maybe-here'>maybe here</a> or <a name='there'>over</a></p>"
  308. greencloth = "make anchors [# here #] or [# maybe here #] or [# over -> there #]\n"
  309. assert_renders_greencloth greencloth, html
  310. end
  311. # TODO: there are differents in this test about how cg support writing anchors
  312. # this is a reduced support of it
  313. test "anchors and links" do
  314. html = "<p>link to <a href='/page/anchors#like-so'>anchors</a> or <a href='/page/like#so'>maybe</a> or <a href='#so'>just</a> or <a href='#so'>so</a></p>"
  315. greencloth = "link to [anchors -> anchors#like so] or [maybe -> like#so] or [just -> #so] or [so -> #so]\n"
  316. assert_renders_greencloth greencloth, html
  317. end
  318. test "more anchors" do
  319. html = "<p><a href='#5'>link</a> to a numeric anchor <a name='5'>5</a></p>"
  320. greencloth = "[link -> #5] to a numeric anchor [# 5 #]\n"
  321. assert_renders_greencloth greencloth, html
  322. end
  323. test "3 links without /" do
  324. html = "<p><a href='some'>some</a> and <a href='other'>other</a> and <a href='one_more'>one_more</a></p>"
  325. greencloth = "[some] and [other] and [one_more]\n"
  326. assert_renders_greencloth greencloth, html
  327. end
  328. end
  329. end