attributecommand.js 9.3 KB

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