attributecommand.js 9.7 KB

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