8
0

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 Range from '@ckeditor/ckeditor5-engine/src/model/range';
  8. import Position from '@ckeditor/ckeditor5-engine/src/model/position';
  9. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. describe( 'AttributeCommand', () => {
  11. const attrKey = 'bold';
  12. let editor, command, model, doc, root;
  13. beforeEach( () => {
  14. return ModelTestEditor
  15. .create()
  16. .then( newEditor => {
  17. editor = newEditor;
  18. model = editor.model;
  19. doc = model.document;
  20. root = doc.getRoot();
  21. command = new AttributeCommand( editor, attrKey );
  22. model.schema.registerItem( 'p', '$block' );
  23. model.schema.registerItem( 'h1', '$block' );
  24. model.schema.registerItem( 'img', '$inline' );
  25. model.schema.objects.add( 'img' );
  26. // Allow block in "root" (DIV)
  27. model.schema.allow( { name: '$block', inside: '$root' } );
  28. // Bold text is allowed only in P.
  29. model.schema.allow( { name: '$text', attributes: 'bold', inside: 'p' } );
  30. model.schema.allow( { name: 'p', attributes: 'bold', inside: '$root' } );
  31. // Disallow bold on image.
  32. model.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. model.change( () => {
  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. model.change( () => {
  48. doc.selection.removeAttribute( attrKey );
  49. } );
  50. expect( command.value ).to.be.false;
  51. } );
  52. // See https://github.com/ckeditor/ckeditor5-core/issues/73#issuecomment-311572827.
  53. it( 'is false when selection contains object with nested editable', () => {
  54. model.schema.registerItem( 'caption', '$block' );
  55. model.schema.allow( { name: '$inline', inside: 'caption' } );
  56. model.schema.allow( { name: 'caption', inside: 'img' } );
  57. model.schema.allow( { name: '$text', attributes: 'bold', inside: 'caption' } );
  58. setData( model, '<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( model ) ).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. model.schema.registerItem( 'x', '$block' );
  72. model.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( model, '<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( model, '<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( model, '<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( model, '<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( model, '<p>fo[ob]ar</p>' );
  98. command.isEnabled = false;
  99. command.execute();
  100. expect( getData( model ) ).to.equal( '<p>fo[ob]ar</p>' );
  101. } );
  102. it( 'should add attribute on selected nodes if the command value was false', () => {
  103. setData( model, '<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( model ) ).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( model, '<p>abc[<$text bold="true">foo]bar</$text>xyz</p>' );
  111. expect( command.value ).to.be.true;
  112. command.execute();
  113. expect( getData( model ) ).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( model, '<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( model ) ).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( model, '<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( model ) ).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( model, '<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( model, '<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. model.change( () => {
  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( model, '<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. model.change( () => {
  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. model.change( () => {
  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. model.schema.registerItem( 'image', '$block' );
  175. setData( model, '<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( model ) )
  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 parent batch for storing undo steps', () => {
  182. setData( model, '<p>a[bc<$text bold="true">fo]obar</$text>xyz</p>' );
  183. model.change( writer => {
  184. expect( writer.batch.deltas.length ).to.equal( 0 );
  185. command.execute();
  186. expect( writer.batch.deltas.length ).to.equal( 1 );
  187. } );
  188. expect( getData( model ) ).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( model, '<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( model, '<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( model, '<p>[]</p>' );
  209. doc.on( 'changesDone', spy );
  210. command.execute();
  211. expect( spy.calledOnce ).to.be.true;
  212. } );
  213. } );
  214. } );
  215. } );