8
0

attributecommand.js 9.4 KB

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