/**
* @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, '[
' + 'Foo B.' + 'Foo I.' + '
' ); } ); it( 'should convert innerHTML (few elements) of div.raw-html-embed', () => { editor.setData( '' ); 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( '