8
0

indentblock.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 IndentBlock from '../src/indentblock';
  13. import IndentBlockCommand from '../src/indentblockcommand';
  14. class Indent extends Plugin {
  15. init() {
  16. const editor = this.editor;
  17. editor.commands.add( 'indent', new MultiCommand( editor ) );
  18. editor.commands.add( 'outdent', new MultiCommand( editor ) );
  19. }
  20. }
  21. describe( 'IndentBlock', () => {
  22. let editor, model, doc;
  23. testUtils.createSinonSandbox();
  24. afterEach( () => {
  25. if ( editor ) {
  26. return editor.destroy();
  27. }
  28. } );
  29. it( 'should be named', () => {
  30. expect( IndentBlock.pluginName ).to.equal( 'IndentBlock' );
  31. } );
  32. it( 'should be loaded', () => {
  33. return createTestEditor()
  34. .then( newEditor => {
  35. expect( newEditor.plugins.get( IndentBlock ) ).to.be.instanceOf( IndentBlock );
  36. } );
  37. } );
  38. it( 'should set proper schema rules', () => {
  39. return createTestEditor()
  40. .then( newEditor => {
  41. model = newEditor.model;
  42. expect( model.schema.checkAttribute( [ 'heading1' ], 'indent' ) ).to.be.true;
  43. expect( model.schema.checkAttribute( [ 'paragraph' ], 'indent' ) ).to.be.true;
  44. } );
  45. } );
  46. it( 'should register indent block command', () => {
  47. return createTestEditor()
  48. .then( newEditor => {
  49. const command = newEditor.commands.get( 'indentBlock' );
  50. expect( command ).to.be.instanceof( IndentBlockCommand );
  51. } );
  52. } );
  53. describe( 'conversion', () => {
  54. describe( 'using offset', () => {
  55. beforeEach( () => {
  56. return createTestEditor( { indentBlock: { offset: 50, unit: 'px' } } )
  57. .then( newEditor => {
  58. editor = newEditor;
  59. model = editor.model;
  60. doc = model.document;
  61. } );
  62. } );
  63. it( 'should convert margin-left to indent attribute', () => {
  64. editor.setData( '<p style="margin-left:50px">foo</p>' );
  65. const paragraph = doc.getRoot().getChild( 0 );
  66. expect( paragraph.hasAttribute( 'indent' ) ).to.be.true;
  67. expect( paragraph.getAttribute( 'indent' ) ).to.equal( '50px' );
  68. expect( editor.getData() ).to.equal( '<p style="margin-left:50px;">foo</p>' );
  69. expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
  70. .to.equal( '<p style="margin-left:50px">foo</p>' );
  71. } );
  72. } );
  73. describe( 'using classes', () => {
  74. beforeEach( () => {
  75. return createTestEditor( {
  76. indentBlock: {
  77. classes: [ 'indent-1', 'indent-2', 'indent-3', 'indent-4' ]
  78. }
  79. } ).then( newEditor => {
  80. editor = newEditor;
  81. model = editor.model;
  82. doc = model.document;
  83. } );
  84. } );
  85. it( 'should convert class to indent attribute', () => {
  86. editor.setData( '<p class="indent-1">foo</p>' );
  87. const paragraph = doc.getRoot().getChild( 0 );
  88. expect( paragraph.hasAttribute( 'indent' ) ).to.be.true;
  89. expect( paragraph.getAttribute( 'indent' ) ).to.equal( 'indent-1' );
  90. const expectedView = '<p class="indent-1">foo</p>';
  91. expect( editor.getData() ).to.equal( expectedView );
  92. expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( expectedView );
  93. } );
  94. } );
  95. } );
  96. function createTestEditor( extraConfig = {} ) {
  97. return VirtualTestEditor
  98. .create( Object.assign( {
  99. plugins: [ Paragraph, HeadingEditing, Indent, IndentBlock ]
  100. }, extraConfig ) );
  101. }
  102. } );