autolink.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. it( 'should be named', () => {
  16. expect( AutoLink.pluginName ).to.equal( 'AutoLink' );
  17. } );
  18. describe( 'auto link behavior', () => {
  19. let editor, model;
  20. beforeEach( async () => {
  21. editor = await ModelTestEditor.create( {
  22. plugins: [ Paragraph, Input, LinkEditing, AutoLink, UndoEditing, Enter, ShiftEnter ]
  23. } );
  24. model = editor.model;
  25. setData( model, '<paragraph>[]</paragraph>' );
  26. } );
  27. it( 'does not add linkHref attribute to a text link while typing', () => {
  28. simulateTyping( 'https://www.cksource.com' );
  29. expect( getData( model ) ).to.equal(
  30. '<paragraph>https://www.cksource.com[]</paragraph>'
  31. );
  32. } );
  33. it( 'adds linkHref attribute to a text link after space', () => {
  34. simulateTyping( 'https://www.cksource.com ' );
  35. expect( getData( model ) ).to.equal(
  36. '<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text> []</paragraph>'
  37. );
  38. } );
  39. it( 'adds linkHref attribute to a text link after space (inside paragraph)', () => {
  40. setData( model, '<paragraph>Foo Bar [] Baz</paragraph>' );
  41. simulateTyping( 'https://www.cksource.com ' );
  42. expect( getData( model ) ).to.equal(
  43. '<paragraph>Foo Bar <$text linkHref="https://www.cksource.com">https://www.cksource.com</$text> [] Baz</paragraph>'
  44. );
  45. } );
  46. it( 'adds linkHref attribute to a text link after a soft break', () => {
  47. setData( model, '<paragraph>https://www.cksource.com[]</paragraph>' );
  48. editor.execute( 'shiftEnter' );
  49. // TODO: should test with selection but master has a bug. See: https://github.com/ckeditor/ckeditor5/issues/7459.
  50. expect( getData( model, { withoutSelection: true } ) ).to.equal(
  51. '<paragraph>' +
  52. '<$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>' +
  53. '<softBreak></softBreak>' +
  54. '</paragraph>'
  55. );
  56. } );
  57. it( 'does not add linkHref attribute to a text link after double soft break', () => {
  58. setData( model, '<paragraph>https://www.cksource.com<softBreak></softBreak>[]</paragraph>' );
  59. editor.execute( 'shiftEnter' );
  60. expect( getData( model ) ).to.equal(
  61. '<paragraph>https://www.cksource.com<softBreak></softBreak><softBreak></softBreak>[]</paragraph>'
  62. );
  63. } );
  64. it( 'adds linkHref attribute to a text link on enter', () => {
  65. setData( model, '<paragraph>https://www.cksource.com[]</paragraph>' );
  66. editor.execute( 'enter' );
  67. expect( getData( model ) ).to.equal(
  68. '<paragraph>' +
  69. '<$text linkHref="https://www.cksource.com">https://www.cksource.com</$text>' +
  70. '</paragraph>' +
  71. '<paragraph>[]</paragraph>'
  72. );
  73. } );
  74. it( 'can undo auto-linking (after space)', () => {
  75. simulateTyping( 'https://www.cksource.com ' );
  76. editor.commands.execute( 'undo' );
  77. expect( getData( model ) ).to.equal(
  78. '<paragraph>https://www.cksource.com []</paragraph>'
  79. );
  80. } );
  81. it( 'can undo auto-linking (after <softBreak>)', () => {
  82. setData( model, '<paragraph>https://www.cksource.com[]</paragraph>' );
  83. editor.execute( 'shiftEnter' );
  84. editor.commands.execute( 'undo' );
  85. expect( getData( model ) ).to.equal(
  86. '<paragraph>https://www.cksource.com<softBreak></softBreak>[]</paragraph>'
  87. );
  88. } );
  89. it( 'can undo auto-linking (after enter)', () => {
  90. setData( model, '<paragraph>https://www.cksource.com[]</paragraph>' );
  91. editor.execute( 'enter' );
  92. editor.commands.execute( 'undo' );
  93. expect( getData( model ) ).to.equal(
  94. '<paragraph>https://www.cksource.com</paragraph>' +
  95. '<paragraph>[]</paragraph>'
  96. );
  97. } );
  98. function simulateTyping( text ) {
  99. const letters = text.split( '' );
  100. for ( const letter of letters ) {
  101. editor.execute( 'input', { text: letter } );
  102. }
  103. }
  104. } );
  105. } );