toggleattributecommand.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Editor from '../../src/editor/editor';
  6. import Document from '@ckeditor/ckeditor5-engine/src/model/document';
  7. import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
  8. import ToggleAttributeCommand from '../../src/command/toggleattributecommand';
  9. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  10. import Position from '@ckeditor/ckeditor5-engine/src/model/position';
  11. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. describe( 'ToggleAttributeCommand', () => {
  13. const attrKey = 'bold';
  14. let editor, command, modelDoc, root;
  15. beforeEach( () => {
  16. editor = new Editor();
  17. editor.document = new Document();
  18. modelDoc = editor.document;
  19. root = modelDoc.createRoot();
  20. command = new ToggleAttributeCommand( 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. it( 'should add attribute on selected nodes if the command value was false', () => {
  45. setData( modelDoc, '<p>a[bc<$text bold="true">fo]obar</$text>xyz</p>' );
  46. expect( command.value ).to.be.false;
  47. command._doExecute();
  48. expect( command.value ).to.be.true;
  49. expect( getData( modelDoc ) ).to.equal( '<p>a[<$text bold="true">bcfo]obar</$text>xyz</p>' );
  50. } );
  51. it( 'should remove attribute from selected nodes if the command value was true', () => {
  52. setData( modelDoc, '<p>abc[<$text bold="true">foo]bar</$text>xyz</p>' );
  53. expect( command.value ).to.be.true;
  54. command._doExecute();
  55. expect( getData( modelDoc ) ).to.equal( '<p>abc[foo]<$text bold="true">bar</$text>xyz</p>' );
  56. expect( command.value ).to.be.false;
  57. } );
  58. it( 'should add attribute on selected nodes if execute parameter was set to true', () => {
  59. setData( modelDoc, '<p>abc<$text bold="true">foob[ar</$text>x]yz</p>' );
  60. expect( command.value ).to.be.true;
  61. command._doExecute( { forceValue: true } );
  62. expect( command.value ).to.be.true;
  63. expect( getData( modelDoc ) ).to.equal( '<p>abc<$text bold="true">foob[arx</$text>]yz</p>' );
  64. } );
  65. it( 'should remove attribute on selected nodes if execute parameter was set to false', () => {
  66. setData( modelDoc, '<p>a[bc<$text bold="true">fo]obar</$text>xyz</p>' );
  67. command._doExecute( { forceValue: false } );
  68. expect( command.value ).to.be.false;
  69. expect( getData( modelDoc ) ).to.equal( '<p>a[bcfo]<$text bold="true">obar</$text>xyz</p>' );
  70. } );
  71. it( 'should change selection attribute if selection is collapsed in non-empty parent', () => {
  72. setData( modelDoc, '<p>a[]bc<$text bold="true">foobar</$text>xyz</p><p></p>' );
  73. expect( command.value ).to.be.false;
  74. command._doExecute();
  75. expect( command.value ).to.be.true;
  76. expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.true;
  77. command._doExecute();
  78. expect( command.value ).to.be.false;
  79. expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.false;
  80. } );
  81. it( 'should not store attribute change on selection if selection is collapsed in non-empty parent', () => {
  82. setData( modelDoc, '<p>a[]bc<$text bold="true">foobar</$text>xyz</p>' );
  83. command._doExecute();
  84. // It should not save that bold was executed at position ( root, [ 0, 1 ] ).
  85. // Simulate clicking right arrow key by changing selection ranges.
  86. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 2 ] ) ) ] );
  87. // Get back to previous selection.
  88. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 1 ] ) ) ] );
  89. expect( command.value ).to.be.false;
  90. } );
  91. it( 'should change selection attribute and store it if selection is collapsed in empty parent', () => {
  92. setData( modelDoc, '<p>abc<$text bold="true">foobar</$text>xyz</p><p>[]</p>' );
  93. expect( command.value ).to.be.false;
  94. command._doExecute();
  95. expect( command.value ).to.be.true;
  96. expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.true;
  97. // Attribute should be stored.
  98. // Simulate clicking somewhere else in the editor.
  99. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 2 ] ) ) ] );
  100. expect( command.value ).to.be.false;
  101. // Go back to where attribute was stored.
  102. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) ) ] );
  103. // Attribute should be restored.
  104. expect( command.value ).to.be.true;
  105. command._doExecute();
  106. expect( command.value ).to.be.false;
  107. expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.false;
  108. } );
  109. it( 'should not apply attribute change where it would invalid schema', () => {
  110. modelDoc.schema.registerItem( 'image', '$block' );
  111. setData( modelDoc, '<p>ab[c<img></img><$text bold="true">foobar</$text>xy<img></img>]z</p>' );
  112. expect( command.isEnabled ).to.be.true;
  113. command._doExecute();
  114. expect( getData( modelDoc ) )
  115. .to.equal( '<p>ab[<$text bold="true">c</$text><img></img><$text bold="true">foobarxy</$text><img></img>]z</p>' );
  116. } );
  117. it( 'should use provided batch for storing undo steps', () => {
  118. const batch = new Batch( new Document() );
  119. setData( modelDoc, '<p>a[bc<$text bold="true">fo]obar</$text>xyz</p>' );
  120. expect( batch.deltas.length ).to.equal( 0 );
  121. command._doExecute( { batch } );
  122. expect( batch.deltas.length ).to.equal( 1 );
  123. expect( getData( modelDoc ) ).to.equal( '<p>a[<$text bold="true">bcfo]obar</$text>xyz</p>' );
  124. } );
  125. describe( 'should cause firing model document changesDone event', () => {
  126. let spy;
  127. beforeEach( () => {
  128. spy = sinon.spy();
  129. } );
  130. it( 'collapsed selection in non-empty parent', () => {
  131. setData( modelDoc, '<p>x[]y</p>' );
  132. modelDoc.on( 'changesDone', spy );
  133. command._doExecute();
  134. expect( spy.calledOnce ).to.be.true;
  135. } );
  136. it( 'non-collapsed selection', () => {
  137. setData( modelDoc, '<p>[xy]</p>' );
  138. modelDoc.on( 'changesDone', spy );
  139. command._doExecute();
  140. expect( spy.calledOnce ).to.be.true;
  141. } );
  142. it( 'in empty parent', () => {
  143. setData( modelDoc, '<p>[]</p>' );
  144. modelDoc.on( 'changesDone', spy );
  145. command._doExecute();
  146. expect( spy.calledOnce ).to.be.true;
  147. } );
  148. } );
  149. } );
  150. describe( '_checkEnabled', () => {
  151. describe( '_checkEnabled', () => {
  152. // This test doesn't tests every possible case.
  153. // Method `_checkEnabled` uses `isAttributeAllowedInSelection` helper which is fully tested in his own test.
  154. beforeEach( () => {
  155. modelDoc.schema.registerItem( 'x', '$block' );
  156. modelDoc.schema.disallow( { name: '$text', inside: 'x', attributes: 'link' } );
  157. } );
  158. describe( 'when selection is collapsed', () => {
  159. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  160. setData( modelDoc, '<p>f[]oo</p>' );
  161. expect( command._checkEnabled() ).to.be.true;
  162. } );
  163. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  164. setData( modelDoc, '<x>fo[]o</x>' );
  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. setData( modelDoc, '<p>[foo]</p>' );
  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. setData( modelDoc, '<x>[foo]</x>' );
  175. expect( command._checkEnabled() ).to.be.false;
  176. } );
  177. } );
  178. } );
  179. } );
  180. } );