alignmentcommand.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * @license Copyright (c) 2003-2019, 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. contentLanguage: 'ar'
  42. } ).then( newEditor => {
  43. model = newEditor.model;
  44. command = new AlignmentCommand( newEditor );
  45. newEditor.commands.add( 'alignment', command );
  46. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  47. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  48. model.schema.extend( 'paragraph', { allowAttributes: 'alignment' } );
  49. setModelData( model, '<paragraph>x[]x</paragraph>' );
  50. expect( command ).to.have.property( 'value', 'right' );
  51. return newEditor.destroy();
  52. } );
  53. } );
  54. } );
  55. describe( 'isEnabled', () => {
  56. it( 'is true when selection is in a block which can have added alignment', () => {
  57. setModelData( model, '<paragraph>x[]x</paragraph>' );
  58. expect( command ).to.have.property( 'isEnabled', true );
  59. } );
  60. it( 'is false when selection is in a block which can not be aligned', () => {
  61. setModelData( model, '<div>x[]x</div>' );
  62. expect( command ).to.have.property( 'isEnabled', false );
  63. } );
  64. } );
  65. describe( 'execute()', () => {
  66. describe( 'applying alignment', () => {
  67. it( 'adds alignment to block element', () => {
  68. setModelData( model, '<paragraph>x[]x</paragraph>' );
  69. editor.execute( 'alignment', { value: 'center' } );
  70. expect( getModelData( model ) ).to.equal( '<paragraph alignment="center">x[]x</paragraph>' );
  71. } );
  72. it( 'should remove alignment from single block element if already has one (LTR content)', () => {
  73. setModelData( model, '<paragraph alignment="center">x[]x</paragraph>' );
  74. editor.execute( 'alignment', { value: 'left' } );
  75. expect( getModelData( model ) ).to.equal( '<paragraph>x[]x</paragraph>' );
  76. } );
  77. it( 'should remove alignment from single block element if already has one (RTL content)', () => {
  78. return ModelTestEditor.create( {
  79. contentLanguage: 'ar'
  80. } ).then( newEditor => {
  81. model = newEditor.model;
  82. command = new AlignmentCommand( newEditor );
  83. newEditor.commands.add( 'alignment', command );
  84. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  85. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  86. model.schema.extend( 'paragraph', { allowAttributes: 'alignment' } );
  87. setModelData( model, '<paragraph alignment="center">x[]x</paragraph>' );
  88. newEditor.execute( 'alignment', { value: 'right' } );
  89. expect( getModelData( model ) ).to.equal( '<paragraph>x[]x</paragraph>' );
  90. return newEditor.destroy();
  91. } );
  92. } );
  93. it( 'adds alignment to all selected blocks', () => {
  94. setModelData( model, '<paragraph>x[x</paragraph><paragraph>xx</paragraph><paragraph>x]x</paragraph>' );
  95. editor.execute( 'alignment', { value: 'center' } );
  96. expect( getModelData( model ) ).to.equal(
  97. '<paragraph alignment="center">x[x</paragraph>' +
  98. '<paragraph alignment="center">xx</paragraph>' +
  99. '<paragraph alignment="center">x]x</paragraph>'
  100. );
  101. } );
  102. it( 'sets alignment on all selected blocks as first block', () => {
  103. setModelData(
  104. model,
  105. '<paragraph>x[x</paragraph>' +
  106. '<paragraph >xx</paragraph>' +
  107. '<paragraph alignment="center">x]x</paragraph>'
  108. );
  109. editor.execute( 'alignment', { value: 'center' } );
  110. expect( getModelData( model ) ).to.equal(
  111. '<paragraph alignment="center">x[x</paragraph>' +
  112. '<paragraph alignment="center">xx</paragraph>' +
  113. '<paragraph alignment="center">x]x</paragraph>'
  114. );
  115. } );
  116. it( 'should remove alignment if block has the same alignment set', () => {
  117. setModelData( model, '<paragraph alignment="center">x[]x</paragraph>' );
  118. editor.execute( 'alignment', { value: 'center' } );
  119. expect( getModelData( model ) ).to.equal( '<paragraph>x[]x</paragraph>' );
  120. } );
  121. } );
  122. describe( 'applying default alignment', () => {
  123. it( 'removes alignment from block element when passed as value', () => {
  124. setModelData( model, '<paragraph alignment="justify">x[]x</paragraph>' );
  125. editor.execute( 'alignment', { value: 'left' } );
  126. expect( getModelData( model ) ).to.equal( '<paragraph>x[]x</paragraph>' );
  127. } );
  128. it( 'removes alignment from block element when no value is passed', () => {
  129. setModelData( model, '<paragraph alignment="justify">x[]x</paragraph>' );
  130. editor.execute( 'alignment' );
  131. expect( getModelData( model ) ).to.equal( '<paragraph>x[]x</paragraph>' );
  132. } );
  133. it( 'removes alignment from all selected blocks', () => {
  134. setModelData( model,
  135. '<paragraph alignment="center">x[x</paragraph>' +
  136. '<paragraph alignment="center">xx</paragraph>' +
  137. '<paragraph alignment="center">x]x</paragraph>'
  138. );
  139. editor.execute( 'alignment', { value: 'left' } );
  140. expect( getModelData( model ) ).to.equal(
  141. '<paragraph>x[x</paragraph><paragraph>xx</paragraph><paragraph>x]x</paragraph>'
  142. );
  143. } );
  144. it( 'removes alignment from all selected blocks even if one has no alignment defined', () => {
  145. setModelData( model,
  146. '<paragraph alignment="center">x[x</paragraph>' +
  147. '<paragraph>xx</paragraph>' +
  148. '<paragraph alignment="center">x]x</paragraph>'
  149. );
  150. editor.execute( 'alignment', { value: 'left' } );
  151. expect( getModelData( model ) ).to.equal(
  152. '<paragraph>x[x</paragraph><paragraph>xx</paragraph><paragraph>x]x</paragraph>'
  153. );
  154. } );
  155. } );
  156. } );
  157. } );