indentblock.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 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. beforeEach( () => {
  59. return createTestEditor( { indentBlock: { offset: 50, unit: 'px' } } )
  60. .then( newEditor => {
  61. editor = newEditor;
  62. model = editor.model;
  63. doc = model.document;
  64. } );
  65. } );
  66. it( 'should convert margin-left to indent attribute (known offset)', () => {
  67. editor.setData( '<p style="margin-left:50px">foo</p>' );
  68. const paragraph = doc.getRoot().getChild( 0 );
  69. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  70. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '50px' );
  71. expect( editor.getData() ).to.equal( '<p style="margin-left:50px;">foo</p>' );
  72. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  73. .to.equal( '<p style="margin-left:50px">foo</p>' );
  74. } );
  75. it( 'should convert margin-left to indent attribute (any offset)', () => {
  76. editor.setData( '<p style="margin-left:42em">foo</p>' );
  77. const paragraph = doc.getRoot().getChild( 0 );
  78. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  79. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '42em' );
  80. expect( editor.getData() ).to.equal( '<p style="margin-left:42em;">foo</p>' );
  81. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  82. .to.equal( '<p style="margin-left:42em">foo</p>' );
  83. } );
  84. it( 'should convert margin shortcut to indent attribute (one entry)', () => {
  85. editor.setData( '<p style="margin:42em">foo</p>' );
  86. const paragraph = doc.getRoot().getChild( 0 );
  87. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  88. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '42em' );
  89. expect( editor.getData() ).to.equal( '<p style="margin-left:42em;">foo</p>' );
  90. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  91. .to.equal( '<p style="margin-left:42em">foo</p>' );
  92. } );
  93. it( 'should convert margin shortcut to indent attribute (two entries)', () => {
  94. editor.setData( '<p style="margin:24em 42em">foo</p>' );
  95. const paragraph = doc.getRoot().getChild( 0 );
  96. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  97. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '42em' );
  98. expect( editor.getData() ).to.equal( '<p style="margin-left:42em;">foo</p>' );
  99. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  100. .to.equal( '<p style="margin-left:42em">foo</p>' );
  101. } );
  102. it( 'should convert margin shortcut to indent attribute (three entries)', () => {
  103. editor.setData( '<p style="margin:24em 42em 20em">foo</p>' );
  104. const paragraph = doc.getRoot().getChild( 0 );
  105. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  106. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '42em' );
  107. expect( editor.getData() ).to.equal( '<p style="margin-left:42em;">foo</p>' );
  108. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  109. .to.equal( '<p style="margin-left:42em">foo</p>' );
  110. } );
  111. it( 'should convert margin shortcut to indent attribute (four entries)', () => {
  112. editor.setData( '<p style="margin:24em 40em 24em 42em">foo</p>' );
  113. const paragraph = doc.getRoot().getChild( 0 );
  114. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  115. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '42em' );
  116. expect( editor.getData() ).to.equal( '<p style="margin-left:42em;">foo</p>' );
  117. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  118. .to.equal( '<p style="margin-left:42em">foo</p>' );
  119. } );
  120. it( 'should not convert class to indent attribute', () => {
  121. editor.setData( '<p class="indent-1">foo</p>' );
  122. const paragraph = doc.getRoot().getChild( 0 );
  123. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.false;
  124. const expectedView = '<p>foo</p>';
  125. expect( editor.getData() ).to.equal( expectedView );
  126. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  127. } );
  128. } );
  129. describe( 'using classes', () => {
  130. beforeEach( () => {
  131. return createTestEditor( {
  132. indentBlock: {
  133. classes: [ 'indent-1', 'indent-2', 'indent-3', 'indent-4' ]
  134. }
  135. } ).then( newEditor => {
  136. editor = newEditor;
  137. model = editor.model;
  138. doc = model.document;
  139. } );
  140. } );
  141. it( 'should convert class to indent attribute', () => {
  142. editor.setData( '<p class="indent-1">foo</p>' );
  143. const paragraph = doc.getRoot().getChild( 0 );
  144. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  145. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( 'indent-1' );
  146. const expectedView = '<p class="indent-1">foo</p>';
  147. expect( editor.getData() ).to.equal( expectedView );
  148. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  149. } );
  150. it( 'should not convert unknown class to indent attribute', () => {
  151. editor.setData( '<p class="indent-7">foo</p>' );
  152. const paragraph = doc.getRoot().getChild( 0 );
  153. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.false;
  154. const expectedView = '<p>foo</p>';
  155. expect( editor.getData() ).to.equal( expectedView );
  156. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  157. } );
  158. it( 'should not convert margin-left to indent attribute (known offset)', () => {
  159. editor.setData( '<p style="margin-left:50px">foo</p>' );
  160. const paragraph = doc.getRoot().getChild( 0 );
  161. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.false;
  162. const expectedView = '<p>foo</p>';
  163. expect( editor.getData() ).to.equal( expectedView );
  164. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  165. } );
  166. } );
  167. } );
  168. function createTestEditor( extraConfig = {} ) {
  169. return VirtualTestEditor
  170. .create( Object.assign( {
  171. plugins: [ Paragraph, HeadingEditing, IndentEditing, IndentBlock ]
  172. }, extraConfig ) );
  173. }
  174. } );