alignmentediting.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 AlignmentEditing from '../src/alignmentediting';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import ImageCaptionEditing from '@ckeditor/ckeditor5-image/src/imagecaption/imagecaptionediting';
  8. import ListEditing from '@ckeditor/ckeditor5-list/src/listediting';
  9. import HeadingEditing from '@ckeditor/ckeditor5-heading/src/headingediting';
  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 command', () => {
  29. expect( editor.commands.get( 'alignment' ) ).to.be.instanceOf( AlignmentCommand );
  30. } );
  31. it( 'allows for alignment in $blocks', () => {
  32. expect( model.schema.checkAttribute( [ '$root', '$block' ], 'alignment' ) ).to.be.true;
  33. } );
  34. it( 'its attribute is marked with a formatting property', () => {
  35. expect( model.schema.getAttributeProperties( 'alignment' ) ).to.deep.equal( {
  36. isFormatting: true
  37. } );
  38. } );
  39. describe( 'integration', () => {
  40. beforeEach( () => {
  41. return VirtualTestEditor
  42. .create( {
  43. plugins: [ AlignmentEditing, ImageCaptionEditing, Paragraph, ListEditing, HeadingEditing ]
  44. } )
  45. .then( newEditor => {
  46. editor = newEditor;
  47. model = editor.model;
  48. } );
  49. } );
  50. it( 'is allowed on paragraph', () => {
  51. expect( model.schema.checkAttribute( [ '$root', 'paragraph' ], 'alignment' ) ).to.be.true;
  52. } );
  53. it( 'is allowed on listItem', () => {
  54. expect( model.schema.checkAttribute( [ '$root', 'listItem' ], 'listType' ) ).to.be.true;
  55. expect( model.schema.checkAttribute( [ '$root', 'listItem' ], 'listIndent' ) ).to.be.true;
  56. expect( model.schema.checkAttribute( [ '$root', 'listItem' ], 'alignment' ) ).to.be.true;
  57. } );
  58. it( 'is allowed on heading', () => {
  59. expect( model.schema.checkAttribute( [ '$root', 'heading1' ], 'alignment' ) ).to.be.true;
  60. } );
  61. it( 'is disallowed on caption', () => {
  62. expect( model.schema.checkAttribute( [ '$root', 'image', 'caption' ], 'alignment' ) ).to.be.false;
  63. } );
  64. } );
  65. describe( 'left alignment', () => {
  66. it( 'adds converters to the data pipeline', () => {
  67. const data = '<p style="text-align:left;">x</p>';
  68. editor.setData( data );
  69. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  70. expect( editor.getData() ).to.equal( '<p>x</p>' );
  71. } );
  72. it( 'adds a converter to the view pipeline for removing attribute', () => {
  73. setModelData( model, '<paragraph alignment="center">[]x</paragraph>' );
  74. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  75. editor.execute( 'alignment' );
  76. expect( editor.getData() ).to.equal( '<p>x</p>' );
  77. } );
  78. } );
  79. describe( 'center alignment', () => {
  80. it( 'adds converters to the data pipeline', () => {
  81. const data = '<p style="text-align:center;">x</p>';
  82. editor.setData( data );
  83. expect( getModelData( model ) ).to.equal( '<paragraph alignment="center">[]x</paragraph>' );
  84. expect( editor.getData() ).to.equal( data );
  85. } );
  86. it( 'adds a converter to the view pipeline', () => {
  87. setModelData( model, '<paragraph alignment="center">[]x</paragraph>' );
  88. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  89. } );
  90. it( 'adds a converter to the view pipeline for changing attribute', () => {
  91. setModelData( model, '<paragraph alignment="right">[]x</paragraph>' );
  92. expect( editor.getData() ).to.equal( '<p style="text-align:right;">x</p>' );
  93. editor.execute( 'alignment', { value: 'center' } );
  94. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  95. } );
  96. } );
  97. describe( 'right alignment', () => {
  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( 'justify alignment', () => {
  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.downcastDispatcher );
  124. blockDefaultConversion( editor.editing.downcastDispatcher );
  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. editor.execute( 'alignment' );
  130. expect( editor.getData() ).to.equal( '<p>x</p>' );
  131. } );
  132. } );
  133. describe( 'should work with broken styles', () => {
  134. it( 'should ignore empty style', () => {
  135. const data = '<p style="text-align:">x</p>';
  136. editor.setData( data );
  137. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  138. expect( editor.getData() ).to.equal( '<p>x</p>' );
  139. } );
  140. it( 'should ignore not known style', () => {
  141. const data = '<p style="text-align:unset;">x</p>';
  142. editor.setData( data );
  143. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  144. expect( editor.getData() ).to.equal( '<p>x</p>' );
  145. } );
  146. } );
  147. describe( 'config', () => {
  148. describe( 'options', () => {
  149. describe( 'default value', () => {
  150. it( 'should be set', () => {
  151. expect( editor.config.get( 'alignment.options' ) ).to.deep.equal( [ 'left', 'right', 'center', 'justify' ] );
  152. } );
  153. } );
  154. } );
  155. } );
  156. } );
  157. function blockDefaultConversion( dispatcher ) {
  158. dispatcher.on( 'attribute:alignment', ( evt, data, conversionApi ) => {
  159. conversionApi.consumable.consume( data.item, evt.name );
  160. }, { 'priority': 'high' } );
  161. }