alignmentediting.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. * @license Copyright (c) 2003-2018, 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, model;
  15. beforeEach( () => {
  16. return VirtualTestEditor
  17. .create( {
  18. plugins: [ AlignmentEditing, Paragraph ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. model = editor.model;
  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( model.schema.checkAttribute( [ '$root', '$block' ], '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. model = editor.model;
  46. } );
  47. } );
  48. it( 'is allowed on paragraph', () => {
  49. expect( model.schema.checkAttribute( [ '$root', 'paragraph' ], 'alignment' ) ).to.be.true;
  50. } );
  51. it( 'is allowed on listItem', () => {
  52. expect( model.schema.checkAttribute( [ '$root', 'listItem' ], 'type' ) ).to.be.true;
  53. expect( model.schema.checkAttribute( [ '$root', 'listItem' ], 'indent' ) ).to.be.true;
  54. expect( model.schema.checkAttribute( [ '$root', 'listItem' ], 'alignment' ) ).to.be.true;
  55. } );
  56. it( 'is allowed on heading', () => {
  57. expect( model.schema.checkAttribute( [ '$root', 'heading1' ], 'alignment' ) ).to.be.true;
  58. } );
  59. it( 'is disallowed on caption', () => {
  60. expect( model.schema.checkAttribute( [ '$root', 'image', 'caption' ], 'alignment' ) ).to.be.false;
  61. } );
  62. } );
  63. describe( 'alignLeft', () => {
  64. it( 'adds converters to the data pipeline', () => {
  65. const data = '<p style="text-align:left;">x</p>';
  66. editor.setData( data );
  67. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  68. expect( editor.getData() ).to.equal( '<p>x</p>' );
  69. } );
  70. it( 'adds a converter to the view pipeline for removing attribute', () => {
  71. setModelData( model, '<paragraph alignment="center">[]x</paragraph>' );
  72. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  73. const command = editor.commands.get( 'alignLeft' );
  74. command.execute();
  75. expect( editor.getData() ).to.equal( '<p>x</p>' );
  76. } );
  77. } );
  78. describe( 'alignCenter', () => {
  79. it( 'adds converters to the data pipeline', () => {
  80. const data = '<p style="text-align:center;">x</p>';
  81. editor.setData( data );
  82. expect( getModelData( model ) ).to.equal( '<paragraph alignment="center">[]x</paragraph>' );
  83. expect( editor.getData() ).to.equal( data );
  84. } );
  85. it( 'adds a converter to the view pipeline', () => {
  86. setModelData( model, '<paragraph alignment="center">[]x</paragraph>' );
  87. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  88. } );
  89. it( 'adds a converter to the view pipeline for changing attribute', () => {
  90. setModelData( model, '<paragraph alignment="right">[]x</paragraph>' );
  91. expect( editor.getData() ).to.equal( '<p style="text-align:right;">x</p>' );
  92. const command = editor.commands.get( 'alignCenter' );
  93. command.execute();
  94. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  95. } );
  96. } );
  97. describe( 'alignRight', () => {
  98. it( 'adds converters to the data pipeline', () => {
  99. const data = '<p style="text-align:right;">x</p>';
  100. editor.setData( data );
  101. expect( getModelData( model ) ).to.equal( '<paragraph alignment="right">[]x</paragraph>' );
  102. expect( editor.getData() ).to.equal( data );
  103. } );
  104. it( 'adds a converter to the view pipeline', () => {
  105. setModelData( model, '<paragraph alignment="right">[]x</paragraph>' );
  106. expect( editor.getData() ).to.equal( '<p style="text-align:right;">x</p>' );
  107. } );
  108. } );
  109. describe( 'alignJustify', () => {
  110. it( 'adds converters to the data pipeline', () => {
  111. const data = '<p style="text-align:justify;">x</p>';
  112. editor.setData( data );
  113. expect( getModelData( model ) ).to.equal( '<paragraph alignment="justify">[]x</paragraph>' );
  114. expect( editor.getData() ).to.equal( data );
  115. } );
  116. it( 'adds a converter to the view pipeline', () => {
  117. setModelData( model, '<paragraph alignment="justify">[]x</paragraph>' );
  118. expect( editor.getData() ).to.equal( '<p style="text-align:justify;">x</p>' );
  119. } );
  120. } );
  121. describe( 'should be extensible', () => {
  122. it( 'converters in the data pipeline', () => {
  123. blockDefaultConversion( editor.data.modelToView );
  124. blockDefaultConversion( editor.editing.modelToView );
  125. const data = '<p style="text-align:justify;">x</p>';
  126. editor.setData( data );
  127. expect( getModelData( model ) ).to.equal( '<paragraph alignment="justify">[]x</paragraph>' );
  128. expect( editor.getData() ).to.equal( '<p>x</p>' );
  129. const command = editor.commands.get( 'alignLeft' );
  130. command.execute();
  131. expect( editor.getData() ).to.equal( '<p>x</p>' );
  132. } );
  133. } );
  134. describe( 'should work with broken styles', () => {
  135. it( 'should ignore empty style', () => {
  136. const data = '<p style="text-align:">x</p>';
  137. editor.setData( data );
  138. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  139. expect( editor.getData() ).to.equal( '<p>x</p>' );
  140. } );
  141. it( 'should ignore not known style', () => {
  142. const data = '<p style="text-align:unset;">x</p>';
  143. editor.setData( data );
  144. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  145. expect( editor.getData() ).to.equal( '<p>x</p>' );
  146. } );
  147. } );
  148. describe( 'config', () => {
  149. describe( 'options', () => {
  150. describe( 'default value', () => {
  151. it( 'should be set', () => {
  152. expect( editor.config.get( 'alignment.options' ) ).to.deep.equal( [ 'left', 'right', 'center', 'justify' ] );
  153. } );
  154. } );
  155. it( 'should customize commands', () => {
  156. return VirtualTestEditor
  157. .create( {
  158. alignment: { options: [ 'left', 'right' ] },
  159. plugins: [ AlignmentEditing, Paragraph ]
  160. } )
  161. .then( editor => {
  162. expect( editor.commands.get( 'alignLeft' ), 'adds alignLeft' ).to.be.instanceof( AlignmentCommand );
  163. expect( editor.commands.get( 'alignRight' ), 'adds alignLeft' ).to.be.instanceof( AlignmentCommand );
  164. expect( editor.commands.get( 'alignCenter' ), 'does not add alignCenter' ).to.be.undefined;
  165. expect( editor.commands.get( 'alignJustify' ), 'does not add alignJustify' ).to.be.undefined;
  166. } );
  167. } );
  168. } );
  169. } );
  170. } );
  171. function blockDefaultConversion( dispatcher ) {
  172. dispatcher.on( 'attribute:alignment', ( evt, data, consumable ) => {
  173. consumable.consume( data.item, evt.name );
  174. }, { 'priority': 'high' } );
  175. }