autolink.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 not add linkHref attribute to a text link while typing', () => {
  30. simulateTyping( 'https://www.cksource.com' );
  31. expect( getData( model ) ).to.equal(
  32. '<paragraph>https://www.cksource.com[]</paragraph>'
  33. );
  34. } );
  35. it( 'adds linkHref attribute to a text link after space', () => {
  36. simulateTyping( 'https://www.cksource.com ' );
  37. expect( getData( model ) ).to.equal(
  38. '<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text> []</paragraph>'
  39. );
  40. } );
  41. it( 'adds linkHref attribute to a text link after space (inside paragraph)', () => {
  42. setData( model, '<paragraph>Foo Bar [] Baz</paragraph>' );
  43. simulateTyping( 'https://www.cksource.com ' );
  44. expect( getData( model ) ).to.equal(
  45. '<paragraph>Foo Bar <$text linkHref="https://www.cksource.com">https://www.cksource.com</$text> [] Baz</paragraph>'
  46. );
  47. } );
  48. it( 'adds linkHref attribute to a text link after a soft break', () => {
  49. setData( model, '<paragraph>https://www.cksource.com[]</paragraph>' );
  50. editor.execute( 'shiftEnter' );
  51. // TODO: should test with selection but master has a bug. See: https://github.com/ckeditor/ckeditor5/issues/7459.
  52. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  53. '<paragraph>' +
  54. '<$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>' +
  55. '<softBreak></softBreak>' +
  56. '</paragraph>'
  57. );
  58. } );
  59. it( 'does not add linkHref attribute to a text link after double soft break', () => {
  60. setData( model, '<paragraph>https://www.cksource.com<softBreak></softBreak>[]</paragraph>' );
  61. editor.execute( 'shiftEnter' );
  62. expect( getData( model ) ).to.equal(
  63. '<paragraph>https://www.cksource.com<softBreak></softBreak><softBreak></softBreak>[]</paragraph>'
  64. );
  65. } );
  66. it( 'adds linkHref attribute to a text link on enter', () => {
  67. setData( model, '<paragraph>https://www.cksource.com[]</paragraph>' );
  68. editor.execute( 'enter' );
  69. expect( getData( model ) ).to.equal(
  70. '<paragraph>' +
  71. '<$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>' +
  72. '</paragraph>' +
  73. '<paragraph>[]</paragraph>'
  74. );
  75. } );
  76. it( 'adds "mailto://" to link of detected email addresses', () => {
  77. simulateTyping( 'newsletter@cksource.com ' );
  78. expect( getData( model ) ).to.equal(
  79. '<paragraph><$text linkHref="mailto://newsletter@cksource.com">newsletter@cksource.com</$text> []</paragraph>'
  80. );
  81. } );
  82. const supportedURLs = [
  83. 'http://cksource.com',
  84. 'https://cksource.com',
  85. 'http://www.cksource.com',
  86. 'hTtP://WwW.cKsOuRcE.cOm',
  87. 'http://foo.bar.cksource.com',
  88. 'http://www.cksource.com/some/path/index.html#abc',
  89. 'http://www.cksource.com/some/path/index.html?foo=bar',
  90. 'http://www.cksource.com/some/path/index.html?foo=bar#abc',
  91. 'http://localhost',
  92. 'ftp://cksource.com',
  93. 'mailto://cksource@cksource.com',
  94. 'www.cksource.com',
  95. 'cksource.com'
  96. ];
  97. const unsupportedURLs = [
  98. 'http://www.cksource.com/some/path/index.html#abc?foo=bar', // Wrong #? sequence.
  99. 'http:/cksource.com'
  100. ];
  101. for ( const supportedURL of supportedURLs ) {
  102. it( `should detect "${ supportedURL }" as a valid URL`, () => {
  103. simulateTyping( supportedURL + ' ' );
  104. expect( getData( model ) ).to.equal(
  105. `<paragraph><$text linkHref="${ supportedURL }">${ supportedURL }</$text> []</paragraph>` );
  106. } );
  107. }
  108. for ( const unsupportedURL of unsupportedURLs ) {
  109. it( `should not detect "${ unsupportedURL }" as a valid URL`, () => {
  110. simulateTyping( unsupportedURL + ' ' );
  111. expect( getData( model ) ).to.equal(
  112. `<paragraph>${ unsupportedURL } []</paragraph>` );
  113. } );
  114. }
  115. } );
  116. describe( 'Undo integration', () => {
  117. let model;
  118. beforeEach( async () => {
  119. editor = await ModelTestEditor.create( {
  120. plugins: [ Paragraph, Input, Enter, ShiftEnter, LinkEditing, AutoLink, UndoEditing ]
  121. } );
  122. model = editor.model;
  123. setData( model, '<paragraph>https://www.cksource.com[]</paragraph>' );
  124. } );
  125. it( 'should undo auto-linking (after space)', () => {
  126. simulateTyping( ' ' );
  127. editor.commands.execute( 'undo' );
  128. expect( getData( model ) ).to.equal(
  129. '<paragraph>https://www.cksource.com []</paragraph>'
  130. );
  131. } );
  132. it( 'should undo auto-linking (after <softBreak>)', () => {
  133. editor.execute( 'shiftEnter' );
  134. editor.commands.execute( 'undo' );
  135. expect( getData( model ) ).to.equal(
  136. '<paragraph>https://www.cksource.com<softBreak></softBreak>[]</paragraph>'
  137. );
  138. } );
  139. it( 'should undo auto-linking (after enter)', () => {
  140. editor.execute( 'enter' );
  141. editor.commands.execute( 'undo' );
  142. expect( getData( model ) ).to.equal(
  143. '<paragraph>https://www.cksource.com</paragraph>' +
  144. '<paragraph>[]</paragraph>'
  145. );
  146. } );
  147. } );
  148. describe( 'Code blocks integration', () => {
  149. let model;
  150. beforeEach( async () => {
  151. editor = await ModelTestEditor.create( {
  152. plugins: [ Paragraph, Input, Enter, ShiftEnter, LinkEditing, AutoLink, CodeBlockEditing ]
  153. } );
  154. model = editor.model;
  155. } );
  156. it( 'should be disabled inside code blocks', () => {
  157. setData( model, '<codeBlock language="plaintext">some [] code</codeBlock>' );
  158. const plugin = editor.plugins.get( 'AutoLink' );
  159. simulateTyping( 'www.cksource.com' );
  160. expect( plugin.isEnabled ).to.be.false;
  161. expect( getData( model, { withoutSelection: true } ) )
  162. .to.equal( '<codeBlock language="plaintext">some www.cksource.com code</codeBlock>' );
  163. } );
  164. } );
  165. function simulateTyping( text ) {
  166. const letters = text.split( '' );
  167. for ( const letter of letters ) {
  168. editor.execute( 'input', { text: letter } );
  169. }
  170. }
  171. } );