8
0

attributecommand.js 9.2 KB

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