autolink.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 Input from '@ckeditor/ckeditor5-typing/src/input';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. import LinkEditing from '../src/linkediting';
  10. import AutoLink from '../src/autolink';
  11. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  12. describe( 'AutoLink', () => {
  13. it( 'should be named', () => {
  14. expect( AutoLink.pluginName ).to.equal( 'AutoLink' );
  15. } );
  16. describe( 'auto link behavior', () => {
  17. let editor;
  18. beforeEach( async () => {
  19. editor = await ModelTestEditor.create( { plugins: [ Paragraph, Input, LinkEditing, AutoLink, UndoEditing ] } );
  20. setData( editor.model, '<paragraph>[]</paragraph>' );
  21. } );
  22. it( 'does not add linkHref attribute to a text link while typing', () => {
  23. simulateTyping( 'https://www.cksource.com' );
  24. expect( getData( editor.model ) ).to.equal(
  25. '<paragraph>https://www.cksource.com[]</paragraph>'
  26. );
  27. } );
  28. it( 'adds linkHref attribute to a text link after space', () => {
  29. simulateTyping( 'https://www.cksource.com ' );
  30. expect( getData( editor.model ) ).to.equal(
  31. '<paragraph><$text linkHref="https://www.cksource.com">https://www.cksource.com</$text> []</paragraph>'
  32. );
  33. } );
  34. it( 'can undo auto-linking', () => {
  35. simulateTyping( 'https://www.cksource.com ' );
  36. editor.commands.execute( 'undo' );
  37. expect( getData( editor.model ) ).to.equal(
  38. '<paragraph>https://www.cksource.com []</paragraph>'
  39. );
  40. } );
  41. function simulateTyping( text ) {
  42. const letters = text.split( '' );
  43. for ( const letter of letters ) {
  44. editor.execute( 'input', { text: letter } );
  45. }
  46. }
  47. } );
  48. } );