typogr.test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*!
  2. * typographer
  3. * Copyright(c) 2011 Eugene Kalinin
  4. * MIT Licensed
  5. */
  6. var tp = require('../typogr')
  7. , assert = require('assert');
  8. describe('typogr', function() {
  9. it('amp tests', function() {
  10. ['One & two', 'One & two', 'One & two'].forEach( function (val) {
  11. assert.equal(tp.amp(val), 'One <span class="amp">&amp;</span> two');
  12. });
  13. assert.equal(tp.amp('One&nbsp;&amp;&nbsp;two'),
  14. 'One&nbsp;<span class="amp">&amp;</span>&nbsp;two');
  15. // It won't mess up & that are already wrapped, in entities or URLs
  16. assert.equal(tp.amp('One <span class="amp">&amp;</span> two'),
  17. 'One <span class="amp">&amp;</span> two');
  18. assert.equal(tp.amp('&ldquo;this&rdquo; & <a href="/?that&amp;test">that</a>'),
  19. '&ldquo;this&rdquo; <span class="amp">&amp;</span> <a href="/?that&amp;test">that</a>');
  20. // It should ignore standalone amps that are in attributes
  21. assert.equal(tp.amp('<link href="xyz.html" title="One & Two">xyz</link>'),
  22. '<link href="xyz.html" title="One & Two">xyz</link>');
  23. // It should ignore amps inside script tags
  24. assert.equal(tp.amp('<span><script>1 & 3 == 3</script></span>'), '<span><script>1 & 3 == 3</script></span>');
  25. });
  26. it('ord tests', function() {
  27. assert.equal(tp.ord('1st'), '1<span class="ord">st</span>');
  28. assert.equal(tp.ord('2nd'), '2<span class="ord">nd</span>');
  29. assert.equal(tp.ord('3rd'), '3<span class="ord">rd</span>');
  30. assert.equal(tp.ord('10th'), '10<span class="ord">th</span>');
  31. assert.equal(tp.ord('37th'), '37<span class="ord">th</span>');
  32. assert.equal(tp.ord('1000th'), '1000<span class="ord">th</span>');
  33. // Make sure it does not happen within attributes
  34. assert.equal(tp.ord('<span data-test="1st">1st</span>'), '<span data-test="1st">1<span class="ord">st</span></span>');
  35. });
  36. it('quotes tests', function() {
  37. assert.equal(tp.initQuotes('"With primes"'), '<span class="dquo">"</span>With primes"');
  38. assert.equal(tp.initQuotes("'With single primes'"), '<span class="quo">\'</span>With single primes\'');
  39. assert.equal(tp.initQuotes('<a href="#">"With primes and a link"</a>'),
  40. '<a href="#"><span class="dquo">"</span>With primes and a link"</a>');
  41. assert.equal(tp.initQuotes('&#8220;With smartypanted quotes&#8221;'),
  42. '<span class="dquo">&#8220;</span>With smartypanted quotes&#8221;');
  43. assert.equal(tp.initQuotes('<h1> <strong>&lsquo;With</strong> single primes ...</h1>'),
  44. '<h1> <strong><span class="quo">&lsquo;</span>With</strong> single primes ...</h1>');
  45. assert.equal(tp.initQuotes('<h2> &#8220;Jayhawks&#8221; & KU fans ... </h2>'),
  46. '<h2> <span class="dquo">&#8220;</span>Jayhawks&#8221; & KU fans ... </h2>');
  47. });
  48. it('widont tests', function() {
  49. assert.equal(tp.widont('A very simple test'), 'A very simple<span class="widont">&nbsp;</span>test');
  50. // Single word items shouldn't be changed
  51. assert.equal(tp.widont('Test'), 'Test');
  52. assert.equal(tp.widont(' Test'), ' Test');
  53. assert.equal(tp.widont('<ul><li>Test</p></li><ul>'), '<ul><li>Test</p></li><ul>');
  54. assert.equal(tp.widont('<ul><li> Test</p></li><ul>'), '<ul><li> Test</p></li><ul>');
  55. // neither should two or three word items
  56. assert.equal(tp.widont('<h1>One Two</h1>'), '<h1>One Two</h1>')
  57. assert.equal(tp.widont('<h1>One Two Three</h1>'), '<h1>One Two Three</h1>')
  58. assert.equal(tp.widont('<h1>One Two Three</h1>'), '<h1>One Two Three</h1>')
  59. assert.equal(tp.widont('<h1><a href="#">Links</a> should work</h1>'), '<h1><a href="#">Links</a> should work</h1>')
  60. assert.equal(tp.widont('<p>In a couple of paragraphs</p><p>the paragraph number two</p>'),
  61. '<p>In a couple of<span class="widont">&nbsp;</span>paragraphs</p><p>the paragraph number<span class="widont">&nbsp;</span>two</p>');
  62. assert.equal(tp.widont('<h1><a href="#">In a link inside a heading</i> </a></h1>'),
  63. '<h1><a href="#">In a link inside a<span class="widont">&nbsp;</span>heading</i> </a></h1>');
  64. assert.equal(tp.widont('<h1><a href="#">In a link</a> followed by other text</h1>'),
  65. '<h1><a href="#">In a link</a> followed by other<span class="widont">&nbsp;</span>text</h1>');
  66. // Empty HTMLs shouldn't error
  67. assert.equal(tp.widont('<h1><a href="#"></a></h1>'), '<h1><a href="#"></a></h1>');
  68. assert.equal(tp.widont('<div>Divs get no love!</div>'), '<div>Divs get no love!</div>');
  69. assert.equal(tp.widont('<pre>Neither do PREs</pre>'), '<pre>Neither do PREs</pre>');
  70. assert.equal(tp.widont('<div><p>But divs with paragraphs do!</p></div>'),
  71. '<div><p>But divs with paragraphs<span class="widont">&nbsp;</span>do!</p></div>');
  72. // It should ignore inline tags
  73. assert.equal(tp.widont('<p>Testing with a tag at <strong>the end</strong>.</p>'), '<p>Testing with a tag at <strong>the<span class="widont">&nbsp;</span>end</strong>.</p>');
  74. assert.equal(tp.widont('<p>Testing with <strong>multiple inline</strong> tags at <a href="#"><strong><em>the end</em></strong></a>.</p>'), '<p>Testing with <strong>multiple inline</strong> tags at <a href="#"><strong><em>the<span class="widont">&nbsp;</span>end</em></strong></a>.</p>');
  75. // It should also take commas into consideration
  76. assert.equal(tp.widont('<p>Start of the paragraph ... before they get deleted-I mean, published.</p>'),
  77. '<p>Start of the paragraph ... before they get deleted-I mean,<span class="widont">&nbsp;</span>published.</p>');
  78. });
  79. it('caps tests', function() {
  80. assert.equal(tp.caps('A message from KU'),
  81. 'A message from <span class="caps">KU</span>');
  82. // Uses the smartypants tokenizer to not screw with HTML or with tags it shouldn't.
  83. assert.equal(tp.caps('<PRE>CAPS</pre> more CAPS'),
  84. '<PRE>CAPS</pre> more <span class="caps">CAPS</span>');
  85. assert.equal(tp.caps('A message from 2KU2 with digits'),
  86. 'A message from <span class="caps">2KU2</span> with digits');
  87. assert.equal(tp.caps('Dotted caps followed by spaces should never include them in the wrap D.O.T. like so.'),
  88. 'Dotted caps followed by spaces should never include them in the wrap <span class="caps">D.O.T.</span> like so.');
  89. // All caps with with apostrophes in them shouldn't break. Only handles dump apostrophes though.
  90. assert.equal(tp.caps("JIMMY'S"),
  91. '<span class="caps">JIMMY\'S</span>');
  92. assert.equal(tp.caps("<i>D.O.T.</i>HE34T<b>RFID</b>"),
  93. '<i><span class="caps">D.O.T.</span></i><span class="caps">HE34T</span><b><span class="caps">RFID</span></b>');
  94. });
  95. it('tokenize', function() {
  96. assert.deepEqual( tp.tokenize('<h1>test header</h1>'+
  97. '<p>some <b>other</b> text</p> '+
  98. 'and appendix ...'),
  99. [ { type: 'tag', txt: '<h1>' },
  100. { type: 'text', txt: 'test header' },
  101. { type: 'tag', txt: '</h1>' },
  102. { type: 'tag', txt: '<p>' },
  103. { type: 'text', txt: 'some ' },
  104. { type: 'tag', txt: '<b>' },
  105. { type: 'text', txt: 'other' },
  106. { type: 'tag', txt: '</b>' },
  107. { type: 'text', txt: ' text' },
  108. { type: 'tag', txt: '</p>' },
  109. { type: 'text', txt: ' and appendix ...' } ]
  110. );
  111. });
  112. it('smartEscapes', function() {
  113. assert.deepEqual( tp.smartEscapes( '\\" : \\\' : \\- : \\. : \\\\ : \\`'),
  114. '&#34; : &#39; : &#45; : &#46; : &#92; : &#96;');
  115. })
  116. it('smartDashes', function() {
  117. assert.deepEqual( tp.smartDashes( '-- : --- : -- : ---'),
  118. '&#8211; : &#8212; : &#8211; : &#8212;');
  119. assert.deepEqual( tp.smartDashes( '<!--:-->:<!-- valid html comment -->'),
  120. '<!--:-->:<!-- valid html comment -->');
  121. });
  122. it('smartEllipses', function() {
  123. assert.deepEqual( tp.smartEllipses( '. ... : . . . .'),
  124. '. &#8230; : &#8230; .');
  125. });
  126. it('smartBackticks', function() {
  127. assert.deepEqual( tp.smartBackticks( "``Isn't this fun?''"),
  128. "&#8220;Isn't this fun?&#8221;");
  129. });
  130. it('smartQuotes', function() {
  131. assert.deepEqual( tp.smartQuotes( '"Isn\'t this fun?"'),
  132. '&#8220;Isn&#8217;t this fun?&#8221;');
  133. });
  134. it('smartypants', function() {
  135. assert.deepEqual( tp.smartypants( 'The "Green" man'),
  136. 'The &#8220;Green&#8221; man');
  137. assert.deepEqual( tp.smartypants( '"<a href="http://example.com">switched off</a>".'),
  138. '&#8220;<a href="http://example.com">switched off</a>&#8221;.');
  139. assert.deepEqual( tp.smartypants('<a href="">markdown</a>\'s popularity is growing'),
  140. '<a href="">markdown</a>&#8217;s popularity is growing');
  141. assert.deepEqual( tp.smartypants("<p>I love rock 'n' roll</p>"),
  142. '<p>I love rock &#8217;n&#8217; roll</p>');
  143. });
  144. it('typogrify', function() {
  145. assert.deepEqual( tp.typogrify(
  146. '<h2>"Jayhawks" & KU fans act extremely obnoxiously</h2>'),
  147. '<h2><span class="dquo">&#8220;</span>Jayhawks&#8221; <span class="amp">&amp;</span> <span class=\"caps\">KU</span> fans act extremely<span class="widont">&nbsp;</span>obnoxiously</h2>');
  148. assert.equal( tp('<h2>"Jayhawks" & KU fans act extremely obnoxiously</h2>').typogrify(),
  149. '<h2><span class="dquo">&#8220;</span>Jayhawks&#8221; <span class="amp">&amp;</span> <span class=\"caps\">KU</span> fans act extremely<span class="widont">&nbsp;</span>obnoxiously</h2>');
  150. assert.equal( tp('<h2>"Jayhawks" & KU fans act extremely obnoxiously</h2>').chain().typogrify().value(),
  151. '<h2><span class="dquo">&#8220;</span>Jayhawks&#8221; <span class="amp">&amp;</span> <span class=\"caps\">KU</span> fans act extremely<span class="widont">&nbsp;</span>obnoxiously</h2>');
  152. assert.deepEqual( tp.typogrify({
  153. html: function () {
  154. return '<h2>"Jayhawks" & KU fans act extremely obnoxiously</h2>';
  155. },
  156. selector: '#some-test',
  157. jquery: '1.6.3-test'
  158. }),
  159. '<h2><span class="dquo">&#8220;</span>Jayhawks&#8221; <span class="amp">&amp;</span> <span class=\"caps\">KU</span> fans act extremely<span class="widont">&nbsp;</span>obnoxiously</h2>');
  160. assert.doesNotThrow(function () {
  161. tp.typogrify("");
  162. });
  163. });
  164. });