fontcommand.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 FontCommand from '../src/fontcommand';
  6. import Command from '@ckeditor/ckeditor5-core/src/command';
  7. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  8. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. describe( 'FontCommand', () => {
  10. let editor, model, doc, root, command;
  11. beforeEach( () => {
  12. return ModelTestEditor.create()
  13. .then( newEditor => {
  14. editor = newEditor;
  15. model = editor.model;
  16. doc = model.document;
  17. root = doc.getRoot();
  18. command = new FontCommand( editor, 'font' );
  19. editor.commands.add( 'font', command );
  20. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  21. model.schema.register( 'img', {
  22. allowWhere: [ '$block', '$text' ],
  23. isObject: true
  24. } );
  25. model.schema.extend( '$text', { allowAttributes: 'font' } );
  26. } );
  27. } );
  28. afterEach( () => {
  29. editor.destroy();
  30. } );
  31. it( 'is a command', () => {
  32. expect( FontCommand.prototype ).to.be.instanceOf( Command );
  33. expect( command ).to.be.instanceOf( Command );
  34. } );
  35. describe( 'value', () => {
  36. it( 'is set to font value when selection is in text with font attribute', () => {
  37. setData( model, '<paragraph><$text font="foo">fo[]o</$text></paragraph>' );
  38. expect( command ).to.have.property( 'value', 'foo' );
  39. } );
  40. it( 'is undefined when selection is not in text with font attribute', () => {
  41. setData( model, '<paragraph>fo[]o</paragraph>' );
  42. expect( command ).to.have.property( 'value', undefined );
  43. } );
  44. } );
  45. describe( 'isEnabled', () => {
  46. it( 'is true when selection is on text which can have font added', () => {
  47. setData( model, '<paragraph>fo[]o</paragraph>' );
  48. expect( command ).to.have.property( 'isEnabled', true );
  49. } );
  50. } );
  51. describe( 'execute()', () => {
  52. it( 'should do nothing if the command is disabled', () => {
  53. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  54. command.isEnabled = false;
  55. command.execute( { value: 'foo' } );
  56. expect( getData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  57. } );
  58. it( 'should add font attribute on selected text', () => {
  59. setData( model, '<paragraph>a[bc<$text font="foo">fo]obar</$text>xyz</paragraph>' );
  60. expect( command.value ).to.be.undefined;
  61. command.execute( { value: 'foo' } );
  62. expect( command.value ).to.equal( 'foo' );
  63. expect( getData( model ) ).to.equal( '<paragraph>a[<$text font="foo">bcfo]obar</$text>xyz</paragraph>' );
  64. } );
  65. it( 'should add font attribute on selected nodes (multiple nodes)', () => {
  66. setData(
  67. model,
  68. '<paragraph>abcabc[abc</paragraph>' +
  69. '<paragraph>foofoofoo</paragraph>' +
  70. '<paragraph>barbar]bar</paragraph>'
  71. );
  72. command.execute( { value: 'foo' } );
  73. expect( command.value ).to.equal( 'foo' );
  74. expect( getData( model ) ).to.equal(
  75. '<paragraph>abcabc[<$text font="foo">abc</$text></paragraph>' +
  76. '<paragraph><$text font="foo">foofoofoo</$text></paragraph>' +
  77. '<paragraph><$text font="foo">barbar</$text>]bar</paragraph>'
  78. );
  79. } );
  80. it( 'should change font attribute on selected nodes', () => {
  81. setData(
  82. model,
  83. '<paragraph>abc[abc<$text font="text-small">abc</$text></paragraph>' +
  84. '<paragraph><$text font="text-small">foofoofoo</$text></paragraph>' +
  85. '<paragraph><$text font="text-small">bar]bar</$text>bar</paragraph>'
  86. );
  87. command.execute( { value: 'foo' } );
  88. expect( command.value ).to.equal( 'foo' );
  89. expect( getData( model ) ).to.equal(
  90. '<paragraph>abc[<$text font="foo">abcabc</$text></paragraph>' +
  91. '<paragraph><$text font="foo">foofoofoo</$text></paragraph>' +
  92. '<paragraph><$text font="foo">bar</$text>]<$text font="text-small">bar</$text>bar</paragraph>'
  93. );
  94. } );
  95. it( 'should remove font attribute on selected nodes when passing undefined value', () => {
  96. setData(
  97. model,
  98. '<paragraph>abcabc[<$text font="foo">abc</$text></paragraph>' +
  99. '<paragraph><$text font="foo">foofoofoo</$text></paragraph>' +
  100. '<paragraph><$text font="foo">barbar</$text>]bar</paragraph>'
  101. );
  102. expect( command.value ).to.equal( 'foo' );
  103. command.execute();
  104. expect( command.value ).to.be.undefined;
  105. expect( getData( model ) ).to.equal(
  106. '<paragraph>abcabc[abc</paragraph>' +
  107. '<paragraph>foofoofoo</paragraph>' +
  108. '<paragraph>barbar]bar</paragraph>'
  109. );
  110. } );
  111. it( 'should change selection attribute if selection is collapsed in non-empty parent', () => {
  112. setData( model, '<paragraph>a[]bc<$text font="foo">foobar</$text>xyz</paragraph><paragraph></paragraph>' );
  113. expect( command.value ).to.be.undefined;
  114. command.execute( { value: 'foo' } );
  115. expect( command.value ).to.equal( 'foo' );
  116. expect( doc.selection.hasAttribute( 'font' ) ).to.be.true;
  117. command.execute();
  118. expect( command.value ).to.be.undefined;
  119. expect( doc.selection.hasAttribute( 'font' ) ).to.be.false;
  120. } );
  121. it( 'should not store attribute change on selection if selection is collapsed in non-empty parent', () => {
  122. setData( model, '<paragraph>a[]bc<$text font="foo">foobar</$text>xyz</paragraph>' );
  123. command.execute( { value: 'foo' } );
  124. // It should not save that bold was executed at position ( root, [ 0, 1 ] ).
  125. model.change( writer => {
  126. // Simulate clicking right arrow key by changing selection ranges.
  127. writer.setSelection( root.getNodeByPath( [ 0 ] ), 2 );
  128. // Get back to previous selection.
  129. writer.setSelection( root.getNodeByPath( [ 0 ] ), 1 );
  130. } );
  131. expect( command.value ).to.be.undefined;
  132. } );
  133. it( 'should change selection attribute and store it if selection is collapsed in empty parent', () => {
  134. setData( model, '<paragraph>abc<$text font="foo">foobar</$text>xyz</paragraph><paragraph>[]</paragraph>' );
  135. expect( command.value ).to.be.undefined;
  136. command.execute( { value: 'foo' } );
  137. expect( command.value ).to.equal( 'foo' );
  138. expect( doc.selection.hasAttribute( 'font' ) ).to.be.true;
  139. // Attribute should be stored.
  140. // Simulate clicking somewhere else in the editor.
  141. model.change( writer => {
  142. writer.setSelection( root.getNodeByPath( [ 0 ] ), 2 );
  143. } );
  144. expect( command.value ).to.be.undefined;
  145. // Go back to where attribute was stored.
  146. model.change( writer => {
  147. writer.setSelection( root.getNodeByPath( [ 1 ] ), 0 );
  148. } );
  149. // Attribute should be restored.
  150. expect( command.value ).to.equal( 'foo' );
  151. command.execute();
  152. expect( command.value ).to.be.undefined;
  153. expect( doc.selection.hasAttribute( 'font' ) ).to.be.false;
  154. } );
  155. it( 'should not apply attribute change where it would invalid schema', () => {
  156. model.schema.register( 'image', { inheritAllFrom: '$block' } );
  157. setData( model, '<paragraph>ab[c<img></img><$text font="foo">foobar</$text>xy<img></img>]z</paragraph>' );
  158. expect( command.isEnabled ).to.be.true;
  159. command.execute( { value: 'foo' } );
  160. expect( getData( model ) ).to.equal(
  161. '<paragraph>ab[<$text font="foo">c</$text><img></img><$text font="foo">foobarxy</$text><img></img>]z</paragraph>'
  162. );
  163. } );
  164. it( 'should use parent batch for storing undo steps', () => {
  165. setData( model, '<paragraph>a[bc<$text font="foo">fo]obar</$text>xyz</paragraph>' );
  166. model.change( writer => {
  167. expect( writer.batch.operations.length ).to.equal( 0 );
  168. command.execute( { value: 'foo' } );
  169. expect( writer.batch.operations.length ).to.equal( 1 );
  170. } );
  171. expect( getData( model ) ).to.equal( '<paragraph>a[<$text font="foo">bcfo]obar</$text>xyz</paragraph>' );
  172. } );
  173. describe( 'should cause firing model change event', () => {
  174. let spy;
  175. beforeEach( () => {
  176. spy = sinon.spy();
  177. } );
  178. it( 'collapsed selection in non-empty parent', () => {
  179. setData( model, '<paragraph>x[]y</paragraph>' );
  180. model.document.on( 'change', spy );
  181. command.execute( { value: 'foo' } );
  182. expect( spy.called ).to.be.true;
  183. } );
  184. it( 'non-collapsed selection', () => {
  185. setData( model, '<paragraph>[xy]</paragraph>' );
  186. model.document.on( 'change', spy );
  187. command.execute( { value: 'foo' } );
  188. expect( spy.called ).to.be.true;
  189. } );
  190. it( 'in empty parent', () => {
  191. setData( model, '<paragraph>[]</paragraph>' );
  192. model.document.on( 'change', spy );
  193. command.execute( { value: 'foo' } );
  194. expect( spy.called ).to.be.true;
  195. } );
  196. } );
  197. } );
  198. } );