alignmentcommand.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 AlignmentCommand from '../src/alignmentcommand';
  6. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import Command from '@ckeditor/ckeditor5-core/src/command';
  8. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  9. describe( 'AlignmentCommand', () => {
  10. let editor, model, command;
  11. beforeEach( () => {
  12. return ModelTestEditor.create()
  13. .then( newEditor => {
  14. model = newEditor.model;
  15. command = new AlignmentCommand( newEditor );
  16. editor = newEditor;
  17. editor.commands.add( 'alignment', command );
  18. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  19. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  20. model.schema.extend( 'paragraph', { allowAttributes: 'alignment' } );
  21. } );
  22. } );
  23. afterEach( () => {
  24. editor.destroy();
  25. } );
  26. it( 'is a command', () => {
  27. expect( AlignmentCommand.prototype ).to.be.instanceOf( Command );
  28. expect( command ).to.be.instanceOf( Command );
  29. } );
  30. describe( 'value', () => {
  31. it( 'is set to block alignment when selection is in block that has alignment attribute set', () => {
  32. setModelData( model, '<paragraph alignment="center">x[]x</paragraph>' );
  33. expect( command ).to.have.property( 'value', 'center' );
  34. } );
  35. it( 'is set to default alignment when selection is in block with default alignment (LTR content)', () => {
  36. setModelData( model, '<paragraph>x[]x</paragraph>' );
  37. expect( command ).to.have.property( 'value', 'left' );
  38. } );
  39. it( 'is set to default alignment when selection is in block with default alignment (RTL content)', () => {
  40. return ModelTestEditor.create( {
  41. language: {
  42. content: 'ar'
  43. }
  44. } ).then( newEditor => {
  45. model = newEditor.model;
  46. command = new AlignmentCommand( newEditor );
  47. newEditor.commands.add( 'alignment', command );
  48. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  49. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  50. model.schema.extend( 'paragraph', { allowAttributes: 'alignment' } );
  51. setModelData( model, '<paragraph>x[]x</paragraph>' );
  52. expect( command ).to.have.property( 'value', 'right' );
  53. return newEditor.destroy();
  54. } );
  55. } );
  56. } );
  57. describe( 'isEnabled', () => {
  58. it( 'is true when selection is in a block which can have added alignment', () => {
  59. setModelData( model, '<paragraph>x[]x</paragraph>' );
  60. expect( command ).to.have.property( 'isEnabled', true );
  61. } );
  62. it( 'is false when selection is in a block which can not be aligned', () => {
  63. setModelData( model, '<div>x[]x</div>' );
  64. expect( command ).to.have.property( 'isEnabled', false );
  65. } );
  66. } );
  67. describe( 'execute()', () => {
  68. describe( 'applying alignment', () => {
  69. it( 'adds alignment to block element', () => {
  70. setModelData( model, '<paragraph>x[]x</paragraph>' );
  71. editor.execute( 'alignment', { value: 'center' } );
  72. expect( getModelData( model ) ).to.equal( '<paragraph alignment="center">x[]x</paragraph>' );
  73. } );
  74. it( 'should remove alignment from single block element if already has one (LTR content)', () => {
  75. setModelData( model, '<paragraph alignment="center">x[]x</paragraph>' );
  76. editor.execute( 'alignment', { value: 'left' } );
  77. expect( getModelData( model ) ).to.equal( '<paragraph>x[]x</paragraph>' );
  78. } );
  79. it( 'should remove alignment from single block element if already has one (RTL content)', () => {
  80. return ModelTestEditor.create( {
  81. language: {
  82. content: 'ar'
  83. }
  84. } ).then( newEditor => {
  85. model = newEditor.model;
  86. command = new AlignmentCommand( newEditor );
  87. newEditor.commands.add( 'alignment', command );
  88. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  89. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  90. model.schema.extend( 'paragraph', { allowAttributes: 'alignment' } );
  91. setModelData( model, '<paragraph alignment="center">x[]x</paragraph>' );
  92. newEditor.execute( 'alignment', { value: 'right' } );
  93. expect( getModelData( model ) ).to.equal( '<paragraph>x[]x</paragraph>' );
  94. return newEditor.destroy();
  95. } );
  96. } );
  97. it( 'adds alignment to all selected blocks', () => {
  98. setModelData( model, '<paragraph>x[x</paragraph><paragraph>xx</paragraph><paragraph>x]x</paragraph>' );
  99. editor.execute( 'alignment', { value: 'center' } );
  100. expect( getModelData( model ) ).to.equal(
  101. '<paragraph alignment="center">x[x</paragraph>' +
  102. '<paragraph alignment="center">xx</paragraph>' +
  103. '<paragraph alignment="center">x]x</paragraph>'
  104. );
  105. } );
  106. it( 'sets alignment on all selected blocks as first block', () => {
  107. setModelData(
  108. model,
  109. '<paragraph>x[x</paragraph>' +
  110. '<paragraph >xx</paragraph>' +
  111. '<paragraph alignment="center">x]x</paragraph>'
  112. );
  113. editor.execute( 'alignment', { value: 'center' } );
  114. expect( getModelData( model ) ).to.equal(
  115. '<paragraph alignment="center">x[x</paragraph>' +
  116. '<paragraph alignment="center">xx</paragraph>' +
  117. '<paragraph alignment="center">x]x</paragraph>'
  118. );
  119. } );
  120. it( 'should remove alignment if block has the same alignment set', () => {
  121. setModelData( model, '<paragraph alignment="center">x[]x</paragraph>' );
  122. editor.execute( 'alignment', { value: 'center' } );
  123. expect( getModelData( model ) ).to.equal( '<paragraph>x[]x</paragraph>' );
  124. } );
  125. } );
  126. describe( 'applying default alignment', () => {
  127. it( 'removes alignment from block element when passed as value', () => {
  128. setModelData( model, '<paragraph alignment="justify">x[]x</paragraph>' );
  129. editor.execute( 'alignment', { value: 'left' } );
  130. expect( getModelData( model ) ).to.equal( '<paragraph>x[]x</paragraph>' );
  131. } );
  132. it( 'removes alignment from block element when no value is passed', () => {
  133. setModelData( model, '<paragraph alignment="justify">x[]x</paragraph>' );
  134. editor.execute( 'alignment' );
  135. expect( getModelData( model ) ).to.equal( '<paragraph>x[]x</paragraph>' );
  136. } );
  137. it( 'removes alignment from all selected blocks', () => {
  138. setModelData( model,
  139. '<paragraph alignment="center">x[x</paragraph>' +
  140. '<paragraph alignment="center">xx</paragraph>' +
  141. '<paragraph alignment="center">x]x</paragraph>'
  142. );
  143. editor.execute( 'alignment', { value: 'left' } );
  144. expect( getModelData( model ) ).to.equal(
  145. '<paragraph>x[x</paragraph><paragraph>xx</paragraph><paragraph>x]x</paragraph>'
  146. );
  147. } );
  148. it( 'removes alignment from all selected blocks even if one has no alignment defined', () => {
  149. setModelData( model,
  150. '<paragraph alignment="center">x[x</paragraph>' +
  151. '<paragraph>xx</paragraph>' +
  152. '<paragraph alignment="center">x]x</paragraph>'
  153. );
  154. editor.execute( 'alignment', { value: 'left' } );
  155. expect( getModelData( model ) ).to.equal(
  156. '<paragraph>x[x</paragraph><paragraph>xx</paragraph><paragraph>x]x</paragraph>'
  157. );
  158. } );
  159. } );
  160. } );
  161. } );