alignmentediting.js 9.2 KB

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