8
0

attributecommand.js 11 KB

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