linkediting.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
  12. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  13. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  14. import { isLinkElement } from '../src/utils';
  15. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  16. import {
  17. downcastMarkerToHighlight,
  18. downcastAttributeToElement
  19. } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
  20. /* global document */
  21. describe( 'LinkEditing', () => {
  22. let editor, model, view;
  23. beforeEach( () => {
  24. return VirtualTestEditor
  25. .create( {
  26. plugins: [ Paragraph, LinkEditing, Enter ]
  27. } )
  28. .then( newEditor => {
  29. editor = newEditor;
  30. model = editor.model;
  31. view = editor.editing.view;
  32. } );
  33. } );
  34. it( 'should be loaded', () => {
  35. expect( editor.plugins.get( LinkEditing ) ).to.be.instanceOf( LinkEditing );
  36. } );
  37. it( 'should set proper schema rules', () => {
  38. expect( model.schema.checkAttribute( [ '$block', '$text' ], 'linkHref' ) ).to.be.true;
  39. expect( model.schema.checkAttribute( [ '$clipboardHolder', '$text' ], 'linkHref' ) ).to.be.true;
  40. expect( model.schema.checkAttribute( [ '$block' ], 'linkHref' ) ).to.be.false;
  41. } );
  42. it( 'should bind two-step caret movement to `linkHref` attribute', () => {
  43. // Let's check only the minimum to not duplicated `bindTwoStepCaretToAttribute()` tests.
  44. // Testing minimum is better then testing using spies that might give false positive results.
  45. // Put selection before the link element.
  46. setModelData( editor.model, '<paragraph>foo[]<$text linkHref="url">b</$text>ar</paragraph>' );
  47. // The selection's gravity is not overridden because selection land here not as a result of `keydown`.
  48. expect( editor.model.document.selection.isGravityOverridden ).to.false;
  49. // So let's simulate `keydown` event.
  50. editor.editing.view.document.fire( 'keydown', {
  51. keyCode: keyCodes.arrowright,
  52. preventDefault: () => {},
  53. domTarget: document.body
  54. } );
  55. expect( editor.model.document.selection.isGravityOverridden ).to.true;
  56. } );
  57. describe( 'command', () => {
  58. it( 'should register link command', () => {
  59. const command = editor.commands.get( 'link' );
  60. expect( command ).to.be.instanceOf( LinkCommand );
  61. } );
  62. it( 'should register unlink command', () => {
  63. const command = editor.commands.get( 'unlink' );
  64. expect( command ).to.be.instanceOf( UnlinkCommand );
  65. } );
  66. } );
  67. describe( 'data pipeline conversions', () => {
  68. it( 'should convert `<a href="url">` to `linkHref="url"` attribute', () => {
  69. editor.setData( '<p><a href="url">foo</a>bar</p>' );
  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. it( 'should be integrated with autoparagraphing', () => {
  75. editor.setData( '<a href="url">foo</a>bar' );
  76. expect( getModelData( model, { withoutSelection: true } ) )
  77. .to.equal( '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  78. expect( editor.getData() ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  79. } );
  80. // https://github.com/ckeditor/ckeditor5/issues/500
  81. it( 'should not pick up `<a name="foo">`', () => {
  82. editor.setData( '<p><a name="foo">foo</a>bar</p>' );
  83. expect( getModelData( model, { withoutSelection: true } ) )
  84. .to.equal( '<paragraph>foobar</paragraph>' );
  85. } );
  86. // CKEditor 4 does. And CKEditor 5's balloon allows creating such links.
  87. it( 'should pick up `<a href="">`', () => {
  88. editor.setData( '<p><a href="">foo</a>bar</p>' );
  89. expect( getModelData( model, { withoutSelection: true } ) )
  90. .to.equal( '<paragraph><$text linkHref="">foo</$text>bar</paragraph>' );
  91. expect( editor.getData() ).to.equal( '<p><a href="">foo</a>bar</p>' );
  92. } );
  93. } );
  94. describe( 'editing pipeline conversion', () => {
  95. it( 'should convert attribute', () => {
  96. setModelData( model, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  97. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p><a href="url">foo</a>bar</p>' );
  98. } );
  99. it( 'should convert to link element instance', () => {
  100. setModelData( model, '<paragraph><$text linkHref="url">foo</$text>bar</paragraph>' );
  101. const element = editor.editing.view.document.getRoot().getChild( 0 ).getChild( 0 );
  102. expect( isLinkElement( element ) ).to.be.true;
  103. } );
  104. // https://github.com/ckeditor/ckeditor5-link/issues/121
  105. it( 'should should set priority for `linkHref` higher than all other attribute elements', () => {
  106. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  107. editor.conversion.for( 'downcast' ).add( downcastAttributeToElement( { model: 'foo', view: 'f' } ) );
  108. setModelData( model,
  109. '<paragraph>' +
  110. '<$text linkHref="url">a</$text><$text foo="true" linkHref="url">b</$text><$text linkHref="url">c</$text>' +
  111. '</paragraph>' );
  112. expect( editor.getData() ).to.equal( '<p><a href="url">a<f>b</f>c</a></p>' );
  113. } );
  114. } );
  115. describe( 'link highlighting', () => {
  116. it( 'should convert the highlight to a proper view classes', () => {
  117. setModelData( model,
  118. '<paragraph>foo <$text linkHref="url">b{}ar</$text> baz</paragraph>'
  119. );
  120. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.true;
  121. expect( getViewData( view ) ).to.equal(
  122. '<p>foo <a class="ck ck-link_selected" href="url">b{}ar</a> baz</p>'
  123. );
  124. } );
  125. it( 'should work whenever selection has linkHref attribute - link start', () => {
  126. setModelData( model,
  127. '<paragraph>foo {}<$text linkHref="url">bar</$text> baz</paragraph>'
  128. );
  129. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.false;
  130. model.change( writer => {
  131. writer.setSelectionAttribute( 'linkHref', 'url' );
  132. } );
  133. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.true;
  134. expect( getViewData( view ) ).to.equal(
  135. '<p>foo <a class="ck ck-link_selected" href="url">{}bar</a> baz</p>'
  136. );
  137. } );
  138. it( 'should work whenever selection has linkHref attribute - link end', () => {
  139. setModelData( model,
  140. '<paragraph>foo <$text linkHref="url">bar</$text>{} baz</paragraph>'
  141. );
  142. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.true;
  143. expect( getViewData( view ) ).to.equal(
  144. '<p>foo <a class="ck ck-link_selected" href="url">bar{}</a> baz</p>'
  145. );
  146. } );
  147. it( 'should render highlight correctly after splitting the link', () => {
  148. setModelData( model,
  149. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  150. );
  151. editor.execute( 'enter' );
  152. expect( getModelData( model ) ).to.equal(
  153. '<paragraph>foo <$text linkHref="url">li</$text></paragraph>' +
  154. '<paragraph><$text linkHref="url">[]nk</$text> baz</paragraph>'
  155. );
  156. expect( model.document.selection.hasAttribute( 'linkHref' ) ).to.be.true;
  157. } );
  158. it( 'should remove classes when selection is moved out from the link', () => {
  159. setModelData( model,
  160. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  161. );
  162. expect( getViewData( view ) ).to.equal(
  163. '<p>foo <a class="ck ck-link_selected" href="url">li{}nk</a> baz</p>'
  164. );
  165. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) );
  166. expect( getViewData( view ) ).to.equal(
  167. '<p>{}foo <a href="url">link</a> baz</p>'
  168. );
  169. } );
  170. it( 'should work correctly when selection is moved inside link', () => {
  171. setModelData( model,
  172. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  173. );
  174. expect( getViewData( view ) ).to.equal(
  175. '<p>foo <a class="ck ck-link_selected" href="url">li{}nk</a> baz</p>'
  176. );
  177. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 5 ) );
  178. expect( getViewData( view ) ).to.equal(
  179. '<p>foo <a class="ck ck-link_selected" href="url">l{}ink</a> baz</p>'
  180. );
  181. } );
  182. describe( 'downcast conversion integration', () => {
  183. it( 'works for the #insert event', () => {
  184. setModelData( model,
  185. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  186. );
  187. model.change( writer => {
  188. writer.insertText( 'FOO', { linkHref: 'url' }, model.document.selection.getFirstPosition() );
  189. } );
  190. expect( getViewData( view ) ).to.equal(
  191. '<p>foo <a class="ck ck-link_selected" href="url">liFOO{}nk</a> baz</p>'
  192. );
  193. } );
  194. it( 'works for the #remove event', () => {
  195. setModelData( model,
  196. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  197. );
  198. model.change( writer => {
  199. writer.remove( ModelRange.createFromParentsAndOffsets(
  200. model.document.getRoot().getChild( 0 ), 0,
  201. model.document.getRoot().getChild( 0 ), 5 )
  202. );
  203. } );
  204. expect( getViewData( view ) ).to.equal(
  205. '<p><a class="ck ck-link_selected" href="url">i{}nk</a> baz</p>'
  206. );
  207. } );
  208. it( 'works for the #attribute event', () => {
  209. setModelData( model,
  210. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  211. );
  212. model.change( writer => {
  213. writer.setAttribute( 'linkHref', 'new-url', new ModelRange(
  214. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  215. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  216. );
  217. } );
  218. expect( getViewData( view ) ).to.equal(
  219. '<p>foo <a href="url">l</a><a class="ck ck-link_selected" href="new-url">i{}n</a><a href="url">k</a> baz</p>'
  220. );
  221. } );
  222. it( 'works for the #selection event', () => {
  223. setModelData( model,
  224. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  225. );
  226. model.change( writer => {
  227. writer.setSelection( new ModelRange(
  228. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  229. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  230. );
  231. } );
  232. expect( getViewData( view ) ).to.equal(
  233. '<p>foo <a class="ck ck-link_selected" href="url">l{in}k</a> baz</p>'
  234. );
  235. } );
  236. it( 'works for the #addMarker and #removeMarker events', () => {
  237. downcastMarkerToHighlight( { model: 'fooMarker', view: {} } )( editor.editing.downcastDispatcher );
  238. setModelData( model,
  239. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  240. );
  241. model.change( writer => {
  242. writer.setMarker( 'fooMarker', ModelRange.createFromParentsAndOffsets(
  243. model.document.getRoot().getChild( 0 ), 0,
  244. model.document.getRoot().getChild( 0 ), 5 )
  245. );
  246. } );
  247. expect( getViewData( view ) ).to.equal(
  248. '<p><span>foo </span><a class="ck ck-link_selected" href="url"><span>l</span>i{}nk</a> baz</p>'
  249. );
  250. model.change( writer => writer.removeMarker( 'fooMarker' ) );
  251. expect( getViewData( view ) ).to.equal(
  252. '<p>foo <a class="ck ck-link_selected" href="url">li{}nk</a> baz</p>'
  253. );
  254. } );
  255. } );
  256. } );
  257. } );