8
0

alignmentediting.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 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. describe( 'integration', () => {
  35. beforeEach( () => {
  36. return VirtualTestEditor
  37. .create( {
  38. plugins: [ AlignmentEditing, ImageCaptionEditing, Paragraph, ListEditing, HeadingEditing ]
  39. } )
  40. .then( newEditor => {
  41. editor = newEditor;
  42. model = editor.model;
  43. } );
  44. } );
  45. it( 'is allowed on paragraph', () => {
  46. expect( model.schema.checkAttribute( [ '$root', 'paragraph' ], 'alignment' ) ).to.be.true;
  47. } );
  48. it( 'is allowed on listItem', () => {
  49. expect( model.schema.checkAttribute( [ '$root', 'listItem' ], 'type' ) ).to.be.true;
  50. expect( model.schema.checkAttribute( [ '$root', 'listItem' ], 'indent' ) ).to.be.true;
  51. expect( model.schema.checkAttribute( [ '$root', 'listItem' ], 'alignment' ) ).to.be.true;
  52. } );
  53. it( 'is allowed on heading', () => {
  54. expect( model.schema.checkAttribute( [ '$root', 'heading1' ], 'alignment' ) ).to.be.true;
  55. } );
  56. it( 'is disallowed on caption', () => {
  57. expect( model.schema.checkAttribute( [ '$root', 'image', 'caption' ], 'alignment' ) ).to.be.false;
  58. } );
  59. } );
  60. describe( 'left alignment', () => {
  61. it( 'adds converters to the data pipeline', () => {
  62. const data = '<p style="text-align:left;">x</p>';
  63. editor.setData( data );
  64. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  65. expect( editor.getData() ).to.equal( '<p>x</p>' );
  66. } );
  67. it( 'adds a converter to the view pipeline for removing attribute', () => {
  68. setModelData( model, '<paragraph alignment="center">[]x</paragraph>' );
  69. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  70. editor.execute( 'alignment' );
  71. expect( editor.getData() ).to.equal( '<p>x</p>' );
  72. } );
  73. } );
  74. describe( 'center alignment', () => {
  75. it( 'adds converters to the data pipeline', () => {
  76. const data = '<p style="text-align:center;">x</p>';
  77. editor.setData( data );
  78. expect( getModelData( model ) ).to.equal( '<paragraph alignment="center">[]x</paragraph>' );
  79. expect( editor.getData() ).to.equal( data );
  80. } );
  81. it( 'adds a converter to the view pipeline', () => {
  82. setModelData( model, '<paragraph alignment="center">[]x</paragraph>' );
  83. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  84. } );
  85. it( 'adds a converter to the view pipeline for changing attribute', () => {
  86. setModelData( model, '<paragraph alignment="right">[]x</paragraph>' );
  87. expect( editor.getData() ).to.equal( '<p style="text-align:right;">x</p>' );
  88. editor.execute( 'alignment', { value: 'center' } );
  89. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  90. } );
  91. } );
  92. describe( 'right alignment', () => {
  93. it( 'adds converters to the data pipeline', () => {
  94. const data = '<p style="text-align:right;">x</p>';
  95. editor.setData( data );
  96. expect( getModelData( model ) ).to.equal( '<paragraph alignment="right">[]x</paragraph>' );
  97. expect( editor.getData() ).to.equal( data );
  98. } );
  99. it( 'adds a converter to the view pipeline', () => {
  100. setModelData( model, '<paragraph alignment="right">[]x</paragraph>' );
  101. expect( editor.getData() ).to.equal( '<p style="text-align:right;">x</p>' );
  102. } );
  103. } );
  104. describe( 'justify alignment', () => {
  105. it( 'adds converters to the data pipeline', () => {
  106. const data = '<p style="text-align:justify;">x</p>';
  107. editor.setData( data );
  108. expect( getModelData( model ) ).to.equal( '<paragraph alignment="justify">[]x</paragraph>' );
  109. expect( editor.getData() ).to.equal( data );
  110. } );
  111. it( 'adds a converter to the view pipeline', () => {
  112. setModelData( model, '<paragraph alignment="justify">[]x</paragraph>' );
  113. expect( editor.getData() ).to.equal( '<p style="text-align:justify;">x</p>' );
  114. } );
  115. } );
  116. describe( 'should be extensible', () => {
  117. it( 'converters in the data pipeline', () => {
  118. blockDefaultConversion( editor.data.downcastDispatcher );
  119. blockDefaultConversion( editor.editing.downcastDispatcher );
  120. const data = '<p style="text-align:justify;">x</p>';
  121. editor.setData( data );
  122. expect( getModelData( model ) ).to.equal( '<paragraph alignment="justify">[]x</paragraph>' );
  123. expect( editor.getData() ).to.equal( '<p>x</p>' );
  124. editor.execute( 'alignment' );
  125. expect( editor.getData() ).to.equal( '<p>x</p>' );
  126. } );
  127. } );
  128. describe( 'should work with broken styles', () => {
  129. it( 'should ignore empty style', () => {
  130. const data = '<p style="text-align:">x</p>';
  131. editor.setData( data );
  132. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  133. expect( editor.getData() ).to.equal( '<p>x</p>' );
  134. } );
  135. it( 'should ignore not known style', () => {
  136. const data = '<p style="text-align:unset;">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. } );
  142. describe( 'config', () => {
  143. describe( 'options', () => {
  144. describe( 'default value', () => {
  145. it( 'should be set', () => {
  146. expect( editor.config.get( 'alignment.options' ) ).to.deep.equal( [ 'left', 'right', 'center', 'justify' ] );
  147. } );
  148. } );
  149. } );
  150. } );
  151. } );
  152. function blockDefaultConversion( dispatcher ) {
  153. dispatcher.on( 'attribute:alignment', ( evt, data, conversionApi ) => {
  154. conversionApi.consumable.consume( data.item, evt.name );
  155. }, { 'priority': 'high' } );
  156. }