8
0

alignmentediting.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. describe( 'LTR content', () => {
  67. it( 'adds converters to the data pipeline', () => {
  68. const data = '<p style="text-align:left;">x</p>';
  69. editor.setData( data );
  70. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  71. expect( editor.getData() ).to.equal( '<p>x</p>' );
  72. } );
  73. it( 'adds a converter to the view pipeline', () => {
  74. setModelData( model, '<paragraph alignment="left">[]x</paragraph>' );
  75. expect( editor.getData() ).to.equal( '<p>x</p>' );
  76. } );
  77. } );
  78. describe( 'RTL content', () => {
  79. it( 'adds converters to the data pipeline', () => {
  80. return VirtualTestEditor
  81. .create( {
  82. contentLanguage: 'ar',
  83. plugins: [ AlignmentEditing, Paragraph ]
  84. } )
  85. .then( newEditor => {
  86. const model = newEditor.model;
  87. const data = '<p style="text-align:left;">x</p>';
  88. newEditor.setData( data );
  89. expect( getModelData( model ) ).to.equal( '<paragraph alignment="left">[]x</paragraph>' );
  90. expect( newEditor.getData() ).to.equal( '<p style="text-align:left;">x</p>' );
  91. return newEditor.destroy();
  92. } );
  93. } );
  94. it( 'adds a converter to the view pipeline', () => {
  95. return VirtualTestEditor
  96. .create( {
  97. contentLanguage: 'ar',
  98. plugins: [ AlignmentEditing, Paragraph ]
  99. } )
  100. .then( newEditor => {
  101. const model = newEditor.model;
  102. setModelData( model, '<paragraph alignment="left">[]x</paragraph>' );
  103. expect( newEditor.getData() ).to.equal( '<p style="text-align:left;">x</p>' );
  104. return newEditor.destroy();
  105. } );
  106. } );
  107. } );
  108. it( 'adds a converter to the view pipeline for removing attribute', () => {
  109. setModelData( model, '<paragraph alignment="center">[]x</paragraph>' );
  110. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  111. editor.execute( 'alignment' );
  112. expect( editor.getData() ).to.equal( '<p>x</p>' );
  113. } );
  114. } );
  115. describe( 'center alignment', () => {
  116. it( 'adds converters to the data pipeline', () => {
  117. const data = '<p style="text-align:center;">x</p>';
  118. editor.setData( data );
  119. expect( getModelData( model ) ).to.equal( '<paragraph alignment="center">[]x</paragraph>' );
  120. expect( editor.getData() ).to.equal( data );
  121. } );
  122. it( 'adds a converter to the view pipeline', () => {
  123. setModelData( model, '<paragraph alignment="center">[]x</paragraph>' );
  124. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  125. } );
  126. it( 'adds a converter to the view pipeline for changing attribute', () => {
  127. setModelData( model, '<paragraph alignment="right">[]x</paragraph>' );
  128. expect( editor.getData() ).to.equal( '<p style="text-align:right;">x</p>' );
  129. editor.execute( 'alignment', { value: 'center' } );
  130. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  131. } );
  132. } );
  133. describe( 'right alignment', () => {
  134. describe( 'LTR content', () => {
  135. it( 'adds converters to the data pipeline', () => {
  136. const data = '<p style="text-align:right;">x</p>';
  137. editor.setData( data );
  138. expect( getModelData( model ) ).to.equal( '<paragraph alignment="right">[]x</paragraph>' );
  139. expect( editor.getData() ).to.equal( data );
  140. } );
  141. it( 'adds a converter to the view pipeline', () => {
  142. setModelData( model, '<paragraph alignment="right">[]x</paragraph>' );
  143. expect( editor.getData() ).to.equal( '<p style="text-align:right;">x</p>' );
  144. } );
  145. } );
  146. describe( 'RTL content', () => {
  147. it( 'adds converters to the data pipeline', () => {
  148. return VirtualTestEditor
  149. .create( {
  150. contentLanguage: 'ar',
  151. plugins: [ AlignmentEditing, Paragraph ]
  152. } )
  153. .then( newEditor => {
  154. const model = newEditor.model;
  155. const data = '<p style="text-align:right;">x</p>';
  156. newEditor.setData( data );
  157. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  158. expect( newEditor.getData() ).to.equal( '<p>x</p>' );
  159. return newEditor.destroy();
  160. } );
  161. } );
  162. it( 'adds a converter to the view pipeline', () => {
  163. return VirtualTestEditor
  164. .create( {
  165. contentLanguage: 'ar',
  166. plugins: [ AlignmentEditing, Paragraph ]
  167. } )
  168. .then( newEditor => {
  169. const model = newEditor.model;
  170. setModelData( model, '<paragraph alignment="right">[]x</paragraph>' );
  171. expect( newEditor.getData() ).to.equal( '<p>x</p>' );
  172. return newEditor.destroy();
  173. } );
  174. } );
  175. } );
  176. } );
  177. describe( 'justify alignment', () => {
  178. it( 'adds converters to the data pipeline', () => {
  179. const data = '<p style="text-align:justify;">x</p>';
  180. editor.setData( data );
  181. expect( getModelData( model ) ).to.equal( '<paragraph alignment="justify">[]x</paragraph>' );
  182. expect( editor.getData() ).to.equal( data );
  183. } );
  184. it( 'adds a converter to the view pipeline', () => {
  185. setModelData( model, '<paragraph alignment="justify">[]x</paragraph>' );
  186. expect( editor.getData() ).to.equal( '<p style="text-align:justify;">x</p>' );
  187. } );
  188. } );
  189. describe( 'should be extensible', () => {
  190. it( 'converters in the data pipeline', () => {
  191. blockDefaultConversion( editor.data.downcastDispatcher );
  192. blockDefaultConversion( editor.editing.downcastDispatcher );
  193. const data = '<p style="text-align:justify;">x</p>';
  194. editor.setData( data );
  195. expect( getModelData( model ) ).to.equal( '<paragraph alignment="justify">[]x</paragraph>' );
  196. expect( editor.getData() ).to.equal( '<p>x</p>' );
  197. editor.execute( 'alignment' );
  198. expect( editor.getData() ).to.equal( '<p>x</p>' );
  199. } );
  200. } );
  201. describe( 'should work with broken styles', () => {
  202. it( 'should ignore empty style', () => {
  203. const data = '<p style="text-align:">x</p>';
  204. editor.setData( data );
  205. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  206. expect( editor.getData() ).to.equal( '<p>x</p>' );
  207. } );
  208. it( 'should ignore not known style', () => {
  209. const data = '<p style="text-align:unset;">x</p>';
  210. editor.setData( data );
  211. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  212. expect( editor.getData() ).to.equal( '<p>x</p>' );
  213. } );
  214. } );
  215. describe( 'config', () => {
  216. describe( 'options', () => {
  217. describe( 'default value', () => {
  218. it( 'should be set', () => {
  219. expect( editor.config.get( 'alignment.options' ) ).to.deep.equal( [ 'left', 'right', 'center', 'justify' ] );
  220. } );
  221. } );
  222. } );
  223. } );
  224. } );
  225. function blockDefaultConversion( dispatcher ) {
  226. dispatcher.on( 'attribute:alignment', ( evt, data, conversionApi ) => {
  227. conversionApi.consumable.consume( data.item, evt.name );
  228. }, { 'priority': 'high' } );
  229. }