8
0

attributecommand.js 8.6 KB

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