attributecommand.js 10.0 KB

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