8
0

indentblock.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  11. import IndentEditing from '../src/indentediting';
  12. import IndentBlock from '../src/indentblock';
  13. import IndentBlockCommand from '../src/indentblockcommand';
  14. describe( 'IndentBlock', () => {
  15. let editor, model, doc;
  16. testUtils.createSinonSandbox();
  17. afterEach( () => {
  18. if ( editor ) {
  19. return editor.destroy();
  20. }
  21. } );
  22. it( 'should be named', () => {
  23. expect( IndentBlock.pluginName ).to.equal( 'IndentBlock' );
  24. } );
  25. it( 'should be loaded', () => {
  26. return createTestEditor()
  27. .then( newEditor => {
  28. expect( newEditor.plugins.get( IndentBlock ) ).to.be.instanceOf( IndentBlock );
  29. } );
  30. } );
  31. it( 'should set proper schema rules', () => {
  32. return createTestEditor()
  33. .then( newEditor => {
  34. model = newEditor.model;
  35. expect( model.schema.checkAttribute( [ 'paragraph' ], 'blockIndent' ) ).to.be.true;
  36. expect( model.schema.checkAttribute( [ 'heading1' ], 'blockIndent' ) ).to.be.true;
  37. expect( model.schema.checkAttribute( [ 'heading2' ], 'blockIndent' ) ).to.be.true;
  38. expect( model.schema.checkAttribute( [ 'heading3' ], 'blockIndent' ) ).to.be.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: 1, unit: 'em' } );
  53. } );
  54. } );
  55. } );
  56. } );
  57. describe( 'conversion', () => {
  58. describe( 'using offset', () => {
  59. beforeEach( () => {
  60. return createTestEditor( { indentBlock: { offset: 50, unit: 'px' } } )
  61. .then( newEditor => {
  62. editor = newEditor;
  63. model = editor.model;
  64. doc = model.document;
  65. } );
  66. } );
  67. it( 'should convert margin-left to indent attribute (known offset)', () => {
  68. editor.setData( '<p style="margin-left:50px">foo</p>' );
  69. const paragraph = doc.getRoot().getChild( 0 );
  70. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  71. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '50px' );
  72. expect( editor.getData() ).to.equal( '<p style="margin-left:50px;">foo</p>' );
  73. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  74. .to.equal( '<p style="margin-left:50px">foo</p>' );
  75. } );
  76. it( 'should convert margin-left to indent attribute (any offset)', () => {
  77. editor.setData( '<p style="margin-left:42em">foo</p>' );
  78. const paragraph = doc.getRoot().getChild( 0 );
  79. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  80. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '42em' );
  81. expect( editor.getData() ).to.equal( '<p style="margin-left:42em;">foo</p>' );
  82. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  83. .to.equal( '<p style="margin-left:42em">foo</p>' );
  84. } );
  85. it( 'should convert margin shortcut to indent attribute (one entry)', () => {
  86. editor.setData( '<p style="margin:42em">foo</p>' );
  87. const paragraph = doc.getRoot().getChild( 0 );
  88. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  89. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '42em' );
  90. expect( editor.getData() ).to.equal( '<p style="margin-left:42em;">foo</p>' );
  91. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  92. .to.equal( '<p style="margin-left:42em">foo</p>' );
  93. } );
  94. it( 'should convert margin shortcut to indent attribute (two entries)', () => {
  95. editor.setData( '<p style="margin:24em 42em">foo</p>' );
  96. const paragraph = doc.getRoot().getChild( 0 );
  97. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  98. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '42em' );
  99. expect( editor.getData() ).to.equal( '<p style="margin-left:42em;">foo</p>' );
  100. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  101. .to.equal( '<p style="margin-left:42em">foo</p>' );
  102. } );
  103. it( 'should convert margin shortcut to indent attribute (three entries)', () => {
  104. editor.setData( '<p style="margin:24em 42em 20em">foo</p>' );
  105. const paragraph = doc.getRoot().getChild( 0 );
  106. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  107. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '42em' );
  108. expect( editor.getData() ).to.equal( '<p style="margin-left:42em;">foo</p>' );
  109. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  110. .to.equal( '<p style="margin-left:42em">foo</p>' );
  111. } );
  112. it( 'should convert margin shortcut to indent attribute (four entries)', () => {
  113. editor.setData( '<p style="margin:24em 40em 24em 42em">foo</p>' );
  114. const paragraph = doc.getRoot().getChild( 0 );
  115. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  116. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( '42em' );
  117. expect( editor.getData() ).to.equal( '<p style="margin-left:42em;">foo</p>' );
  118. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  119. .to.equal( '<p style="margin-left:42em">foo</p>' );
  120. } );
  121. it( 'should not convert class to indent attribute', () => {
  122. editor.setData( '<p class="indent-1">foo</p>' );
  123. const paragraph = doc.getRoot().getChild( 0 );
  124. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.false;
  125. const expectedView = '<p>foo</p>';
  126. expect( editor.getData() ).to.equal( expectedView );
  127. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  128. } );
  129. } );
  130. describe( 'using classes', () => {
  131. beforeEach( () => {
  132. return createTestEditor( {
  133. indentBlock: {
  134. classes: [ 'indent-1', 'indent-2', 'indent-3', 'indent-4' ]
  135. }
  136. } ).then( newEditor => {
  137. editor = newEditor;
  138. model = editor.model;
  139. doc = model.document;
  140. } );
  141. } );
  142. it( 'should convert class to indent attribute', () => {
  143. editor.setData( '<p class="indent-1">foo</p>' );
  144. const paragraph = doc.getRoot().getChild( 0 );
  145. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.true;
  146. expect( paragraph.getAttribute( 'blockIndent' ) ).to.equal( 'indent-1' );
  147. const expectedView = '<p class="indent-1">foo</p>';
  148. expect( editor.getData() ).to.equal( expectedView );
  149. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  150. } );
  151. it( 'should not convert unknown class to indent attribute', () => {
  152. editor.setData( '<p class="indent-7">foo</p>' );
  153. const paragraph = doc.getRoot().getChild( 0 );
  154. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.false;
  155. const expectedView = '<p>foo</p>';
  156. expect( editor.getData() ).to.equal( expectedView );
  157. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  158. } );
  159. it( 'should not convert margin-left to indent attribute (known offset)', () => {
  160. editor.setData( '<p style="margin-left:50px">foo</p>' );
  161. const paragraph = doc.getRoot().getChild( 0 );
  162. expect( paragraph.hasAttribute( 'blockIndent' ) ).to.be.false;
  163. const expectedView = '<p>foo</p>';
  164. expect( editor.getData() ).to.equal( expectedView );
  165. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  166. } );
  167. } );
  168. } );
  169. function createTestEditor( extraConfig = {} ) {
  170. return VirtualTestEditor
  171. .create( Object.assign( {
  172. plugins: [ Paragraph, HeadingEditing, IndentEditing, IndentBlock ]
  173. }, extraConfig ) );
  174. }
  175. } );