indentblock.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import HeadingEditing from '@ckeditor/ckeditor5-heading/src/headingediting';
  10. import IndentEditing from '../src/indentediting';
  11. import IndentBlock from '../src/indentblock';
  12. import IndentBlockCommand from '../src/indentblockcommand';
  13. describe( 'IndentBlock', () => {
  14. let editor, model, doc;
  15. testUtils.createSinonSandbox();
  16. afterEach( () => {
  17. if ( editor ) {
  18. return editor.destroy();
  19. }
  20. } );
  21. it( 'should be named', () => {
  22. expect( IndentBlock.pluginName ).to.equal( 'IndentBlock' );
  23. } );
  24. it( 'should be loaded', () => {
  25. return createTestEditor()
  26. .then( newEditor => {
  27. expect( newEditor.plugins.get( IndentBlock ) ).to.be.instanceOf( IndentBlock );
  28. } );
  29. } );
  30. it( 'should set proper schema rules', () => {
  31. return createTestEditor()
  32. .then( newEditor => {
  33. model = newEditor.model;
  34. expect( model.schema.checkAttribute( [ 'paragraph' ], 'blockIndent' ) ).to.be.true;
  35. expect( model.schema.checkAttribute( [ 'heading1' ], 'blockIndent' ) ).to.be.true;
  36. expect( model.schema.checkAttribute( [ 'heading2' ], 'blockIndent' ) ).to.be.true;
  37. expect( model.schema.checkAttribute( [ 'heading3' ], 'blockIndent' ) ).to.be.true;
  38. expect( model.schema.getAttributeProperties( 'blockIndent' ) ).to.deep.equal( { isFormatting: true } );
  39. } );
  40. } );
  41. it( 'should register indent block command', () => {
  42. return createTestEditor()
  43. .then( newEditor => {
  44. const command = newEditor.commands.get( 'indentBlock' );
  45. expect( command ).to.be.instanceof( IndentBlockCommand );
  46. } );
  47. } );
  48. describe( 'config', () => {
  49. describe( 'default value', () => {
  50. it( 'should be set', () => {
  51. return createTestEditor().then( editor => {
  52. expect( editor.config.get( 'indentBlock' ) ).to.deep.equal( { offset: 40, unit: 'px' } );
  53. } );
  54. } );
  55. } );
  56. } );
  57. describe( 'conversion', () => {
  58. describe( 'using offset', () => {
  59. describe( 'left–to–right content', () => {
  60. beforeEach( () => {
  61. return createTestEditor( {
  62. indentBlock: { offset: 50, unit: 'px' }
  63. } ).then( newEditor => {
  64. editor = newEditor;
  65. model = editor.model;
  66. doc = model.document;
  67. } );
  68. } );
  69. it( 'should convert margin-left to indent attribute (known offset)', () => {
  70. editor.setData( '<p style="margin-left:50px">foo</p>' );
  71. const paragraph = doc.getRoot().getChild( 0 );
  72. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  73. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '50px' );
  74. expect( editor.getData() ).to.equal( '<p style="margin-left:50px;">foo</p>' );
  75. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  76. .to.equal( '<p style="margin-left:50px">foo</p>' );
  77. } );
  78. it( 'should convert margin-left to indent attribute (any offset)', () => {
  79. editor.setData( '<p style="margin-left:42em">foo</p>' );
  80. const paragraph = doc.getRoot().getChild( 0 );
  81. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  82. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '42em' );
  83. expect( editor.getData() ).to.equal( '<p style="margin-left:42em;">foo</p>' );
  84. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  85. .to.equal( '<p style="margin-left:42em">foo</p>' );
  86. } );
  87. it( 'should convert margin shortcut to indent attribute (one entry)', () => {
  88. editor.setData( '<p style="margin:42em">foo</p>' );
  89. const paragraph = doc.getRoot().getChild( 0 );
  90. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  91. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '42em' );
  92. expect( editor.getData() ).to.equal( '<p style="margin-left:42em;">foo</p>' );
  93. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  94. .to.equal( '<p style="margin-left:42em">foo</p>' );
  95. } );
  96. it( 'should convert margin shortcut to indent attribute (two entries)', () => {
  97. editor.setData( '<p style="margin:24em 42em">foo</p>' );
  98. const paragraph = doc.getRoot().getChild( 0 );
  99. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  100. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '42em' );
  101. expect( editor.getData() ).to.equal( '<p style="margin-left:42em;">foo</p>' );
  102. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  103. .to.equal( '<p style="margin-left:42em">foo</p>' );
  104. } );
  105. it( 'should convert margin shortcut to indent attribute (three entries)', () => {
  106. editor.setData( '<p style="margin:24em 42em 20em">foo</p>' );
  107. const paragraph = doc.getRoot().getChild( 0 );
  108. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  109. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '42em' );
  110. expect( editor.getData() ).to.equal( '<p style="margin-left:42em;">foo</p>' );
  111. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  112. .to.equal( '<p style="margin-left:42em">foo</p>' );
  113. } );
  114. it( 'should convert margin shortcut to indent attribute (four entries)', () => {
  115. editor.setData( '<p style="margin:24em 40em 24em 42em">foo</p>' );
  116. const paragraph = doc.getRoot().getChild( 0 );
  117. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  118. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '42em' );
  119. expect( editor.getData() ).to.equal( '<p style="margin-left:42em;">foo</p>' );
  120. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  121. .to.equal( '<p style="margin-left:42em">foo</p>' );
  122. } );
  123. } );
  124. describe( 'right–to–left content', () => {
  125. beforeEach( () => {
  126. return createTestEditor( {
  127. indentBlock: { offset: 50, unit: 'px' },
  128. language: {
  129. content: 'ar'
  130. }
  131. } ).then( newEditor => {
  132. editor = newEditor;
  133. model = editor.model;
  134. doc = model.document;
  135. } );
  136. } );
  137. it( 'should convert margin-right to indent attribute (known offset)', () => {
  138. editor.setData( '<p style="margin-right:50px">foo</p>' );
  139. const paragraph = doc.getRoot().getChild( 0 );
  140. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  141. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '50px' );
  142. expect( editor.getData() ).to.equal( '<p style="margin-right:50px;">foo</p>' );
  143. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  144. .to.equal( '<p style="margin-right:50px">foo</p>' );
  145. } );
  146. it( 'should convert margin-right to indent attribute (any offset)', () => {
  147. editor.setData( '<p style="margin-right:42em">foo</p>' );
  148. const paragraph = doc.getRoot().getChild( 0 );
  149. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  150. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '42em' );
  151. expect( editor.getData() ).to.equal( '<p style="margin-right:42em;">foo</p>' );
  152. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  153. .to.equal( '<p style="margin-right:42em">foo</p>' );
  154. } );
  155. it( 'should convert margin shortcut to indent attribute (one entry)', () => {
  156. editor.setData( '<p style="margin:42em">foo</p>' );
  157. const paragraph = doc.getRoot().getChild( 0 );
  158. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  159. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '42em' );
  160. expect( editor.getData() ).to.equal( '<p style="margin-right:42em;">foo</p>' );
  161. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  162. .to.equal( '<p style="margin-right:42em">foo</p>' );
  163. } );
  164. it( 'should convert margin shortcut to indent attribute (two entries)', () => {
  165. editor.setData( '<p style="margin:24em 42em">foo</p>' );
  166. const paragraph = doc.getRoot().getChild( 0 );
  167. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  168. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '42em' );
  169. expect( editor.getData() ).to.equal( '<p style="margin-right:42em;">foo</p>' );
  170. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  171. .to.equal( '<p style="margin-right:42em">foo</p>' );
  172. } );
  173. it( 'should convert margin shortcut to indent attribute (three entries)', () => {
  174. editor.setData( '<p style="margin:24em 42em 20em">foo</p>' );
  175. const paragraph = doc.getRoot().getChild( 0 );
  176. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  177. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '42em' );
  178. expect( editor.getData() ).to.equal( '<p style="margin-right:42em;">foo</p>' );
  179. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  180. .to.equal( '<p style="margin-right:42em">foo</p>' );
  181. } );
  182. it( 'should convert margin shortcut to indent attribute (four entries)', () => {
  183. editor.setData( '<p style="margin:24em 40em 24em 42em">foo</p>' );
  184. const paragraph = doc.getRoot().getChild( 0 );
  185. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  186. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '40em' );
  187. expect( editor.getData() ).to.equal( '<p style="margin-right:40em;">foo</p>' );
  188. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  189. .to.equal( '<p style="margin-right:40em">foo</p>' );
  190. } );
  191. } );
  192. it( 'should not convert class to indent attribute', () => {
  193. return createTestEditor( {
  194. indentBlock: { offset: 50, unit: 'px' }
  195. } ).then( newEditor => {
  196. editor = newEditor;
  197. model = editor.model;
  198. doc = model.document;
  199. editor.setData( '<p class="indent-1">foo</p>' );
  200. const paragraph = doc.getRoot().getChild( 0 );
  201. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.false;
  202. const expectedView = '<p>foo</p>';
  203. expect( editor.getData() ).to.equal( expectedView );
  204. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  205. } );
  206. } );
  207. } );
  208. describe( 'using classes', () => {
  209. beforeEach( () => {
  210. return createTestEditor( {
  211. indentBlock: {
  212. classes: [ 'indent-1', 'indent-2', 'indent-3', 'indent-4' ]
  213. }
  214. } ).then( newEditor => {
  215. editor = newEditor;
  216. model = editor.model;
  217. doc = model.document;
  218. } );
  219. } );
  220. it( 'should convert class to indent attribute', () => {
  221. editor.setData( '<p class="indent-1">foo</p>' );
  222. const paragraph = doc.getRoot().getChild( 0 );
  223. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  224. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( 'indent-1' );
  225. const expectedView = '<p class="indent-1">foo</p>';
  226. expect( editor.getData() ).to.equal( expectedView );
  227. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  228. } );
  229. it( 'should not convert unknown class to indent attribute', () => {
  230. editor.setData( '<p class="indent-7">foo</p>' );
  231. const paragraph = doc.getRoot().getChild( 0 );
  232. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.false;
  233. const expectedView = '<p>foo</p>';
  234. expect( editor.getData() ).to.equal( expectedView );
  235. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  236. } );
  237. it( 'should not convert margin-left to indent attribute (known offset)', () => {
  238. editor.setData( '<p style="margin-left:50px">foo</p>' );
  239. const paragraph = doc.getRoot().getChild( 0 );
  240. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.false;
  241. const expectedView = '<p>foo</p>';
  242. expect( editor.getData() ).to.equal( expectedView );
  243. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  244. } );
  245. } );
  246. } );
  247. function createTestEditor( extraConfig = {} ) {
  248. return VirtualTestEditor
  249. .create( Object.assign( {
  250. plugins: [ Paragraph, HeadingEditing, IndentEditing, IndentBlock ]
  251. }, extraConfig ) );
  252. }
  253. } );