alignmentcommand.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * @license Copyright (c) 2003-2018, 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, defaultAlignmentCommand;
  11. beforeEach( () => {
  12. return ModelTestEditor.create()
  13. .then( newEditor => {
  14. model = newEditor.model;
  15. command = new AlignmentCommand( newEditor, 'center', false );
  16. defaultAlignmentCommand = new AlignmentCommand( newEditor, 'left', true );
  17. editor = newEditor;
  18. editor.commands.add( 'alignCenter', command );
  19. editor.commands.add( 'alignLeft', defaultAlignmentCommand );
  20. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  21. model.schema.extend( '$block', { allowAttributes: 'alignment' } );
  22. } );
  23. } );
  24. afterEach( () => {
  25. editor.destroy();
  26. } );
  27. it( 'is a command', () => {
  28. expect( AlignmentCommand.prototype ).to.be.instanceOf( Command );
  29. expect( command ).to.be.instanceOf( Command );
  30. } );
  31. describe( 'value', () => {
  32. it( 'is true when selection is in block with command type alignment', () => {
  33. setModelData( model, '<paragraph alignment="center">x[]x</paragraph>' );
  34. expect( command ).to.have.property( 'value', true );
  35. } );
  36. it( 'is false when selection is inside block that has different alignment', () => {
  37. setModelData( model, '<paragraph alignment="justify">x[]x</paragraph>' );
  38. expect( command ).to.have.property( 'value', false );
  39. } );
  40. it( 'is true when selection is in block with default alignment', () => {
  41. setModelData( model, '<paragraph>x[]x</paragraph>' );
  42. expect( defaultAlignmentCommand ).to.have.property( 'value', true );
  43. } );
  44. it( 'is false when selection is inside block that has different alignment (default style)', () => {
  45. setModelData( model, '<paragraph alignment="justify">x[]x</paragraph>' );
  46. expect( defaultAlignmentCommand ).to.have.property( 'value', false );
  47. } );
  48. } );
  49. describe( 'isEnabled', () => {
  50. it( 'is true when selection is in a block which can have added alignment', () => {
  51. setModelData( model, '<paragraph>x[]x</paragraph>' );
  52. expect( command ).to.have.property( 'isEnabled', true );
  53. } );
  54. } );
  55. describe( 'execute()', () => {
  56. describe( 'applying alignment', () => {
  57. it( 'adds alignment to block element', () => {
  58. setModelData( model, '<paragraph>x[]x</paragraph>' );
  59. editor.execute( 'alignCenter' );
  60. expect( getModelData( model ) ).to.equal( '<paragraph alignment="center">x[]x</paragraph>' );
  61. } );
  62. it( 'should remove alignment from single block element if already has one', () => {
  63. setModelData( model, '<paragraph alignment="center">x[]x</paragraph>' );
  64. editor.execute( 'alignCenter' );
  65. expect( getModelData( model ) ).to.equal( '<paragraph>x[]x</paragraph>' );
  66. } );
  67. it( 'adds alignment to all selected blocks', () => {
  68. setModelData( model, '<paragraph>x[x</paragraph><paragraph>xx</paragraph><paragraph>x]x</paragraph>' );
  69. editor.execute( 'alignCenter' );
  70. expect( getModelData( model ) ).to.equal(
  71. '<paragraph alignment="center">x[x</paragraph>' +
  72. '<paragraph alignment="center">xx</paragraph>' +
  73. '<paragraph alignment="center">x]x</paragraph>'
  74. );
  75. } );
  76. it( 'sets alignment on all selected blocks as first block', () => {
  77. setModelData(
  78. model,
  79. '<paragraph>x[x</paragraph>' +
  80. '<paragraph >xx</paragraph>' +
  81. '<paragraph alignment="center">x]x</paragraph>'
  82. );
  83. editor.execute( 'alignCenter' );
  84. expect( getModelData( model ) ).to.equal(
  85. '<paragraph alignment="center">x[x</paragraph>' +
  86. '<paragraph alignment="center">xx</paragraph>' +
  87. '<paragraph alignment="center">x]x</paragraph>'
  88. );
  89. } );
  90. } );
  91. describe( 'applying default alignment', () => {
  92. it( 'removes alignment from block element', () => {
  93. setModelData( model, '<paragraph alignment="justify">x[]x</paragraph>' );
  94. editor.execute( 'alignLeft' );
  95. expect( getModelData( model ) ).to.equal( '<paragraph>x[]x</paragraph>' );
  96. } );
  97. it( 'removes alignment from all selected blocks', () => {
  98. setModelData( model,
  99. '<paragraph alignment="center">x[x</paragraph>' +
  100. '<paragraph alignment="center">xx</paragraph>' +
  101. '<paragraph alignment="center">x]x</paragraph>'
  102. );
  103. editor.execute( 'alignLeft' );
  104. expect( getModelData( model ) ).to.equal(
  105. '<paragraph>x[x</paragraph><paragraph>xx</paragraph><paragraph>x]x</paragraph>'
  106. );
  107. } );
  108. it( 'removes alignment from all selected blocks even if one has no alignment defined', () => {
  109. setModelData( model,
  110. '<paragraph alignment="center">x[x</paragraph>' +
  111. '<paragraph>xx</paragraph>' +
  112. '<paragraph alignment="center">x]x</paragraph>'
  113. );
  114. editor.execute( 'alignLeft' );
  115. expect( getModelData( model ) ).to.equal(
  116. '<paragraph>x[x</paragraph><paragraph>xx</paragraph><paragraph>x]x</paragraph>'
  117. );
  118. } );
  119. } );
  120. } );
  121. } );