horizontallineediting.js 3.7 KB

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