attributecommand.js 8.7 KB

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