attributecommand.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import AttributeCommand from '../src/attributecommand';
  6. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  7. import Document from '@ckeditor/ckeditor5-engine/src/model/document';
  8. import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
  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( 'AttributeCommand', () => {
  13. const attrKey = 'bold';
  14. let editor, command, doc, root;
  15. beforeEach( () => {
  16. return ModelTestEditor
  17. .create()
  18. .then( newEditor => {
  19. editor = newEditor;
  20. doc = editor.document;
  21. root = doc.getRoot();
  22. command = new AttributeCommand( editor, attrKey );
  23. doc.schema.registerItem( 'p', '$block' );
  24. doc.schema.registerItem( 'h1', '$block' );
  25. doc.schema.registerItem( 'img', '$inline' );
  26. doc.schema.objects.add( 'img' );
  27. // Allow block in "root" (DIV)
  28. doc.schema.allow( { name: '$block', inside: '$root' } );
  29. // Bold text is allowed only in P.
  30. doc.schema.allow( { name: '$text', attributes: 'bold', inside: 'p' } );
  31. doc.schema.allow( { name: 'p', attributes: 'bold', inside: '$root' } );
  32. // Disallow bold on image.
  33. doc.schema.disallow( { name: 'img', attributes: 'bold', inside: '$root' } );
  34. } );
  35. } );
  36. afterEach( () => {
  37. command.destroy();
  38. return editor.destroy();
  39. } );
  40. describe( 'value', () => {
  41. it( 'is true when selection has the attribute', () => {
  42. doc.enqueueChanges( () => {
  43. doc.selection.setAttribute( attrKey, true );
  44. } );
  45. expect( command.value ).to.be.true;
  46. } );
  47. it( 'is false when selection does not have the attribute', () => {
  48. doc.enqueueChanges( () => {
  49. doc.selection.removeAttribute( attrKey );
  50. } );
  51. expect( command.value ).to.be.false;
  52. } );
  53. // See https://github.com/ckeditor/ckeditor5-core/issues/73#issuecomment-311572827.
  54. it( 'is false when selection contains object with nested editable', () => {
  55. doc.schema.registerItem( 'caption', '$block' );
  56. doc.schema.allow( { name: '$inline', inside: 'caption' } );
  57. doc.schema.allow( { name: 'caption', inside: 'img' } );
  58. doc.schema.allow( { name: '$text', attributes: 'bold', inside: 'caption' } );
  59. setData( doc, '<p>[<img><caption>Some caption inside the image.</caption></img>]</p>' );
  60. expect( command.value ).to.be.false;
  61. command.execute();
  62. expect( command.value ).to.be.false;
  63. expect( getData( doc ) ).to.equal(
  64. '<p>[<img><caption><$text bold="true">Some caption inside the image.</$text></caption></img>]</p>'
  65. );
  66. } );
  67. } );
  68. describe( 'isEnabled', () => {
  69. // This test doesn't tests every possible case.
  70. // Method `refresh()` uses `checkAttributeInSelection()` which is fully tested in its own test.
  71. beforeEach( () => {
  72. doc.schema.registerItem( 'x', '$block' );
  73. doc.schema.disallow( { name: '$text', inside: 'x', attributes: 'link' } );
  74. } );
  75. describe( 'when selection is collapsed', () => {
  76. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  77. setData( doc, '<p>f[]oo</p>' );
  78. expect( command.isEnabled ).to.be.true;
  79. } );
  80. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  81. setData( doc, '<x>fo[]o</x>' );
  82. expect( command.isEnabled ).to.be.false;
  83. } );
  84. } );
  85. describe( 'when selection is not collapsed', () => {
  86. it( 'should return true if there is at least one node in selection that can have the attribute', () => {
  87. setData( doc, '<p>[foo]</p>' );
  88. expect( command.isEnabled ).to.be.true;
  89. } );
  90. it( 'should return false if there are no nodes in selection that can have the attribute', () => {
  91. setData( doc, '<x>[foo]</x>' );
  92. expect( command.isEnabled ).to.be.false;
  93. } );
  94. } );
  95. } );
  96. describe( 'execute()', () => {
  97. it( 'should do nothing if the command is disabled', () => {
  98. setData( doc, '<p>fo[ob]ar</p>' );
  99. command.isEnabled = false;
  100. command.execute();
  101. expect( getData( doc ) ).to.equal( '<p>fo[ob]ar</p>' );
  102. } );
  103. it( 'should add attribute on selected nodes if the command value was false', () => {
  104. setData( doc, '<p>a[bc<$text bold="true">fo]obar</$text>xyz</p>' );
  105. expect( command.value ).to.be.false;
  106. command.execute();
  107. expect( command.value ).to.be.true;
  108. expect( getData( doc ) ).to.equal( '<p>a[<$text bold="true">bcfo]obar</$text>xyz</p>' );
  109. } );
  110. it( 'should remove attribute from selected nodes if the command value was true', () => {
  111. setData( doc, '<p>abc[<$text bold="true">foo]bar</$text>xyz</p>' );
  112. expect( command.value ).to.be.true;
  113. command.execute();
  114. expect( getData( doc ) ).to.equal( '<p>abc[foo]<$text bold="true">bar</$text>xyz</p>' );
  115. expect( command.value ).to.be.false;
  116. } );
  117. it( 'should add attribute on selected nodes if execute parameter was set to true', () => {
  118. setData( doc, '<p>abc<$text bold="true">foob[ar</$text>x]yz</p>' );
  119. expect( command.value ).to.be.true;
  120. command.execute( { forceValue: true } );
  121. expect( command.value ).to.be.true;
  122. expect( getData( doc ) ).to.equal( '<p>abc<$text bold="true">foob[arx</$text>]yz</p>' );
  123. } );
  124. it( 'should remove attribute on selected nodes if execute parameter was set to false', () => {
  125. setData( doc, '<p>a[bc<$text bold="true">fo]obar</$text>xyz</p>' );
  126. command.execute( { forceValue: false } );
  127. expect( command.value ).to.be.false;
  128. expect( getData( doc ) ).to.equal( '<p>a[bcfo]<$text bold="true">obar</$text>xyz</p>' );
  129. } );
  130. it( 'should change selection attribute if selection is collapsed in non-empty parent', () => {
  131. setData( doc, '<p>a[]bc<$text bold="true">foobar</$text>xyz</p><p></p>' );
  132. expect( command.value ).to.be.false;
  133. command.execute();
  134. expect( command.value ).to.be.true;
  135. expect( doc.selection.hasAttribute( 'bold' ) ).to.be.true;
  136. command.execute();
  137. expect( command.value ).to.be.false;
  138. expect( doc.selection.hasAttribute( 'bold' ) ).to.be.false;
  139. } );
  140. it( 'should not store attribute change on selection if selection is collapsed in non-empty parent', () => {
  141. setData( doc, '<p>a[]bc<$text bold="true">foobar</$text>xyz</p>' );
  142. command.execute();
  143. // It should not save that bold was executed at position ( root, [ 0, 1 ] ).
  144. doc.enqueueChanges( () => {
  145. // Simulate clicking right arrow key by changing selection ranges.
  146. doc.selection.setRanges( [ new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 2 ] ) ) ] );
  147. // Get back to previous selection.
  148. doc.selection.setRanges( [ new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 1 ] ) ) ] );
  149. } );
  150. expect( command.value ).to.be.false;
  151. } );
  152. it( 'should change selection attribute and store it if selection is collapsed in empty parent', () => {
  153. setData( doc, '<p>abc<$text bold="true">foobar</$text>xyz</p><p>[]</p>' );
  154. expect( command.value ).to.be.false;
  155. command.execute();
  156. expect( command.value ).to.be.true;
  157. expect( doc.selection.hasAttribute( 'bold' ) ).to.be.true;
  158. // Attribute should be stored.
  159. // Simulate clicking somewhere else in the editor.
  160. doc.enqueueChanges( () => {
  161. doc.selection.setRanges( [ new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 2 ] ) ) ] );
  162. } );
  163. expect( command.value ).to.be.false;
  164. // Go back to where attribute was stored.
  165. doc.enqueueChanges( () => {
  166. doc.selection.setRanges( [ new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) ) ] );
  167. } );
  168. // Attribute should be restored.
  169. expect( command.value ).to.be.true;
  170. command.execute();
  171. expect( command.value ).to.be.false;
  172. expect( doc.selection.hasAttribute( 'bold' ) ).to.be.false;
  173. } );
  174. it( 'should not apply attribute change where it would invalid schema', () => {
  175. doc.schema.registerItem( 'image', '$block' );
  176. setData( doc, '<p>ab[c<img></img><$text bold="true">foobar</$text>xy<img></img>]z</p>' );
  177. expect( command.isEnabled ).to.be.true;
  178. command.execute();
  179. expect( getData( doc ) )
  180. .to.equal( '<p>ab[<$text bold="true">c</$text><img></img><$text bold="true">foobarxy</$text><img></img>]z</p>' );
  181. } );
  182. it( 'should use provided batch for storing undo steps', () => {
  183. const batch = new Batch( new Document() );
  184. setData( doc, '<p>a[bc<$text bold="true">fo]obar</$text>xyz</p>' );
  185. expect( batch.deltas.length ).to.equal( 0 );
  186. command.execute( { batch } );
  187. expect( batch.deltas.length ).to.equal( 1 );
  188. expect( getData( doc ) ).to.equal( '<p>a[<$text bold="true">bcfo]obar</$text>xyz</p>' );
  189. } );
  190. describe( 'should cause firing model document changesDone event', () => {
  191. let spy;
  192. beforeEach( () => {
  193. spy = sinon.spy();
  194. } );
  195. it( 'collapsed selection in non-empty parent', () => {
  196. setData( doc, '<p>x[]y</p>' );
  197. doc.on( 'changesDone', spy );
  198. command.execute();
  199. expect( spy.calledOnce ).to.be.true;
  200. } );
  201. it( 'non-collapsed selection', () => {
  202. setData( doc, '<p>[xy]</p>' );
  203. doc.on( 'changesDone', spy );
  204. command.execute();
  205. expect( spy.calledOnce ).to.be.true;
  206. } );
  207. it( 'in empty parent', () => {
  208. setData( doc, '<p>[]</p>' );
  209. doc.on( 'changesDone', spy );
  210. command.execute();
  211. expect( spy.calledOnce ).to.be.true;
  212. } );
  213. } );
  214. } );
  215. } );