toggleattributecommand.js 9.6 KB

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