attributecommand.js 9.3 KB

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