8
0

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