| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- /**
- * @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 = '<img src="data:/xxx,<script>void</script>" onload="void;">';
- 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, '[<rawHtml></rawHtml>]' );
- expect( editor.getData() ).to.equal( '<div class="raw-html-embed"></div>' );
- } );
- it( 'should put the HTML from the `value` attribute (in `rawHtml`) into the data', () => {
- setModelData( model, '[<rawHtml></rawHtml>]' );
- model.change( writer => {
- writer.setAttribute( 'value', '<b>Foo.</b>', model.document.getRoot().getChild( 0 ) );
- } );
- expect( editor.getData() ).to.equal(
- '<div class="raw-html-embed">' +
- '<b>Foo.</b>' +
- '</div>'
- );
- } );
- } );
- describe( 'view to model', () => {
- it( 'should convert innerHTML (single element) of div.raw-html-embed', () => {
- editor.setData(
- '<div class="raw-html-embed">' +
- '<b>Foo.</b>' +
- '</div>'
- );
- const rawHtml = model.document.getRoot().getChild( 0 );
- expect( rawHtml.getAttribute( 'value' ) ).to.equal( '<b>Foo.</b>' );
- } );
- it( 'should convert innerHTML (single element with children) of div.raw-html-embed', () => {
- editor.setData(
- '<div class="raw-html-embed">' +
- '<p>' +
- '<b>Foo B.</b>' +
- '<i>Foo I.</i>' +
- '</p>' +
- '</div>'
- );
- const rawHtml = model.document.getRoot().getChild( 0 );
- expect( rawHtml.getAttribute( 'value' ) ).to.equal(
- '<p>' +
- '<b>Foo B.</b>' +
- '<i>Foo I.</i>' +
- '</p>'
- );
- } );
- it( 'should convert innerHTML (few elements) of div.raw-html-embed', () => {
- editor.setData(
- '<div class="raw-html-embed">' +
- '<b>Foo B.</b>' +
- '<i>Foo I.</i>' +
- '<u>Foo U.</u>' +
- '</div>'
- );
- const rawHtml = model.document.getRoot().getChild( 0 );
- expect( rawHtml.getAttribute( 'value' ) ).to.equal(
- '<b>Foo B.</b>' +
- '<i>Foo I.</i>' +
- '<u>Foo U.</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(
- '<div>' +
- '<div class="raw-html-embed">' +
- '<b>Foo.</b>' +
- '</div>' +
- '</div>'
- );
- expect( getModelData( model, { withoutSelection: true } ) )
- .to.equal( '<div></div>' );
- } );
- it( 'should not convert inner `div.raw-html-embed` that is a child of outer div.raw-html-embed', () => {
- editor.setData(
- '<div class="raw-html-embed">' +
- '<div class="raw-html-embed">' +
- '<b>Foo B.</b>' +
- '<i>Foo I.</i>' +
- '<u>Foo U.</u>' +
- '</div>' +
- '</div>'
- );
- const rawHtml = model.document.getRoot().getChild( 0 );
- expect( rawHtml.getAttribute( 'value' ) ).to.equal(
- '<div class="raw-html-embed">' +
- '<b>Foo B.</b>' +
- '<i>Foo I.</i>' +
- '<u>Foo U.</u>' +
- '</div>'
- );
- } );
- } );
- } );
- describe( 'conversion in editing pipeline (model to view)', () => {
- describe( 'without previews (htmlEmbed.dataInPreviews=false)', () => {
- it( 'converted element should be widgetized', () => {
- setModelData( model, '<rawHtml></rawHtml>' );
- 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, '<rawHtml></rawHtml>' );
- model.change( writer => {
- writer.setAttribute( 'value', '<b>Foo.</b>', 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, '<rawHtml></rawHtml>' );
- 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, '<rawHtml></rawHtml>' );
- model.change( writer => {
- writer.setAttribute( 'value', '<b>Foo.</b>', 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', () => {
- } );
- } );
- } );
- } );
|