WPTexturize.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * @group formatting
  4. */
  5. class Tests_Formatting_WPTexturize extends WP_UnitTestCase {
  6. function test_dashes() {
  7. $this->assertEquals('Hey &#8212; boo?', wptexturize('Hey -- boo?'));
  8. $this->assertEquals('<a href="http://xx--xx">Hey &#8212; boo?</a>', wptexturize('<a href="http://xx--xx">Hey -- boo?</a>'));
  9. }
  10. function test_disable() {
  11. $this->assertEquals('<pre>---</pre>', wptexturize('<pre>---</pre>'));
  12. $this->assertEquals('[a]a&#8211;b[code]---[/code]a&#8211;b[/a]', wptexturize('[a]a--b[code]---[/code]a--b[/a]'));
  13. $this->assertEquals('<pre><code></code>--</pre>', wptexturize('<pre><code></code>--</pre>'));
  14. $this->assertEquals('<code>---</code>', wptexturize('<code>---</code>'));
  15. $this->assertEquals('<code>href="baba"</code> &#8220;baba&#8221;', wptexturize('<code>href="baba"</code> "baba"'));
  16. $enabled_tags_inside_code = '<code>curl -s <a href="http://x/">baba</a> | grep sfive | cut -d "\"" -f 10 &gt; topmp3.txt</code>';
  17. $this->assertEquals($enabled_tags_inside_code, wptexturize($enabled_tags_inside_code));
  18. $double_nest = '<pre>"baba"<code>"baba"<pre></pre></code>"baba"</pre>';
  19. $this->assertEquals($double_nest, wptexturize($double_nest));
  20. $invalid_nest = '<pre></code>"baba"</pre>';
  21. $this->assertEquals($invalid_nest, wptexturize($invalid_nest));
  22. }
  23. //WP Ticket #1418
  24. function test_bracketed_quotes_1418() {
  25. $this->assertEquals('(&#8220;test&#8221;)', wptexturize('("test")'));
  26. $this->assertEquals('(&#8216;test&#8217;)', wptexturize("('test')"));
  27. $this->assertEquals('(&#8217;twas)', wptexturize("('twas)"));
  28. }
  29. //WP Ticket #3810
  30. function test_bracketed_quotes_3810() {
  31. $this->assertEquals('A dog (&#8220;Hubertus&#8221;) was sent out.', wptexturize('A dog ("Hubertus") was sent out.'));
  32. }
  33. //WP Ticket #4539
  34. function test_basic_quotes() {
  35. $this->assertEquals('test&#8217;s', wptexturize('test\'s'));
  36. $this->assertEquals('test&#8217;s', wptexturize('test\'s'));
  37. $this->assertEquals('&#8216;quoted&#8217;', wptexturize('\'quoted\''));
  38. $this->assertEquals('&#8220;quoted&#8221;', wptexturize('"quoted"'));
  39. $this->assertEquals('space before &#8216;quoted&#8217; space after', wptexturize('space before \'quoted\' space after'));
  40. $this->assertEquals('space before &#8220;quoted&#8221; space after', wptexturize('space before "quoted" space after'));
  41. $this->assertEquals('(&#8216;quoted&#8217;)', wptexturize('(\'quoted\')'));
  42. $this->assertEquals('{&#8220;quoted&#8221;}', wptexturize('{"quoted"}'));
  43. $this->assertEquals('&#8216;qu(ot)ed&#8217;', wptexturize('\'qu(ot)ed\''));
  44. $this->assertEquals('&#8220;qu{ot}ed&#8221;', wptexturize('"qu{ot}ed"'));
  45. $this->assertEquals(' &#8216;test&#8217;s quoted&#8217; ', wptexturize(' \'test\'s quoted\' '));
  46. $this->assertEquals(' &#8220;test&#8217;s quoted&#8221; ', wptexturize(' "test\'s quoted" '));
  47. }
  48. /**
  49. * @ticket 4539
  50. * @ticket 15241
  51. */
  52. function test_full_sentences_with_unmatched_single_quotes() {
  53. $this->assertEquals(
  54. 'That means every moment you&#8217;re working on something without it being in the public it&#8217;s actually dying.',
  55. wptexturize("That means every moment you're working on something without it being in the public it's actually dying.")
  56. );
  57. }
  58. /**
  59. * @ticket 4539
  60. */
  61. function test_quotes() {
  62. $this->assertEquals('&#8220;Quoted String&#8221;', wptexturize('"Quoted String"'));
  63. $this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a>&#8221;', wptexturize('Here is "<a href="http://example.com">a test with a link</a>"'));
  64. $this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link and a period</a>&#8221;.', wptexturize('Here is "<a href="http://example.com">a test with a link and a period</a>".'));
  65. $this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a>&#8221; and a space.', wptexturize('Here is "<a href="http://example.com">a test with a link</a>" and a space.'));
  66. $this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a> and some text quoted&#8221;', wptexturize('Here is "<a href="http://example.com">a test with a link</a> and some text quoted"'));
  67. $this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a>&#8221;, and a comma.', wptexturize('Here is "<a href="http://example.com">a test with a link</a>", and a comma.'));
  68. $this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a>&#8221;; and a semi-colon.', wptexturize('Here is "<a href="http://example.com">a test with a link</a>"; and a semi-colon.'));
  69. $this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a>&#8221;- and a dash.', wptexturize('Here is "<a href="http://example.com">a test with a link</a>"- and a dash.'));
  70. $this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a>&#8221;&#8230; and ellipses.', wptexturize('Here is "<a href="http://example.com">a test with a link</a>"... and ellipses.'));
  71. $this->assertEquals('Here is &#8220;a test <a href="http://example.com">with a link</a>&#8221;.', wptexturize('Here is "a test <a href="http://example.com">with a link</a>".'));
  72. $this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a>&#8221;and a work stuck to the end.', wptexturize('Here is "<a href="http://example.com">a test with a link</a>"and a work stuck to the end.'));
  73. $this->assertEquals('A test with a finishing number, &#8220;like 23&#8221;.', wptexturize('A test with a finishing number, "like 23".'));
  74. $this->assertEquals('A test with a number, &#8220;like 62&#8221;, is nice to have.', wptexturize('A test with a number, "like 62", is nice to have.'));
  75. }
  76. /**
  77. * @ticket 4539
  78. */
  79. function test_quotes_before_s() {
  80. $this->assertEquals('test&#8217;s', wptexturize("test's"));
  81. $this->assertEquals('&#8216;test&#8217;s', wptexturize("'test's"));
  82. $this->assertEquals('&#8216;test&#8217;s&#8217;', wptexturize("'test's'"));
  83. $this->assertEquals('&#8216;string&#8217;', wptexturize("'string'"));
  84. $this->assertEquals('&#8216;string&#8217;s&#8217;', wptexturize("'string's'"));
  85. }
  86. /**
  87. * @ticket 4539
  88. */
  89. function test_quotes_before_numbers() {
  90. $this->assertEquals('Class of &#8217;99', wptexturize("Class of '99"));
  91. $this->assertEquals('Class of &#8217;99&#8217;s', wptexturize("Class of '99's"));
  92. $this->assertEquals('&#8216;Class of &#8217;99&#8217;', wptexturize("'Class of '99'"));
  93. $this->assertEquals('&#8216;Class of &#8217;99&#8217;s&#8217;', wptexturize("'Class of '99's'"));
  94. $this->assertEquals('&#8216;Class of &#8217;99&#8217;s&#8217;', wptexturize("'Class of '99&#8217;s'"));
  95. $this->assertEquals('&#8220;Class of 99&#8221;', wptexturize("\"Class of 99\""));
  96. $this->assertEquals('&#8220;Class of &#8217;99&#8221;', wptexturize("\"Class of '99\""));
  97. }
  98. function test_quotes_after_numbers() {
  99. $this->assertEquals('Class of &#8217;99', wptexturize("Class of '99"));
  100. }
  101. /**
  102. * @ticket 4539
  103. * @ticket 15241
  104. */
  105. function test_other_html() {
  106. $this->assertEquals('&#8216;<strong>', wptexturize("'<strong>"));
  107. $this->assertEquals('&#8216;<strong>Quoted Text</strong>&#8217;,', wptexturize("'<strong>Quoted Text</strong>',"));
  108. $this->assertEquals('&#8220;<strong>Quoted Text</strong>&#8221;,', wptexturize('"<strong>Quoted Text</strong>",'));
  109. }
  110. function test_x() {
  111. $this->assertEquals('14&#215;24', wptexturize("14x24"));
  112. }
  113. function test_minutes_seconds() {
  114. $this->assertEquals('9&#8242;', wptexturize('9\''));
  115. $this->assertEquals('9&#8243;', wptexturize("9\""));
  116. $this->assertEquals('a 9&#8242; b', wptexturize('a 9\' b'));
  117. $this->assertEquals('a 9&#8243; b', wptexturize("a 9\" b"));
  118. $this->assertEquals('&#8220;a 9&#8242; b&#8221;', wptexturize('"a 9\' b"'));
  119. $this->assertEquals('&#8216;a 9&#8243; b&#8217;', wptexturize("'a 9\" b'"));
  120. }
  121. /**
  122. * @ticket 8775
  123. */
  124. function test_wptexturize_quotes_around_numbers() {
  125. $this->assertEquals('&#8220;12345&#8221;', wptexturize('"12345"'));
  126. $this->assertEquals('&#8216;12345&#8217;', wptexturize('\'12345\''));
  127. $this->assertEquals('&#8220;a 9&#8242; plus a &#8216;9&#8217;, maybe a 9&#8242; &#8216;9&#8217; &#8221;', wptexturize('"a 9\' plus a \'9\', maybe a 9\' \'9\' "'));
  128. $this->assertEquals('<p>&#8216;99<br />&#8216;123&#8217;<br />&#8217;tis<br />&#8216;s&#8217;</p>', wptexturize('<p>\'99<br />\'123\'<br />\'tis<br />\'s\'</p>'));
  129. }
  130. /**
  131. * @ticket 8912
  132. */
  133. function test_wptexturize_html_comments() {
  134. $this->assertEquals('<!--[if !IE]>--><!--<![endif]-->', wptexturize('<!--[if !IE]>--><!--<![endif]-->'));
  135. $this->assertEquals('<!--[if !IE]>"a 9\' plus a \'9\', maybe a 9\' \'9\' "<![endif]-->', wptexturize('<!--[if !IE]>"a 9\' plus a \'9\', maybe a 9\' \'9\' "<![endif]-->'));
  136. $this->assertEquals('<ul><li>Hello.</li><!--<li>Goodbye.</li>--></ul>', wptexturize('<ul><li>Hello.</li><!--<li>Goodbye.</li>--></ul>'));
  137. }
  138. /**
  139. * @ticket 4539
  140. * @ticket 15241
  141. */
  142. function test_entity_quote_cuddling() {
  143. $this->assertEquals('&nbsp;&#8220;Testing&#8221;', wptexturize('&nbsp;"Testing"'));
  144. $this->assertEquals('&#38;&#8220;Testing&#8221;', wptexturize('&#38;"Testing"'));
  145. }
  146. /**
  147. * @ticket 22823
  148. */
  149. function test_apostrophes_before_primes() {
  150. $this->assertEquals( 'WordPress 3.5&#8217;s release date', wptexturize( "WordPress 3.5's release date" ) );
  151. }
  152. /**
  153. * @ticket 23185
  154. */
  155. function test_spaces_around_hyphens() {
  156. $this->assertEquals( ' &#8211; ', wptexturize( ' - ' ) );
  157. $this->assertEquals( '&nbsp;&#8211;&nbsp;', wptexturize( '&nbsp;-&nbsp;' ) );
  158. $this->assertEquals( ' &#8211;&nbsp;', wptexturize( ' -&nbsp;' ) );
  159. $this->assertEquals( '&nbsp;&#8211; ', wptexturize( '&nbsp;- ') );
  160. $this->assertEquals( ' &#8212; ', wptexturize( ' -- ' ) );
  161. $this->assertEquals( '&nbsp;&#8212;&nbsp;', wptexturize( '&nbsp;--&nbsp;' ) );
  162. $this->assertEquals( ' &#8212;&nbsp;', wptexturize( ' --&nbsp;' ) );
  163. $this->assertEquals( '&nbsp;&#8212; ', wptexturize( '&nbsp;-- ') );
  164. }
  165. }