highlightcommand.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 HighlightCommand from './../src/highlightcommand';
  6. import Command from '@ckeditor/ckeditor5-core/src/command';
  7. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  8. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. describe( 'HighlightCommand', () => {
  10. let editor, model, doc, root, command;
  11. beforeEach( () => {
  12. return ModelTestEditor.create()
  13. .then( newEditor => {
  14. editor = newEditor;
  15. model = editor.model;
  16. doc = model.document;
  17. root = doc.getRoot();
  18. command = new HighlightCommand( newEditor );
  19. editor.commands.add( 'highlight', command );
  20. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  21. model.schema.addAttributeCheck( ( ctx, attributeName ) => {
  22. // Allow 'highlight' on p>$text.
  23. if ( ctx.endsWith( 'p $text' ) && attributeName == 'highlight' ) {
  24. return true;
  25. }
  26. } );
  27. } );
  28. } );
  29. afterEach( () => {
  30. editor.destroy();
  31. } );
  32. it( 'is a command', () => {
  33. expect( HighlightCommand.prototype ).to.be.instanceOf( Command );
  34. expect( command ).to.be.instanceOf( Command );
  35. } );
  36. describe( 'value', () => {
  37. it( 'is set to highlight attribute value when selection is in text with highlight attribute', () => {
  38. setData( model, '<p><$text highlight="yellowMarker">fo[o]</$text></p>' );
  39. expect( command ).to.have.property( 'value', 'yellowMarker' );
  40. } );
  41. it( 'is undefined when selection is not in text with highlight attribute', () => {
  42. setData( model, '<p>fo[]o</p>' );
  43. expect( command ).to.have.property( 'value', undefined );
  44. } );
  45. } );
  46. describe( 'isEnabled', () => {
  47. beforeEach( () => {
  48. model.schema.register( 'x', { inheritAllFrom: '$block' } );
  49. } );
  50. it( 'is true when selection is on text which can have highlight added', () => {
  51. setData( model, '<p>fo[]o</p>' );
  52. expect( command ).to.have.property( 'isEnabled', true );
  53. } );
  54. it( 'is false when selection is on text which can not have highlight added', () => {
  55. setData( model, '<x>fo[]o</x>' );
  56. expect( command.isEnabled ).to.be.false;
  57. } );
  58. } );
  59. describe( 'execute()', () => {
  60. describe( 'with option.value set', () => {
  61. describe( 'on collapsed range', () => {
  62. it( 'should change entire highlight when inside highlighted text', () => {
  63. setData( model, '<p>abc<$text highlight="yellowMarker">foo[]bar</$text>xyz</p>' );
  64. expect( command.value ).to.equal( 'yellowMarker' );
  65. command.execute( { value: 'greenMarker' } );
  66. expect( getData( model ) ).to.equal( '<p>abc<$text highlight="greenMarker">foo[]bar</$text>xyz</p>' );
  67. expect( command.value ).to.equal( 'greenMarker' );
  68. } );
  69. it( 'should remove entire highlight when inside highlighted text of the same value', () => {
  70. setData( model, '<p>abc<$text highlight="yellowMarker">foo[]bar</$text>xyz</p>' );
  71. expect( command.value ).to.equal( 'yellowMarker' );
  72. command.execute( { value: 'yellowMarker' } );
  73. expect( getData( model ) ).to.equal( '<p>abcfoo[]barxyz</p>' );
  74. expect( command.value ).to.be.undefined;
  75. } );
  76. it( 'should change selection attribute in non-empty parent', () => {
  77. setData( model, '<p>a[]bc<$text highlight="yellowMarker">foobar</$text>xyz</p>' );
  78. expect( command.value ).to.be.undefined;
  79. command.execute( { value: 'foo' } );
  80. expect( command.value ).to.equal( 'foo' );
  81. expect( doc.selection.hasAttribute( 'highlight' ) ).to.be.true;
  82. command.execute();
  83. expect( command.value ).to.be.undefined;
  84. expect( doc.selection.hasAttribute( 'highlight' ) ).to.be.false;
  85. // Execute remove highlight on selection without 'highlight' attribute should do nothing.
  86. command.execute();
  87. expect( command.value ).to.be.undefined;
  88. expect( doc.selection.hasAttribute( 'highlight' ) ).to.be.false;
  89. } );
  90. it( 'should not store attribute change on selection if selection is collapsed in non-empty parent', () => {
  91. setData( model, '<p>a[]bc<$text highlight="yellowMarker">foobar</$text>xyz</p>' );
  92. command.execute( { value: 'foo' } );
  93. // It should not save that bold was executed at position ( root, [ 0, 1 ] ).
  94. model.change( writer => {
  95. // Simulate clicking right arrow key by changing selection ranges.
  96. writer.setSelection( root.getNodeByPath( [ 0 ] ), 2 );
  97. // Get back to previous selection.
  98. writer.setSelection( root.getNodeByPath( [ 0 ] ), 1 );
  99. } );
  100. expect( command.value ).to.be.undefined;
  101. } );
  102. it( 'should change selection attribute and store it if selection is collapsed in empty parent', () => {
  103. setData( model, '<p>abc<$text highlight="yellowMarker">foobar</$text>xyz</p><p>[]</p>' );
  104. expect( command.value ).to.be.undefined;
  105. command.execute( { value: 'foo' } );
  106. expect( command.value ).to.equal( 'foo' );
  107. expect( doc.selection.hasAttribute( 'highlight' ) ).to.be.true;
  108. // Attribute should be stored.
  109. // Simulate clicking somewhere else in the editor.
  110. model.change( writer => {
  111. writer.setSelection( root.getNodeByPath( [ 0 ] ), 2 );
  112. } );
  113. expect( command.value ).to.be.undefined;
  114. // Go back to where attribute was stored.
  115. model.change( writer => {
  116. writer.setSelection( root.getNodeByPath( [ 1 ] ), 0 );
  117. } );
  118. // Attribute should be restored.
  119. expect( command.value ).to.equal( 'foo' );
  120. command.execute();
  121. expect( command.value ).to.be.undefined;
  122. expect( doc.selection.hasAttribute( 'highlight' ) ).to.be.false;
  123. } );
  124. // https://github.com/ckeditor/ckeditor5-highlight/issues/8
  125. it( 'should change selection attribute on consecutive calls', () => {
  126. setData( model, '<p>abcfoobar[] foobar</p>' );
  127. expect( command.value ).to.be.undefined;
  128. command.execute( { value: 'greenMarker' } );
  129. expect( command.value ).to.equal( 'greenMarker' );
  130. expect( doc.selection.getAttribute( 'highlight' ) ).to.equal( 'greenMarker' );
  131. command.execute( { value: 'pinkMarker' } );
  132. expect( command.value ).to.equal( 'pinkMarker' );
  133. expect( doc.selection.getAttribute( 'highlight' ) ).to.equal( 'pinkMarker' );
  134. } );
  135. } );
  136. describe( 'on not collapsed range', () => {
  137. it( 'should set highlight attribute on selected node when passed as parameter', () => {
  138. setData( model, '<p>a[bc<$text highlight="yellowMarker">fo]obar</$text>xyz</p>' );
  139. expect( command.value ).to.be.undefined;
  140. command.execute( { value: 'yellowMarker' } );
  141. expect( command.value ).to.equal( 'yellowMarker' );
  142. expect( getData( model ) ).to.equal( '<p>a[<$text highlight="yellowMarker">bcfo]obar</$text>xyz</p>' );
  143. } );
  144. it( 'should set highlight attribute on selected node when passed as parameter (multiple nodes)', () => {
  145. setData(
  146. model,
  147. '<p>abcabc[abc</p>' +
  148. '<p>foofoofoo</p>' +
  149. '<p>barbar]bar</p>'
  150. );
  151. command.execute( { value: 'yellowMarker' } );
  152. expect( command.value ).to.equal( 'yellowMarker' );
  153. expect( getData( model ) ).to.equal(
  154. '<p>abcabc[<$text highlight="yellowMarker">abc</$text></p>' +
  155. '<p><$text highlight="yellowMarker">foofoofoo</$text></p>' +
  156. '<p><$text highlight="yellowMarker">barbar</$text>]bar</p>'
  157. );
  158. } );
  159. it( 'should set highlight attribute on selected nodes when passed as parameter only on selected characters', () => {
  160. setData( model, '<p>abc[<$text highlight="yellowMarker">foo]bar</$text>xyz</p>' );
  161. expect( command.value ).to.equal( 'yellowMarker' );
  162. command.execute( { value: 'foo' } );
  163. expect( getData( model ) ).to.equal(
  164. '<p>abc[<$text highlight="foo">foo</$text>]<$text highlight="yellowMarker">bar</$text>xyz</p>'
  165. );
  166. expect( command.value ).to.equal( 'foo' );
  167. } );
  168. } );
  169. } );
  170. describe( 'with undefined option.value', () => {
  171. describe( 'on collapsed range', () => {
  172. it( 'should remove entire highlight when inside highlighted text', () => {
  173. setData( model, '<p>abc<$text highlight="yellowMarker">foo[]bar</$text>xyz</p>' );
  174. expect( command.value ).to.equal( 'yellowMarker' );
  175. command.execute();
  176. expect( getData( model ) ).to.equal( '<p>abcfoo[]barxyz</p>' );
  177. expect( command.value ).to.be.undefined;
  178. } );
  179. } );
  180. describe( 'on not collapsed range', () => {
  181. it( 'should remove highlight attribute on selected node when undefined passed as parameter', () => {
  182. setData( model, '<p>abc[<$text highlight="yellowMarker">foo]bar</$text>xyz</p>' );
  183. expect( command.value ).to.equal( 'yellowMarker' );
  184. command.execute();
  185. expect( getData( model ) ).to.equal( '<p>abc[foo]<$text highlight="yellowMarker">bar</$text>xyz</p>' );
  186. expect( command.value ).to.be.undefined;
  187. } );
  188. } );
  189. } );
  190. } );
  191. } );