indentblock.js 12 KB

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