highlightcommand.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 '@ckeditor/ckeditor5-engine/src/model/position';
  10. import Range from '@ckeditor/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="yellowMarker">fo[o]</$text></p>' );
  41. expect( command ).to.have.property( 'value', 'yellowMarker' );
  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="yellowMarker">foo[]bar</$text>xyz</p>' );
  66. expect( command.value ).to.equal( 'yellowMarker' );
  67. command.execute( { value: 'greenMarker' } );
  68. expect( getData( model ) ).to.equal( '<p>abc<$text highlight="greenMarker">foo[]bar</$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="yellowMarker">foo[]bar</$text>xyz</p>' );
  73. expect( command.value ).to.equal( 'yellowMarker' );
  74. command.execute( { value: 'yellowMarker' } );
  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="yellowMarker">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="yellowMarker">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( writer => {
  97. // Simulate clicking right arrow key by changing selection ranges.
  98. writer.setSelection( [ new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 2 ] ) ) ] );
  99. // Get back to previous selection.
  100. writer.setSelection( [ 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="yellowMarker">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( writer => {
  113. writer.setSelection( [ 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( writer => {
  118. writer.setSelection( [ 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 selection attribute on consecutive calls', () => {
  127. setData( model, '<p>abcfoobar[] foobar</p>' );
  128. expect( command.value ).to.be.undefined;
  129. command.execute( { value: 'greenMarker' } );
  130. expect( command.value ).to.equal( 'greenMarker' );
  131. expect( doc.selection.hasAttribute( 'highlight' ) ).to.be.true;
  132. command.execute( { value: 'pinkMarker' } );
  133. expect( command.value ).to.equal( 'pinkMarker' );
  134. expect( doc.selection.hasAttribute( 'highlight' ) ).to.be.true;
  135. } );
  136. } );
  137. describe( 'on not collapsed range', () => {
  138. it( 'should set highlight attribute on selected node when passed as parameter', () => {
  139. setData( model, '<p>a[bc<$text highlight="yellowMarker">fo]obar</$text>xyz</p>' );
  140. expect( command.value ).to.be.undefined;
  141. command.execute( { value: 'yellowMarker' } );
  142. expect( command.value ).to.equal( 'yellowMarker' );
  143. expect( getData( model ) ).to.equal( '<p>a[<$text highlight="yellowMarker">bcfo]obar</$text>xyz</p>' );
  144. } );
  145. it( 'should set highlight attribute on selected node when passed as parameter (multiple nodes)', () => {
  146. setData(
  147. model,
  148. '<p>abcabc[abc</p>' +
  149. '<p>foofoofoo</p>' +
  150. '<p>barbar]bar</p>'
  151. );
  152. command.execute( { value: 'yellowMarker' } );
  153. expect( command.value ).to.equal( 'yellowMarker' );
  154. expect( getData( model ) ).to.equal(
  155. '<p>abcabc[<$text highlight="yellowMarker">abc</$text></p>' +
  156. '<p><$text highlight="yellowMarker">foofoofoo</$text></p>' +
  157. '<p><$text highlight="yellowMarker">barbar</$text>]bar</p>'
  158. );
  159. } );
  160. it( 'should set highlight attribute on selected nodes when passed as parameter only on selected characters', () => {
  161. setData( model, '<p>abc[<$text highlight="yellowMarker">foo]bar</$text>xyz</p>' );
  162. expect( command.value ).to.equal( 'yellowMarker' );
  163. command.execute( { value: 'foo' } );
  164. expect( getData( model ) ).to.equal(
  165. '<p>abc[<$text highlight="foo">foo</$text>]<$text highlight="yellowMarker">bar</$text>xyz</p>'
  166. );
  167. expect( command.value ).to.equal( 'foo' );
  168. } );
  169. } );
  170. } );
  171. describe( 'with undefined option.value', () => {
  172. describe( 'on collapsed range', () => {
  173. it( 'should remove entire highlight when inside highlighted text', () => {
  174. setData( model, '<p>abc<$text highlight="yellowMarker">foo[]bar</$text>xyz</p>' );
  175. expect( command.value ).to.equal( 'yellowMarker' );
  176. command.execute();
  177. expect( getData( model ) ).to.equal( '<p>abcfoo[]barxyz</p>' );
  178. expect( command.value ).to.be.undefined;
  179. } );
  180. } );
  181. describe( 'on not collapsed range', () => {
  182. it( 'should remove highlight attribute on selected node when undefined passed as parameter', () => {
  183. setData( model, '<p>abc[<$text highlight="yellowMarker">foo]bar</$text>xyz</p>' );
  184. expect( command.value ).to.equal( 'yellowMarker' );
  185. command.execute();
  186. expect( getData( model ) ).to.equal( '<p>abc[foo]<$text highlight="yellowMarker">bar</$text>xyz</p>' );
  187. expect( command.value ).to.be.undefined;
  188. } );
  189. } );
  190. } );
  191. } );
  192. } );