/** * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license */ /* global console, document */ import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils'; import HTMLEmbedEditing from '../src/htmlembedediting'; import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor'; import HTMLEmbedUpdateCommand from '../src/htmlembedupdatecommand'; import HTMLEmbedInsertCommand from '../src/htmlembedinsertcommand'; import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; import { isRawHtmlWidget } from '../src/utils'; describe( 'HTMLEMbedEditing', () => { let element, editor, model, view, viewDocument; testUtils.createSinonSandbox(); beforeEach( () => { element = document.createElement( 'div' ); document.body.appendChild( element ); return ClassicTestEditor .create( element, { plugins: [ HTMLEmbedEditing ] } ) .then( newEditor => { editor = newEditor; model = editor.model; view = editor.editing.view; viewDocument = view.document; } ); } ); afterEach( () => { return editor.destroy() .then( () => { element.remove(); } ); } ); it( 'should have pluginName', () => { expect( HTMLEmbedEditing.pluginName ).to.equal( 'HTMLEmbedEditing' ); } ); it( 'should be loaded', () => { expect( editor.plugins.get( HTMLEmbedEditing ) ).to.be.instanceOf( HTMLEmbedEditing ); } ); it( 'should set proper schema rules', () => { expect( model.schema.checkChild( [ '$root' ], 'rawHtml' ) ).to.be.true; expect( model.schema.checkAttribute( [ '$root', 'rawHtml' ], 'value' ) ).to.be.true; expect( model.schema.isObject( 'rawHtml' ) ).to.be.true; expect( model.schema.checkChild( [ '$root', 'rawHtml' ], '$text' ) ).to.be.false; expect( model.schema.checkChild( [ '$root', '$block' ], 'rawHtml' ) ).to.be.false; } ); describe( 'commands', () => { it( 'should register htmlEmbedUpdate command', () => { expect( editor.commands.get( 'htmlEmbedUpdate' ) ).to.be.instanceOf( HTMLEmbedUpdateCommand ); } ); it( 'should register htmlEmbedInsert command', () => { expect( editor.commands.get( 'htmlEmbedInsert' ) ).to.be.instanceOf( HTMLEmbedInsertCommand ); } ); } ); describe( 'config', () => { let htmlEmbed; beforeEach( () => { htmlEmbed = editor.config.get( 'htmlEmbed' ); } ); describe( 'htmlEmbed.previewsInData', () => { it( 'should be set to `false` by default', () => { expect( htmlEmbed.previewsInData ).to.equal( false ); } ); } ); describe( 'htmlEmbed.sanitizeHtml', () => { beforeEach( () => { sinon.stub( console, 'warn' ); } ); it( 'should return an object with cleaned html and a note whether something has changed', () => { expect( htmlEmbed.sanitizeHtml( 'foo' ) ).to.deep.equal( { html: 'foo', hasModified: false } ); } ); it( 'should return an input string (without any modifications)', () => { const unsafeHtml = ''; expect( htmlEmbed.sanitizeHtml( unsafeHtml ).html ).to.deep.equal( unsafeHtml ); } ); it( 'should display a warning when using the default sanitizer', () => { htmlEmbed.sanitizeHtml( 'foo' ); expect( console.warn.callCount ).to.equal( 1 ); expect( console.warn.firstCall.args[ 0 ] ).to.equal( 'html-embed-provide-sanitize-function' ); } ); } ); } ); describe( 'conversion in data pipeline', () => { describe( 'model to view', () => { it( 'should convert an empty `rawHtml` element', () => { setModelData( model, '[]' ); expect( editor.getData() ).to.equal( '
' ); } ); it( 'should put the HTML from the `value` attribute (in `rawHtml`) into the data', () => { setModelData( model, '[]' ); model.change( writer => { writer.setAttribute( 'value', 'Foo.', model.document.getRoot().getChild( 0 ) ); } ); expect( editor.getData() ).to.equal( '
' + 'Foo.' + '
' ); } ); } ); describe( 'view to model', () => { it( 'should convert innerHTML (single element) of div.raw-html-embed', () => { editor.setData( '
' + 'Foo.' + '
' ); const rawHtml = model.document.getRoot().getChild( 0 ); expect( rawHtml.getAttribute( 'value' ) ).to.equal( 'Foo.' ); } ); it( 'should convert innerHTML (single element with children) of div.raw-html-embed', () => { editor.setData( '
' + '

' + 'Foo B.' + 'Foo I.' + '

' + '
' ); const rawHtml = model.document.getRoot().getChild( 0 ); expect( rawHtml.getAttribute( 'value' ) ).to.equal( '

' + 'Foo B.' + 'Foo I.' + '

' ); } ); it( 'should convert innerHTML (few elements) of div.raw-html-embed', () => { editor.setData( '
' + 'Foo B.' + 'Foo I.' + 'Foo U.' + '
' ); const rawHtml = model.document.getRoot().getChild( 0 ); expect( rawHtml.getAttribute( 'value' ) ).to.equal( 'Foo B.' + 'Foo I.' + 'Foo U.' ); } ); it( 'should not convert in wrong context', () => { model.schema.register( 'div', { inheritAllFrom: '$block' } ); model.schema.addChildCheck( ( ctx, childDef ) => { if ( ctx.endsWith( '$root' ) && childDef.name == 'rawHtml' ) { return false; } } ); editor.conversion.elementToElement( { model: 'div', view: 'div' } ); editor.setData( '
' + '
' + 'Foo.' + '
' + '
' ); expect( getModelData( model, { withoutSelection: true } ) ) .to.equal( '
' ); } ); it( 'should not convert inner `div.raw-html-embed` that is a child of outer div.raw-html-embed', () => { editor.setData( '
' + '
' + 'Foo B.' + 'Foo I.' + 'Foo U.' + '
' + '
' ); const rawHtml = model.document.getRoot().getChild( 0 ); expect( rawHtml.getAttribute( 'value' ) ).to.equal( '
' + 'Foo B.' + 'Foo I.' + 'Foo U.' + '
' ); } ); } ); } ); describe( 'conversion in editing pipeline (model to view)', () => { describe( 'without previews (htmlEmbed.dataInPreviews=false)', () => { it( 'converted element should be widgetized', () => { setModelData( model, '' ); const widget = viewDocument.getRoot().getChild( 0 ); expect( widget.name ).to.equal( 'div' ); expect( isRawHtmlWidget( widget ) ).to.be.true; } ); it( 'should update the edit source element when the `value` attribute has changed', () => { setModelData( model, '' ); model.change( writer => { writer.setAttribute( 'value', 'Foo.', model.document.getRoot().getChild( 0 ) ); } ); // TODO: Assertions. } ); // TODO: More tests. it.skip( 'should render the edit source element', () => { } ); it.skip( 'should render the preview placeholder element', () => { } ); it.skip( 'should render the toggle mode icon', () => { } ); it.skip( 'should update the `value` attribute after applying changes in the edit source element', () => { } ); it.skip( 'should remove DOM events when destroying the plugin', () => { } ); it.skip( 'should show the preview element by default', () => { } ); } ); describe( 'with previews (htmlEmbed.dataInPreviews=true)', () => { let element, editor, model, view, viewDocument, sanitizeHtml; testUtils.createSinonSandbox(); beforeEach( () => { element = document.createElement( 'div' ); document.body.appendChild( element ); // The default sanitize function without `console.warn`. sanitizeHtml = input => ( { html: input, hasChanged: false } ); return ClassicTestEditor .create( element, { plugins: [ HTMLEmbedEditing ], htmlEmbed: { previewsInData: true, sanitizeHtml } } ) .then( newEditor => { editor = newEditor; model = editor.model; view = editor.editing.view; viewDocument = view.document; } ); } ); afterEach( () => { return editor.destroy() .then( () => { element.remove(); } ); } ); it( 'converted element should be widgetized', () => { setModelData( model, '' ); const widget = viewDocument.getRoot().getChild( 0 ); expect( widget.name ).to.equal( 'div' ); expect( isRawHtmlWidget( widget ) ).to.be.true; } ); it( 'should update the source and preview elements when the `value` attribute has changed', () => { setModelData( model, '' ); model.change( writer => { writer.setAttribute( 'value', 'Foo.', model.document.getRoot().getChild( 0 ) ); } ); // TODO: Assertions. } ); // TODO: More tests. it.skip( 'should render the edit source element', () => { } ); it.skip( 'should render the preview placeholder element', () => { } ); it.skip( 'should render the toggle mode icon', () => { } ); it.skip( 'should update the `value` attribute after applying changes in the edit source element', () => { } ); it.skip( 'should re-render the preview element after applying changes in the edit source element', () => { } ); it.skip( 'should remove DOM events when destroying the plugin', () => { } ); it.skip( 'should show the preview element by default', () => { } ); } ); } ); } );