inlinehighlight.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 setupHighlight from '../../src/utils/inlinehighlight';
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  9. import '@ckeditor/ckeditor5-core/tests/_utils/assertions/attribute';
  10. /* global document */
  11. describe( 'LinkEditing', () => {
  12. let element, editor, model, view;
  13. beforeEach( async () => {
  14. element = document.createElement( 'div' );
  15. document.body.appendChild( element );
  16. editor = await ClassicTestEditor.create( element );
  17. model = editor.model;
  18. view = editor.editing.view;
  19. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  20. editor.conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  21. model.schema.extend( '$text', { allowAttributes: 'linkHref' } );
  22. editor.conversion.for( 'editingDowncast' )
  23. .attributeToElement( { model: 'linkHref', view: ( href, writer ) => {
  24. return writer.createAttributeElement( 'a', { href } );
  25. } } );
  26. // Setup highlight over selected link.
  27. setupHighlight( editor, 'linkHref', 'a', 'ck-link_selected' );
  28. } );
  29. afterEach( async () => {
  30. element.remove();
  31. await editor.destroy();
  32. } );
  33. describe( 'link highlighting', () => {
  34. it( 'should convert the highlight to a proper view classes', () => {
  35. setModelData( model,
  36. '<paragraph>foo <$text linkHref="url">b{}ar</$text> baz</paragraph>'
  37. );
  38. expect( model.document.selection ).to.have.attribute( 'linkHref' );
  39. expect( getViewData( view ) ).to.equal(
  40. '<p>foo <a class="ck-link_selected" href="url">b{}ar</a> baz</p>'
  41. );
  42. } );
  43. it( 'should work whenever selection has linkHref attribute - link start', () => {
  44. setModelData( model,
  45. '<paragraph>foo {}<$text linkHref="url">bar</$text> baz</paragraph>'
  46. );
  47. expect( model.document.selection ).to.not.have.attribute( 'linkHref' );
  48. model.change( writer => {
  49. writer.setSelectionAttribute( 'linkHref', 'url' );
  50. } );
  51. expect( model.document.selection ).to.have.attribute( 'linkHref' );
  52. expect( getViewData( view ) ).to.equal(
  53. '<p>foo <a class="ck-link_selected" href="url">{}bar</a> baz</p>'
  54. );
  55. } );
  56. it( 'should work whenever selection has linkHref attribute - link end', () => {
  57. setModelData( model,
  58. '<paragraph>foo <$text linkHref="url">bar</$text>{} baz</paragraph>'
  59. );
  60. expect( model.document.selection ).to.have.attribute( 'linkHref' );
  61. expect( getViewData( view ) ).to.equal(
  62. '<p>foo <a class="ck-link_selected" href="url">bar{}</a> baz</p>'
  63. );
  64. } );
  65. it( 'should render highlight correctly after splitting the link', () => {
  66. setModelData( model,
  67. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  68. );
  69. editor.execute( 'enter' );
  70. expect( getModelData( model ) ).to.equal(
  71. '<paragraph>foo <$text linkHref="url">li</$text></paragraph>' +
  72. '<paragraph><$text linkHref="url">[]nk</$text> baz</paragraph>'
  73. );
  74. expect( model.document.selection ).to.have.attribute( 'linkHref' );
  75. expect( getViewData( view ) ).to.equal(
  76. '<p>foo <a href="url">li</a></p>' +
  77. '<p><a class="ck-link_selected" href="url">{}nk</a> baz</p>'
  78. );
  79. } );
  80. it( 'should remove classes when selection is moved out from the link', () => {
  81. setModelData( model,
  82. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  83. );
  84. expect( getViewData( view ) ).to.equal(
  85. '<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
  86. );
  87. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) );
  88. expect( getViewData( view ) ).to.equal(
  89. '<p>{}foo <a href="url">link</a> baz</p>'
  90. );
  91. } );
  92. it( 'should work correctly when selection is moved inside link', () => {
  93. setModelData( model,
  94. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  95. );
  96. expect( getViewData( view ) ).to.equal(
  97. '<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
  98. );
  99. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 5 ) );
  100. expect( getViewData( view ) ).to.equal(
  101. '<p>foo <a class="ck-link_selected" href="url">l{}ink</a> baz</p>'
  102. );
  103. } );
  104. describe( 'downcast conversion integration', () => {
  105. it( 'works for the #insert event', () => {
  106. setModelData( model,
  107. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  108. );
  109. model.change( writer => {
  110. writer.insertText( 'FOO', { linkHref: 'url' }, model.document.selection.getFirstPosition() );
  111. } );
  112. expect( getViewData( view ) ).to.equal(
  113. '<p>foo <a class="ck-link_selected" href="url">liFOO{}nk</a> baz</p>'
  114. );
  115. } );
  116. it( 'works for the #remove event', () => {
  117. setModelData( model,
  118. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  119. );
  120. model.change( writer => {
  121. writer.remove( writer.createRange(
  122. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  123. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  124. ) );
  125. } );
  126. expect( getViewData( view ) ).to.equal(
  127. '<p><a class="ck-link_selected" href="url">i{}nk</a> baz</p>'
  128. );
  129. } );
  130. it( 'works for the #attribute event', () => {
  131. setModelData( model,
  132. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  133. );
  134. model.change( writer => {
  135. writer.setAttribute( 'linkHref', 'new-url', writer.createRange(
  136. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  137. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  138. );
  139. } );
  140. expect( getViewData( view ) ).to.equal(
  141. '<p>foo <a href="url">l</a><a class="ck-link_selected" href="new-url">i{}n</a><a href="url">k</a> baz</p>'
  142. );
  143. } );
  144. it( 'works for the #selection event', () => {
  145. setModelData( model,
  146. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  147. );
  148. model.change( writer => {
  149. writer.setSelection( writer.createRange(
  150. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  151. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  152. );
  153. } );
  154. expect( getViewData( view ) ).to.equal(
  155. '<p>foo <a class="ck-link_selected" href="url">l{in}k</a> baz</p>'
  156. );
  157. } );
  158. it( 'works for the addMarker and removeMarker events', () => {
  159. editor.conversion.for( 'editingDowncast' ).markerToHighlight( { model: 'fooMarker', view: {} } );
  160. setModelData( model,
  161. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  162. );
  163. model.change( writer => {
  164. const range = writer.createRange(
  165. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  166. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  167. );
  168. writer.addMarker( 'fooMarker', { range, usingOperation: true } );
  169. } );
  170. expect( getViewData( view ) ).to.equal(
  171. '<p><span>foo </span><a class="ck-link_selected" href="url"><span>l</span>i{}nk</a> baz</p>'
  172. );
  173. model.change( writer => writer.removeMarker( 'fooMarker' ) );
  174. expect( getViewData( view ) ).to.equal(
  175. '<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
  176. );
  177. } );
  178. } );
  179. } );
  180. } );