alignmentcommand.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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', () => {
  36. setModelData( model, '<paragraph>x[]x</paragraph>' );
  37. expect( command ).to.have.property( 'value', 'left' );
  38. } );
  39. } );
  40. describe( 'isEnabled', () => {
  41. it( 'is true when selection is in a block which can have added alignment', () => {
  42. setModelData( model, '<paragraph>x[]x</paragraph>' );
  43. expect( command ).to.have.property( 'isEnabled', true );
  44. } );
  45. it( 'is false when selection is in a block which can not be aligned', () => {
  46. setModelData( model, '<div>x[]x</div>' );
  47. expect( command ).to.have.property( 'isEnabled', false );
  48. } );
  49. } );
  50. describe( 'execute()', () => {
  51. describe( 'applying alignment', () => {
  52. it( 'adds alignment to block element', () => {
  53. setModelData( model, '<paragraph>x[]x</paragraph>' );
  54. editor.execute( 'alignment', { value: 'center' } );
  55. expect( getModelData( model ) ).to.equal( '<paragraph alignment="center">x[]x</paragraph>' );
  56. } );
  57. it( 'should remove alignment from single block element if already has one', () => {
  58. setModelData( model, '<paragraph alignment="center">x[]x</paragraph>' );
  59. editor.execute( 'alignment', { value: 'left' } );
  60. expect( getModelData( model ) ).to.equal( '<paragraph>x[]x</paragraph>' );
  61. } );
  62. it( 'adds alignment to all selected blocks', () => {
  63. setModelData( model, '<paragraph>x[x</paragraph><paragraph>xx</paragraph><paragraph>x]x</paragraph>' );
  64. editor.execute( 'alignment', { value: 'center' } );
  65. expect( getModelData( model ) ).to.equal(
  66. '<paragraph alignment="center">x[x</paragraph>' +
  67. '<paragraph alignment="center">xx</paragraph>' +
  68. '<paragraph alignment="center">x]x</paragraph>'
  69. );
  70. } );
  71. it( 'sets alignment on all selected blocks as first block', () => {
  72. setModelData(
  73. model,
  74. '<paragraph>x[x</paragraph>' +
  75. '<paragraph >xx</paragraph>' +
  76. '<paragraph alignment="center">x]x</paragraph>'
  77. );
  78. editor.execute( 'alignment', { value: 'center' } );
  79. expect( getModelData( model ) ).to.equal(
  80. '<paragraph alignment="center">x[x</paragraph>' +
  81. '<paragraph alignment="center">xx</paragraph>' +
  82. '<paragraph alignment="center">x]x</paragraph>'
  83. );
  84. } );
  85. it( 'should remove alignment if block has the same alignment set', () => {
  86. setModelData( model, '<paragraph alignment="center">x[]x</paragraph>' );
  87. editor.execute( 'alignment', { value: 'center' } );
  88. expect( getModelData( model ) ).to.equal( '<paragraph>x[]x</paragraph>' );
  89. } );
  90. } );
  91. describe( 'applying default alignment', () => {
  92. it( 'removes alignment from block element when passed as value', () => {
  93. setModelData( model, '<paragraph alignment="justify">x[]x</paragraph>' );
  94. editor.execute( 'alignment', { value: 'left' } );
  95. expect( getModelData( model ) ).to.equal( '<paragraph>x[]x</paragraph>' );
  96. } );
  97. it( 'removes alignment from block element when no value is passed', () => {
  98. setModelData( model, '<paragraph alignment="justify">x[]x</paragraph>' );
  99. editor.execute( 'alignment' );
  100. expect( getModelData( model ) ).to.equal( '<paragraph>x[]x</paragraph>' );
  101. } );
  102. it( 'removes alignment from all selected blocks', () => {
  103. setModelData( model,
  104. '<paragraph alignment="center">x[x</paragraph>' +
  105. '<paragraph alignment="center">xx</paragraph>' +
  106. '<paragraph alignment="center">x]x</paragraph>'
  107. );
  108. editor.execute( 'alignment', { value: 'left' } );
  109. expect( getModelData( model ) ).to.equal(
  110. '<paragraph>x[x</paragraph><paragraph>xx</paragraph><paragraph>x]x</paragraph>'
  111. );
  112. } );
  113. it( 'removes alignment from all selected blocks even if one has no alignment defined', () => {
  114. setModelData( model,
  115. '<paragraph alignment="center">x[x</paragraph>' +
  116. '<paragraph>xx</paragraph>' +
  117. '<paragraph alignment="center">x]x</paragraph>'
  118. );
  119. editor.execute( 'alignment', { value: 'left' } );
  120. expect( getModelData( model ) ).to.equal(
  121. '<paragraph>x[x</paragraph><paragraph>xx</paragraph><paragraph>x]x</paragraph>'
  122. );
  123. } );
  124. } );
  125. } );
  126. } );