links.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import { testDataProcessor } from '../_utils/utils';
  6. describe( 'GFMDataProcessor', () => {
  7. describe( 'links', () => {
  8. it( 'should not autolink', () => {
  9. testDataProcessor(
  10. 'Link: http://example.com/.',
  11. '<p>Link: http://example.com/.</p>'
  12. );
  13. } );
  14. it( 'should not autolink with params', () => {
  15. testDataProcessor(
  16. 'Link: http://example.com/?foo=1&bar=2.',
  17. '<p>Link: http://example.com/?foo=1&bar=2.</p>'
  18. );
  19. } );
  20. it( 'should not autolink inside list', () => {
  21. testDataProcessor(
  22. '* http://example.com/',
  23. '<ul><li>http://example.com/</li></ul>'
  24. );
  25. } );
  26. it( 'should not autolink inside blockquote', () => {
  27. testDataProcessor(
  28. '> Blockquoted: http://example.com/',
  29. '<blockquote>' +
  30. '<p>Blockquoted: http://example.com/</p>' +
  31. '</blockquote>'
  32. );
  33. } );
  34. it( 'should not autolink inside inline code', () => {
  35. testDataProcessor(
  36. '`<http://example.com/>`',
  37. '<p><code><http://example.com/></code></p>'
  38. );
  39. } );
  40. it( 'should not autolink inside code block', () => {
  41. testDataProcessor(
  42. ' <http://example.com/>',
  43. '<pre><code><http://example.com/></code></pre>',
  44. // When converting back, code block will be normalized to ```.
  45. '```\n' +
  46. '<http://example.com/>\n' +
  47. '```'
  48. );
  49. } );
  50. it( 'should not process already linked #1', () => {
  51. testDataProcessor(
  52. 'Already linked: [http://example.com/](http://example.com/)',
  53. '<p>Already linked: <a href="http://example.com/">http://example.com/</a></p>'
  54. );
  55. } );
  56. it( 'should not process already linked #2', () => {
  57. testDataProcessor(
  58. 'Already linked: [**http://example.com/**](http://example.com/)',
  59. '<p>Already linked: <a href="http://example.com/"><strong>http://example.com/</strong></a></p>'
  60. );
  61. } );
  62. it( 'should process inline links', () => {
  63. testDataProcessor(
  64. '[URL](/url/)',
  65. '<p><a href="/url/">URL</a></p>'
  66. );
  67. } );
  68. it( 'should process inline links with title', () => {
  69. testDataProcessor(
  70. '[URL and title](/url/ "title")',
  71. '<p><a href="/url/" title="title">URL and title</a></p>'
  72. );
  73. } );
  74. it( 'should process inline links with title preceded by two spaces', () => {
  75. testDataProcessor(
  76. '[URL and title](/url/ "title preceded by two spaces")',
  77. '<p><a href="/url/" title="title preceded by two spaces">URL and title</a></p>',
  78. // When converting back spaces will be normalized to one space.
  79. '[URL and title](/url/ "title preceded by two spaces")'
  80. );
  81. } );
  82. it( 'should process inline links with title preceded by tab', () => {
  83. testDataProcessor(
  84. '[URL and title](/url/ "title preceded by tab")',
  85. '<p><a href="/url/" title="title preceded by tab">URL and title</a></p>',
  86. // When converting back tab will be normalized to one space.
  87. '[URL and title](/url/ "title preceded by tab")'
  88. );
  89. } );
  90. it( 'should process inline links with title that has spaces afterwards', () => {
  91. testDataProcessor(
  92. '[URL and title](/url/ "title has spaces afterward" )',
  93. '<p><a href="/url/" title="title has spaces afterward">URL and title</a></p>',
  94. // When converting back spaces will be removed.
  95. '[URL and title](/url/ "title has spaces afterward")'
  96. );
  97. } );
  98. // it( 'should process empty link', () => {
  99. // testDataProcessor(
  100. // '[Empty]()',
  101. //
  102. // '<p><a href="">Empty</a></p>'
  103. // );
  104. // } );
  105. it( 'should process reference links', () => {
  106. testDataProcessor(
  107. 'Foo [bar][1].\n\n' +
  108. '[1]: /url/ "Title"',
  109. '<p>Foo <a href="/url/" title="Title">bar</a>.</p>',
  110. // After converting back reference links will be converted to normal links.
  111. // This might be a problem when switching between source and editor.
  112. 'Foo [bar](/url/ "Title").'
  113. );
  114. } );
  115. it( 'should process reference links - without space', () => {
  116. testDataProcessor(
  117. 'Foo [bar][1].\n\n' +
  118. '[1]: /url/ "Title"',
  119. '<p>Foo <a href="/url/" title="Title">bar</a>.</p>',
  120. 'Foo [bar](/url/ "Title").'
  121. );
  122. } );
  123. it( 'should process reference links - with embedded brackets', () => {
  124. testDataProcessor(
  125. 'With [embedded [brackets]][b].\n\n' +
  126. '[b]: /url/',
  127. '<p>With <a href="/url/">embedded [brackets]</a>.</p>',
  128. 'With [embedded \\[brackets\\]](/url/).'
  129. );
  130. } );
  131. it( 'should process reference links - with reference indented once', () => {
  132. testDataProcessor(
  133. 'Indented [once][].\n\n' +
  134. ' [once]: /url',
  135. '<p>Indented <a href="/url">once</a>.</p>',
  136. 'Indented [once](/url).'
  137. );
  138. } );
  139. it( 'should process reference links - with reference indented twice', () => {
  140. testDataProcessor(
  141. 'Indented [twice][].\n\n' +
  142. ' [twice]: /url',
  143. '<p>Indented <a href="/url">twice</a>.</p>',
  144. 'Indented [twice](/url).'
  145. );
  146. } );
  147. it( 'should process reference links - with reference indented three times', () => {
  148. testDataProcessor(
  149. 'Indented [trice][].\n\n' +
  150. ' [trice]: /url',
  151. '<p>Indented <a href="/url">trice</a>.</p>',
  152. 'Indented [trice](/url).'
  153. );
  154. } );
  155. it( 'should process reference links when title and reference are same #1', () => {
  156. testDataProcessor(
  157. '[this][this]\n\n' +
  158. '[this]: foo',
  159. '<p><a href="foo">this</a></p>',
  160. '[this](foo)'
  161. );
  162. } );
  163. it( 'should process reference links when title and reference are same #2', () => {
  164. testDataProcessor(
  165. '[this][this]\n\n' +
  166. '[this]: foo',
  167. '<p><a href="foo">this</a></p>',
  168. '[this](foo)'
  169. );
  170. } );
  171. it( 'should process reference links when only title is provided and is same as reference #1', () => {
  172. testDataProcessor(
  173. '[this][]\n\n' +
  174. '[this]: foo',
  175. '<p><a href="foo">this</a></p>',
  176. '[this](foo)'
  177. );
  178. } );
  179. it( 'should process reference links when only title is provided and is same as reference #2', () => {
  180. testDataProcessor(
  181. '[this][]\n\n' +
  182. '[this]: foo',
  183. '<p><a href="foo">this</a></p>',
  184. '[this](foo)'
  185. );
  186. } );
  187. it( 'should process reference links when only title is provided and is same as reference #3', () => {
  188. testDataProcessor(
  189. '[this]\n\n' +
  190. '[this]: foo',
  191. '<p><a href="foo">this</a></p>',
  192. '[this](foo)'
  193. );
  194. } );
  195. it( 'should not process reference links when reference is not found #1', () => {
  196. testDataProcessor(
  197. '[this][]',
  198. '<p>[this][]</p>',
  199. '\\[this\\]\\[\\]'
  200. );
  201. } );
  202. it( 'should not process reference links when reference is not found #2', () => {
  203. testDataProcessor(
  204. '[this]',
  205. '<p>[this]</p>',
  206. '\\[this\\]'
  207. );
  208. } );
  209. it( 'should process reference links nested in brackets #1', () => {
  210. testDataProcessor(
  211. '[a reference inside [this][]]\n\n' +
  212. '[this]: foo',
  213. '<p>[a reference inside <a href="foo">this</a>]</p>',
  214. '\\[a reference inside [this](foo)\\]'
  215. );
  216. } );
  217. it( 'should process reference links nested in brackets #2', () => {
  218. testDataProcessor(
  219. '[a reference inside [this]]\n\n' +
  220. '[this]: foo',
  221. '<p>[a reference inside <a href="foo">this</a>]</p>',
  222. '\\[a reference inside [this](foo)\\]'
  223. );
  224. } );
  225. it( 'should not process reference links when title is same as reference but reference is different', () => {
  226. testDataProcessor(
  227. '[this](/something/else/)\n\n' +
  228. '[this]: foo',
  229. '<p><a href="/something/else/">this</a></p>',
  230. '[this](/something/else/)'
  231. );
  232. } );
  233. it( 'should not process reference links suppressed by backslashes', () => {
  234. testDataProcessor(
  235. 'Suppress \\[this] and [this\\].\n\n' +
  236. '[this]: foo',
  237. '<p>Suppress [this] and [this].</p>',
  238. 'Suppress \\[this\\] and \\[this\\].'
  239. );
  240. } );
  241. it( 'should process reference links when used across multiple lines #1', () => {
  242. testDataProcessor(
  243. 'This is [multiline\n' +
  244. 'reference]\n\n' +
  245. '[multiline reference]: foo',
  246. '<p>This is <a href="foo">multiline<br></br>reference</a></p>',
  247. 'This is [multiline\n' +
  248. 'reference](foo)'
  249. );
  250. } );
  251. it( 'should process reference links when used across multiple lines #2', () => {
  252. testDataProcessor(
  253. 'This is [multiline \n' +
  254. 'reference]\n\n' +
  255. '[multiline reference]: foo',
  256. '<p>This is <a href="foo">multiline<br></br>reference</a></p>',
  257. 'This is [multiline\n' +
  258. 'reference](foo)'
  259. );
  260. } );
  261. it( 'should process reference links case-insensitive', () => {
  262. testDataProcessor(
  263. '[hi]\n\n' +
  264. '[HI]: /url',
  265. '<p><a href="/url">hi</a></p>',
  266. '[hi](/url)'
  267. );
  268. } );
  269. } );
  270. } );