highlightcommand.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import Command from '@ckeditor/ckeditor5-core/src/command';
  8. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  9. import { getData, setData } from '../../ckeditor5-engine/src/dev-utils/model';
  10. describe( 'HighlightCommand', () => {
  11. let editor, doc, command;
  12. beforeEach( () => {
  13. return ModelTestEditor.create()
  14. .then( newEditor => {
  15. doc = newEditor.document;
  16. command = new HighlightCommand( newEditor );
  17. editor = newEditor;
  18. editor.commands.add( 'highlight', command );
  19. doc.schema.registerItem( 'paragraph', '$block' );
  20. doc.schema.allow( { name: '$inline', attributes: 'highlight', inside: '$block' } );
  21. } );
  22. } );
  23. afterEach( () => {
  24. editor.destroy();
  25. } );
  26. it( 'is a command', () => {
  27. expect( HighlightCommand.prototype ).to.be.instanceOf( Command );
  28. expect( command ).to.be.instanceOf( Command );
  29. } );
  30. describe( 'value', () => {
  31. it( 'is set to highlight attribute value when selection is in text with highlight attribute', () => {
  32. setModelData( doc, '<paragraph><$text highlight="marker">fo[]o</$text></paragraph>' );
  33. expect( command ).to.have.property( 'value', 'marker' );
  34. } );
  35. it( 'is undefined when selection is not in text with highlight attribute', () => {
  36. setModelData( doc, '<paragraph>fo[]o</paragraph>' );
  37. expect( command ).to.have.property( 'value', undefined );
  38. } );
  39. } );
  40. describe( 'isEnabled', () => {
  41. it( 'is true when selection is on text which can have highlight added', () => {
  42. setModelData( doc, '<paragraph>fo[]o</paragraph>' );
  43. expect( command ).to.have.property( 'isEnabled', true );
  44. } );
  45. } );
  46. describe( 'execute()', () => {
  47. it( 'should add highlight attribute on selected nodes nodes when passed as parameter', () => {
  48. setData( doc, '<paragraph>a[bc<$text highlight="marker">fo]obar</$text>xyz</paragraph>' );
  49. expect( command.value ).to.be.undefined;
  50. command.execute( { class: 'marker' } );
  51. expect( command.value ).to.equal( 'marker' );
  52. expect( getData( doc ) ).to.equal( '<paragraph>a[<$text highlight="marker">bcfo]obar</$text>xyz</paragraph>' );
  53. } );
  54. it( 'should set highlight attribute on selected nodes when passed as parameter', () => {
  55. setData( doc, '<paragraph>abc[<$text highlight="marker">foo]bar</$text>xyz</paragraph>' );
  56. expect( command.value ).to.equal( 'marker' );
  57. command.execute( { class: 'foo' } );
  58. expect( getData( doc ) ).to.equal(
  59. '<paragraph>abc[<$text highlight="foo">foo</$text>]<$text highlight="marker">bar</$text>xyz</paragraph>'
  60. );
  61. expect( command.value ).to.equal( 'foo' );
  62. } );
  63. it( 'should remove highlight attribute on selected nodes nodes when undefined passed as parameter', () => {
  64. setData( doc, '<paragraph>abc[<$text highlight="marker">foo]bar</$text>xyz</paragraph>' );
  65. expect( command.value ).to.equal( 'marker' );
  66. command.execute();
  67. expect( getData( doc ) ).to.equal( '<paragraph>abc[foo]<$text highlight="marker">bar</$text>xyz</paragraph>' );
  68. expect( command.value ).to.be.undefined;
  69. } );
  70. it( 'should do nothing on collapsed range', () => {
  71. setData( doc, '<paragraph>abc<$text highlight="marker">foo[]bar</$text>xyz</paragraph>' );
  72. expect( command.value ).to.equal( 'marker' );
  73. command.execute();
  74. expect( getData( doc ) ).to.equal( '<paragraph>abc<$text highlight="marker">foo[]bar</$text>xyz</paragraph>' );
  75. expect( command.value ).to.equal( 'marker' );
  76. } );
  77. } );
  78. } );