highlightcommand.js 8.7 KB

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