indentblock.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 convert margin shortcut to indent attribute (one entry)', () => {
  92. editor.setData( '<p style="margin:42em">foo</p>' );
  93. const paragraph = doc.getRoot().getChild( 0 );
  94. expect( paragraph.hasAttribute( 'indent' ) ).to.be.true;
  95. expect( paragraph.getAttribute( 'indent' ) ).to.equal( '42em' );
  96. expect( editor.getData() ).to.equal( '<p style="margin-left:42em;">foo</p>' );
  97. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  98. .to.equal( '<p style="margin-left:42em">foo</p>' );
  99. } );
  100. it( 'should convert margin shortcut to indent attribute (two entries)', () => {
  101. editor.setData( '<p style="margin:24em 42em">foo</p>' );
  102. const paragraph = doc.getRoot().getChild( 0 );
  103. expect( paragraph.hasAttribute( 'indent' ) ).to.be.true;
  104. expect( paragraph.getAttribute( 'indent' ) ).to.equal( '42em' );
  105. expect( editor.getData() ).to.equal( '<p style="margin-left:42em;">foo</p>' );
  106. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  107. .to.equal( '<p style="margin-left:42em">foo</p>' );
  108. } );
  109. it( 'should convert margin shortcut to indent attribute (three entries)', () => {
  110. editor.setData( '<p style="margin:24em 42em 20em">foo</p>' );
  111. const paragraph = doc.getRoot().getChild( 0 );
  112. expect( paragraph.hasAttribute( 'indent' ) ).to.be.true;
  113. expect( paragraph.getAttribute( 'indent' ) ).to.equal( '42em' );
  114. expect( editor.getData() ).to.equal( '<p style="margin-left:42em;">foo</p>' );
  115. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  116. .to.equal( '<p style="margin-left:42em">foo</p>' );
  117. } );
  118. it( 'should convert margin shortcut to indent attribute (four entries)', () => {
  119. editor.setData( '<p style="margin:24em 40em 24em 42em">foo</p>' );
  120. const paragraph = doc.getRoot().getChild( 0 );
  121. expect( paragraph.hasAttribute( 'indent' ) ).to.be.true;
  122. expect( paragraph.getAttribute( 'indent' ) ).to.equal( '42em' );
  123. expect( editor.getData() ).to.equal( '<p style="margin-left:42em;">foo</p>' );
  124. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  125. .to.equal( '<p style="margin-left:42em">foo</p>' );
  126. } );
  127. it( 'should not convert class to indent attribute', () => {
  128. editor.setData( '<p class="indent-1">foo</p>' );
  129. const paragraph = doc.getRoot().getChild( 0 );
  130. expect( paragraph.hasAttribute( 'indent' ) ).to.be.false;
  131. const expectedView = '<p>foo</p>';
  132. expect( editor.getData() ).to.equal( expectedView );
  133. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  134. } );
  135. } );
  136. describe( 'using classes', () => {
  137. beforeEach( () => {
  138. return createTestEditor( {
  139. indentBlock: {
  140. classes: [ 'indent-1', 'indent-2', 'indent-3', 'indent-4' ]
  141. }
  142. } ).then( newEditor => {
  143. editor = newEditor;
  144. model = editor.model;
  145. doc = model.document;
  146. } );
  147. } );
  148. it( 'should convert class to indent attribute', () => {
  149. editor.setData( '<p class="indent-1">foo</p>' );
  150. const paragraph = doc.getRoot().getChild( 0 );
  151. expect( paragraph.hasAttribute( 'indent' ) ).to.be.true;
  152. expect( paragraph.getAttribute( 'indent' ) ).to.equal( 'indent-1' );
  153. const expectedView = '<p class="indent-1">foo</p>';
  154. expect( editor.getData() ).to.equal( expectedView );
  155. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  156. } );
  157. it( 'should not convert unknown class to indent attribute', () => {
  158. editor.setData( '<p class="indent-7">foo</p>' );
  159. const paragraph = doc.getRoot().getChild( 0 );
  160. expect( paragraph.hasAttribute( 'indent' ) ).to.be.false;
  161. const expectedView = '<p>foo</p>';
  162. expect( editor.getData() ).to.equal( expectedView );
  163. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  164. } );
  165. it( 'should not convert margin-left to indent attribute (known offset)', () => {
  166. editor.setData( '<p style="margin-left:50px">foo</p>' );
  167. const paragraph = doc.getRoot().getChild( 0 );
  168. expect( paragraph.hasAttribute( 'indent' ) ).to.be.false;
  169. const expectedView = '<p>foo</p>';
  170. expect( editor.getData() ).to.equal( expectedView );
  171. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  172. } );
  173. } );
  174. } );
  175. describe( 'tab key handling callback', () => {
  176. let domEvtDataStub;
  177. beforeEach( () => {
  178. return createTestEditor()
  179. .then( newEditor => {
  180. editor = newEditor;
  181. model = editor.model;
  182. doc = model.document;
  183. domEvtDataStub = {
  184. keyCode: getCode( 'Tab' ),
  185. preventDefault: sinon.spy(),
  186. stopPropagation: sinon.spy()
  187. };
  188. sinon.spy( editor, 'execute' );
  189. } );
  190. } );
  191. it( 'should execute indentBlock command on tab key', () => {
  192. editor.setData( '<p>foo</p>' );
  193. editor.model.change( writer => writer.setSelection( doc.getRoot().getChild( 0 ), 0 ) );
  194. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  195. sinon.assert.calledOnce( editor.execute );
  196. sinon.assert.calledWithExactly( editor.execute, 'indentBlock' );
  197. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  198. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  199. } );
  200. it( 'should execute outdentBlock command on Shift+Tab keystroke', () => {
  201. domEvtDataStub.keyCode += getCode( 'Shift' );
  202. editor.setData( '<p style="margin-left:1em;">foo</p>' );
  203. editor.model.change( writer => writer.setSelection( doc.getRoot().getChild( 0 ), 0 ) );
  204. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  205. sinon.assert.calledOnce( editor.execute );
  206. sinon.assert.calledWithExactly( editor.execute, 'outdentBlock' );
  207. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  208. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  209. } );
  210. it( 'should not indent if command is disabled', () => {
  211. editor.model.schema.register( 'block', { inheritAllFrom: '$block' } );
  212. editor.conversion.elementToElement( { model: 'block', view: 'block' } );
  213. editor.setData( '<block>foo</block>' );
  214. editor.model.change( writer => writer.setSelection( doc.getRoot().getChild( 0 ), 0 ) );
  215. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  216. expect( editor.execute.called ).to.be.false;
  217. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  218. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  219. } );
  220. it( 'should not indent or outdent if alt+tab is pressed', () => {
  221. domEvtDataStub.keyCode += getCode( 'alt' );
  222. editor.setData( '<p style="margin-left:1em;">foo</p>' );
  223. editor.model.change( writer => writer.setSelection( doc.getRoot().getChild( 0 ), 0 ) );
  224. editor.editing.view.document.fire( 'keydown', domEvtDataStub );
  225. expect( editor.execute.called ).to.be.false;
  226. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  227. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  228. } );
  229. } );
  230. function createTestEditor( extraConfig = {} ) {
  231. return ClassicTestEditor
  232. .create( element, Object.assign( {
  233. plugins: [ Paragraph, HeadingEditing, Indent, IndentBlock ]
  234. }, extraConfig ) );
  235. }
  236. } );