indentblock.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  11. import MultiCommand from '@ckeditor/ckeditor5-core/src/multicommand';
  12. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  13. import IndentBlock from '../src/indentblock';
  14. import IndentBlockCommand from '../src/indentblockcommand';
  15. class Indent extends Plugin {
  16. init() {
  17. const editor = this.editor;
  18. editor.commands.add( 'indent', new MultiCommand( editor ) );
  19. editor.commands.add( 'outdent', new MultiCommand( editor ) );
  20. }
  21. }
  22. describe( 'IndentBlock', () => {
  23. let editor, model, doc;
  24. testUtils.createSinonSandbox();
  25. afterEach( () => {
  26. if ( editor ) {
  27. return editor.destroy();
  28. }
  29. } );
  30. it( 'should be named', () => {
  31. expect( IndentBlock.pluginName ).to.equal( 'IndentBlock' );
  32. } );
  33. it( 'should be loaded', () => {
  34. return createTestEditor()
  35. .then( newEditor => {
  36. expect( newEditor.plugins.get( IndentBlock ) ).to.be.instanceOf( IndentBlock );
  37. } );
  38. } );
  39. it( 'should set proper schema rules', () => {
  40. return createTestEditor()
  41. .then( newEditor => {
  42. model = newEditor.model;
  43. expect( model.schema.checkAttribute( [ 'paragraph' ], 'indent' ) ).to.be.true;
  44. expect( model.schema.checkAttribute( [ 'heading1' ], 'indent' ) ).to.be.true;
  45. expect( model.schema.checkAttribute( [ 'heading2' ], 'indent' ) ).to.be.true;
  46. expect( model.schema.checkAttribute( [ 'heading3' ], 'indent' ) ).to.be.true;
  47. } );
  48. } );
  49. it( 'should register indent block command', () => {
  50. return createTestEditor()
  51. .then( newEditor => {
  52. const command = newEditor.commands.get( 'indentBlock' );
  53. expect( command ).to.be.instanceof( IndentBlockCommand );
  54. } );
  55. } );
  56. describe( 'config', () => {
  57. describe( 'default value', () => {
  58. it( 'should be set', () => {
  59. return createTestEditor().then( editor => {
  60. expect( editor.config.get( 'indentBlock' ) ).to.deep.equal( { offset: 1, unit: 'em' } );
  61. } );
  62. } );
  63. } );
  64. } );
  65. describe( 'conversion', () => {
  66. describe( 'using offset', () => {
  67. beforeEach( () => {
  68. return createTestEditor( { indentBlock: { offset: 50, unit: 'px' } } )
  69. .then( newEditor => {
  70. editor = newEditor;
  71. model = editor.model;
  72. doc = model.document;
  73. } );
  74. } );
  75. it( 'should convert margin-left to indent attribute (known offset)', () => {
  76. editor.setData( '<p style="margin-left:50px">foo</p>' );
  77. const paragraph = doc.getRoot().getChild( 0 );
  78. expect( paragraph.hasAttribute( 'indent' ) ).to.be.true;
  79. expect( paragraph.getAttribute( 'indent' ) ).to.equal( '50px' );
  80. expect( editor.getData() ).to.equal( '<p style="margin-left:50px;">foo</p>' );
  81. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  82. .to.equal( '<p style="margin-left:50px">foo</p>' );
  83. } );
  84. it( 'should convert margin-left to indent attribute (any offset)', () => {
  85. editor.setData( '<p style="margin-left:42em">foo</p>' );
  86. const paragraph = doc.getRoot().getChild( 0 );
  87. expect( paragraph.hasAttribute( 'indent' ) ).to.be.true;
  88. expect( paragraph.getAttribute( 'indent' ) ).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 not convert class to indent attribute', () => {
  94. editor.setData( '<p class="indent-1">foo</p>' );
  95. const paragraph = doc.getRoot().getChild( 0 );
  96. expect( paragraph.hasAttribute( 'indent' ) ).to.be.false;
  97. const expectedView = '<p>foo</p>';
  98. expect( editor.getData() ).to.equal( expectedView );
  99. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  100. } );
  101. } );
  102. describe( 'using classes', () => {
  103. beforeEach( () => {
  104. return createTestEditor( {
  105. indentBlock: {
  106. classes: [ 'indent-1', 'indent-2', 'indent-3', 'indent-4' ]
  107. }
  108. } ).then( newEditor => {
  109. editor = newEditor;
  110. model = editor.model;
  111. doc = model.document;
  112. } );
  113. } );
  114. it( 'should convert class to indent attribute', () => {
  115. editor.setData( '<p class="indent-1">foo</p>' );
  116. const paragraph = doc.getRoot().getChild( 0 );
  117. expect( paragraph.hasAttribute( 'indent' ) ).to.be.true;
  118. expect( paragraph.getAttribute( 'indent' ) ).to.equal( 'indent-1' );
  119. const expectedView = '<p class="indent-1">foo</p>';
  120. expect( editor.getData() ).to.equal( expectedView );
  121. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  122. } );
  123. it( 'should not convert unknown class to indent attribute', () => {
  124. editor.setData( '<p class="indent-7">foo</p>' );
  125. const paragraph = doc.getRoot().getChild( 0 );
  126. expect( paragraph.hasAttribute( 'indent' ) ).to.be.false;
  127. const expectedView = '<p>foo</p>';
  128. expect( editor.getData() ).to.equal( expectedView );
  129. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  130. } );
  131. it( 'should not convert margin-left to indent attribute (known offset)', () => {
  132. editor.setData( '<p style="margin-left:50px">foo</p>' );
  133. const paragraph = doc.getRoot().getChild( 0 );
  134. expect( paragraph.hasAttribute( 'indent' ) ).to.be.false;
  135. const expectedView = '<p>foo</p>';
  136. expect( editor.getData() ).to.equal( expectedView );
  137. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  138. } );
  139. } );
  140. } );
  141. describe( 'tab key handling callback', () => {
  142. let domEvtDataStub;
  143. beforeEach( () => {
  144. return createTestEditor()
  145. .then( newEditor => {
  146. editor = newEditor;
  147. model = editor.model;
  148. doc = model.document;
  149. domEvtDataStub = {
  150. keyCode: getCode( 'Tab' ),
  151. preventDefault: sinon.spy(),
  152. stopPropagation: sinon.spy()
  153. };
  154. sinon.spy( editor, 'execute' );
  155. } );
  156. } );
  157. it( 'should execute indentBlock command on tab key', () => {
  158. editor.setData( '<p>foo</p>' );
  159. editor.model.change( writer => writer.setSelection( doc.getRoot().getChild( 0 ), 0 ) );
  160. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  161. sinon.assert.calledOnce( editor.execute );
  162. sinon.assert.calledWithExactly( editor.execute, 'indentBlock' );
  163. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  164. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  165. } );
  166. it( 'should execute outdentBlock command on Shift+Tab keystroke', () => {
  167. domEvtDataStub.keyCode += getCode( 'Shift' );
  168. editor.setData( '<p style="margin-left:1em;">foo</p>' );
  169. editor.model.change( writer => writer.setSelection( doc.getRoot().getChild( 0 ), 0 ) );
  170. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  171. sinon.assert.calledOnce( editor.execute );
  172. sinon.assert.calledWithExactly( editor.execute, 'outdentBlock' );
  173. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  174. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  175. } );
  176. it( 'should not indent if command is disabled', () => {
  177. editor.model.schema.register( 'block', { inheritAllFrom: '$block' } );
  178. editor.conversion.elementToElement( { model: 'block', view: 'block' } );
  179. editor.setData( '<block>foo</block>' );
  180. editor.model.change( writer => writer.setSelection( doc.getRoot().getChild( 0 ), 0 ) );
  181. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  182. expect( editor.execute.called ).to.be.false;
  183. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  184. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  185. } );
  186. it( 'should not indent or outdent if alt+tab is pressed', () => {
  187. domEvtDataStub.keyCode += getCode( 'alt' );
  188. editor.setData( '<p style="margin-left:1em;">foo</p>' );
  189. editor.model.change( writer => writer.setSelection( doc.getRoot().getChild( 0 ), 0 ) );
  190. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  191. expect( editor.execute.called ).to.be.false;
  192. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  193. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  194. } );
  195. } );
  196. function createTestEditor( extraConfig = {} ) {
  197. return VirtualTestEditor
  198. .create( Object.assign( {
  199. plugins: [ Paragraph, HeadingEditing, Indent, IndentBlock ]
  200. }, extraConfig ) );
  201. }
  202. } );