linkediting.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 Enter from '@ckeditor/ckeditor5-enter/src/enter';
  11. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  13. import { isLinkElement } from '../src/utils';
  14. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  15. import { downcastAttributeToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
  16. /* global document */
  17. describe( 'LinkEditing', () => {
  18. let editor, model, view;
  19. beforeEach( () => {
  20. return VirtualTestEditor
  21. .create( {
  22. plugins: [ Paragraph, LinkEditing, Enter ]
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. model = editor.model;
  27. view = editor.editing.view;
  28. } );
  29. } );
  30. it( 'should be loaded', () => {
  31. expect( editor.plugins.get( LinkEditing ) ).to.be.instanceOf( LinkEditing );
  32. } );
  33. it( 'should set proper schema rules', () => {
  34. expect( model.schema.checkAttribute( [ '$block', '$text' ], 'linkHref' ) ).to.be.true;
  35. expect( model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'linkHref' ) ).to.be.true;
  36. expect( model.schema.checkAttribute( [ '$block' ], 'linkHref' ) ).to.be.false;
  37. } );
  38. it( 'should bind two-step caret movement to `linkHref` attribute', () => {
  39. // Let's check only the minimum to not duplicated `bindTwoStepCaretToAttribute()` tests.
  40. // Testing minimum is better then testing using spies that might give false positive results.
  41. // Put selection before the link element.
  42. setModelData( editor.model, '<paragraph>foo[]<$text linkHref="url">b</$text>ar</paragraph>' );
  43. // The selection's gravity is not overridden because selection land here not as a result of `keydown`.
  44. expect( editor.model.document.selection.isGravityOverridden ).to.false;
  45. // So let's simulate `keydown` event.
  46. editor.editing.view.document.fire( 'keydown', {
  47. keyCode: keyCodes.arrowright,
  48. preventDefault: () => {},
  49. domTarget: document.body
  50. } );
  51. expect( editor.model.document.selection.isGravityOverridden ).to.true;
  52. } );
  53. describe( 'command', () => {
  54. it( 'should register link command', () => {
  55. const command = editor.commands.get( 'link' );
  56. expect( command ).to.be.instanceOf( LinkCommand );
  57. } );
  58. it( 'should register unlink command', () => {
  59. const command = editor.commands.get( 'unlink' );
  60. expect( command ).to.be.instanceOf( UnlinkCommand );
  61. } );
  62. } );
  63. describe( 'data pipeline conversions', () => {
  64. it( 'should convert `<a href="url">` to `linkHref="url"` attribute', () => {
  65. editor.setData( '<p><a href="url">foo</a>bar</p>' );
  66. expect( getModelData( model, { withoutSelection: true } ) )
  67. .to.equal( '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  68. expect( editor.getData() ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  69. } );
  70. it( 'should be integrated with autoparagraphing', () => {
  71. editor.setData( '<a href="url">foo</a>bar' );
  72. expect( getModelData( model, { withoutSelection: true } ) )
  73. .to.equal( '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  74. expect( editor.getData() ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  75. } );
  76. // https://github.com/ckeditor/ckeditor5/issues/500
  77. it( 'should not pick up `<a name="foo">`', () => {
  78. editor.setData( '<p><a name="foo">foo</a>bar</p>' );
  79. expect( getModelData( model, { withoutSelection: true } ) )
  80. .to.equal( '<paragraph>foobar</paragraph>' );
  81. } );
  82. // CKEditor 4 does. And CKEditor 5's balloon allows creating such links.
  83. it( 'should pick up `<a href="">`', () => {
  84. editor.setData( '<p><a href="">foo</a>bar</p>' );
  85. expect( getModelData( model, { withoutSelection: true } ) )
  86. .to.equal( '<paragraph><$text linkHref="">foo</$text>bar</paragraph>' );
  87. expect( editor.getData() ).to.equal( '<p><a href="">foo</a>bar</p>' );
  88. } );
  89. } );
  90. describe( 'editing pipeline conversion', () => {
  91. it( 'should convert attribute', () => {
  92. setModelData( model, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  93. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  94. } );
  95. it( 'should convert to link element instance', () => {
  96. setModelData( model, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  97. const element = editor.editing.view.document.getRoot().getChild( 0 ).getChild( 0 );
  98. expect( isLinkElement( element ) ).to.be.true;
  99. } );
  100. // https://github.com/ckeditor/ckeditor5-link/issues/121
  101. it( 'should should set priority for `linkHref` higher than all other attribute elements', () => {
  102. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  103. editor.conversion.for( 'downcast' ).add( downcastAttributeToElement( { model: 'foo', view: 'f' } ) );
  104. setModelData( model,
  105. '<paragraph>' +
  106. '<$text linkHref="url">a</$text><$text foo="true" linkHref="url">b</$text><$text linkHref="url">c</$text>' +
  107. '</paragraph>' );
  108. expect( editor.getData() ).to.equal( '<p><a href="url">a<f>b</f>c</a></p>' );
  109. } );
  110. } );
  111. describe( 'highlight link boundaries', () => {
  112. it( 'should create marker in model when selection is inside a link', () => {
  113. expect( model.markers.has( 'linkBoundaries' ) ).to.be.false;
  114. setModelData( model,
  115. '<paragraph>foo <$text linkHref="url">b{}ar</$text> baz</paragraph>'
  116. );
  117. expect( model.markers.has( 'linkBoundaries' ) ).to.be.true;
  118. const marker = model.markers.get( 'linkBoundaries' );
  119. expect( marker.getStart().path ).to.deep.equal( [ 0, 4 ] );
  120. expect( marker.getEnd().path ).to.deep.equal( [ 0, 7 ] );
  121. } );
  122. it( 'should convert link boundaries marker to proper view', () => {
  123. setModelData( model,
  124. '<paragraph>foo <$text linkHref="url">b{}ar</$text> baz</paragraph>'
  125. );
  126. expect( getViewData( view ) ).to.equal(
  127. '<p>foo <span class="ck-link_selected"><a href="url">b{}ar</a></span> baz</p>'
  128. );
  129. } );
  130. it( 'should work whenever selection has linkHref attribute', () => {
  131. setModelData( model,
  132. '<paragraph>foo {}<$text linkHref="url">bar</$text> baz</paragraph>'
  133. );
  134. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.false;
  135. model.change( writer => {
  136. writer.setSelectionAttribute( 'linkHref', 'url' );
  137. } );
  138. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.true;
  139. expect( model.markers.has( 'linkBoundaries' ) ).to.be.true;
  140. const marker = model.markers.get( 'linkBoundaries' );
  141. expect( marker.getStart().path ).to.deep.equal( [ 0, 4 ] );
  142. expect( marker.getEnd().path ).to.deep.equal( [ 0, 7 ] );
  143. } );
  144. it( 'should render highlight correctly after splitting the link', () => {
  145. setModelData( model,
  146. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  147. );
  148. editor.execute( 'enter' );
  149. expect( getModelData( model ) ).to.equal(
  150. '<paragraph>foo <$text linkHref="url">li</$text></paragraph>' +
  151. '<paragraph><$text linkHref="url">[]nk</$text> baz</paragraph>'
  152. );
  153. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.true;
  154. expect( model.markers.has( 'linkBoundaries' ) ).to.be.true;
  155. const marker = model.markers.get( 'linkBoundaries' );
  156. expect( marker.getStart().path ).to.deep.equal( [ 1, 0 ] );
  157. expect( marker.getEnd().path ).to.deep.equal( [ 1, 2 ] );
  158. } );
  159. } );
  160. } );