alignmentediting.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import AlignmentEditing from '../src/alignmentediting';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import ImageCaptionEngine from '@ckeditor/ckeditor5-image/src/imagecaption/imagecaptionengine';
  8. import ListEngine from '@ckeditor/ckeditor5-list/src/listengine';
  9. import HeadingEngine from '@ckeditor/ckeditor5-heading/src/headingengine';
  10. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  11. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  12. import AlignmentCommand from '../src/alignmentcommand';
  13. describe( 'AlignmentEditing', () => {
  14. let editor, doc;
  15. beforeEach( () => {
  16. return VirtualTestEditor
  17. .create( {
  18. plugins: [ AlignmentEditing, Paragraph ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. doc = editor.document;
  23. } );
  24. } );
  25. afterEach( () => {
  26. editor.destroy();
  27. } );
  28. it( 'adds alignment commands', () => {
  29. expect( editor.commands.get( 'alignLeft' ) ).to.be.instanceOf( AlignmentCommand );
  30. expect( editor.commands.get( 'alignRight' ) ).to.be.instanceOf( AlignmentCommand );
  31. expect( editor.commands.get( 'alignCenter' ) ).to.be.instanceOf( AlignmentCommand );
  32. expect( editor.commands.get( 'alignJustify' ) ).to.be.instanceOf( AlignmentCommand );
  33. } );
  34. it( 'allows for alignment in $blocks', () => {
  35. expect( doc.schema.check( { name: '$block', inside: '$root', attributes: 'alignment' } ) ).to.be.true;
  36. } );
  37. describe( 'integration', () => {
  38. beforeEach( () => {
  39. return VirtualTestEditor
  40. .create( {
  41. plugins: [ AlignmentEditing, ImageCaptionEngine, Paragraph, ListEngine, HeadingEngine ]
  42. } )
  43. .then( newEditor => {
  44. editor = newEditor;
  45. doc = editor.document;
  46. } );
  47. } );
  48. it( 'is allowed on paragraph', () => {
  49. expect( doc.schema.check( { name: 'paragraph', attributes: 'alignment' } ) ).to.be.true;
  50. } );
  51. it( 'is allowed on listItem', () => {
  52. expect( doc.schema.check( { name: 'listItem', attributes: [ 'type', 'indent', 'alignment' ] } ) ).to.be.true;
  53. } );
  54. it( 'is allowed on heading', () => {
  55. expect( doc.schema.check( { name: 'heading1', attributes: 'alignment' } ) ).to.be.true;
  56. } );
  57. it( 'is disallowed on figcaption', () => {
  58. expect( doc.schema.check( { name: 'figcaption', attributes: 'alignment' } ) ).to.be.false;
  59. } );
  60. } );
  61. describe( 'alignLeft', () => {
  62. it( 'adds converters to the data pipeline', () => {
  63. const data = '<p style="text-align:left;">x</p>';
  64. editor.setData( data );
  65. expect( getModelData( doc ) ).to.equal( '<paragraph>[]x</paragraph>' );
  66. expect( editor.getData() ).to.equal( '<p>x</p>' );
  67. } );
  68. } );
  69. describe( 'alignCenter', () => {
  70. it( 'adds converters to the data pipeline', () => {
  71. const data = '<p style="text-align:center;">x</p>';
  72. editor.setData( data );
  73. expect( getModelData( doc ) ).to.equal( '<paragraph alignment="center">[]x</paragraph>' );
  74. expect( editor.getData() ).to.equal( data );
  75. } );
  76. it( 'adds a converter to the view pipeline', () => {
  77. setModelData( doc, '<paragraph alignment="center">[]x</paragraph>' );
  78. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  79. } );
  80. } );
  81. describe( 'alignRight', () => {
  82. it( 'adds converters to the data pipeline', () => {
  83. const data = '<p style="text-align:right;">x</p>';
  84. editor.setData( data );
  85. expect( getModelData( doc ) ).to.equal( '<paragraph alignment="right">[]x</paragraph>' );
  86. expect( editor.getData() ).to.equal( data );
  87. } );
  88. it( 'adds a converter to the view pipeline', () => {
  89. setModelData( doc, '<paragraph alignment="right">[]x</paragraph>' );
  90. expect( editor.getData() ).to.equal( '<p style="text-align:right;">x</p>' );
  91. } );
  92. } );
  93. describe( 'alignJustify', () => {
  94. it( 'adds converters to the data pipeline', () => {
  95. const data = '<p style="text-align:justify;">x</p>';
  96. editor.setData( data );
  97. expect( getModelData( doc ) ).to.equal( '<paragraph alignment="justify">[]x</paragraph>' );
  98. expect( editor.getData() ).to.equal( data );
  99. } );
  100. it( 'adds a converter to the view pipeline', () => {
  101. setModelData( doc, '<paragraph alignment="justify">[]x</paragraph>' );
  102. expect( editor.getData() ).to.equal( '<p style="text-align:justify;">x</p>' );
  103. } );
  104. } );
  105. describe( 'should work with broken styles', () => {
  106. it( 'should ignore empty style', () => {
  107. const data = '<p style="text-align:">x</p>';
  108. editor.setData( data );
  109. expect( getModelData( doc ) ).to.equal( '<paragraph>[]x</paragraph>' );
  110. expect( editor.getData() ).to.equal( '<p>x</p>' );
  111. } );
  112. it( 'should ignore not known style', () => {
  113. const data = '<p style="text-align:unset;">x</p>';
  114. editor.setData( data );
  115. expect( getModelData( doc ) ).to.equal( '<paragraph>[]x</paragraph>' );
  116. expect( editor.getData() ).to.equal( '<p>x</p>' );
  117. } );
  118. } );
  119. describe( 'config', () => {
  120. describe( 'styles', () => {
  121. describe( 'default value', () => {
  122. it( 'should be set', () => {
  123. expect( editor.config.get( 'alignment.styles' ) ).to.deep.equal( [ 'left', 'right', 'center', 'justify' ] );
  124. } );
  125. } );
  126. it( 'should customize commands', () => {
  127. return VirtualTestEditor
  128. .create( {
  129. alignment: { styles: [ 'left', 'right' ] },
  130. plugins: [ AlignmentEditing, Paragraph ]
  131. } )
  132. .then( editor => {
  133. expect( editor.commands.get( 'alignLeft' ), 'adds alignLeft' ).to.be.instanceof( AlignmentCommand );
  134. expect( editor.commands.get( 'alignRight' ), 'adds alignLeft' ).to.be.instanceof( AlignmentCommand );
  135. expect( editor.commands.get( 'alignCenter' ), 'does not add alignCenter' ).to.be.undefined;
  136. expect( editor.commands.get( 'alignJustify' ), 'does not add alignJustify' ).to.be.undefined;
  137. } );
  138. } );
  139. } );
  140. } );
  141. } );