8
0

attributecommand.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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( '_doExecute', () => {
  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' ] ), new Element( 'p' ) ] );
  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._doExecute();
  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._doExecute();
  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._doExecute( 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._doExecute( 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 in non-empty parent', () => {
  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._doExecute();
  84. expect( command.value ).to.be.true;
  85. expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.true;
  86. command._doExecute();
  87. expect( command.value ).to.be.false;
  88. expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.false;
  89. } );
  90. it( 'should not store attribute change on selection if selection is collapsed in non-empty parent', () => {
  91. modelDoc.selection.addRange( new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 1 ] ) ) );
  92. command._doExecute();
  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 change selection attribute and store it if selection is collapsed in empty parent', () => {
  101. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) ) ] );
  102. expect( command.value ).to.be.false;
  103. command._doExecute();
  104. expect( command.value ).to.be.true;
  105. expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.true;
  106. // Attribute should be stored.
  107. // Simulate clicking somewhere else in the editor.
  108. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 2 ] ) ) ] );
  109. expect( command.value ).to.be.false;
  110. // Go back to where attribute was stored.
  111. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) ) ] );
  112. // Attribute should be restored.
  113. expect( command.value ).to.be.true;
  114. command._doExecute();
  115. expect( command.value ).to.be.false;
  116. expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.false;
  117. } );
  118. it( 'should not apply attribute change where it would invalid schema', () => {
  119. p.insertChildren( 3, new Element( 'image' ) );
  120. p.insertChildren( 12, new Element( 'image' ) );
  121. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 13 ] ) ) ] );
  122. expect( command.isEnabled ).to.be.true;
  123. command._doExecute();
  124. let expectedHas = [ 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 ];
  125. for ( let i = 0; i < expectedHas.length; i++ ) {
  126. expect( p.getChild( i ).hasAttribute( attrKey ) ).to.equal( !!expectedHas[ i ] );
  127. }
  128. } );
  129. } );
  130. describe( '_checkEnabled', () => {
  131. beforeEach( () => {
  132. root.insertChildren( 0, [
  133. new Element( 'p', [], [
  134. 'foo',
  135. new Element( 'img' ),
  136. new Element( 'img' ),
  137. 'bar'
  138. ] ),
  139. new Element( 'div' ),
  140. new Element( 'p' )
  141. ] );
  142. } );
  143. describe( 'when selection is collapsed', () => {
  144. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  145. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 1 ] ) ) ] );
  146. expect( command._checkEnabled() ).to.be.true;
  147. } );
  148. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  149. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) ) ] );
  150. expect( command._checkEnabled() ).to.be.false;
  151. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
  152. expect( command._checkEnabled() ).to.be.false;
  153. } );
  154. } );
  155. describe( 'when selection is not collapsed', () => {
  156. it( 'should return true if there is at least one node in selection that can have the attribute', () => {
  157. // Simple selection on a few characters.
  158. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 3 ] ) ) ] );
  159. expect( command._checkEnabled() ).to.be.true;
  160. // Selection spans over characters but also include nodes that can't have attribute.
  161. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 6 ] ) ) ] );
  162. expect( command._checkEnabled() ).to.be.true;
  163. // Selection on whole root content. Characters in P can have an attribute so it's valid.
  164. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ) ] );
  165. expect( command._checkEnabled() ).to.be.true;
  166. // Selection on empty P. P can have the attribute.
  167. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 3 ] ) ) ] );
  168. expect( command._checkEnabled() ).to.be.true;
  169. } );
  170. it( 'should return false if there are no nodes in selection that can have the attribute', () => {
  171. // Selection on DIV which can't have bold text.
  172. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) ) ] );
  173. expect( command._checkEnabled() ).to.be.false;
  174. // Selection on two images which can't be bold.
  175. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 0, 5 ] ) ) ] );
  176. expect( command._checkEnabled() ).to.be.false;
  177. } );
  178. } );
  179. it( 'should return false if selection has no ranges', () => {
  180. modelDoc.selection.removeAllRanges();
  181. expect( command._checkEnabled() ).to.be.false;
  182. } );
  183. } );