highlightcommand.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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, doc, command;
  11. beforeEach( () => {
  12. return ModelTestEditor.create()
  13. .then( newEditor => {
  14. doc = newEditor.document;
  15. command = new HighlightCommand( newEditor );
  16. editor = newEditor;
  17. editor.commands.add( 'highlight', command );
  18. doc.schema.registerItem( 'paragraph', '$block' );
  19. doc.schema.allow( { name: '$inline', attributes: 'highlight', inside: '$block' } );
  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( doc, '<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( doc, '<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( doc, '<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( doc, '<paragraph>a[bc<$text highlight="marker">fo]obar</$text>xyz</paragraph>' );
  48. expect( command.value ).to.be.undefined;
  49. command.execute( { class: 'marker' } );
  50. expect( command.value ).to.equal( 'marker' );
  51. expect( getData( doc ) ).to.equal( '<paragraph>a[<$text highlight="marker">bcfo]obar</$text>xyz</paragraph>' );
  52. } );
  53. it( 'should set highlight attribute on selected nodes when passed as parameter', () => {
  54. setData( doc, '<paragraph>abc[<$text highlight="marker">foo]bar</$text>xyz</paragraph>' );
  55. expect( command.value ).to.equal( 'marker' );
  56. command.execute( { class: 'foo' } );
  57. expect( getData( doc ) ).to.equal(
  58. '<paragraph>abc[<$text highlight="foo">foo</$text>]<$text highlight="marker">bar</$text>xyz</paragraph>'
  59. );
  60. expect( command.value ).to.equal( 'foo' );
  61. } );
  62. it( 'should remove highlight attribute on selected nodes nodes when undefined passed as parameter', () => {
  63. setData( doc, '<paragraph>abc[<$text highlight="marker">foo]bar</$text>xyz</paragraph>' );
  64. expect( command.value ).to.equal( 'marker' );
  65. command.execute();
  66. expect( getData( doc ) ).to.equal( '<paragraph>abc[foo]<$text highlight="marker">bar</$text>xyz</paragraph>' );
  67. expect( command.value ).to.be.undefined;
  68. } );
  69. it( 'should do nothing on collapsed range', () => {
  70. setData( doc, '<paragraph>abc<$text highlight="marker">foo[]bar</$text>xyz</paragraph>' );
  71. expect( command.value ).to.equal( 'marker' );
  72. command.execute();
  73. expect( getData( doc ) ).to.equal( '<paragraph>abc<$text highlight="marker">foo[]bar</$text>xyz</paragraph>' );
  74. expect( command.value ).to.equal( 'marker' );
  75. } );
  76. } );
  77. } );