8
0

attributecommand.js 8.1 KB

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