horizontallineediting.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @license Copyright (c) 2003-2020, 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 HorizontalLineEditing from '../src/horizontallineediting';
  7. import HorizontalLineCommand from '../src/horizontallinecommand';
  8. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  10. import { isWidget } from '@ckeditor/ckeditor5-widget/src/utils';
  11. import env from '@ckeditor/ckeditor5-utils/src/env';
  12. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  13. describe( 'HorizontalLineEditing', () => {
  14. let editor, model, view, viewDocument;
  15. testUtils.createSinonSandbox();
  16. beforeEach( () => {
  17. // Most tests assume non-edge environment but we do not set `contenteditable=false` on Edge so stub `env.isEdge`.
  18. testUtils.sinon.stub( env, 'isEdge' ).get( () => false );
  19. return VirtualTestEditor
  20. .create( {
  21. plugins: [ HorizontalLineEditing ]
  22. } )
  23. .then( newEditor => {
  24. editor = newEditor;
  25. model = editor.model;
  26. view = editor.editing.view;
  27. viewDocument = view.document;
  28. } );
  29. } );
  30. it( 'should have pluginName', () => {
  31. expect( HorizontalLineEditing.pluginName ).to.equal( 'HorizontalLineEditing' );
  32. } );
  33. it( 'should be loaded', () => {
  34. expect( editor.plugins.get( HorizontalLineEditing ) ).to.be.instanceOf( HorizontalLineEditing );
  35. } );
  36. it( 'should set proper schema lines', () => {
  37. expect( model.schema.checkChild( [ '$root' ], 'horizontalLine' ) ).to.be.true;
  38. expect( model.schema.isObject( 'horizontalLine' ) ).to.be.true;
  39. expect( model.schema.checkChild( [ '$root', 'horizontalLine' ], '$text' ) ).to.be.false;
  40. expect( model.schema.checkChild( [ '$root', '$block' ], 'horizontalLine' ) ).to.be.false;
  41. } );
  42. it( 'should register imageInsert command', () => {
  43. expect( editor.commands.get( 'horizontalLine' ) ).to.be.instanceOf( HorizontalLineCommand );
  44. } );
  45. describe( 'conversion in data pipeline', () => {
  46. describe( 'model to view', () => {
  47. it( 'should convert', () => {
  48. setModelData( model, '<horizontalLine></horizontalLine>' );
  49. expect( editor.getData() ).to.equal( '<hr>' );
  50. } );
  51. } );
  52. describe( 'view to model', () => {
  53. it( 'should convert the <hr> element', () => {
  54. editor.setData( '<hr>' );
  55. expect( getModelData( model, { withoutSelection: true } ) )
  56. .to.equal( '<horizontalLine></horizontalLine>' );
  57. } );
  58. it( 'should not convert in wrong context', () => {
  59. model.schema.register( 'div', { inheritAllFrom: '$block' } );
  60. model.schema.addChildCheck( ( ctx, childDef ) => {
  61. if ( ctx.endsWith( '$root' ) && childDef.name == 'horizontalLine' ) {
  62. return false;
  63. }
  64. } );
  65. editor.conversion.elementToElement( { model: 'div', view: 'div' } );
  66. editor.setData( '<div><hr></div>' );
  67. expect( getModelData( model, { withoutSelection: true } ) )
  68. .to.equal( '<div></div>' );
  69. } );
  70. } );
  71. } );
  72. describe( 'conversion in editing pipeline', () => {
  73. describe( 'model to view', () => {
  74. it( 'should convert', () => {
  75. setModelData( model, '<horizontalLine></horizontalLine>' );
  76. expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
  77. '<div class="ck-horizontal-line ck-widget" contenteditable="false"><hr></hr></div>'
  78. );
  79. } );
  80. it( 'converted element should be widgetized', () => {
  81. setModelData( model, '<horizontalLine></horizontalLine>' );
  82. const widget = viewDocument.getRoot().getChild( 0 );
  83. expect( widget.name ).to.equal( 'div' );
  84. expect( isHorizontalLineWidget( widget ) ).to.be.true;
  85. } );
  86. } );
  87. } );
  88. function isHorizontalLineWidget( viewElement ) {
  89. return !!viewElement.getCustomProperty( 'horizontalLine' ) && isWidget( viewElement );
  90. }
  91. } );