autolink.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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( 'does not add linkHref attribute on enter when the link is selected', () => {
  85. setData( model, '<paragraph>[https://www.cksource.com]</paragraph>' );
  86. editor.execute( 'enter' );
  87. expect( getData( model ) ).to.equal(
  88. '<paragraph>[]</paragraph>'
  89. );
  90. } );
  91. it( 'does not add linkHref attribute on enter when the whole paragraph containing the link is selected', () => {
  92. setData( model, '<paragraph>[This feature also works with e-mail addresses: https://www.cksource.com]</paragraph>' );
  93. editor.execute( 'enter' );
  94. expect( getData( model ) ).to.equal(
  95. '<paragraph>[]</paragraph>'
  96. );
  97. } );
  98. it( 'adds linkHref attribute on enter when the link (that contains www) is partially selected (end)', () => {
  99. setData( model, '<paragraph>https://www.ckso[urce.com]</paragraph>' );
  100. editor.execute( 'enter' );
  101. expect( getData( model ) ).to.equal(
  102. '<paragraph><$text linkHref="https://www.ckso">https://www.ckso</$text></paragraph><paragraph>[]</paragraph>'
  103. );
  104. } );
  105. it( 'does not add linkHref attribute on enter when the link (that does not contain www) is partially selected (end)', () => {
  106. setData( model, '<paragraph>https://ckso[urce.com]</paragraph>' );
  107. editor.execute( 'enter' );
  108. expect( getData( model ) ).to.equal(
  109. '<paragraph>https://ckso</paragraph><paragraph>[]</paragraph>'
  110. );
  111. } );
  112. it( 'does not add linkHref attribute on enter when the link is partially selected (beginning)', () => {
  113. setData( model, '<paragraph>[https://www.ckso]urce.com</paragraph>' );
  114. editor.execute( 'enter' );
  115. expect( getData( model ) ).to.equal(
  116. '<paragraph></paragraph><paragraph>[]urce.com</paragraph>'
  117. );
  118. } );
  119. it( 'adds linkHref attribute to a text link after space (inside paragraph)', () => {
  120. setData( model, '<paragraph>Foo Bar [] Baz</paragraph>' );
  121. simulateTyping( 'https://www.cksource.com ' );
  122. expect( getData( model ) ).to.equal(
  123. '<paragraph>Foo Bar <$text linkHref="https://www.cksource.com">https://www.cksource.com</$text> [] Baz</paragraph>'
  124. );
  125. } );
  126. it( 'adds linkHref attribute to a text link on shift enter', () => {
  127. setData( model, '<paragraph>https://www.cksource.com[]</paragraph>' );
  128. editor.execute( 'shiftEnter' );
  129. // TODO: should test with selection but master has a bug. See: https://github.com/ckeditor/ckeditor5/issues/7459.
  130. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  131. '<paragraph>' +
  132. '<$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>' +
  133. '<softBreak></softBreak>' +
  134. '</paragraph>'
  135. );
  136. } );
  137. it( 'does not add linkHref attribute to a text link after double soft break', () => {
  138. setData( model, '<paragraph>https://www.cksource.com<softBreak></softBreak>[]</paragraph>' );
  139. editor.execute( 'shiftEnter' );
  140. expect( getData( model ) ).to.equal(
  141. '<paragraph>https://www.cksource.com<softBreak></softBreak><softBreak></softBreak>[]</paragraph>'
  142. );
  143. } );
  144. it( 'adds linkHref attribute to a text link on enter', () => {
  145. setData( model, '<paragraph>https://www.cksource.com[]</paragraph>' );
  146. editor.execute( 'enter' );
  147. expect( getData( model ) ).to.equal(
  148. '<paragraph>' +
  149. '<$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>' +
  150. '</paragraph>' +
  151. '<paragraph>[]</paragraph>'
  152. );
  153. } );
  154. it( 'adds "mailto:" to link of detected email addresses', () => {
  155. simulateTyping( 'newsletter@cksource.com ' );
  156. expect( getData( model ) ).to.equal(
  157. '<paragraph><$text linkHref="mailto:newsletter@cksource.com">newsletter@cksource.com</$text> []</paragraph>'
  158. );
  159. } );
  160. it( 'adds default protocol to link of detected www addresses', () => {
  161. editor.config.set( 'link.defaultProtocol', 'http://' );
  162. simulateTyping( 'www.cksource.com ' );
  163. expect( getData( model ) ).to.equal(
  164. '<paragraph><$text linkHref="http://www.cksource.com">www.cksource.com</$text> []</paragraph>'
  165. );
  166. } );
  167. // Some examples came from https://mathiasbynens.be/demo/url-regex.
  168. describe( 'supported URL', () => {
  169. const supportedURLs = [
  170. 'http://cksource.com',
  171. 'https://cksource.com',
  172. 'https://cksource.com:8080',
  173. 'http://www.cksource.com',
  174. 'hTtP://WwW.cKsOuRcE.cOm',
  175. 'www.cksource.com',
  176. 'http://foo.bar.cksource.com',
  177. 'http://www.cksource.com/some/path/index.html#abc',
  178. 'http://www.cksource.com/some/path/index.html?foo=bar',
  179. 'http://www.cksource.com/some/path/index.html?foo=bar#abc',
  180. 'http://www.cksource.com:8080/some/path/index.html?foo=bar#abc',
  181. 'http://www.cksource.com/some/path/index.html#abc?foo=bar',
  182. 'ftp://cksource.com',
  183. 'http://cksource.com/foo_bar',
  184. 'http://cksource.com/foo_bar/',
  185. 'http://cksource.com/foo_bar_(wikipedia)',
  186. 'http://cksource.com/foo_bar_(wikipedia)_(again)',
  187. 'http://www.cksource.com/wpstyle/?p=364',
  188. 'http://www.cksource.com/wpstyle/?bar=baz&inga=42&quux',
  189. 'http://userid:password@example.com:8080' +
  190. 'http://userid:password@example.com:8080/' +
  191. 'http://userid@cksource.com' +
  192. 'http://userid@cksource.com/' +
  193. 'http://userid@cksource.com:8080' +
  194. 'http://userid@cksource.com:8080/' +
  195. 'http://userid:password@cksource.com' +
  196. 'http://userid:password@cksource.com/' +
  197. 'http://🥳df.ws/123',
  198. 'http://🥳.ws/富',
  199. 'http://🥳.ws',
  200. 'http://🥳.ws/',
  201. 'http://cksource.com/blah_(wikipedia)#cite-1',
  202. 'http://cksource.com/blah_(wikipedia)_blah#cite-1',
  203. 'http://cksource.com/unicode_(🥳)_in_parens',
  204. 'http://cksource.com/(something)?after=parens',
  205. 'http://🥳.cksource.com/',
  206. 'http://code.cksource.com/woot/#&product=browser',
  207. 'http://j.mp',
  208. 'ftp://cksource.com/baz',
  209. 'http://cksource.com/?q=Test%20URL-encoded%20stuff',
  210. 'http://مثال.إختبار',
  211. 'http://例子.测试',
  212. 'http://उदाहरण.परीक्षा',
  213. 'http://1337.net',
  214. 'http://a.b-c.de'
  215. ];
  216. for ( const supportedURL of supportedURLs ) {
  217. it( `should detect "${ supportedURL }" as a valid URL`, () => {
  218. simulateTyping( supportedURL + ' ' );
  219. expect( getData( model ) ).to.equal(
  220. `<paragraph><$text linkHref="${ supportedURL }">${ supportedURL }</$text> []</paragraph>` );
  221. } );
  222. }
  223. } );
  224. describe( 'invalid or supported URL', () => {
  225. // Some examples came from https://mathiasbynens.be/demo/url-regex.
  226. const unsupportedOrInvalid = [
  227. 'http://',
  228. 'http://.',
  229. 'http://..',
  230. 'http://../',
  231. 'http://🥳',
  232. 'http://?',
  233. 'http://??',
  234. 'http://??/',
  235. 'http://#',
  236. 'http://##',
  237. 'http://##/',
  238. '//',
  239. '//a',
  240. '///a',
  241. '///',
  242. 'http:///a',
  243. 'rdar://1234',
  244. 'h://test',
  245. ':// foo bar',
  246. 'ftps://foo.bar/',
  247. 'http://-error-.invalid/',
  248. 'http://localhost',
  249. 'http:/cksource.com',
  250. 'cksource.com',
  251. 'ww.cksource.com'
  252. ];
  253. for ( const unsupportedURL of unsupportedOrInvalid ) {
  254. it( `should not detect "${ unsupportedURL }" as a valid URL`, () => {
  255. simulateTyping( unsupportedURL + ' ' );
  256. expect( getData( model ) ).to.equal(
  257. `<paragraph>${ unsupportedURL } []</paragraph>` );
  258. } );
  259. }
  260. } );
  261. } );
  262. describe( 'Undo integration', () => {
  263. let model;
  264. beforeEach( async () => {
  265. editor = await ModelTestEditor.create( {
  266. plugins: [ Paragraph, Input, Enter, ShiftEnter, LinkEditing, AutoLink, UndoEditing ]
  267. } );
  268. model = editor.model;
  269. setData( model, '<paragraph>https://www.cksource.com[]</paragraph>' );
  270. } );
  271. it( 'should undo auto-linking (after space)', () => {
  272. simulateTyping( ' ' );
  273. editor.commands.execute( 'undo' );
  274. expect( getData( model ) ).to.equal(
  275. '<paragraph>https://www.cksource.com []</paragraph>'
  276. );
  277. } );
  278. it( 'should undo auto-linking (after <softBreak>)', () => {
  279. editor.execute( 'shiftEnter' );
  280. editor.commands.execute( 'undo' );
  281. expect( getData( model ) ).to.equal(
  282. '<paragraph>https://www.cksource.com<softBreak></softBreak>[]</paragraph>'
  283. );
  284. } );
  285. it( 'should undo auto-linking (after enter)', () => {
  286. editor.execute( 'enter' );
  287. editor.commands.execute( 'undo' );
  288. expect( getData( model ) ).to.equal(
  289. '<paragraph>https://www.cksource.com</paragraph>' +
  290. '<paragraph>[]</paragraph>'
  291. );
  292. } );
  293. } );
  294. describe( 'Code blocks integration', () => {
  295. let model;
  296. beforeEach( async () => {
  297. editor = await ModelTestEditor.create( {
  298. plugins: [ Paragraph, Input, Enter, ShiftEnter, LinkEditing, AutoLink, CodeBlockEditing ]
  299. } );
  300. model = editor.model;
  301. } );
  302. it( 'should be disabled inside code blocks (on space)', () => {
  303. setData( model, '<codeBlock language="plaintext">some [] code</codeBlock>' );
  304. const plugin = editor.plugins.get( 'AutoLink' );
  305. simulateTyping( 'www.cksource.com' );
  306. expect( plugin.isEnabled ).to.be.false;
  307. expect( getData( model, { withoutSelection: true } ) )
  308. .to.equal( '<codeBlock language="plaintext">some www.cksource.com code</codeBlock>' );
  309. } );
  310. it( 'should be disabled inside code blocks (on enter)', () => {
  311. setData( model, '<codeBlock language="plaintext">some www.cksource.com[] code</codeBlock>' );
  312. const plugin = editor.plugins.get( 'AutoLink' );
  313. editor.execute( 'enter' );
  314. expect( plugin.isEnabled ).to.be.false;
  315. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  316. '<codeBlock language="plaintext">some www.cksource.com</codeBlock>' +
  317. '<codeBlock language="plaintext"> code</codeBlock>'
  318. );
  319. } );
  320. it( 'should be disabled inside code blocks (on shift-enter)', () => {
  321. setData( model, '<codeBlock language="plaintext">some www.cksource.com[] code</codeBlock>' );
  322. const plugin = editor.plugins.get( 'AutoLink' );
  323. editor.execute( 'shiftEnter' );
  324. expect( plugin.isEnabled ).to.be.false;
  325. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  326. '<codeBlock language="plaintext">some www.cksource.com<softBreak></softBreak> code</codeBlock>'
  327. );
  328. } );
  329. } );
  330. function simulateTyping( text ) {
  331. const letters = text.split( '' );
  332. for ( const letter of letters ) {
  333. editor.execute( 'input', { text: letter } );
  334. }
  335. }
  336. } );