autolink.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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( 'adds linkHref attribute to a text link after space (inside paragraph)', () => {
  61. setData( model, '<paragraph>Foo Bar [] Baz</paragraph>' );
  62. simulateTyping( 'https://www.cksource.com ' );
  63. expect( getData( model ) ).to.equal(
  64. '<paragraph>Foo Bar <$text linkHref="https://www.cksource.com">https://www.cksource.com</$text> [] Baz</paragraph>'
  65. );
  66. } );
  67. it( 'adds linkHref attribute to a text link after a soft break', () => {
  68. setData( model, '<paragraph>https://www.cksource.com[]</paragraph>' );
  69. editor.execute( 'shiftEnter' );
  70. // TODO: should test with selection but master has a bug. See: https://github.com/ckeditor/ckeditor5/issues/7459.
  71. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  72. '<paragraph>' +
  73. '<$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>' +
  74. '<softBreak></softBreak>' +
  75. '</paragraph>'
  76. );
  77. } );
  78. it( 'does not add linkHref attribute to a text link after double soft break', () => {
  79. setData( model, '<paragraph>https://www.cksource.com<softBreak></softBreak>[]</paragraph>' );
  80. editor.execute( 'shiftEnter' );
  81. expect( getData( model ) ).to.equal(
  82. '<paragraph>https://www.cksource.com<softBreak></softBreak><softBreak></softBreak>[]</paragraph>'
  83. );
  84. } );
  85. it( 'adds linkHref attribute to a text link on enter', () => {
  86. setData( model, '<paragraph>https://www.cksource.com[]</paragraph>' );
  87. editor.execute( 'enter' );
  88. expect( getData( model ) ).to.equal(
  89. '<paragraph>' +
  90. '<$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>' +
  91. '</paragraph>' +
  92. '<paragraph>[]</paragraph>'
  93. );
  94. } );
  95. it( 'adds "mailto://" to link of detected email addresses', () => {
  96. simulateTyping( 'newsletter@cksource.com ' );
  97. expect( getData( model ) ).to.equal(
  98. '<paragraph><$text linkHref="mailto://newsletter@cksource.com">newsletter@cksource.com</$text> []</paragraph>'
  99. );
  100. } );
  101. // Some examples came from https://mathiasbynens.be/demo/url-regex.
  102. describe( 'supported URL', () => {
  103. const supportedURLs = [
  104. 'http://cksource.com',
  105. 'https://cksource.com',
  106. 'https://cksource.com:8080',
  107. 'http://www.cksource.com',
  108. 'hTtP://WwW.cKsOuRcE.cOm',
  109. 'www.cksource.com',
  110. 'http://foo.bar.cksource.com',
  111. 'http://www.cksource.com/some/path/index.html#abc',
  112. 'http://www.cksource.com/some/path/index.html?foo=bar',
  113. 'http://www.cksource.com/some/path/index.html?foo=bar#abc',
  114. 'http://www.cksource.com:8080/some/path/index.html?foo=bar#abc',
  115. 'http://www.cksource.com/some/path/index.html#abc?foo=bar',
  116. 'ftp://cksource.com',
  117. 'http://cksource.com/foo_bar',
  118. 'http://cksource.com/foo_bar/',
  119. 'http://cksource.com/foo_bar_(wikipedia)',
  120. 'http://cksource.com/foo_bar_(wikipedia)_(again)',
  121. 'http://www.cksource.com/wpstyle/?p=364',
  122. 'http://www.cksource.com/wpstyle/?bar=baz&inga=42&quux',
  123. 'http://userid:password@example.com:8080' +
  124. 'http://userid:password@example.com:8080/' +
  125. 'http://userid@cksource.com' +
  126. 'http://userid@cksource.com/' +
  127. 'http://userid@cksource.com:8080' +
  128. 'http://userid@cksource.com:8080/' +
  129. 'http://userid:password@cksource.com' +
  130. 'http://userid:password@cksource.com/' +
  131. 'http://🥳df.ws/123',
  132. 'http://🥳.ws/富',
  133. 'http://🥳.ws',
  134. 'http://🥳.ws/',
  135. 'http://cksource.com/blah_(wikipedia)#cite-1',
  136. 'http://cksource.com/blah_(wikipedia)_blah#cite-1',
  137. 'http://cksource.com/unicode_(🥳)_in_parens',
  138. 'http://cksource.com/(something)?after=parens',
  139. 'http://🥳.cksource.com/',
  140. 'http://code.cksource.com/woot/#&product=browser',
  141. 'http://j.mp',
  142. 'ftp://cksource.com/baz',
  143. 'http://cksource.com/?q=Test%20URL-encoded%20stuff',
  144. 'http://مثال.إختبار',
  145. 'http://例子.测试',
  146. 'http://उदाहरण.परीक्षा',
  147. 'http://1337.net',
  148. 'http://a.b-c.de'
  149. ];
  150. for ( const supportedURL of supportedURLs ) {
  151. it( `should detect "${ supportedURL }" as a valid URL`, () => {
  152. simulateTyping( supportedURL + ' ' );
  153. expect( getData( model ) ).to.equal(
  154. `<paragraph><$text linkHref="${ supportedURL }">${ supportedURL }</$text> []</paragraph>` );
  155. } );
  156. }
  157. } );
  158. describe( 'invalid or supported URL', () => {
  159. // Some examples came from https://mathiasbynens.be/demo/url-regex.
  160. const unsupportedOrInvalid = [
  161. 'http://',
  162. 'http://.',
  163. 'http://..',
  164. 'http://../',
  165. 'http://🥳',
  166. 'http://?',
  167. 'http://??',
  168. 'http://??/',
  169. 'http://#',
  170. 'http://##',
  171. 'http://##/',
  172. '//',
  173. '//a',
  174. '///a',
  175. '///',
  176. 'http:///a',
  177. 'rdar://1234',
  178. 'h://test',
  179. ':// foo bar',
  180. 'ftps://foo.bar/',
  181. 'http://-error-.invalid/',
  182. 'http://localhost',
  183. 'http:/cksource.com',
  184. 'cksource.com',
  185. 'ww.cksource.com'
  186. ];
  187. for ( const unsupportedURL of unsupportedOrInvalid ) {
  188. it( `should not detect "${ unsupportedURL }" as a valid URL`, () => {
  189. simulateTyping( unsupportedURL + ' ' );
  190. expect( getData( model ) ).to.equal(
  191. `<paragraph>${ unsupportedURL } []</paragraph>` );
  192. } );
  193. }
  194. } );
  195. } );
  196. describe( 'Undo integration', () => {
  197. let model;
  198. beforeEach( async () => {
  199. editor = await ModelTestEditor.create( {
  200. plugins: [ Paragraph, Input, Enter, ShiftEnter, LinkEditing, AutoLink, UndoEditing ]
  201. } );
  202. model = editor.model;
  203. setData( model, '<paragraph>https://www.cksource.com[]</paragraph>' );
  204. } );
  205. it( 'should undo auto-linking (after space)', () => {
  206. simulateTyping( ' ' );
  207. editor.commands.execute( 'undo' );
  208. expect( getData( model ) ).to.equal(
  209. '<paragraph>https://www.cksource.com []</paragraph>'
  210. );
  211. } );
  212. it( 'should undo auto-linking (after <softBreak>)', () => {
  213. editor.execute( 'shiftEnter' );
  214. editor.commands.execute( 'undo' );
  215. expect( getData( model ) ).to.equal(
  216. '<paragraph>https://www.cksource.com<softBreak></softBreak>[]</paragraph>'
  217. );
  218. } );
  219. it( 'should undo auto-linking (after enter)', () => {
  220. editor.execute( 'enter' );
  221. editor.commands.execute( 'undo' );
  222. expect( getData( model ) ).to.equal(
  223. '<paragraph>https://www.cksource.com</paragraph>' +
  224. '<paragraph>[]</paragraph>'
  225. );
  226. } );
  227. } );
  228. describe( 'Code blocks integration', () => {
  229. let model;
  230. beforeEach( async () => {
  231. editor = await ModelTestEditor.create( {
  232. plugins: [ Paragraph, Input, Enter, ShiftEnter, LinkEditing, AutoLink, CodeBlockEditing ]
  233. } );
  234. model = editor.model;
  235. } );
  236. it( 'should be disabled inside code blocks (on space)', () => {
  237. setData( model, '<codeBlock language="plaintext">some [] code</codeBlock>' );
  238. const plugin = editor.plugins.get( 'AutoLink' );
  239. simulateTyping( 'www.cksource.com' );
  240. expect( plugin.isEnabled ).to.be.false;
  241. expect( getData( model, { withoutSelection: true } ) )
  242. .to.equal( '<codeBlock language="plaintext">some www.cksource.com code</codeBlock>' );
  243. } );
  244. it( 'should be disabled inside code blocks (on enter)', () => {
  245. setData( model, '<codeBlock language="plaintext">some www.cksource.com[] code</codeBlock>' );
  246. const plugin = editor.plugins.get( 'AutoLink' );
  247. editor.execute( 'enter' );
  248. expect( plugin.isEnabled ).to.be.false;
  249. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  250. '<codeBlock language="plaintext">some www.cksource.com</codeBlock>' +
  251. '<codeBlock language="plaintext"> code</codeBlock>'
  252. );
  253. } );
  254. it( 'should be disabled inside code blocks (on shift-enter)', () => {
  255. setData( model, '<codeBlock language="plaintext">some www.cksource.com[] code</codeBlock>' );
  256. const plugin = editor.plugins.get( 'AutoLink' );
  257. editor.execute( 'shiftEnter' );
  258. expect( plugin.isEnabled ).to.be.false;
  259. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  260. '<codeBlock language="plaintext">some www.cksource.com<softBreak></softBreak> code</codeBlock>'
  261. );
  262. } );
  263. } );
  264. function simulateTyping( text ) {
  265. const letters = text.split( '' );
  266. for ( const letter of letters ) {
  267. editor.execute( 'input', { text: letter } );
  268. }
  269. }
  270. } );