attributecommand.js 9.1 KB

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