attributecommand.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import Editor from '/ckeditor5/core/editor.js';
  7. import AttributeCommand from '/ckeditor5/core/attributecommand.js';
  8. import Text from '/ckeditor5/core/treemodel/text.js';
  9. import Range from '/ckeditor5/core/treemodel/range.js';
  10. import Position from '/ckeditor5/core/treemodel/position.js';
  11. import Element from '/ckeditor5/core/treemodel/element.js';
  12. let element, editor, command, modelDoc, root;
  13. const attrKey = 'bold';
  14. beforeEach( () => {
  15. element = document.createElement( 'div' );
  16. document.body.appendChild( element );
  17. editor = new Editor( element );
  18. modelDoc = editor.document;
  19. root = modelDoc.createRoot( 'root' );
  20. command = new AttributeCommand( editor, attrKey );
  21. } );
  22. describe( 'value', () => {
  23. it( 'should be set to true or false basing on selection attribute', () => {
  24. modelDoc.selection.setAttribute( attrKey, true );
  25. expect( command.value ).to.be.true;
  26. modelDoc.selection.removeAttribute( attrKey );
  27. expect( command.value ).to.be.false;
  28. } );
  29. } );
  30. describe( 'execute', () => {
  31. beforeEach( () => {
  32. let attrs = {};
  33. attrs[ attrKey ] = true;
  34. root.insertChildren( 0, [ 'abc', new Text( 'foobar', attrs ), 'xyz' ] );
  35. } );
  36. it( 'should add attribute on selected nodes if the command value was false', () => {
  37. modelDoc.selection.addRange( new Range( new Position( root, [ 1 ] ), new Position( root, [ 5 ] ) ) );
  38. expect( command.value ).to.be.false;
  39. command.execute();
  40. expect( command.value ).to.be.true;
  41. expect( root.getChild( 1 ).hasAttribute( attrKey ) ).to.be.true;
  42. expect( root.getChild( 2 ).hasAttribute( attrKey ) ).to.be.true;
  43. } );
  44. it( 'should remove attribute from selected nodes if the command value was true', () => {
  45. modelDoc.selection.addRange( new Range( new Position( root, [ 3 ] ), new Position( root, [ 6 ] ) ) );
  46. expect( command.value ).to.be.true;
  47. command.execute();
  48. expect( command.value ).to.be.false;
  49. expect( root.getChild( 3 ).hasAttribute( attrKey ) ).to.be.false;
  50. expect( root.getChild( 4 ).hasAttribute( attrKey ) ).to.be.false;
  51. expect( root.getChild( 5 ).hasAttribute( attrKey ) ).to.be.false;
  52. } );
  53. it( 'should add attribute on selected nodes if execute parameter was set to true', () => {
  54. modelDoc.selection.addRange( new Range( new Position( root, [ 7 ] ), new Position( root, [ 10 ] ) ) );
  55. expect( command.value ).to.be.true;
  56. command.execute( true );
  57. expect( command.value ).to.be.true;
  58. expect( root.getChild( 9 ).hasAttribute( attrKey ) ).to.be.true;
  59. } );
  60. it( 'should remove attribute on selected nodes if execute parameter was set to false', () => {
  61. modelDoc.selection.addRange( new Range( new Position( root, [ 1 ] ), new Position( root, [ 5 ] ) ) );
  62. expect( command.value ).to.be.false;
  63. command.execute( false );
  64. expect( command.value ).to.be.false;
  65. expect( root.getChild( 3 ).hasAttribute( attrKey ) ).to.be.false;
  66. expect( root.getChild( 4 ).hasAttribute( attrKey ) ).to.be.false;
  67. } );
  68. it( 'should change selection attribute if selection is collapsed and is not at the beginning of node', () => {
  69. modelDoc.selection.addRange( new Range( new Position( root, [ 1 ] ), new Position( root, [ 1 ] ) ) );
  70. expect( command.value ).to.be.false;
  71. command.execute();
  72. expect( command.value ).to.be.true;
  73. expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.true;
  74. // It should not save that bold was executed at position ( root, [ 1 ] ).
  75. // Simulate clicking right arrow key by changing selection ranges.
  76. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  77. // Get back to previous selection.
  78. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 1 ] ), new Position( root, [ 1 ] ) ) ] );
  79. expect( command.value ).to.be.false;
  80. } );
  81. it( 'should change selection parent element attribute if selection is collapsed and in empty node', () => {
  82. root.insertChildren( 0, new Element( 'p' ) );
  83. modelDoc.selection.addRange( new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) );
  84. expect( command.value ).to.be.false;
  85. command.execute();
  86. expect( command.value ).to.be.true;
  87. expect( root.getChild( 0 ).hasAttribute( 'bold' ) ).to.be.true;
  88. // It should save that bold was set on selection's parent node.
  89. // Simulate clicking right arrow key by changing selection ranges.
  90. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  91. // Get back to previous selection.
  92. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
  93. expect( command.value ).to.be.true;
  94. } );
  95. } );