8
0

highlightcommand.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * @license Copyright (c) 2003-2017, 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. describe( 'HighlightCommand', () => {
  10. let editor, model, command;
  11. beforeEach( () => {
  12. return ModelTestEditor.create()
  13. .then( newEditor => {
  14. editor = newEditor;
  15. model = editor.model;
  16. command = new HighlightCommand( newEditor );
  17. editor.commands.add( 'highlight', command );
  18. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  19. model.schema.extend( '$text', { allowAttributes: 'highlight' } );
  20. } );
  21. } );
  22. afterEach( () => {
  23. editor.destroy();
  24. } );
  25. it( 'is a command', () => {
  26. expect( HighlightCommand.prototype ).to.be.instanceOf( Command );
  27. expect( command ).to.be.instanceOf( Command );
  28. } );
  29. describe( 'value', () => {
  30. it( 'is set to highlight attribute value when selection is in text with highlight attribute', () => {
  31. setData( model, '<paragraph><$text highlight="marker">fo[o]</$text></paragraph>' );
  32. expect( command ).to.have.property( 'value', 'marker' );
  33. } );
  34. it( 'is undefined when selection is not in text with highlight attribute', () => {
  35. setData( model, '<paragraph>fo[]o</paragraph>' );
  36. expect( command ).to.have.property( 'value', undefined );
  37. } );
  38. } );
  39. describe( 'isEnabled', () => {
  40. it( 'is true when selection is on text which can have highlight added', () => {
  41. setData( model, '<paragraph>fo[]o</paragraph>' );
  42. expect( command ).to.have.property( 'isEnabled', true );
  43. } );
  44. } );
  45. describe( 'execute()', () => {
  46. describe( 'with option.value set', () => {
  47. describe( 'on collapsed range', () => {
  48. it( 'should change entire highlight when inside highlighted text', () => {
  49. setData( model, '<paragraph>abc<$text highlight="marker">foo[]bar</$text>xyz</paragraph>' );
  50. expect( command.value ).to.equal( 'marker' );
  51. command.execute( { value: 'greenMarker' } );
  52. expect( getData( model ) ).to.equal( '<paragraph>abc[<$text highlight="greenMarker">foobar</$text>]xyz</paragraph>' );
  53. expect( command.value ).to.equal( 'greenMarker' );
  54. } );
  55. it( 'should remove entire highlight when inside highlighted text of the same value', () => {
  56. setData( model, '<paragraph>abc<$text highlight="marker">foo[]bar</$text>xyz</paragraph>' );
  57. expect( command.value ).to.equal( 'marker' );
  58. command.execute( { value: 'marker' } );
  59. expect( getData( model ) ).to.equal( '<paragraph>abcfoo[]barxyz</paragraph>' );
  60. expect( command.value ).to.be.undefined;
  61. } );
  62. it( 'should change entire highlight when inside highlighted text', () => {
  63. setData( model, '<paragraph>abc<$text highlight="marker">foo[]bar</$text>xyz</paragraph>' );
  64. expect( command.value ).to.equal( 'marker' );
  65. command.execute( { value: 'greenMarker' } );
  66. expect( getData( model ) ).to.equal( '<paragraph>abc[<$text highlight="greenMarker">foobar</$text>]xyz</paragraph>' );
  67. expect( command.value ).to.equal( 'greenMarker' );
  68. } );
  69. } );
  70. describe( 'on not collapsed range', () => {
  71. it( 'should set highlight attribute on selected node when passed as parameter', () => {
  72. setData( model, '<paragraph>a[bc<$text highlight="marker">fo]obar</$text>xyz</paragraph>' );
  73. expect( command.value ).to.be.undefined;
  74. command.execute( { value: 'marker' } );
  75. expect( command.value ).to.equal( 'marker' );
  76. expect( getData( model ) ).to.equal( '<paragraph>a[<$text highlight="marker">bcfo]obar</$text>xyz</paragraph>' );
  77. } );
  78. it( 'should set highlight attribute on selected node when passed as parameter (multiple nodes)', () => {
  79. setData(
  80. model,
  81. '<paragraph>abcabc[abc</paragraph>' +
  82. '<paragraph>foofoofoo</paragraph>' +
  83. '<paragraph>barbar]bar</paragraph>'
  84. );
  85. command.execute( { value: 'marker' } );
  86. expect( command.value ).to.equal( 'marker' );
  87. expect( getData( model ) ).to.equal(
  88. '<paragraph>abcabc[<$text highlight="marker">abc</$text></paragraph>' +
  89. '<paragraph><$text highlight="marker">foofoofoo</$text></paragraph>' +
  90. '<paragraph><$text highlight="marker">barbar</$text>]bar</paragraph>'
  91. );
  92. } );
  93. it( 'should set highlight attribute on selected nodes when passed as parameter only on selected characters', () => {
  94. setData( model, '<paragraph>abc[<$text highlight="marker">foo]bar</$text>xyz</paragraph>' );
  95. expect( command.value ).to.equal( 'marker' );
  96. command.execute( { value: 'foo' } );
  97. expect( getData( model ) ).to.equal(
  98. '<paragraph>abc[<$text highlight="foo">foo</$text>]<$text highlight="marker">bar</$text>xyz</paragraph>'
  99. );
  100. expect( command.value ).to.equal( 'foo' );
  101. } );
  102. } );
  103. } );
  104. describe( 'with undefined option.value', () => {
  105. describe( 'on collapsed range', () => {
  106. it( 'should remove entire highlight when inside highlighted text', () => {
  107. setData( model, '<paragraph>abc<$text highlight="marker">foo[]bar</$text>xyz</paragraph>' );
  108. expect( command.value ).to.equal( 'marker' );
  109. command.execute();
  110. expect( getData( model ) ).to.equal( '<paragraph>abcfoo[]barxyz</paragraph>' );
  111. expect( command.value ).to.be.undefined;
  112. } );
  113. } );
  114. describe( 'on not collapsed range', () => {
  115. it( 'should remove highlight attribute on selected node when undefined passed as parameter', () => {
  116. setData( model, '<paragraph>abc[<$text highlight="marker">foo]bar</$text>xyz</paragraph>' );
  117. expect( command.value ).to.equal( 'marker' );
  118. command.execute();
  119. expect( getData( model ) ).to.equal( '<paragraph>abc[foo]<$text highlight="marker">bar</$text>xyz</paragraph>' );
  120. expect( command.value ).to.be.undefined;
  121. } );
  122. } );
  123. } );
  124. } );
  125. } );