fontcommand.js 8.6 KB

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