8
0

highlightcommand.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. it( 'should add highlight attribute on selected nodes nodes when passed as parameter', () => {
  47. setData( model, '<paragraph>a[bc<$text highlight="marker">fo]obar</$text>xyz</paragraph>' );
  48. expect( command.value ).to.be.undefined;
  49. command.execute( { value: 'marker' } );
  50. expect( command.value ).to.equal( 'marker' );
  51. expect( getData( model ) ).to.equal( '<paragraph>a[<$text highlight="marker">bcfo]obar</$text>xyz</paragraph>' );
  52. } );
  53. it( 'should add highlight attribute on selected nodes nodes when passed as parameter (multiple nodes)', () => {
  54. setData(
  55. model,
  56. '<paragraph>abcabc[abc</paragraph>' +
  57. '<paragraph>foofoofoo</paragraph>' +
  58. '<paragraph>barbar]bar</paragraph>'
  59. );
  60. command.execute( { value: 'marker' } );
  61. expect( command.value ).to.equal( 'marker' );
  62. expect( getData( model ) ).to.equal(
  63. '<paragraph>abcabc[<$text highlight="marker">abc</$text></paragraph>' +
  64. '<paragraph><$text highlight="marker">foofoofoo</$text></paragraph>' +
  65. '<paragraph><$text highlight="marker">barbar</$text>]bar</paragraph>'
  66. );
  67. } );
  68. it( 'should set highlight attribute on selected nodes when passed as parameter', () => {
  69. setData( model, '<paragraph>abc[<$text highlight="marker">foo]bar</$text>xyz</paragraph>' );
  70. expect( command.value ).to.equal( 'marker' );
  71. command.execute( { value: 'foo' } );
  72. expect( getData( model ) ).to.equal(
  73. '<paragraph>abc[<$text highlight="foo">foo</$text>]<$text highlight="marker">bar</$text>xyz</paragraph>'
  74. );
  75. expect( command.value ).to.equal( 'foo' );
  76. } );
  77. it( 'should remove highlight attribute on selected nodes nodes when undefined passed as parameter', () => {
  78. setData( model, '<paragraph>abc[<$text highlight="marker">foo]bar</$text>xyz</paragraph>' );
  79. expect( command.value ).to.equal( 'marker' );
  80. command.execute();
  81. expect( getData( model ) ).to.equal( '<paragraph>abc[foo]<$text highlight="marker">bar</$text>xyz</paragraph>' );
  82. expect( command.value ).to.be.undefined;
  83. } );
  84. it( 'should change entire highlight on collapsed range when inside highlighted text', () => {
  85. setData( model, '<paragraph>abc<$text highlight="marker">foo[]bar</$text>xyz</paragraph>' );
  86. expect( command.value ).to.equal( 'marker' );
  87. command.execute( { value: 'greenMarker' } );
  88. expect( getData( model ) ).to.equal( '<paragraph>abc[<$text highlight="greenMarker">foobar</$text>]xyz</paragraph>' );
  89. expect( command.value ).to.equal( 'greenMarker' );
  90. } );
  91. it( 'should remove entire highlight on collapsed range when inside highlighted text of the same value', () => {
  92. setData( model, '<paragraph>abc<$text highlight="marker">foo[]bar</$text>xyz</paragraph>' );
  93. expect( command.value ).to.equal( 'marker' );
  94. command.execute( { value: 'marker' } );
  95. expect( getData( model ) ).to.equal( '<paragraph>abcfoo[]barxyz</paragraph>' );
  96. expect( command.value ).to.be.undefined;
  97. } );
  98. } );
  99. } );