attributecommand.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import AttributeCommand from '../src/attributecommand';
  6. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  7. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. describe( 'AttributeCommand', () => {
  9. const attrKey = 'bold';
  10. let editor, command, model, doc, root;
  11. beforeEach( () => {
  12. return ModelTestEditor
  13. .create()
  14. .then( newEditor => {
  15. editor = newEditor;
  16. model = editor.model;
  17. doc = model.document;
  18. root = doc.getRoot();
  19. command = new AttributeCommand( editor, attrKey );
  20. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  21. model.schema.register( 'h1', { inheritAllFrom: '$block' } );
  22. model.schema.register( 'img', {
  23. allowWhere: [ '$block', '$text' ],
  24. isObject: true
  25. } );
  26. model.schema.addAttributeCheck( ( ctx, attributeName ) => {
  27. // Allow 'bold' on p>$text.
  28. if ( ctx.endsWith( 'p $text' ) && attributeName == 'bold' ) {
  29. return true;
  30. }
  31. } );
  32. } );
  33. } );
  34. afterEach( () => {
  35. command.destroy();
  36. return editor.destroy();
  37. } );
  38. describe( 'value', () => {
  39. it( 'is true when collapsed selection has the attribute', () => {
  40. model.change( writer => {
  41. writer.setSelectionAttribute( attrKey, true );
  42. } );
  43. expect( command.value ).to.be.true;
  44. } );
  45. it( 'is false when collapsed selection does not have the attribute', () => {
  46. model.change( writer => {
  47. writer.setSelectionAttribute( attrKey, true );
  48. } );
  49. model.change( writer => {
  50. writer.removeSelectionAttribute( attrKey );
  51. } );
  52. expect( command.value ).to.be.false;
  53. } );
  54. it( 'is true when the first item that allows attribute has the attribute set #1', () => {
  55. setData( model, '<p><$text bold="true">fo[o</$text></p><h1>b]ar</h1>' );
  56. expect( command.value ).to.be.true;
  57. } );
  58. it( 'is true when the first item that allows attribute has the attribute set #2', () => {
  59. setData( model, '<h1>fo[o</h1><p><$text bold="true">f</$text>o]o</p>' );
  60. expect( command.value ).to.be.true;
  61. } );
  62. it( 'is false when the first item that allows attribute does not have the attribute set #1', () => {
  63. setData( model, '<p>b[a<$text bold="true">r</$text></p><h1>fo]o</h1>' );
  64. expect( command.value ).to.be.false;
  65. } );
  66. it( 'is false when the first item that allows attribute does not have the attribute set #2', () => {
  67. setData( model, '<h1>fo[o</h1><p>b<$text bold="true">r</$text>r]</p>' );
  68. expect( command.value ).to.be.false;
  69. } );
  70. it( 'is true when the first item that allows attribute has the attribute set - object with nested editable', () => {
  71. model.schema.register( 'caption', {
  72. allowContentOf: '$block',
  73. allowIn: 'img',
  74. isLimit: true
  75. } );
  76. model.schema.extend( '$text', {
  77. allowIn: 'caption',
  78. allowAttributes: 'bold'
  79. } );
  80. setData( model, '<p>[<img><caption>Some caption inside the image.</caption></img>]</p>' );
  81. expect( command.value ).to.be.false;
  82. command.execute();
  83. expect( command.value ).to.be.true;
  84. expect( getData( model ) ).to.equal(
  85. '<p>[<img><caption><$text bold="true">Some caption inside the image.</$text></caption></img>]</p>'
  86. );
  87. } );
  88. } );
  89. describe( 'isEnabled', () => {
  90. // This test doesn't tests every possible case.
  91. // Method `refresh()` uses `checkAttributeInSelection()` which is fully tested in its own test.
  92. beforeEach( () => {
  93. model.schema.register( 'x', { inheritAllFrom: '$block' } );
  94. } );
  95. describe( 'when selection is collapsed', () => {
  96. it( 'should return true if characters with the attribute can be placed at caret position', () => {
  97. setData( model, '<p>f[]oo</p>' );
  98. expect( command.isEnabled ).to.be.true;
  99. } );
  100. it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
  101. setData( model, '<x>fo[]o</x>' );
  102. expect( command.isEnabled ).to.be.false;
  103. } );
  104. } );
  105. describe( 'when selection is not collapsed', () => {
  106. it( 'should return true if there is at least one node in selection that can have the attribute', () => {
  107. setData( model, '<p>[foo]</p>' );
  108. expect( command.isEnabled ).to.be.true;
  109. } );
  110. it( 'should return false if there are no nodes in selection that can have the attribute', () => {
  111. setData( model, '<x>[foo]</x>' );
  112. expect( command.isEnabled ).to.be.false;
  113. } );
  114. } );
  115. } );
  116. describe( 'execute()', () => {
  117. it( 'should do nothing if the command is disabled', () => {
  118. setData( model, '<p>fo[ob]ar</p>' );
  119. command.isEnabled = false;
  120. command.execute();
  121. expect( getData( model ) ).to.equal( '<p>fo[ob]ar</p>' );
  122. } );
  123. it( 'should add attribute on selected nodes if the command value was false', () => {
  124. setData( model, '<p>a[bc<$text bold="true">fo]obar</$text>xyz</p>' );
  125. expect( command.value ).to.be.false;
  126. command.execute();
  127. expect( command.value ).to.be.true;
  128. expect( getData( model ) ).to.equal( '<p>a[<$text bold="true">bcfo]obar</$text>xyz</p>' );
  129. } );
  130. it( 'should remove attribute from selected nodes if the command value was true', () => {
  131. setData( model, '<p>abc[<$text bold="true">foo]bar</$text>xyz</p>' );
  132. expect( command.value ).to.be.true;
  133. command.execute();
  134. expect( getData( model ) ).to.equal( '<p>abc[foo]<$text bold="true">bar</$text>xyz</p>' );
  135. expect( command.value ).to.be.false;
  136. } );
  137. it( 'should add attribute on selected nodes if execute parameter was set to true', () => {
  138. setData( model, '<p>abc<$text bold="true">foob[ar</$text>x]yz</p>' );
  139. expect( command.value ).to.be.true;
  140. command.execute( { forceValue: true } );
  141. expect( command.value ).to.be.true;
  142. expect( getData( model ) ).to.equal( '<p>abc<$text bold="true">foob[arx</$text>]yz</p>' );
  143. } );
  144. it( 'should remove attribute on selected nodes if execute parameter was set to false', () => {
  145. setData( model, '<p>a[bc<$text bold="true">fo]obar</$text>xyz</p>' );
  146. command.execute( { forceValue: false } );
  147. expect( command.value ).to.be.false;
  148. expect( getData( model ) ).to.equal( '<p>a[bcfo]<$text bold="true">obar</$text>xyz</p>' );
  149. } );
  150. it( 'should change selection attribute if selection is collapsed in non-empty parent', () => {
  151. setData( model, '<p>a[]bc<$text bold="true">foobar</$text>xyz</p><p></p>' );
  152. expect( command.value ).to.be.false;
  153. command.execute();
  154. expect( command.value ).to.be.true;
  155. expect( doc.selection.hasAttribute( 'bold' ) ).to.be.true;
  156. command.execute();
  157. expect( command.value ).to.be.false;
  158. expect( doc.selection.hasAttribute( 'bold' ) ).to.be.false;
  159. } );
  160. it( 'should not store attribute change on selection if selection is collapsed in non-empty parent', () => {
  161. setData( model, '<p>a[]bc<$text bold="true">foobar</$text>xyz</p>' );
  162. command.execute();
  163. // It should not save that bold was executed at position ( root, [ 0, 1 ] ).
  164. model.change( writer => {
  165. // Simulate clicking right arrow key by changing selection ranges.
  166. writer.setSelection( writer.createRange(
  167. writer.createPositionAt( root.getNodeByPath( [ 0 ] ), 2 )
  168. ) );
  169. // Get back to previous selection.
  170. writer.setSelection( writer.createRange(
  171. writer.createPositionAt( root.getNodeByPath( [ 0 ] ), 1 )
  172. ) );
  173. } );
  174. expect( command.value ).to.be.false;
  175. } );
  176. it( 'should change selection attribute and store it if selection is collapsed in empty parent', () => {
  177. setData( model, '<p>abc<$text bold="true">foobar</$text>xyz</p><p>[]</p>' );
  178. expect( command.value ).to.be.false;
  179. command.execute();
  180. expect( command.value ).to.be.true;
  181. expect( doc.selection.hasAttribute( 'bold' ) ).to.be.true;
  182. // Attribute should be stored.
  183. // Simulate clicking somewhere else in the editor.
  184. model.change( writer => {
  185. writer.setSelection( root.getNodeByPath( [ 0 ] ), 2 );
  186. } );
  187. expect( command.value ).to.be.false;
  188. // Go back to where attribute was stored.
  189. model.change( writer => {
  190. writer.setSelection( root.getNodeByPath( [ 1 ] ), 0 );
  191. } );
  192. // Attribute should be restored.
  193. expect( command.value ).to.be.true;
  194. command.execute();
  195. expect( command.value ).to.be.false;
  196. expect( doc.selection.hasAttribute( 'bold' ) ).to.be.false;
  197. } );
  198. it( 'should not apply attribute change where it would invalid schema', () => {
  199. model.schema.register( 'image', { inheritAllFrom: '$block' } );
  200. setData( model, '<p>ab[c<img></img><$text bold="true">foobar</$text>xy<img></img>]z</p>' );
  201. expect( command.isEnabled ).to.be.true;
  202. command.execute();
  203. expect( getData( model ) )
  204. .to.equal( '<p>ab[<$text bold="true">c</$text><img></img><$text bold="true">foobarxy</$text><img></img>]z</p>' );
  205. } );
  206. it( 'should use parent batch for storing undo steps', () => {
  207. setData( model, '<p>a[bc<$text bold="true">fo]obar</$text>xyz</p>' );
  208. model.change( writer => {
  209. expect( writer.batch.operations.length ).to.equal( 0 );
  210. command.execute();
  211. expect( writer.batch.operations.length ).to.equal( 1 );
  212. } );
  213. expect( getData( model ) ).to.equal( '<p>a[<$text bold="true">bcfo]obar</$text>xyz</p>' );
  214. } );
  215. describe( 'should cause firing model change event', () => {
  216. let spy;
  217. beforeEach( () => {
  218. spy = sinon.spy();
  219. } );
  220. it( 'collapsed selection in non-empty parent', () => {
  221. setData( model, '<p>x[]y</p>' );
  222. model.document.on( 'change', spy );
  223. command.execute();
  224. expect( spy.called ).to.be.true;
  225. } );
  226. it( 'non-collapsed selection', () => {
  227. setData( model, '<p>[xy]</p>' );
  228. model.document.on( 'change', spy );
  229. command.execute();
  230. expect( spy.called ).to.be.true;
  231. } );
  232. it( 'in empty parent', () => {
  233. setData( model, '<p>[]</p>' );
  234. model.document.on( 'change', spy );
  235. command.execute();
  236. expect( spy.called ).to.be.true;
  237. } );
  238. } );
  239. } );
  240. } );