8
0

attributecommand.js 9.0 KB

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