attributecommand.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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/command/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', 'div' );
  20. command = new AttributeCommand( editor, attrKey );
  21. modelDoc.schema.registerItem( 'div', '$block' );
  22. modelDoc.schema.registerItem( 'p', '$block' );
  23. modelDoc.schema.registerItem( 'img', '$inline' );
  24. // Allow block in "root" (DIV)
  25. modelDoc.schema.allow( { name: '$block', inside: 'div' } );
  26. // Bold text is allowed only in P.
  27. modelDoc.schema.allow( { name: '$text', attribute: 'bold', inside: 'p' } );
  28. modelDoc.schema.allow( { name: 'p', attribute: 'bold', inside: 'div' } );
  29. // Disallow bold on image.
  30. modelDoc.schema.disallow( { name: 'img', attribute: 'bold', inside: 'div' } );
  31. } );
  32. describe( 'value', () => {
  33. it( 'should be set to true or false basing on selection attribute', () => {
  34. modelDoc.selection.setAttribute( attrKey, true );
  35. expect( command.value ).to.be.true;
  36. modelDoc.selection.removeAttribute( attrKey );
  37. expect( command.value ).to.be.false;
  38. } );
  39. } );
  40. describe( '_execute', () => {
  41. let p;
  42. beforeEach( () => {
  43. let attrs = {};
  44. attrs[ attrKey ] = true;
  45. root.insertChildren( 0, new Element( 'p', [] , [ 'abc', new Text( 'foobar', attrs ), 'xyz' ] ) );
  46. p = root.getChild( 0 );
  47. } );
  48. it( 'should add attribute on selected nodes if the command value was false', () => {
  49. modelDoc.selection.addRange( new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ) );
  50. expect( command.value ).to.be.false;
  51. command._execute();
  52. expect( command.value ).to.be.true;
  53. expect( p.getChild( 1 ).hasAttribute( attrKey ) ).to.be.true;
  54. expect( p.getChild( 2 ).hasAttribute( attrKey ) ).to.be.true;
  55. } );
  56. it( 'should remove attribute from selected nodes if the command value was true', () => {
  57. modelDoc.selection.addRange( new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 0, 6 ] ) ) );
  58. expect( command.value ).to.be.true;
  59. command._execute();
  60. expect( command.value ).to.be.false;
  61. expect( p.getChild( 3 ).hasAttribute( attrKey ) ).to.be.false;
  62. expect( p.getChild( 4 ).hasAttribute( attrKey ) ).to.be.false;
  63. expect( p.getChild( 5 ).hasAttribute( attrKey ) ).to.be.false;
  64. } );
  65. it( 'should add attribute on selected nodes if execute parameter was set to true', () => {
  66. modelDoc.selection.addRange( new Range( new Position( root, [ 0, 7 ] ), new Position( root, [ 0, 10 ] ) ) );
  67. expect( command.value ).to.be.true;
  68. command._execute( true );
  69. expect( command.value ).to.be.true;
  70. expect( p.getChild( 9 ).hasAttribute( attrKey ) ).to.be.true;
  71. } );
  72. it( 'should remove attribute on selected nodes if execute parameter was set to false', () => {
  73. modelDoc.selection.addRange( new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 5 ] ) ) );
  74. expect( command.value ).to.be.false;
  75. command._execute( false );
  76. expect( command.value ).to.be.false;
  77. expect( p.getChild( 3 ).hasAttribute( attrKey ) ).to.be.false;
  78. expect( p.getChild( 4 ).hasAttribute( attrKey ) ).to.be.false;
  79. } );
  80. it( 'should change selection attribute if selection is collapsed', () => {
  81. modelDoc.selection.addRange( new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 1 ] ) ) );
  82. expect( command.value ).to.be.false;
  83. command._execute();
  84. expect( command.value ).to.be.true;
  85. expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.true;
  86. command._execute();
  87. expect( command.value ).to.be.false;
  88. expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.false;
  89. } );
  90. it( 'should not save that attribute was changed on selection when selection changes', () => {
  91. modelDoc.selection.addRange( new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 1 ] ) ) );
  92. command._execute();
  93. // It should not save that bold was executed at position ( root, [ 0, 1 ] ).
  94. // Simulate clicking right arrow key by changing selection ranges.
  95. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 2 ] ) ) ] );
  96. // Get back to previous selection.
  97. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 1 ] ) ) ] );
  98. expect( command.value ).to.be.false;
  99. } );
  100. it( 'should not throw and do nothing if selection has no ranges', () => {
  101. let spy = sinon.spy();
  102. modelDoc.on( 'change', spy );
  103. modelDoc.selection.removeAllRanges();
  104. command._execute();
  105. expect( spy.called ).to.be.false;
  106. expect( Array.from( modelDoc.selection.getAttributes() ) ).to.deep.equal( [ ] );
  107. } );
  108. it( 'should not apply attribute change where it would invalid schema', () => {
  109. p.insertChildren( 3, new Element( 'image' ) );
  110. p.insertChildren( 12, new Element( 'image' ) );
  111. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 13 ] ) ) ] );
  112. expect( command.isEnabled ).to.be.true;
  113. command._execute();
  114. let expectedHas = [ 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 ];
  115. for ( let i = 0; i < expectedHas.length; i++ ) {
  116. expect( p.getChild( i ).hasAttribute( attrKey ) ).to.equal( !!expectedHas[ i ] );
  117. }
  118. } );
  119. } );
  120. describe( 'checkEnabled', () => {
  121. beforeEach( () => {
  122. root.insertChildren( 0, [
  123. new Element( 'p', [], [
  124. 'foo',
  125. new Element( 'img' ),
  126. new Element( 'img' ),
  127. 'bar'
  128. ] ),
  129. new Element( 'div' ),
  130. new Element( 'p' )
  131. ] );
  132. } );
  133. describe( 'when selection is collapsed', () => {
  134. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  135. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 1 ] ) ) ] );
  136. expect( command.checkEnabled() ).to.be.true;
  137. } );
  138. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  139. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) ) ] );
  140. expect( command.checkEnabled() ).to.be.false;
  141. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  142. expect( command.checkEnabled() ).to.be.false;
  143. } );
  144. } );
  145. describe( 'when selection is not collapsed', () => {
  146. it( 'should return true if there is at least one node in selection that can have the attribute', () => {
  147. // Simple selection on a few characters.
  148. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 3 ] ) ) ] );
  149. expect( command.checkEnabled() ).to.be.true;
  150. // Selection spans over characters but also include nodes that can't have attribute.
  151. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 6 ] ) ) ] );
  152. expect( command.checkEnabled() ).to.be.true;
  153. // Selection on whole root content. Characters in P can have an attribute so it's valid.
  154. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ) ] );
  155. expect( command.checkEnabled() ).to.be.true;
  156. // Selection on empty P. P can have the attribute.
  157. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 3 ] ) ) ] );
  158. expect( command.checkEnabled() ).to.be.true;
  159. } );
  160. it( 'should return false if there are no nodes in selection that can have the attribute', () => {
  161. // Selection on DIV which can't have bold text.
  162. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) ) ] );
  163. expect( command.checkEnabled() ).to.be.false;
  164. // Selection on two images which can't be bold.
  165. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 0, 5 ] ) ) ] );
  166. expect( command.checkEnabled() ).to.be.false;
  167. } );
  168. } );
  169. it( 'should return false if selection has no ranges', () => {
  170. modelDoc.selection.removeAllRanges();
  171. expect( command.checkEnabled() ).to.be.false;
  172. } );
  173. } );