toggleattributecommand.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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( 'is true when selection has the attribute', () => {
  37. modelDoc.enqueueChanges( () => {
  38. modelDoc.selection.setAttribute( attrKey, true );
  39. } );
  40. expect( command.value ).to.be.true;
  41. } );
  42. it( 'is false when selection does not have the attribute', () => {
  43. modelDoc.enqueueChanges( () => {
  44. modelDoc.selection.removeAttribute( attrKey );
  45. } );
  46. expect( command.value ).to.be.false;
  47. } );
  48. } );
  49. describe( 'isEnabled', () => {
  50. // This test doesn't tests every possible case.
  51. // Method `refresh()` uses `isAttributeAllowedInSelection` helper which is fully tested in his own test.
  52. beforeEach( () => {
  53. modelDoc.schema.registerItem( 'x', '$block' );
  54. modelDoc.schema.disallow( { name: '$text', inside: 'x', attributes: 'link' } );
  55. } );
  56. describe( 'when selection is collapsed', () => {
  57. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  58. setData( modelDoc, '<p>f[]oo</p>' );
  59. expect( command.isEnabled ).to.be.true;
  60. } );
  61. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  62. setData( modelDoc, '<x>fo[]o</x>' );
  63. expect( command.isEnabled ).to.be.false;
  64. } );
  65. } );
  66. describe( 'when selection is not collapsed', () => {
  67. it( 'should return true if there is at least one node in selection that can have the attribute', () => {
  68. setData( modelDoc, '<p>[foo]</p>' );
  69. expect( command.isEnabled ).to.be.true;
  70. } );
  71. it( 'should return false if there are no nodes in selection that can have the attribute', () => {
  72. setData( modelDoc, '<x>[foo]</x>' );
  73. expect( command.isEnabled ).to.be.false;
  74. } );
  75. } );
  76. } );
  77. describe( 'execute()', () => {
  78. it( 'should do nothing if the command is disabled', () => {
  79. setData( modelDoc, '<p>fo[ob]ar</p>' );
  80. command.isEnabled = false;
  81. command.execute();
  82. expect( getData( modelDoc ) ).to.equal( '<p>fo[ob]ar</p>' );
  83. } );
  84. it( 'should add attribute on selected nodes if the command value was false', () => {
  85. setData( modelDoc, '<p>a[bc<$text bold="true">fo]obar</$text>xyz</p>' );
  86. expect( command.value ).to.be.false;
  87. command.execute();
  88. expect( command.value ).to.be.true;
  89. expect( getData( modelDoc ) ).to.equal( '<p>a[<$text bold="true">bcfo]obar</$text>xyz</p>' );
  90. } );
  91. it( 'should remove attribute from selected nodes if the command value was true', () => {
  92. setData( modelDoc, '<p>abc[<$text bold="true">foo]bar</$text>xyz</p>' );
  93. expect( command.value ).to.be.true;
  94. command.execute();
  95. expect( getData( modelDoc ) ).to.equal( '<p>abc[foo]<$text bold="true">bar</$text>xyz</p>' );
  96. expect( command.value ).to.be.false;
  97. } );
  98. it( 'should add attribute on selected nodes if execute parameter was set to true', () => {
  99. setData( modelDoc, '<p>abc<$text bold="true">foob[ar</$text>x]yz</p>' );
  100. expect( command.value ).to.be.true;
  101. command.execute( { forceValue: true } );
  102. expect( command.value ).to.be.true;
  103. expect( getData( modelDoc ) ).to.equal( '<p>abc<$text bold="true">foob[arx</$text>]yz</p>' );
  104. } );
  105. it( 'should remove attribute on selected nodes if execute parameter was set to false', () => {
  106. setData( modelDoc, '<p>a[bc<$text bold="true">fo]obar</$text>xyz</p>' );
  107. command.execute( { forceValue: false } );
  108. expect( command.value ).to.be.false;
  109. expect( getData( modelDoc ) ).to.equal( '<p>a[bcfo]<$text bold="true">obar</$text>xyz</p>' );
  110. } );
  111. it( 'should change selection attribute if selection is collapsed in non-empty parent', () => {
  112. setData( modelDoc, '<p>a[]bc<$text bold="true">foobar</$text>xyz</p><p></p>' );
  113. expect( command.value ).to.be.false;
  114. command.execute();
  115. expect( command.value ).to.be.true;
  116. expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.true;
  117. command.execute();
  118. expect( command.value ).to.be.false;
  119. expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.false;
  120. } );
  121. it( 'should not store attribute change on selection if selection is collapsed in non-empty parent', () => {
  122. setData( modelDoc, '<p>a[]bc<$text bold="true">foobar</$text>xyz</p>' );
  123. command.execute();
  124. // It should not save that bold was executed at position ( root, [ 0, 1 ] ).
  125. modelDoc.enqueueChanges( () => {
  126. // Simulate clicking right arrow key by changing selection ranges.
  127. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 2 ] ) ) ] );
  128. // Get back to previous selection.
  129. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 1 ] ) ) ] );
  130. } );
  131. expect( command.value ).to.be.false;
  132. } );
  133. it( 'should change selection attribute and store it if selection is collapsed in empty parent', () => {
  134. setData( modelDoc, '<p>abc<$text bold="true">foobar</$text>xyz</p><p>[]</p>' );
  135. expect( command.value ).to.be.false;
  136. command.execute();
  137. expect( command.value ).to.be.true;
  138. expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.true;
  139. // Attribute should be stored.
  140. // Simulate clicking somewhere else in the editor.
  141. modelDoc.enqueueChanges( () => {
  142. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 2 ] ) ) ] );
  143. } );
  144. expect( command.value ).to.be.false;
  145. // Go back to where attribute was stored.
  146. modelDoc.enqueueChanges( () => {
  147. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) ) ] );
  148. } );
  149. // Attribute should be restored.
  150. expect( command.value ).to.be.true;
  151. command.execute();
  152. expect( command.value ).to.be.false;
  153. expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.false;
  154. } );
  155. it( 'should not apply attribute change where it would invalid schema', () => {
  156. modelDoc.schema.registerItem( 'image', '$block' );
  157. setData( modelDoc, '<p>ab[c<img></img><$text bold="true">foobar</$text>xy<img></img>]z</p>' );
  158. expect( command.isEnabled ).to.be.true;
  159. command.execute();
  160. expect( getData( modelDoc ) )
  161. .to.equal( '<p>ab[<$text bold="true">c</$text><img></img><$text bold="true">foobarxy</$text><img></img>]z</p>' );
  162. } );
  163. it( 'should use provided batch for storing undo steps', () => {
  164. const batch = new Batch( new Document() );
  165. setData( modelDoc, '<p>a[bc<$text bold="true">fo]obar</$text>xyz</p>' );
  166. expect( batch.deltas.length ).to.equal( 0 );
  167. command.execute( { batch } );
  168. expect( batch.deltas.length ).to.equal( 1 );
  169. expect( getData( modelDoc ) ).to.equal( '<p>a[<$text bold="true">bcfo]obar</$text>xyz</p>' );
  170. } );
  171. describe( 'should cause firing model document changesDone event', () => {
  172. let spy;
  173. beforeEach( () => {
  174. spy = sinon.spy();
  175. } );
  176. it( 'collapsed selection in non-empty parent', () => {
  177. setData( modelDoc, '<p>x[]y</p>' );
  178. modelDoc.on( 'changesDone', spy );
  179. command.execute();
  180. expect( spy.calledOnce ).to.be.true;
  181. } );
  182. it( 'non-collapsed selection', () => {
  183. setData( modelDoc, '<p>[xy]</p>' );
  184. modelDoc.on( 'changesDone', spy );
  185. command.execute();
  186. expect( spy.calledOnce ).to.be.true;
  187. } );
  188. it( 'in empty parent', () => {
  189. setData( modelDoc, '<p>[]</p>' );
  190. modelDoc.on( 'changesDone', spy );
  191. command.execute();
  192. expect( spy.calledOnce ).to.be.true;
  193. } );
  194. } );
  195. } );
  196. } );