autolink.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 Enter from '@ckeditor/ckeditor5-enter/src/enter';
  7. import Input from '@ckeditor/ckeditor5-typing/src/input';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import ShiftEnter from '@ckeditor/ckeditor5-enter/src/shiftenter';
  10. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  11. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. import LinkEditing from '../src/linkediting';
  13. import AutoLink from '../src/autolink';
  14. describe( 'AutoLink', () => {
  15. let editor;
  16. it( 'should be named', () => {
  17. expect( AutoLink.pluginName ).to.equal( 'AutoLink' );
  18. } );
  19. describe( 'auto link behavior', () => {
  20. let model;
  21. beforeEach( async () => {
  22. editor = await ModelTestEditor.create( {
  23. plugins: [ Paragraph, Input, LinkEditing, AutoLink, Enter, ShiftEnter ]
  24. } );
  25. model = editor.model;
  26. setData( model, '<paragraph>[]</paragraph>' );
  27. } );
  28. it( 'does not add linkHref attribute to a text link while typing', () => {
  29. simulateTyping( 'https://www.cksource.com' );
  30. expect( getData( model ) ).to.equal(
  31. '<paragraph>https://www.cksource.com[]</paragraph>'
  32. );
  33. } );
  34. it( 'adds linkHref attribute to a text link after space', () => {
  35. simulateTyping( 'https://www.cksource.com ' );
  36. expect( getData( model ) ).to.equal(
  37. '<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text> []</paragraph>'
  38. );
  39. } );
  40. it( 'adds linkHref attribute to a text link after space (inside paragraph)', () => {
  41. setData( model, '<paragraph>Foo Bar [] Baz</paragraph>' );
  42. simulateTyping( 'https://www.cksource.com ' );
  43. expect( getData( model ) ).to.equal(
  44. '<paragraph>Foo Bar <$text linkHref="https://www.cksource.com">https://www.cksource.com</$text> [] Baz</paragraph>'
  45. );
  46. } );
  47. it( 'adds linkHref attribute to a text link after a soft break', () => {
  48. setData( model, '<paragraph>https://www.cksource.com[]</paragraph>' );
  49. editor.execute( 'shiftEnter' );
  50. // TODO: should test with selection but master has a bug. See: https://github.com/ckeditor/ckeditor5/issues/7459.
  51. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  52. '<paragraph>' +
  53. '<$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>' +
  54. '<softBreak></softBreak>' +
  55. '</paragraph>'
  56. );
  57. } );
  58. it( 'does not add linkHref attribute to a text link after double soft break', () => {
  59. setData( model, '<paragraph>https://www.cksource.com<softBreak></softBreak>[]</paragraph>' );
  60. editor.execute( 'shiftEnter' );
  61. expect( getData( model ) ).to.equal(
  62. '<paragraph>https://www.cksource.com<softBreak></softBreak><softBreak></softBreak>[]</paragraph>'
  63. );
  64. } );
  65. it( 'adds linkHref attribute to a text link on enter', () => {
  66. setData( model, '<paragraph>https://www.cksource.com[]</paragraph>' );
  67. editor.execute( 'enter' );
  68. expect( getData( model ) ).to.equal(
  69. '<paragraph>' +
  70. '<$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>' +
  71. '</paragraph>' +
  72. '<paragraph>[]</paragraph>'
  73. );
  74. } );
  75. it( 'adds "mailto://" to link of detected email addresses', () => {
  76. simulateTyping( 'newsletter@cksource.com ' );
  77. expect( getData( model ) ).to.equal(
  78. '<paragraph><$text linkHref="mailto://newsletter@cksource.com">newsletter@cksource.com</$text> []</paragraph>'
  79. );
  80. } );
  81. const supportedURLs = [
  82. 'http://cksource.com',
  83. 'https://cksource.com',
  84. 'http://www.cksource.com',
  85. 'hTtP://WwW.cKsOuRcE.cOm',
  86. 'http://foo.bar.cksource.com',
  87. 'http://www.cksource.com/some/path/index.html#abc',
  88. 'http://www.cksource.com/some/path/index.html?foo=bar',
  89. 'http://www.cksource.com/some/path/index.html?foo=bar#abc',
  90. 'http://localhost',
  91. 'ftp://cksource.com',
  92. 'mailto://cksource@cksource.com',
  93. 'www.cksource.com',
  94. 'cksource.com'
  95. ];
  96. const unsupportedURLs = [
  97. 'http://www.cksource.com/some/path/index.html#abc?foo=bar', // Wrong #? sequence.
  98. 'http:/cksource.com'
  99. ];
  100. for ( const supportedURL of supportedURLs ) {
  101. it( `should detect "${ supportedURL }" as a valid URL`, () => {
  102. simulateTyping( supportedURL + ' ' );
  103. expect( getData( model ) ).to.equal(
  104. `<paragraph><$text linkHref="${ supportedURL }">${ supportedURL }</$text> []</paragraph>` );
  105. } );
  106. }
  107. for ( const unsupportedURL of unsupportedURLs ) {
  108. it( `should not detect "${ unsupportedURL }" as a valid URL`, () => {
  109. simulateTyping( unsupportedURL + ' ' );
  110. expect( getData( model ) ).to.equal(
  111. `<paragraph>${ unsupportedURL } []</paragraph>` );
  112. } );
  113. }
  114. } );
  115. describe( 'Undo integration', () => {
  116. let model;
  117. beforeEach( async () => {
  118. editor = await ModelTestEditor.create( {
  119. plugins: [ Paragraph, Input, LinkEditing, AutoLink, UndoEditing, Enter, ShiftEnter ]
  120. } );
  121. model = editor.model;
  122. setData( model, '<paragraph>https://www.cksource.com[]</paragraph>' );
  123. } );
  124. it( 'should undo auto-linking (after space)', () => {
  125. simulateTyping( ' ' );
  126. editor.commands.execute( 'undo' );
  127. expect( getData( model ) ).to.equal(
  128. '<paragraph>https://www.cksource.com []</paragraph>'
  129. );
  130. } );
  131. it( 'should undo auto-linking (after <softBreak>)', () => {
  132. editor.execute( 'shiftEnter' );
  133. editor.commands.execute( 'undo' );
  134. expect( getData( model ) ).to.equal(
  135. '<paragraph>https://www.cksource.com<softBreak></softBreak>[]</paragraph>'
  136. );
  137. } );
  138. it( 'should undo auto-linking (after enter)', () => {
  139. editor.execute( 'enter' );
  140. editor.commands.execute( 'undo' );
  141. expect( getData( model ) ).to.equal(
  142. '<paragraph>https://www.cksource.com</paragraph>' +
  143. '<paragraph>[]</paragraph>'
  144. );
  145. } );
  146. } );
  147. function simulateTyping( text ) {
  148. const letters = text.split( '' );
  149. for ( const letter of letters ) {
  150. editor.execute( 'input', { text: letter } );
  151. }
  152. }
  153. } );