linkediting.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import LinkEditing from '../src/linkediting';
  6. import LinkCommand from '../src/linkcommand';
  7. import UnlinkCommand from '../src/unlinkcommand';
  8. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  12. import { isLinkElement } from '../src/utils';
  13. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  14. import { downcastAttributeToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
  15. /* global document */
  16. describe( 'LinkEditing', () => {
  17. let editor, model;
  18. beforeEach( () => {
  19. return VirtualTestEditor
  20. .create( {
  21. plugins: [ Paragraph, LinkEditing ]
  22. } )
  23. .then( newEditor => {
  24. editor = newEditor;
  25. model = editor.model;
  26. } );
  27. } );
  28. it( 'should be loaded', () => {
  29. expect( editor.plugins.get( LinkEditing ) ).to.be.instanceOf( LinkEditing );
  30. } );
  31. it( 'should set proper schema rules', () => {
  32. expect( model.schema.checkAttribute( [ '$block', '$text' ], 'linkHref' ) ).to.be.true;
  33. expect( model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'linkHref' ) ).to.be.true;
  34. expect( model.schema.checkAttribute( [ '$block' ], 'linkHref' ) ).to.be.false;
  35. } );
  36. it( 'should bind two-step caret movement to `linkHref` attribute', () => {
  37. // Let's check only the minimum to not duplicated `bindTwoStepCaretToAttribute()` tests.
  38. // Testing minimum is better then testing using spies that might give false positive results.
  39. // Put selection before the link element.
  40. setModelData( editor.model, '<paragraph>foo[]<$text linkHref="url">b</$text>ar</paragraph>' );
  41. // The selection's gravity is not overridden because selection land here not as a result of `keydown`.
  42. expect( editor.model.document.selection.isGravityOverridden ).to.false;
  43. // So let's simulate `keydown` event.
  44. editor.editing.view.document.fire( 'keydown', {
  45. keyCode: keyCodes.arrowright,
  46. preventDefault: () => {},
  47. domTarget: document.body
  48. } );
  49. expect( editor.model.document.selection.isGravityOverridden ).to.true;
  50. } );
  51. describe( 'command', () => {
  52. it( 'should register link command', () => {
  53. const command = editor.commands.get( 'link' );
  54. expect( command ).to.be.instanceOf( LinkCommand );
  55. } );
  56. it( 'should register unlink command', () => {
  57. const command = editor.commands.get( 'unlink' );
  58. expect( command ).to.be.instanceOf( UnlinkCommand );
  59. } );
  60. } );
  61. describe( 'data pipeline conversions', () => {
  62. it( 'should convert `<a href="url">` to `linkHref="url"` attribute', () => {
  63. editor.setData( '<p><a href="url">foo</a>bar</p>' );
  64. expect( getModelData( model, { withoutSelection: true } ) )
  65. .to.equal( '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  66. expect( editor.getData() ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  67. } );
  68. it( 'should be integrated with autoparagraphing', () => {
  69. editor.setData( '<a href="url">foo</a>bar' );
  70. expect( getModelData( model, { withoutSelection: true } ) )
  71. .to.equal( '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  72. expect( editor.getData() ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  73. } );
  74. // https://github.com/ckeditor/ckeditor5/issues/500
  75. it( 'should not pick up `<a name="foo">`', () => {
  76. editor.setData( '<p><a name="foo">foo</a>bar</p>' );
  77. expect( getModelData( model, { withoutSelection: true } ) )
  78. .to.equal( '<paragraph>foobar</paragraph>' );
  79. } );
  80. // CKEditor 4 does. And CKEditor 5's balloon allows creating such links.
  81. it( 'should pick up `<a href="">`', () => {
  82. editor.setData( '<p><a href="">foo</a>bar</p>' );
  83. expect( getModelData( model, { withoutSelection: true } ) )
  84. .to.equal( '<paragraph><$text linkHref="">foo</$text>bar</paragraph>' );
  85. expect( editor.getData() ).to.equal( '<p><a href="">foo</a>bar</p>' );
  86. } );
  87. } );
  88. describe( 'editing pipeline conversion', () => {
  89. it( 'should convert attribute', () => {
  90. setModelData( model, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  91. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  92. } );
  93. it( 'should convert to link element instance', () => {
  94. setModelData( model, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  95. const element = editor.editing.view.document.getRoot().getChild( 0 ).getChild( 0 );
  96. expect( isLinkElement( element ) ).to.be.true;
  97. } );
  98. // https://github.com/ckeditor/ckeditor5-link/issues/121
  99. it( 'should should set priority for `linkHref` higher than all other attribute elements', () => {
  100. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  101. editor.conversion.for( 'downcast' ).add( downcastAttributeToElement( { model: 'foo', view: 'f' } ) );
  102. setModelData( model,
  103. '<paragraph>' +
  104. '<$text linkHref="url">a</$text><$text foo="true" linkHref="url">b</$text><$text linkHref="url">c</$text>' +
  105. '</paragraph>' );
  106. expect( editor.getData() ).to.equal( '<p><a href="url">a<f>b</f>c</a></p>' );
  107. } );
  108. } );
  109. } );