autolink.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import CodeBlockEditing from '@ckeditor/ckeditor5-code-block/src/codeblockediting';
  7. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  8. import Input from '@ckeditor/ckeditor5-typing/src/input';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import ShiftEnter from '@ckeditor/ckeditor5-enter/src/shiftenter';
  11. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  12. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  13. import LinkEditing from '../src/linkediting';
  14. import AutoLink from '../src/autolink';
  15. describe( 'AutoLink', () => {
  16. let editor;
  17. it( 'should be named', () => {
  18. expect( AutoLink.pluginName ).to.equal( 'AutoLink' );
  19. } );
  20. it( 'should be loaded without Enter & ShiftEnter features', async () => {
  21. const editor = await ModelTestEditor.create( {
  22. plugins: [ Paragraph, Input, LinkEditing, AutoLink ]
  23. } );
  24. await editor.destroy();
  25. } );
  26. describe( 'auto link behavior', () => {
  27. let model;
  28. beforeEach( async () => {
  29. editor = await ModelTestEditor.create( {
  30. plugins: [ Paragraph, Input, Enter, ShiftEnter, LinkEditing, AutoLink ]
  31. } );
  32. model = editor.model;
  33. setData( model, '<paragraph>[]</paragraph>' );
  34. } );
  35. it( 'does nothing on typing normal text', () => {
  36. simulateTyping( 'Cupcake ipsum dolor. Sit amet caramels. Pie jelly-o lemon drops fruitcake.' );
  37. expect( getData( model ) ).to.equal(
  38. '<paragraph>Cupcake ipsum dolor. Sit amet caramels. Pie jelly-o lemon drops fruitcake.[]</paragraph>'
  39. );
  40. } );
  41. it( 'does not add linkHref attribute to a text link while typing', () => {
  42. simulateTyping( 'https://www.cksource.com' );
  43. expect( getData( model ) ).to.equal(
  44. '<paragraph>https://www.cksource.com[]</paragraph>'
  45. );
  46. } );
  47. it( 'adds linkHref attribute to a text link after space', () => {
  48. simulateTyping( 'https://www.cksource.com ' );
  49. expect( getData( model ) ).to.equal(
  50. '<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text> []</paragraph>'
  51. );
  52. } );
  53. it( 'does not add linkHref attribute if linkHref is not allowed', () => {
  54. model.schema.addAttributeCheck( () => false ); // Disable all attributes.
  55. simulateTyping( 'https://www.cksource.com ' );
  56. expect( getData( model ) ).to.equal(
  57. '<paragraph>https://www.cksource.com []</paragraph>'
  58. );
  59. } );
  60. it( 'does not add linkHref attribute if plugin is force-disabled (on space)', () => {
  61. editor.plugins.get( 'AutoLink' ).forceDisabled( 'test' );
  62. simulateTyping( 'https://www.cksource.com ' );
  63. expect( getData( model ) ).to.equal(
  64. '<paragraph>https://www.cksource.com []</paragraph>'
  65. );
  66. } );
  67. it( 'does not add linkHref attribute if plugin is force-disabled (on enter)', () => {
  68. setData( model, '<paragraph>https://www.cksource.com[]</paragraph>' );
  69. editor.plugins.get( 'AutoLink' ).forceDisabled( 'test' );
  70. editor.execute( 'enter' );
  71. expect( getData( model ) ).to.equal(
  72. '<paragraph>https://www.cksource.com</paragraph>' +
  73. '<paragraph>[]</paragraph>'
  74. );
  75. } );
  76. it( 'does not add linkHref attribute if plugin is force-disabled (on shift enter)', () => {
  77. setData( model, '<paragraph>https://www.cksource.com[]</paragraph>' );
  78. editor.plugins.get( 'AutoLink' ).forceDisabled( 'test' );
  79. editor.execute( 'shiftEnter' );
  80. expect( getData( model ) ).to.equal(
  81. '<paragraph>https://www.cksource.com<softBreak></softBreak>[]</paragraph>'
  82. );
  83. } );
  84. it( 'adds linkHref attribute to a text link after space (inside paragraph)', () => {
  85. setData( model, '<paragraph>Foo Bar [] Baz</paragraph>' );
  86. simulateTyping( 'https://www.cksource.com ' );
  87. expect( getData( model ) ).to.equal(
  88. '<paragraph>Foo Bar <$text linkHref="https://www.cksource.com">https://www.cksource.com</$text> [] Baz</paragraph>'
  89. );
  90. } );
  91. it( 'adds linkHref attribute to a text link on shift enter', () => {
  92. setData( model, '<paragraph>https://www.cksource.com[]</paragraph>' );
  93. editor.execute( 'shiftEnter' );
  94. // TODO: should test with selection but master has a bug. See: https://github.com/ckeditor/ckeditor5/issues/7459.
  95. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  96. '<paragraph>' +
  97. '<$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>' +
  98. '<softBreak></softBreak>' +
  99. '</paragraph>'
  100. );
  101. } );
  102. it( 'does not add linkHref attribute to a text link after double soft break', () => {
  103. setData( model, '<paragraph>https://www.cksource.com<softBreak></softBreak>[]</paragraph>' );
  104. editor.execute( 'shiftEnter' );
  105. expect( getData( model ) ).to.equal(
  106. '<paragraph>https://www.cksource.com<softBreak></softBreak><softBreak></softBreak>[]</paragraph>'
  107. );
  108. } );
  109. it( 'adds linkHref attribute to a text link on enter', () => {
  110. setData( model, '<paragraph>https://www.cksource.com[]</paragraph>' );
  111. editor.execute( 'enter' );
  112. expect( getData( model ) ).to.equal(
  113. '<paragraph>' +
  114. '<$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>' +
  115. '</paragraph>' +
  116. '<paragraph>[]</paragraph>'
  117. );
  118. } );
  119. it( 'adds "mailto://" to link of detected email addresses', () => {
  120. simulateTyping( 'newsletter@cksource.com ' );
  121. expect( getData( model ) ).to.equal(
  122. '<paragraph><$text linkHref="mailto://newsletter@cksource.com">newsletter@cksource.com</$text> []</paragraph>'
  123. );
  124. } );
  125. // Some examples came from https://mathiasbynens.be/demo/url-regex.
  126. describe( 'supported URL', () => {
  127. const supportedURLs = [
  128. 'http://cksource.com',
  129. 'https://cksource.com',
  130. 'https://cksource.com:8080',
  131. 'http://www.cksource.com',
  132. 'hTtP://WwW.cKsOuRcE.cOm',
  133. 'www.cksource.com',
  134. 'http://foo.bar.cksource.com',
  135. 'http://www.cksource.com/some/path/index.html#abc',
  136. 'http://www.cksource.com/some/path/index.html?foo=bar',
  137. 'http://www.cksource.com/some/path/index.html?foo=bar#abc',
  138. 'http://www.cksource.com:8080/some/path/index.html?foo=bar#abc',
  139. 'http://www.cksource.com/some/path/index.html#abc?foo=bar',
  140. 'ftp://cksource.com',
  141. 'http://cksource.com/foo_bar',
  142. 'http://cksource.com/foo_bar/',
  143. 'http://cksource.com/foo_bar_(wikipedia)',
  144. 'http://cksource.com/foo_bar_(wikipedia)_(again)',
  145. 'http://www.cksource.com/wpstyle/?p=364',
  146. 'http://www.cksource.com/wpstyle/?bar=baz&inga=42&quux',
  147. 'http://userid:password@example.com:8080' +
  148. 'http://userid:password@example.com:8080/' +
  149. 'http://userid@cksource.com' +
  150. 'http://userid@cksource.com/' +
  151. 'http://userid@cksource.com:8080' +
  152. 'http://userid@cksource.com:8080/' +
  153. 'http://userid:password@cksource.com' +
  154. 'http://userid:password@cksource.com/' +
  155. 'http://🥳df.ws/123',
  156. 'http://🥳.ws/富',
  157. 'http://🥳.ws',
  158. 'http://🥳.ws/',
  159. 'http://cksource.com/blah_(wikipedia)#cite-1',
  160. 'http://cksource.com/blah_(wikipedia)_blah#cite-1',
  161. 'http://cksource.com/unicode_(🥳)_in_parens',
  162. 'http://cksource.com/(something)?after=parens',
  163. 'http://🥳.cksource.com/',
  164. 'http://code.cksource.com/woot/#&product=browser',
  165. 'http://j.mp',
  166. 'ftp://cksource.com/baz',
  167. 'http://cksource.com/?q=Test%20URL-encoded%20stuff',
  168. 'http://مثال.إختبار',
  169. 'http://例子.测试',
  170. 'http://उदाहरण.परीक्षा',
  171. 'http://1337.net',
  172. 'http://a.b-c.de'
  173. ];
  174. for ( const supportedURL of supportedURLs ) {
  175. it( `should detect "${ supportedURL }" as a valid URL`, () => {
  176. simulateTyping( supportedURL + ' ' );
  177. expect( getData( model ) ).to.equal(
  178. `<paragraph><$text linkHref="${ supportedURL }">${ supportedURL }</$text> []</paragraph>` );
  179. } );
  180. }
  181. } );
  182. describe( 'invalid or supported URL', () => {
  183. // Some examples came from https://mathiasbynens.be/demo/url-regex.
  184. const unsupportedOrInvalid = [
  185. 'http://',
  186. 'http://.',
  187. 'http://..',
  188. 'http://../',
  189. 'http://🥳',
  190. 'http://?',
  191. 'http://??',
  192. 'http://??/',
  193. 'http://#',
  194. 'http://##',
  195. 'http://##/',
  196. '//',
  197. '//a',
  198. '///a',
  199. '///',
  200. 'http:///a',
  201. 'rdar://1234',
  202. 'h://test',
  203. ':// foo bar',
  204. 'ftps://foo.bar/',
  205. 'http://-error-.invalid/',
  206. 'http://localhost',
  207. 'http:/cksource.com',
  208. 'cksource.com',
  209. 'ww.cksource.com'
  210. ];
  211. for ( const unsupportedURL of unsupportedOrInvalid ) {
  212. it( `should not detect "${ unsupportedURL }" as a valid URL`, () => {
  213. simulateTyping( unsupportedURL + ' ' );
  214. expect( getData( model ) ).to.equal(
  215. `<paragraph>${ unsupportedURL } []</paragraph>` );
  216. } );
  217. }
  218. } );
  219. } );
  220. describe( 'Undo integration', () => {
  221. let model;
  222. beforeEach( async () => {
  223. editor = await ModelTestEditor.create( {
  224. plugins: [ Paragraph, Input, Enter, ShiftEnter, LinkEditing, AutoLink, UndoEditing ]
  225. } );
  226. model = editor.model;
  227. setData( model, '<paragraph>https://www.cksource.com[]</paragraph>' );
  228. } );
  229. it( 'should undo auto-linking (after space)', () => {
  230. simulateTyping( ' ' );
  231. editor.commands.execute( 'undo' );
  232. expect( getData( model ) ).to.equal(
  233. '<paragraph>https://www.cksource.com []</paragraph>'
  234. );
  235. } );
  236. it( 'should undo auto-linking (after <softBreak>)', () => {
  237. editor.execute( 'shiftEnter' );
  238. editor.commands.execute( 'undo' );
  239. expect( getData( model ) ).to.equal(
  240. '<paragraph>https://www.cksource.com<softBreak></softBreak>[]</paragraph>'
  241. );
  242. } );
  243. it( 'should undo auto-linking (after enter)', () => {
  244. editor.execute( 'enter' );
  245. editor.commands.execute( 'undo' );
  246. expect( getData( model ) ).to.equal(
  247. '<paragraph>https://www.cksource.com</paragraph>' +
  248. '<paragraph>[]</paragraph>'
  249. );
  250. } );
  251. } );
  252. describe( 'Code blocks integration', () => {
  253. let model;
  254. beforeEach( async () => {
  255. editor = await ModelTestEditor.create( {
  256. plugins: [ Paragraph, Input, Enter, ShiftEnter, LinkEditing, AutoLink, CodeBlockEditing ]
  257. } );
  258. model = editor.model;
  259. } );
  260. it( 'should be disabled inside code blocks (on space)', () => {
  261. setData( model, '<codeBlock language="plaintext">some [] code</codeBlock>' );
  262. const plugin = editor.plugins.get( 'AutoLink' );
  263. simulateTyping( 'www.cksource.com' );
  264. expect( plugin.isEnabled ).to.be.false;
  265. expect( getData( model, { withoutSelection: true } ) )
  266. .to.equal( '<codeBlock language="plaintext">some www.cksource.com code</codeBlock>' );
  267. } );
  268. it( 'should be disabled inside code blocks (on enter)', () => {
  269. setData( model, '<codeBlock language="plaintext">some www.cksource.com[] code</codeBlock>' );
  270. const plugin = editor.plugins.get( 'AutoLink' );
  271. editor.execute( 'enter' );
  272. expect( plugin.isEnabled ).to.be.false;
  273. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  274. '<codeBlock language="plaintext">some www.cksource.com</codeBlock>' +
  275. '<codeBlock language="plaintext"> code</codeBlock>'
  276. );
  277. } );
  278. it( 'should be disabled inside code blocks (on shift-enter)', () => {
  279. setData( model, '<codeBlock language="plaintext">some www.cksource.com[] code</codeBlock>' );
  280. const plugin = editor.plugins.get( 'AutoLink' );
  281. editor.execute( 'shiftEnter' );
  282. expect( plugin.isEnabled ).to.be.false;
  283. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  284. '<codeBlock language="plaintext">some www.cksource.com<softBreak></softBreak> code</codeBlock>'
  285. );
  286. } );
  287. } );
  288. function simulateTyping( text ) {
  289. const letters = text.split( '' );
  290. for ( const letter of letters ) {
  291. editor.execute( 'input', { text: letter } );
  292. }
  293. }
  294. } );