attributecommand.js 9.0 KB

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