inlinehighlight.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 inlineHighlight 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( 'inlineHighlight', () => {
  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. inlineHighlight( editor, 'linkHref', 'a', 'ck-link_selected' );
  28. } );
  29. afterEach( async () => {
  30. element.remove();
  31. await editor.destroy();
  32. } );
  33. describe( 'attribute 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. model.change( writer => {
  70. const splitPos = model.document.selection.getFirstRange().start;
  71. writer.split( splitPos );
  72. writer.setSelection( splitPos.parent.nextSibling, 0 );
  73. } );
  74. expect( getModelData( model ) ).to.equal(
  75. '<paragraph>foo <$text linkHref="url">li</$text></paragraph>' +
  76. '<paragraph><$text linkHref="url">[]nk</$text> baz</paragraph>'
  77. );
  78. expect( model.document.selection ).to.have.attribute( 'linkHref' );
  79. expect( getViewData( view ) ).to.equal(
  80. '<p>foo <a href="url">li</a></p>' +
  81. '<p><a class="ck-link_selected" href="url">{}nk</a> baz</p>'
  82. );
  83. } );
  84. it( 'should remove classes when selection is moved out from the link', () => {
  85. setModelData( model,
  86. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  87. );
  88. expect( getViewData( view ) ).to.equal(
  89. '<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
  90. );
  91. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) );
  92. expect( getViewData( view ) ).to.equal(
  93. '<p>{}foo <a href="url">link</a> baz</p>'
  94. );
  95. } );
  96. it( 'should work correctly when selection is moved inside link', () => {
  97. setModelData( model,
  98. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  99. );
  100. expect( getViewData( view ) ).to.equal(
  101. '<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
  102. );
  103. model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 5 ) );
  104. expect( getViewData( view ) ).to.equal(
  105. '<p>foo <a class="ck-link_selected" href="url">l{}ink</a> baz</p>'
  106. );
  107. } );
  108. describe( 'downcast conversion integration', () => {
  109. it( 'works for the #insert event', () => {
  110. setModelData( model,
  111. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  112. );
  113. model.change( writer => {
  114. writer.insertText( 'FOO', { linkHref: 'url' }, model.document.selection.getFirstPosition() );
  115. } );
  116. expect( getViewData( view ) ).to.equal(
  117. '<p>foo <a class="ck-link_selected" href="url">liFOO{}nk</a> baz</p>'
  118. );
  119. } );
  120. it( 'works for the #remove event', () => {
  121. setModelData( model,
  122. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  123. );
  124. model.change( writer => {
  125. writer.remove( writer.createRange(
  126. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  127. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  128. ) );
  129. } );
  130. expect( getViewData( view ) ).to.equal(
  131. '<p><a class="ck-link_selected" href="url">i{}nk</a> baz</p>'
  132. );
  133. } );
  134. it( 'works for the #attribute event', () => {
  135. setModelData( model,
  136. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  137. );
  138. model.change( writer => {
  139. writer.setAttribute( 'linkHref', 'new-url', writer.createRange(
  140. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  141. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  142. );
  143. } );
  144. expect( getViewData( view ) ).to.equal(
  145. '<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>'
  146. );
  147. } );
  148. it( 'works for the #selection event', () => {
  149. setModelData( model,
  150. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  151. );
  152. model.change( writer => {
  153. writer.setSelection( writer.createRange(
  154. model.document.selection.getFirstPosition().getShiftedBy( -1 ),
  155. model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
  156. );
  157. } );
  158. expect( getViewData( view ) ).to.equal(
  159. '<p>foo <a class="ck-link_selected" href="url">l{in}k</a> baz</p>'
  160. );
  161. } );
  162. it( 'works for the addMarker and removeMarker events', () => {
  163. editor.conversion.for( 'editingDowncast' ).markerToHighlight( { model: 'fooMarker', view: {} } );
  164. setModelData( model,
  165. '<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
  166. );
  167. model.change( writer => {
  168. const range = writer.createRange(
  169. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
  170. writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
  171. );
  172. writer.addMarker( 'fooMarker', { range, usingOperation: true } );
  173. } );
  174. expect( getViewData( view ) ).to.equal(
  175. '<p><span>foo </span><a class="ck-link_selected" href="url"><span>l</span>i{}nk</a> baz</p>'
  176. );
  177. model.change( writer => writer.removeMarker( 'fooMarker' ) );
  178. expect( getViewData( view ) ).to.equal(
  179. '<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
  180. );
  181. } );
  182. } );
  183. } );
  184. } );