MakeClickable.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. /**
  3. * @group formatting
  4. */
  5. class Tests_Formatting_MakeClickable extends WP_UnitTestCase {
  6. function test_mailto_xss() {
  7. $in = 'testzzz@"STYLE="behavior:url(\'#default#time2\')"onBegin="alert(\'refresh-XSS\')"';
  8. $this->assertSame( $in, make_clickable( $in ) );
  9. }
  10. function test_valid_mailto() {
  11. $valid_emails = array(
  12. 'foo@example.com',
  13. 'foo.bar@example.com',
  14. 'Foo.Bar@a.b.c.d.example.com',
  15. '0@example.com',
  16. 'foo@example-example.com',
  17. );
  18. foreach ( $valid_emails as $email ) {
  19. $this->assertSame( '<a href="mailto:' . $email . '">' . $email . '</a>', make_clickable( $email ) );
  20. }
  21. }
  22. function test_invalid_mailto() {
  23. $invalid_emails = array(
  24. 'foo',
  25. 'foo@',
  26. 'foo@@example.com',
  27. '@example.com',
  28. 'foo @example.com',
  29. 'foo@example',
  30. );
  31. foreach ( $invalid_emails as $email ) {
  32. $this->assertSame( $email, make_clickable( $email ) );
  33. }
  34. }
  35. /**
  36. * Tests that make_clickable() will not link trailing periods, commas,
  37. * and (semi-)colons in URLs with protocol (i.e. http://wordpress.org).
  38. */
  39. function test_strip_trailing_with_protocol() {
  40. $urls_before = array(
  41. 'http://wordpress.org/hello.html',
  42. 'There was a spoon named http://wordpress.org. Alice!',
  43. 'There was a spoon named http://wordpress.org, said Alice.',
  44. 'There was a spoon named http://wordpress.org; said Alice.',
  45. 'There was a spoon named http://wordpress.org: said Alice.',
  46. 'There was a spoon named (http://wordpress.org) said Alice.',
  47. );
  48. $urls_expected = array(
  49. '<a href="http://wordpress.org/hello.html" rel="nofollow">http://wordpress.org/hello.html</a>',
  50. 'There was a spoon named <a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a>. Alice!',
  51. 'There was a spoon named <a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a>, said Alice.',
  52. 'There was a spoon named <a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a>; said Alice.',
  53. 'There was a spoon named <a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a>: said Alice.',
  54. 'There was a spoon named (<a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a>) said Alice.',
  55. );
  56. foreach ( $urls_before as $key => $url ) {
  57. $this->assertSame( $urls_expected[ $key ], make_clickable( $url ) );
  58. }
  59. }
  60. /**
  61. * Tests that make_clickable() will not link trailing periods, commas,
  62. * and (semi-)colons in URLs with protocol (i.e. http://wordpress.org).
  63. */
  64. function test_strip_trailing_with_protocol_nothing_afterwards() {
  65. $urls_before = array(
  66. 'http://wordpress.org/hello.html',
  67. 'There was a spoon named http://wordpress.org.',
  68. 'There was a spoon named http://wordpress.org,',
  69. 'There was a spoon named http://wordpress.org;',
  70. 'There was a spoon named http://wordpress.org:',
  71. 'There was a spoon named (http://wordpress.org)',
  72. 'There was a spoon named (http://wordpress.org)x',
  73. );
  74. $urls_expected = array(
  75. '<a href="http://wordpress.org/hello.html" rel="nofollow">http://wordpress.org/hello.html</a>',
  76. 'There was a spoon named <a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a>.',
  77. 'There was a spoon named <a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a>,',
  78. 'There was a spoon named <a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a>;',
  79. 'There was a spoon named <a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a>:',
  80. 'There was a spoon named (<a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a>)',
  81. 'There was a spoon named (<a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a>)x',
  82. );
  83. foreach ( $urls_before as $key => $url ) {
  84. $this->assertSame( $urls_expected[ $key ], make_clickable( $url ) );
  85. }
  86. }
  87. /**
  88. * Tests that make_clickable() will not link trailing periods, commas,
  89. * and (semi-)colons in URLs without protocol (i.e. www.wordpress.org).
  90. */
  91. function test_strip_trailing_without_protocol() {
  92. $urls_before = array(
  93. 'www.wordpress.org',
  94. 'There was a spoon named www.wordpress.org. Alice!',
  95. 'There was a spoon named www.wordpress.org, said Alice.',
  96. 'There was a spoon named www.wordpress.org; said Alice.',
  97. 'There was a spoon named www.wordpress.org: said Alice.',
  98. 'There was a spoon named www.wordpress.org) said Alice.',
  99. );
  100. $urls_expected = array(
  101. '<a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>',
  102. 'There was a spoon named <a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>. Alice!',
  103. 'There was a spoon named <a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>, said Alice.',
  104. 'There was a spoon named <a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>; said Alice.',
  105. 'There was a spoon named <a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>: said Alice.',
  106. 'There was a spoon named <a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>) said Alice.',
  107. );
  108. foreach ( $urls_before as $key => $url ) {
  109. $this->assertSame( $urls_expected[ $key ], make_clickable( $url ) );
  110. }
  111. }
  112. /**
  113. * Tests that make_clickable() will not link trailing periods, commas,
  114. * and (semi-)colons in URLs without protocol (i.e. www.wordpress.org).
  115. */
  116. function test_strip_trailing_without_protocol_nothing_afterwards() {
  117. $urls_before = array(
  118. 'www.wordpress.org',
  119. 'There was a spoon named www.wordpress.org.',
  120. 'There was a spoon named www.wordpress.org,',
  121. 'There was a spoon named www.wordpress.org;',
  122. 'There was a spoon named www.wordpress.org:',
  123. 'There was a spoon named www.wordpress.org)',
  124. );
  125. $urls_expected = array(
  126. '<a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>',
  127. 'There was a spoon named <a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>.',
  128. 'There was a spoon named <a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>,',
  129. 'There was a spoon named <a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>;',
  130. 'There was a spoon named <a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>:',
  131. 'There was a spoon named <a href="http://www.wordpress.org" rel="nofollow">http://www.wordpress.org</a>)',
  132. );
  133. foreach ( $urls_before as $key => $url ) {
  134. $this->assertSame( $urls_expected[ $key ], make_clickable( $url ) );
  135. }
  136. }
  137. /**
  138. * @ticket 4570
  139. */
  140. function test_iri() {
  141. $urls_before = array(
  142. 'http://www.詹姆斯.com/',
  143. 'http://bg.wikipedia.org/Баба',
  144. 'http://example.com/?a=баба&b=дядо',
  145. );
  146. $urls_expected = array(
  147. '<a href="http://www.詹姆斯.com/" rel="nofollow">http://www.詹姆斯.com/</a>',
  148. '<a href="http://bg.wikipedia.org/Баба" rel="nofollow">http://bg.wikipedia.org/Баба</a>',
  149. '<a href="http://example.com/?a=баба&#038;b=дядо" rel="nofollow">http://example.com/?a=баба&#038;b=дядо</a>',
  150. );
  151. foreach ( $urls_before as $key => $url ) {
  152. $this->assertSame( $urls_expected[ $key ], make_clickable( $url ) );
  153. }
  154. }
  155. /**
  156. * @ticket 10990
  157. */
  158. function test_brackets_in_urls() {
  159. $urls_before = array(
  160. 'http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)',
  161. '(http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software))',
  162. 'blah http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software) blah',
  163. 'blah (http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)) blah',
  164. 'blah blah blah http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software) blah blah',
  165. 'blah blah blah http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)) blah blah',
  166. 'blah blah (http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)) blah blah',
  167. 'blah blah http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software).) blah blah',
  168. 'blah blah http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software).)moreurl blah blah',
  169. 'In his famous speech “You and Your research” (here:
  170. http://www.cs.virginia.edu/~robins/YouAndYourResearch.html)
  171. Richard Hamming wrote about people getting more done with their doors closed, but',
  172. );
  173. $urls_expected = array(
  174. '<a href="http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)" rel="nofollow">http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)</a>',
  175. '(<a href="http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)" rel="nofollow">http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)</a>)',
  176. 'blah <a href="http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)" rel="nofollow">http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)</a> blah',
  177. 'blah (<a href="http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)" rel="nofollow">http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)</a>) blah',
  178. 'blah blah blah <a href="http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)" rel="nofollow">http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)</a> blah blah',
  179. 'blah blah blah <a href="http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)" rel="nofollow">http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)</a>) blah blah',
  180. 'blah blah (<a href="http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)" rel="nofollow">http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)</a>) blah blah',
  181. 'blah blah <a href="http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)" rel="nofollow">http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)</a>.) blah blah',
  182. 'blah blah <a href="http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)" rel="nofollow">http://en.wikipedia.org/wiki/PC_Tools_(Central_Point_Software)</a>.)moreurl blah blah',
  183. 'In his famous speech “You and Your research” (here:
  184. <a href="http://www.cs.virginia.edu/~robins/YouAndYourResearch.html" rel="nofollow">http://www.cs.virginia.edu/~robins/YouAndYourResearch.html</a>)
  185. Richard Hamming wrote about people getting more done with their doors closed, but',
  186. );
  187. foreach ( $urls_before as $key => $url ) {
  188. $this->assertSame( $urls_expected[ $key ], make_clickable( $url ) );
  189. }
  190. }
  191. /**
  192. * Based on real comments which were incorrectly linked.
  193. *
  194. * @ticket 11211
  195. */
  196. function test_real_world_examples() {
  197. $urls_before = array(
  198. 'Example: WordPress, test (some text), I love example.com (http://example.org), it is brilliant',
  199. 'Example: WordPress, test (some text), I love example.com (http://example.com), it is brilliant',
  200. 'Some text followed by a bracketed link with a trailing elipsis (http://example.com)...',
  201. 'In his famous speech “You and Your research” (here: http://www.cs.virginia.edu/~robins/YouAndYourResearch.html) Richard Hamming wrote about people getting more done with their doors closed...',
  202. );
  203. $urls_expected = array(
  204. 'Example: WordPress, test (some text), I love example.com (<a href="http://example.org" rel="nofollow">http://example.org</a>), it is brilliant',
  205. 'Example: WordPress, test (some text), I love example.com (<a href="http://example.com" rel="nofollow">http://example.com</a>), it is brilliant',
  206. 'Some text followed by a bracketed link with a trailing elipsis (<a href="http://example.com" rel="nofollow">http://example.com</a>)...',
  207. 'In his famous speech “You and Your research” (here: <a href="http://www.cs.virginia.edu/~robins/YouAndYourResearch.html" rel="nofollow">http://www.cs.virginia.edu/~robins/YouAndYourResearch.html</a>) Richard Hamming wrote about people getting more done with their doors closed...',
  208. );
  209. foreach ( $urls_before as $key => $url ) {
  210. $this->assertSame( $urls_expected[ $key ], make_clickable( $url ) );
  211. }
  212. }
  213. /**
  214. * @ticket 14993
  215. */
  216. function test_twitter_hash_bang() {
  217. $urls_before = array(
  218. 'http://twitter.com/#!/wordpress/status/25907440233',
  219. 'This is a really good tweet http://twitter.com/#!/wordpress/status/25907440233 !',
  220. 'This is a really good tweet http://twitter.com/#!/wordpress/status/25907440233!',
  221. );
  222. $urls_expected = array(
  223. '<a href="http://twitter.com/#!/wordpress/status/25907440233" rel="nofollow">http://twitter.com/#!/wordpress/status/25907440233</a>',
  224. 'This is a really good tweet <a href="http://twitter.com/#!/wordpress/status/25907440233" rel="nofollow">http://twitter.com/#!/wordpress/status/25907440233</a> !',
  225. 'This is a really good tweet <a href="http://twitter.com/#!/wordpress/status/25907440233" rel="nofollow">http://twitter.com/#!/wordpress/status/25907440233</a>!',
  226. );
  227. foreach ( $urls_before as $key => $url ) {
  228. $this->assertSame( $urls_expected[ $key ], make_clickable( $url ) );
  229. }
  230. }
  231. function test_wrapped_in_angles() {
  232. $before = array(
  233. 'URL wrapped in angle brackets <http://example.com/>',
  234. 'URL wrapped in angle brackets with padding < http://example.com/ >',
  235. 'mailto wrapped in angle brackets <foo@example.com>',
  236. );
  237. $expected = array(
  238. 'URL wrapped in angle brackets <<a href="http://example.com/" rel="nofollow">http://example.com/</a>>',
  239. 'URL wrapped in angle brackets with padding < <a href="http://example.com/" rel="nofollow">http://example.com/</a> >',
  240. 'mailto wrapped in angle brackets <foo@example.com>',
  241. );
  242. foreach ( $before as $key => $url ) {
  243. $this->assertSame( $expected[ $key ], make_clickable( $url ) );
  244. }
  245. }
  246. function test_preceded_by_punctuation() {
  247. $before = array(
  248. 'Comma then URL,http://example.com/',
  249. 'Period then URL.http://example.com/',
  250. 'Semi-colon then URL;http://example.com/',
  251. 'Colon then URL:http://example.com/',
  252. 'Exclamation mark then URL!http://example.com/',
  253. 'Question mark then URL?http://example.com/',
  254. );
  255. $expected = array(
  256. 'Comma then URL,<a href="http://example.com/" rel="nofollow">http://example.com/</a>',
  257. 'Period then URL.<a href="http://example.com/" rel="nofollow">http://example.com/</a>',
  258. 'Semi-colon then URL;<a href="http://example.com/" rel="nofollow">http://example.com/</a>',
  259. 'Colon then URL:<a href="http://example.com/" rel="nofollow">http://example.com/</a>',
  260. 'Exclamation mark then URL!<a href="http://example.com/" rel="nofollow">http://example.com/</a>',
  261. 'Question mark then URL?<a href="http://example.com/" rel="nofollow">http://example.com/</a>',
  262. );
  263. foreach ( $before as $key => $url ) {
  264. $this->assertSame( $expected[ $key ], make_clickable( $url ) );
  265. }
  266. }
  267. function test_dont_break_attributes() {
  268. $urls_before = array(
  269. "<img src='http://trunk.domain/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley'>",
  270. "(<img src='http://trunk.domain/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley'>)",
  271. "http://trunk.domain/testing#something (<img src='http://trunk.domain/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley'>)",
  272. "http://trunk.domain/testing#something
  273. (<img src='http://trunk.domain/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley'>)",
  274. "<span style='text-align:center; display: block;'><object width='425' height='350'><param name='movie' value='https://www.youtube.com/watch?v=72xdCU__XCk&rel=1&fs=1&showsearch=0&showinfo=1&iv_load_policy=1' /> <param name='allowfullscreen' value='true' /> <param name='wmode' value='opaque' /> <embed src='https://www.youtube.com/watch?v=72xdCU__XCk&rel=1&fs=1&showsearch=0&showinfo=1&iv_load_policy=1' type='application/x-shockwave-flash' allowfullscreen='true' width='425' height='350' wmode='opaque'></embed> </object></span>",
  275. '<a href="http://example.com/example.gif" title="Image from http://example.com">Look at this image!</a>',
  276. );
  277. $urls_expected = array(
  278. "<img src='http://trunk.domain/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley'>",
  279. "(<img src='http://trunk.domain/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley'>)",
  280. "<a href=\"http://trunk.domain/testing#something\" rel=\"nofollow\">http://trunk.domain/testing#something</a> (<img src='http://trunk.domain/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley'>)",
  281. "<a href=\"http://trunk.domain/testing#something\" rel=\"nofollow\">http://trunk.domain/testing#something</a>
  282. (<img src='http://trunk.domain/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley'>)",
  283. "<span style='text-align:center; display: block;'><object width='425' height='350'><param name='movie' value='https://www.youtube.com/watch?v=72xdCU__XCk&rel=1&fs=1&showsearch=0&showinfo=1&iv_load_policy=1' /> <param name='allowfullscreen' value='true' /> <param name='wmode' value='opaque' /> <embed src='https://www.youtube.com/watch?v=72xdCU__XCk&rel=1&fs=1&showsearch=0&showinfo=1&iv_load_policy=1' type='application/x-shockwave-flash' allowfullscreen='true' width='425' height='350' wmode='opaque'></embed> </object></span>",
  284. '<a href="http://example.com/example.gif" title="Image from http://example.com">Look at this image!</a>',
  285. );
  286. foreach ( $urls_before as $key => $url ) {
  287. $this->assertSame( $urls_expected[ $key ], make_clickable( $url ) );
  288. }
  289. }
  290. /**
  291. * @ticket 23756
  292. */
  293. function test_no_links_inside_pre_or_code() {
  294. $before = array(
  295. '<pre>http://wordpress.org</pre>',
  296. '<code>http://wordpress.org</code>',
  297. '<pre class="foobar" id="foo">http://wordpress.org</pre>',
  298. '<code class="foobar" id="foo">http://wordpress.org</code>',
  299. '<precustomtag>http://wordpress.org</precustomtag>',
  300. '<codecustomtag>http://wordpress.org</codecustomtag>',
  301. 'URL before pre http://wordpress.org<pre>http://wordpress.org</pre>',
  302. 'URL before code http://wordpress.org<code>http://wordpress.org</code>',
  303. 'URL after pre <PRE>http://wordpress.org</PRE>http://wordpress.org',
  304. 'URL after code <code>http://wordpress.org</code>http://wordpress.org',
  305. 'URL before and after pre http://wordpress.org<pre>http://wordpress.org</pre>http://wordpress.org',
  306. 'URL before and after code http://wordpress.org<code>http://wordpress.org</code>http://wordpress.org',
  307. 'code inside pre <pre>http://wordpress.org <code>http://wordpress.org</code> http://wordpress.org</pre>',
  308. );
  309. $expected = array(
  310. '<pre>http://wordpress.org</pre>',
  311. '<code>http://wordpress.org</code>',
  312. '<pre class="foobar" id="foo">http://wordpress.org</pre>',
  313. '<code class="foobar" id="foo">http://wordpress.org</code>',
  314. '<precustomtag><a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a></precustomtag>',
  315. '<codecustomtag><a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a></codecustomtag>',
  316. 'URL before pre <a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a><pre>http://wordpress.org</pre>',
  317. 'URL before code <a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a><code>http://wordpress.org</code>',
  318. 'URL after pre <PRE>http://wordpress.org</PRE><a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a>',
  319. 'URL after code <code>http://wordpress.org</code><a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a>',
  320. 'URL before and after pre <a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a><pre>http://wordpress.org</pre><a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a>',
  321. 'URL before and after code <a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a><code>http://wordpress.org</code><a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a>',
  322. 'code inside pre <pre>http://wordpress.org <code>http://wordpress.org</code> http://wordpress.org</pre>',
  323. );
  324. foreach ( $before as $key => $url ) {
  325. $this->assertSame( $expected[ $key ], make_clickable( $url ) );
  326. }
  327. }
  328. /**
  329. * @ticket 16892
  330. */
  331. function test_click_inside_html() {
  332. $urls_before = array(
  333. '<span>http://example.com</span>',
  334. '<p>http://example.com/</p>',
  335. );
  336. $urls_expected = array(
  337. '<span><a href="http://example.com" rel="nofollow">http://example.com</a></span>',
  338. '<p><a href="http://example.com/" rel="nofollow">http://example.com/</a></p>',
  339. );
  340. foreach ( $urls_before as $key => $url ) {
  341. $this->assertSame( $urls_expected[ $key ], make_clickable( $url ) );
  342. }
  343. }
  344. function test_no_links_within_links() {
  345. $in = array(
  346. 'Some text with a link <a href="http://example.com">http://example.com</a>',
  347. // '<a href="http://wordpress.org">This is already a link www.wordpress.org</a>', // Fails in 3.3.1 too.
  348. );
  349. foreach ( $in as $text ) {
  350. $this->assertSame( $text, make_clickable( $text ) );
  351. }
  352. }
  353. /**
  354. * @ticket 16892
  355. */
  356. function test_no_segfault() {
  357. $in = str_repeat( 'http://example.com/2011/03/18/post-title/', 256 );
  358. $out = make_clickable( $in );
  359. $this->assertSame( $in, $out );
  360. }
  361. /**
  362. * @ticket 19028
  363. */
  364. function test_line_break_in_existing_clickable_link() {
  365. $html = "<a
  366. href='mailto:someone@example.com'>someone@example.com</a>";
  367. $this->assertSame( $html, make_clickable( $html ) );
  368. }
  369. /**
  370. * @ticket 30162
  371. * @dataProvider data_script_and_style_tags
  372. */
  373. public function test_dont_link_script_and_style_tags( $tag ) {
  374. $this->assertSame( $tag, make_clickable( $tag ) );
  375. }
  376. public function data_script_and_style_tags() {
  377. return array(
  378. array(
  379. '<script>http://wordpress.org</script>',
  380. ),
  381. array(
  382. '<style>http://wordpress.org</style>',
  383. ),
  384. array(
  385. '<script type="text/javascript">http://wordpress.org</script>',
  386. ),
  387. array(
  388. '<style type="text/css">http://wordpress.org</style>',
  389. ),
  390. );
  391. }
  392. /**
  393. * @ticket 48022
  394. * @dataProvider data_add_rel_ugc_in_comments
  395. */
  396. public function test_add_rel_ugc_in_comments( $content, $expected ) {
  397. $comment_id = self::factory()->comment->create(
  398. array(
  399. 'comment_content' => $content,
  400. )
  401. );
  402. ob_start();
  403. comment_text( $comment_id );
  404. $comment_text = ob_get_clean();
  405. $this->assertContains( $expected, make_clickable( $comment_text ) );
  406. }
  407. public function data_add_rel_ugc_in_comments() {
  408. return array(
  409. array(
  410. 'http://wordpress.org',
  411. '<a href="http://wordpress.org" rel="nofollow ugc">http://wordpress.org</a>',
  412. ),
  413. array(
  414. 'www.wordpress.org',
  415. '<p><a href="http://www.wordpress.org" rel="nofollow ugc">http://www.wordpress.org</a>',
  416. ),
  417. );
  418. }
  419. }