8
0

autolink.js 9.0 KB

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