alignmentediting.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. * @license Copyright (c) 2003-2020, 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( 'should have pluginName', () => {
  29. expect( AlignmentEditing.pluginName ).to.equal( 'AlignmentEditing' );
  30. } );
  31. it( 'adds alignment command', () => {
  32. expect( editor.commands.get( 'alignment' ) ).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. it( 'its attribute is marked with a formatting property', () => {
  38. expect( model.schema.getAttributeProperties( 'alignment' ) ).to.deep.equal( {
  39. isFormatting: true
  40. } );
  41. } );
  42. describe( 'integration', () => {
  43. beforeEach( () => {
  44. return VirtualTestEditor
  45. .create( {
  46. plugins: [ AlignmentEditing, ImageCaptionEditing, Paragraph, ListEditing, HeadingEditing ]
  47. } )
  48. .then( newEditor => {
  49. editor = newEditor;
  50. model = editor.model;
  51. } );
  52. } );
  53. it( 'is allowed on paragraph', () => {
  54. expect( model.schema.checkAttribute( [ '$root', 'paragraph' ], 'alignment' ) ).to.be.true;
  55. } );
  56. it( 'is allowed on listItem', () => {
  57. expect( model.schema.checkAttribute( [ '$root', 'listItem' ], 'listType' ) ).to.be.true;
  58. expect( model.schema.checkAttribute( [ '$root', 'listItem' ], 'listIndent' ) ).to.be.true;
  59. expect( model.schema.checkAttribute( [ '$root', 'listItem' ], 'alignment' ) ).to.be.true;
  60. } );
  61. it( 'is allowed on heading', () => {
  62. expect( model.schema.checkAttribute( [ '$root', 'heading1' ], 'alignment' ) ).to.be.true;
  63. } );
  64. it( 'is disallowed on caption', () => {
  65. expect( model.schema.checkAttribute( [ '$root', 'image', 'caption' ], 'alignment' ) ).to.be.false;
  66. } );
  67. } );
  68. describe( 'left alignment', () => {
  69. describe( 'LTR content', () => {
  70. it( 'adds converters to the data pipeline', () => {
  71. const data = '<p style="text-align:left;">x</p>';
  72. editor.setData( data );
  73. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  74. expect( editor.getData() ).to.equal( '<p>x</p>' );
  75. } );
  76. it( 'adds a converter to the view pipeline', () => {
  77. setModelData( model, '<paragraph alignment="left">[]x</paragraph>' );
  78. expect( editor.getData() ).to.equal( '<p>x</p>' );
  79. } );
  80. } );
  81. describe( 'RTL content', () => {
  82. it( 'adds converters to the data pipeline', () => {
  83. return VirtualTestEditor
  84. .create( {
  85. language: {
  86. content: 'ar'
  87. },
  88. plugins: [ AlignmentEditing, Paragraph ]
  89. } )
  90. .then( newEditor => {
  91. const model = newEditor.model;
  92. const data = '<p style="text-align:left;">x</p>';
  93. newEditor.setData( data );
  94. expect( getModelData( model ) ).to.equal( '<paragraph alignment="left">[]x</paragraph>' );
  95. expect( newEditor.getData() ).to.equal( '<p style="text-align:left;">x</p>' );
  96. return newEditor.destroy();
  97. } );
  98. } );
  99. it( 'adds a converter to the view pipeline', () => {
  100. return VirtualTestEditor
  101. .create( {
  102. language: {
  103. content: 'ar'
  104. },
  105. plugins: [ AlignmentEditing, Paragraph ]
  106. } )
  107. .then( newEditor => {
  108. const model = newEditor.model;
  109. setModelData( model, '<paragraph alignment="left">[]x</paragraph>' );
  110. expect( newEditor.getData() ).to.equal( '<p style="text-align:left;">x</p>' );
  111. return newEditor.destroy();
  112. } );
  113. } );
  114. } );
  115. it( 'adds a converter to the view pipeline for removing attribute', () => {
  116. setModelData( model, '<paragraph alignment="center">[]x</paragraph>' );
  117. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  118. editor.execute( 'alignment' );
  119. expect( editor.getData() ).to.equal( '<p>x</p>' );
  120. } );
  121. } );
  122. describe( 'center alignment', () => {
  123. it( 'adds converters to the data pipeline', () => {
  124. const data = '<p style="text-align:center;">x</p>';
  125. editor.setData( data );
  126. expect( getModelData( model ) ).to.equal( '<paragraph alignment="center">[]x</paragraph>' );
  127. expect( editor.getData() ).to.equal( data );
  128. } );
  129. it( 'adds a converter to the view pipeline', () => {
  130. setModelData( model, '<paragraph alignment="center">[]x</paragraph>' );
  131. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  132. } );
  133. it( 'adds a converter to the view pipeline for changing attribute', () => {
  134. setModelData( model, '<paragraph alignment="right">[]x</paragraph>' );
  135. expect( editor.getData() ).to.equal( '<p style="text-align:right;">x</p>' );
  136. editor.execute( 'alignment', { value: 'center' } );
  137. expect( editor.getData() ).to.equal( '<p style="text-align:center;">x</p>' );
  138. } );
  139. } );
  140. describe( 'right alignment', () => {
  141. describe( 'LTR content', () => {
  142. it( 'adds converters to the data pipeline', () => {
  143. const data = '<p style="text-align:right;">x</p>';
  144. editor.setData( data );
  145. expect( getModelData( model ) ).to.equal( '<paragraph alignment="right">[]x</paragraph>' );
  146. expect( editor.getData() ).to.equal( data );
  147. } );
  148. it( 'adds a converter to the view pipeline', () => {
  149. setModelData( model, '<paragraph alignment="right">[]x</paragraph>' );
  150. expect( editor.getData() ).to.equal( '<p style="text-align:right;">x</p>' );
  151. } );
  152. } );
  153. describe( 'RTL content', () => {
  154. it( 'adds converters to the data pipeline', () => {
  155. return VirtualTestEditor
  156. .create( {
  157. language: {
  158. content: 'ar'
  159. },
  160. plugins: [ AlignmentEditing, Paragraph ]
  161. } )
  162. .then( newEditor => {
  163. const model = newEditor.model;
  164. const data = '<p style="text-align:right;">x</p>';
  165. newEditor.setData( data );
  166. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  167. expect( newEditor.getData() ).to.equal( '<p>x</p>' );
  168. return newEditor.destroy();
  169. } );
  170. } );
  171. it( 'adds a converter to the view pipeline', () => {
  172. return VirtualTestEditor
  173. .create( {
  174. language: {
  175. content: 'ar'
  176. },
  177. plugins: [ AlignmentEditing, Paragraph ]
  178. } )
  179. .then( newEditor => {
  180. const model = newEditor.model;
  181. setModelData( model, '<paragraph alignment="right">[]x</paragraph>' );
  182. expect( newEditor.getData() ).to.equal( '<p>x</p>' );
  183. return newEditor.destroy();
  184. } );
  185. } );
  186. } );
  187. } );
  188. describe( 'justify alignment', () => {
  189. it( 'adds converters to the data pipeline', () => {
  190. const data = '<p style="text-align:justify;">x</p>';
  191. editor.setData( data );
  192. expect( getModelData( model ) ).to.equal( '<paragraph alignment="justify">[]x</paragraph>' );
  193. expect( editor.getData() ).to.equal( data );
  194. } );
  195. it( 'adds a converter to the view pipeline', () => {
  196. setModelData( model, '<paragraph alignment="justify">[]x</paragraph>' );
  197. expect( editor.getData() ).to.equal( '<p style="text-align:justify;">x</p>' );
  198. } );
  199. } );
  200. describe( 'should be extensible', () => {
  201. it( 'converters in the data pipeline', () => {
  202. blockDefaultConversion( editor.data.downcastDispatcher );
  203. blockDefaultConversion( editor.editing.downcastDispatcher );
  204. const data = '<p style="text-align:justify;">x</p>';
  205. editor.setData( data );
  206. expect( getModelData( model ) ).to.equal( '<paragraph alignment="justify">[]x</paragraph>' );
  207. expect( editor.getData() ).to.equal( '<p>x</p>' );
  208. editor.execute( 'alignment' );
  209. expect( editor.getData() ).to.equal( '<p>x</p>' );
  210. } );
  211. } );
  212. describe( 'should work with broken styles', () => {
  213. it( 'should ignore empty style', () => {
  214. const data = '<p style="text-align:">x</p>';
  215. editor.setData( data );
  216. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  217. expect( editor.getData() ).to.equal( '<p>x</p>' );
  218. } );
  219. it( 'should ignore not known style', () => {
  220. const data = '<p style="text-align:unset;">x</p>';
  221. editor.setData( data );
  222. expect( getModelData( model ) ).to.equal( '<paragraph>[]x</paragraph>' );
  223. expect( editor.getData() ).to.equal( '<p>x</p>' );
  224. } );
  225. } );
  226. describe( 'config', () => {
  227. describe( 'options', () => {
  228. describe( 'default value', () => {
  229. it( 'should be set', () => {
  230. expect( editor.config.get( 'alignment.options' ) ).to.deep.equal( [ 'left', 'right', 'center', 'justify' ] );
  231. } );
  232. } );
  233. } );
  234. } );
  235. } );
  236. function blockDefaultConversion( dispatcher ) {
  237. dispatcher.on( 'attribute:alignment', ( evt, data, conversionApi ) => {
  238. conversionApi.consumable.consume( data.item, evt.name );
  239. }, { 'priority': 'high' } );
  240. }