test_html2tedi.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. h2t = new html2tedi();
  2. QUnit.module('html2tedi', function() {
  3. QUnit.test('convert initial line text', function(assert) {
  4. //Empty string
  5. console.log("EMPTY STRINGS");
  6. assert.equal((h2t.convert("", "", "")), (""));
  7. assert.equal(h2t.convert(" \n", "", ""), ("\n"));
  8. assert.equal(h2t.convert("\n", "", ""), ("\n"));
  9. });
  10. QUnit.test('titles', function(assert) {
  11. //Titles
  12. console.log("TITLES");
  13. try{
  14. h2t.convert("#####", "", "");
  15. }catch(err) {
  16. assert.equal(err, "Unexpected heading size#####");
  17. }
  18. assert.equal(h2t.convert("paragraph \n#This isn't a title", "", ""), ("paragraph \n#This isn't a title\n"));
  19. assert.equal(h2t.convert("<h1>This isn't a title</h1>", "", ""), ("#This isn't a title\n"));
  20. assert.equal(h2t.convert(" <h1>This isn't a title</h1>", "", ""), ("#This isn't a title\n"));
  21. assert.equal(h2t.convert("\t<h2>This isn't a title</h2>", "", ""), ("##This isn't a title\n"));
  22. assert.equal(h2t.convert("<h2>This isn't a title</h2>", "", ""), ("##This isn't a title\n"));
  23. assert.equal(h2t.convert("<h3>This isn't a title</h3>", "", ""), ("###This isn't a title\n"));
  24. assert.equal(h2t.convert("<h4>This isn't a title</h4>", "", ""), ("###This isn't a title\n"));
  25. assert.equal(h2t.convert("<h4>This isn't a title</h4>", "", ""), "###This isn't a title\n" );
  26. assert.equal(h2t.convert("<h4>This isn't a title</h4>", "", ""), "###This isn't a title\n" );
  27. });
  28. QUnit.test('list', function(assert) {
  29. console.log("LIST");
  30. assert.equal(h2t.convert("<ul>", "", ""), ("__\n"));
  31. assert.equal(h2t.convert("<li>Text", "", ""), ("Text\n"));
  32. assert.equal(h2t.convert("</ul>", "", ""), ("\n"));
  33. assert.equal(h2t.convert("<ul>\n<li>Text\n</ul>", "", ""), ("__\n--Text\n,,\n"));
  34. });
  35. QUnit.test('quotes', function(assert) {
  36. console.log("QUOTE");
  37. assert.equal(h2t.convert("\"Quoted text\"", "", ""), ("\"Quoted text\"\n"));
  38. try{
  39. h2t.convert("\"Bad quoting", "", "")
  40. }catch(err) {
  41. assert.equal(err, "Missing end quotes.\"Bad quoting");
  42. }
  43. });
  44. QUnit.test('links', function(assert) {
  45. console.log("LINKS");
  46. assert.equal(h2t.convert("<a>Test with a open square bracket", "", ""), ("Test with a open square bracket\n"));
  47. assert.equal(h2t.convert("<a href=\">Test with a open square bracket", "", ""), ("Test with a open square bracket\n"));
  48. assert.equal(h2t.convert("<a href=\"\">Test with a open square bracket", "", ""), ("Test with a open square bracket\n"));
  49. assert.equal(h2t.convert("<a href=\"\">Test with a open square bracket</a>", "", ""), ("[() Test with a open square bracket]\n"));
  50. assert.equal(h2t.convert("<a >Test with a open square bracket</a>", "", ""), ("[() Test with a open square bracket]\n"));
  51. assert.equal(h2t.convert("[ ", "", ""), "\\[ \n");
  52. assert.equal(h2t.convert("[( ", "", ""), "\\[\\( \n");
  53. assert.equal(h2t.convert("[() ", "", ""), "\\[\\(\\) \n");
  54. assert.equal(h2t.convert("[() ]", "", ""), "\\[\\(\\) \\]\n");
  55. });
  56. QUnit.test('images', function(assert) {
  57. console.log("IMAGES");
  58. //Can be improved
  59. assert.equal(h2t.convert("<img Test with an open parenthesis", "", ""), ("\n"));
  60. assert.equal(h2t.convert("<img >Test with an open parenthesis", "", ""), ("([] )Test with an open parenthesis\n"));
  61. assert.equal(h2t.convert("<img src=\"\">Test with an open parenthesis", "", ""), ("([] )Test with an open parenthesis\n"));
  62. assert.equal(h2t.convert("<img src=\"\" alt=\"\">Test with an open parenthesis", "", ""), ("([] )Test with an open parenthesis\n"));
  63. assert.equal(h2t.convert("(", "", ""), "\\(\n");
  64. assert.equal(h2t.convert("([", "", ""), "\\(\\[\n");
  65. assert.equal(h2t.convert("([] ", "", ""), "\\(\\[\\] \n");
  66. assert.equal(h2t.convert("([] )", "", ""), "\\(\\[\\] \\)\n");
  67. assert.equal(h2t.convert("([ ] )", "", ""), "\\(\\[ \\] \\)\n");
  68. });
  69. QUnit.test('blocks', function(assert) {
  70. console.log("BLOCKS");
  71. assert.equal(h2t.convert("<div>Test with a open square bracket", "", ""), ("Test with a open square bracket\n"));
  72. assert.equal(h2t.convert("<div >Test with a open square bracket", "", ""), ("{() Test with a open square bracket\n"));
  73. assert.equal(h2t.convert("<div >Test with a open square bracket</div>", "", ""), ("{() Test with a open square bracket}\n"));
  74. assert.equal(h2t.convert("<div class=\">Test with a open square bracket</div>", "", ""), ("{() Test with a open square bracket}\n"));
  75. assert.equal(h2t.convert("<div class=\"sadfasf\">Test with a open square bracket</div>", "", ""), ("{(sadfasf) Test with a open square bracket}\n"));
  76. //raro
  77. assert.equal(h2t.convert("<div class=\">Test with a open square bracket</div>", "", ""), ("{() Test with a open square bracket}\n"));
  78. assert.equal(h2t.convert("{Test with an open parenthesis", "", ""), ("\\{Test with an open parenthesis\n"));
  79. assert.equal(h2t.convert("{(Test with closing square bracket) }", "", ""), "\\{\\(Test with closing square bracket\\) \\}\n");
  80. assert.equal(h2t.convert("{() }", "", ""), "\\{\\(\\) \\}\n");
  81. assert.equal(h2t.convert("{( ) }", "", ""), "\\{\\( \\) \\}\n");
  82. });
  83. QUnit.test('multiples unquoted tags', function(assert) {
  84. console.log("MULTIPLES UNQUOTED TAGS");
  85. assert.equal(h2t.convert("[Test with a open square bracket ( also parenthesis and { brackets", "", ""), ("\\[Test with a open square bracket \\( also parenthesis and \\{ brackets\n"));
  86. assert.equal(h2t.convert("<a href=\"url\">Test with a link <img alt=\"alternative text\" src=\"image url\"></a> and <div class=\"class\">brackets</div>", "", ""), ("[(url) Test with a link ([alternative text] image url)] and {(class) brackets}\n"));
  87. assert.equal(h2t.convert("<div class=\"class\"><a href=\"url\">Test with a link <img alt=\"alternative text\" src=\"image url\"></a> and brackets</div>", "", ""), ("{(class) [(url) Test with a link ([alternative text] image url)] and brackets}\n"));
  88. });
  89. QUnit.test('table', function(assert) {
  90. console.log("TABLE");
  91. assert.equal(h2t.convert("<table>\n<tr>\n<td> first cell </td>\n<td> second cell </td>\n</tr>\n</table>\n", "", ""), " | first cell | second cell |\n");
  92. assert.equal(h2t.convert("<table>\n<tr>\n<td> first cell </td>\n<td> second cell </td>\n</tr>\n<tr>\n<td> third cell </td>\n<td> fourth cell </td>\n</tr>\n</table>\n", "", ""), " | first cell | second cell |\n | third cell | fourth cell |\n");
  93. assert.equal(h2t.convert("<table>\n<tr>\n<td>first cell</td>\n<td>second cell</td>\n</tr>\n<tr>\n<td>third cell</td>\n<td>fourth cell</td>\n</tr>\n</table>", "", ""), " | first cell | second cell |\n | third cell | fourth cell |\n");
  94. assert.equal(h2t.convert("<table>\n<tr>\n<td>first cell</td>\n</tr>\n<tr>\n<td>second cell</td>\n<td>third cell</td>\n</tr>\n</table>\n", "", ""), " | first cell |\n | second cell | third cell |\n");
  95. assert.equal(h2t.convert("<table>\n<tr>\n<td> first cell </td>\n<td> second cell </td>\n</tr>\n</table>\nText line\n<table>\n<tr>\n<td> third cell </td>\n<td> fourth cell </td>\n</tr>\n</table>\n", "", ""), " | first cell | second cell |\nText line\n | third cell | fourth cell |\n");
  96. assert.equal(h2t.convert("Text adhoc for testing\n<table>\n<tr>\n<td>a</td>\n<td>b</td>\n</tr>\n<tr>\n<td>c</td>\n<td>d</td>\n</tr>\n</table>\n", "", ""), "Text adhoc for testing\n | a | b |\n | c | d |\n");
  97. assert.equal(h2t.convert("Text adhoc for testing<table>\n<tr>\n<td> a </td>\n<td> b </td>\n</tr>\n<tr>\n<td> c </td>\n<td> d </td>\n</tr>\n</table>\nText adhoc for testing\n", "", ""), " | a | b |\n | c | d |\nText adhoc for testing\n");
  98. });
  99. QUnit.test('end_table', function(assert) {
  100. console.log("END TABLE");
  101. assert.equal(h2t.convert("<table>\n<tr>\n<td> first cell </td>\n<td> second cell </td>\n</tr>\n</table>\nText line\n<table>\n<tr>\n<td> third cell </td>\n<td> fourth cell </td>\n</tr>\n</table>\n<h1>Title1</h1>", "", ""), " | first cell | second cell |\nText line\n | third cell | fourth cell |\n#Title1\n");
  102. assert.equal(h2t.convert("<table>\n<tr>\n<td> first cell </td>\n<td> second cell </td>\n</tr>\n</table>\nText line\n<table>\n<tr>\n<td> third cell </td>\n<td> fourth cell </td>\n</tr>\n</table>\n<ul>\n<li>Item\n</ul>"), " | first cell | second cell |\nText line\n | third cell | fourth cell |\n__\n--Item\n,,\n");
  103. assert.equal(h2t.convert("__\n| first cell | second cell |\nText line\n| third cell | fourth cell |\n--Item\n,,", "", ""), "__\n| first cell | second cell |\nText line\n| third cell | fourth cell |\n--Item\n,,\n");
  104. assert.equal(h2t.convert("<ul>\n<li>Item\n<table>\n<tr>\n<td> first cell </td>\n<td> second cell </td>\n</tr>\n</table>\nText line\n<table>\n<tr>\n<td> third cell </td>\n<td> fourth cell </td>\n</tr>\n</table>\n</ul>", "", ""), "__\n--Item\n | first cell | second cell |\nText line\n | third cell | fourth cell |\n,,\n");
  105. assert.equal(h2t.convert("<table>\n<tr>\n<td> first cell </td>\n<td> second cell </td>\n</tr>\n</table>\nText line\n<table>\n<tr>\n<td> third cell </td>\n<td> fourth cell </td>\n</tr>\n</table>\n<code>Test</code>", "", ""), " | first cell | second cell |\nText line\n | third cell | fourth cell |\n\n&lt;+Test\n");
  106. assert.equal(h2t.convert("<table>\n<tr>\n<td> first cell </td>\n<td> second cell </td>\n</tr>\n</table>\nText line\n<table>\n<tr>\n<td> third cell </td>\n<td> fourth cell </td>\n</tr>\n</table>\n<pre>Test</pre>\n", "", ""), " | first cell | second cell |\nText line\n | third cell | fourth cell |\n&lt;+ Test\n");
  107. assert.equal(h2t.convert("<table>\n<tr>\n<td> first cell </td>\n<td> second cell </td>\n</tr>\n</table>\nText line\n<table>\n<tr>\n<td> third cell </td>\n<td> fourth cell </td>\n</tr>\n</table>\n<pre>Test with pipe |</pre>\n", "", ""), " | first cell | second cell |\nText line\n | third cell | fourth cell |\n&lt;+ Test with pipe |\n");
  108. });
  109. QUnit.test('new line', function(assert) {
  110. console.log("NEW LINE");
  111. assert.equal(h2t.convert("Lorem Ipsum \n ", "", ""), ("Lorem Ipsum \n\n"));
  112. assert.equal(h2t.convert("First line\nLorem Ipsum \n ", "", ""), ("First line\nLorem Ipsum \n\n"));
  113. assert.equal(h2t.convert("First line \nLorem Ipsum \n ", "", ""), ("First line \nLorem Ipsum \n\n"));
  114. assert.equal(h2t.convert("Lorem<div class=\"class\">Ipsum</div>\n", "", ""), ("Lorem{(class) Ipsum}\n"));
  115. assert.equal(h2t.convert("Lorem\nIpsum\nDolor\n", "", ""), ("Lorem\nIpsum\nDolor\n"));
  116. assert.equal(h2t.convert("<div class=\"class\">text</div>\n", "", ""), ("{(class) text}\n"));
  117. assert.equal(h2t.convert("Lorem<div class=\"class\">text</div>Dolor", "", ""), ("Lorem{(class) text}Dolor\n"));
  118. });
  119. QUnit.test('metatags', function(assert) {
  120. console.log("METATAGS");
  121. assert.equal(h2t.convert("<! Comment commenting things", "", ""), "\n");
  122. assert.equal(h2t.convert("Text adhoc for testing<code> metatag without space at end</code>Text adhoc for testing", "", ""), "Text adhoc for testing\n&lt; metatag without space at end\nText adhoc for testing\n");
  123. assert.equal(h2t.convert("Text adhoc for testing \n<code> metatag without space at end</code>Text adhoc for testing", "", ""), "Text adhoc for testing \n\n&lt; metatag without space at end\nText adhoc for testing\n");
  124. assert.equal(h2t.convert("Text adhoc for testing\n<code> metatag without space at end</code>\nText adhoc for testing ", "", ""), "Text adhoc for testing\n\n&lt;+ metatag without space at end\nText adhoc for testing \n");
  125. assert.equal(h2t.convert("Text adhoc for testing<pre> metatag with space at end</pre>\nText adhoc for testing", "", ""), "&lt;+ Text adhoc for testing metatag with space at end\nText adhoc for testing\n");
  126. assert.equal(h2t.convert("Text adhoc for testing \n<pre> metatag with space at end</pre>\nText adhoc for testing", "", ""), "Text adhoc for testing \n&lt;+ metatag with space at end\nText adhoc for testing\n");
  127. assert.equal(h2t.convert("Text adhoc for testing<pre> metatag with space at end</pre>\nText adhoc for testing", "", ""), "&lt;+ Text adhoc for testing metatag with space at end\nText adhoc for testing\n");
  128. assert.equal(h2t.convert("Text adhoc for testing\n<div class=\"embed\"> embed with space at end</div>\nText adhoc for testing ", "", ""), "Text adhoc for testing\n&lt;&gt; embed with space at end\nText adhoc for testing \n");
  129. });
  130. QUnit.test('misc.', function(assert) {
  131. console.log("MISC.");
  132. assert.equal(h2t.convert("alkasdfasdf \n<h1>This isn't a title</h1>\nksadjfañlskdfj", "", "")
  133. , ("alkasdfasdf \n#This isn't a title\nksadjfañlskdfj\n"));
  134. assert.equal(h2t.convert("alkasdfasdf\n<h1>This isn't a title</h1>\nksadjfañlskdfj", "", "")
  135. , "alkasdfasdf\n#This isn't a title\nksadjfañlskdfj\n");
  136. assert.equal(h2t.convert("Text adhoc for testing<div class=\"class\">container text </div> Text adhoc for testing", "", ""), "Text adhoc for testing{(class) container text } Text adhoc for testing\n");
  137. assert.equal(h2t.convert("Text adhoc for testing \n<div class=\"class\">container text </div>Text adhoc for testing", "", ""), "Text adhoc for testing \n{(class) container text }Text adhoc for testing\n");
  138. assert.equal(h2t.convert("Text adhoc for testing \n<div class=\"class\">container text</div>Text adhoc for testing", "", ""), "Text adhoc for testing \n{(class) container text}Text adhoc for testing\n");
  139. assert.equal(h2t.convert("Paragraph without space at end<code> hello </code>", "", ""), "Paragraph without space at end\n&lt;+ hello \n");
  140. assert.equal(h2t.convert("Paragraph without space at end<code> hello without space at end</code> lsdkjfgsñdlkfjñlk", "", ""), "Paragraph without space at end\n&lt; hello without space at end\nlsdkjfgsñdlkfjñlk\n");
  141. assert.equal(h2t.convert("Paragraph without space at end<code> hello </code>", "", ""), "Paragraph without space at end\n&lt;+ hello \n");
  142. });
  143. QUnit.test('div line', function(assert) {
  144. console.log("div line");
  145. assert.equal(h2t.convert("<div> laskjfdalksjdf </div>", "", "") , ("laskjfdalksjdf \n"));
  146. assert.equal(h2t.convert("<div> <img src=\"\" alt=\"\"> </div>", "", "") , ("([] ) \n"));
  147. assert.equal(h2t.convert("<div> <img src=\"\" alt=\"\"> </div>", "", "") , ("([] ) \n"));
  148. });
  149. });