toggleattributecommand.js 9.1 KB

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