| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
- import MediaEmbedEditing from '../src/mediaembedediting';
- import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
- import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
- describe( 'MediaEmbedEditing', () => {
- let editor, model, doc, view;
- const mediaDefinitions = {
- test: {
- url: /(.*)/,
- html: id => `<iframe src="${ id }"></iframe>`
- }
- };
- testUtils.createSinonSandbox();
- it( 'should be loaded', () => {
- return VirtualTestEditor
- .create( {
- plugins: [ MediaEmbedEditing ],
- } )
- .then( newEditor => {
- expect( newEditor.plugins.get( MediaEmbedEditing ) ).to.be.instanceOf( MediaEmbedEditing );
- } );
- } );
- it( 'should set proper schema rules', () => {
- return VirtualTestEditor
- .create( {
- plugins: [ MediaEmbedEditing ],
- } )
- .then( newEditor => {
- model = newEditor.model;
- expect( model.schema.checkChild( [ '$root' ], 'media' ) ).to.be.true;
- expect( model.schema.checkAttribute( [ '$root', 'media' ], 'url' ) ).to.be.true;
- expect( model.schema.isObject( 'media' ) ).to.be.true;
- expect( model.schema.checkChild( [ '$root', 'media' ], 'media' ) ).to.be.false;
- expect( model.schema.checkChild( [ '$root', 'media' ], '$text' ) ).to.be.false;
- expect( model.schema.checkChild( [ '$root', '$block' ], 'image' ) ).to.be.false;
- } );
- } );
- describe( 'conversion in the data pipeline', () => {
- describe( 'semanticDataOutput=true', () => {
- beforeEach( () => {
- return VirtualTestEditor
- .create( {
- plugins: [ MediaEmbedEditing ],
- mediaEmbed: {
- semanticDataOutput: true,
- media: mediaDefinitions
- }
- } )
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- doc = model.document;
- view = editor.editing.view;
- } );
- } );
- describe( 'model to view', () => {
- it( 'should convert', () => {
- setModelData( model, '<media url="http://ckeditor.com"></media>' );
- expect( editor.getData() ).to.equal(
- '<figure class="media">' +
- '<div data-oembed-url="http://ckeditor.com"></div>' +
- '</figure>' );
- } );
- it( 'should convert (no url)', () => {
- setModelData( model, '<media></media>' );
- expect( editor.getData() ).to.equal(
- '<figure class="media">' +
- '<div></div>' +
- '</figure>' );
- } );
- } );
- describe( 'view to model', () => {
- it( 'should convert media figure', () => {
- editor.setData( '<figure class="media"><div data-oembed-url="http://ckeditor.com"></div></figure>' );
- expect( getModelData( model, { withoutSelection: true } ) )
- .to.equal( '<media url="http://ckeditor.com"></media>' );
- } );
- it( 'should not convert if there is no media class', () => {
- editor.setData( '<figure class="quote">My quote</figure>' );
- expect( getModelData( model, { withoutSelection: true } ) )
- .to.equal( '' );
- } );
- it( 'should not convert if there is no oembed wrapper inside #1', () => {
- editor.setData( '<figure class="media"></figure>' );
- expect( getModelData( model, { withoutSelection: true } ) )
- .to.equal( '' );
- } );
- it( 'should not convert if there is no oembed wrapper inside #2', () => {
- editor.setData( '<figure class="media">test</figure>' );
- expect( getModelData( model, { withoutSelection: true } ) )
- .to.equal( '' );
- } );
- it( 'should not convert when the wrapper has no data-oembed-url attribute', () => {
- editor.setData( '<figure class="media"><div></div></figure>' );
- expect( getModelData( model, { withoutSelection: true } ) )
- .to.equal( '' );
- } );
- it( 'should not convert in the wrong context', () => {
- model.schema.register( 'blockquote', { inheritAllFrom: '$block' } );
- model.schema.addChildCheck( ( ctx, childDef ) => {
- if ( ctx.endsWith( '$root' ) && childDef.name == 'media' ) {
- return false;
- }
- } );
- editor.conversion.elementToElement( { model: 'blockquote', view: 'blockquote' } );
- editor.setData(
- '<blockquote><figure class="media"><div data-oembed-url="http://ckeditor.com"></div></figure></blockquote>' );
- expect( getModelData( model, { withoutSelection: true } ) )
- .to.equal( '<blockquote></blockquote>' );
- } );
- it( 'should not convert if the oembed wrapper is already consumed', () => {
- editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
- const img = data.viewItem.getChild( 0 );
- conversionApi.consumable.consume( img, { name: true } );
- }, { priority: 'high' } );
- editor.setData( '<figure class="media"><div data-oembed-url="http://ckeditor.com"></div></figure>' );
- expect( getModelData( model, { withoutSelection: true } ) )
- .to.equal( '' );
- } );
- it( 'should not convert if the figure is already consumed', () => {
- editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
- conversionApi.consumable.consume( data.viewItem, { name: true, class: 'image' } );
- }, { priority: 'high' } );
- editor.setData( '<figure class="media"><div data-oembed-url="http://ckeditor.com"></div></figure>' );
- expect( getModelData( model, { withoutSelection: true } ) )
- .to.equal( '' );
- } );
- } );
- } );
- describe( 'semanticDataOutput=false', () => {
- beforeEach( () => {
- return VirtualTestEditor
- .create( {
- plugins: [ MediaEmbedEditing ],
- mediaEmbed: {
- media: mediaDefinitions
- }
- } )
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- doc = model.document;
- view = editor.editing.view;
- } );
- } );
- describe( 'conversion in the data pipeline', () => {
- describe( 'model to view', () => {
- it( 'should convert', () => {
- setModelData( model, '<media url="http://ckeditor.com"></media>' );
- expect( editor.getData() ).to.equal(
- '<figure class="media">' +
- '<div data-oembed-url="http://ckeditor.com">' +
- '<div class="ck-media__wrapper__aspect">' +
- '<iframe src="http://ckeditor.com"></iframe>' +
- '</div>' +
- '</div>' +
- '</figure>' );
- } );
- it( 'should convert (no url)', () => {
- setModelData( model, '<media></media>' );
- expect( editor.getData() ).to.equal(
- '<figure class="media">' +
- '<div>' +
- '<div class="ck-media__wrapper__aspect"></div>' +
- '</div>' +
- '</figure>' );
- } );
- } );
- describe( 'view to model', () => {
- it( 'should convert media figure', () => {
- editor.setData(
- '<figure class="media">' +
- '<div data-oembed-url="http://ckeditor.com">' +
- '<div class="ck-media__wrapper__aspect">' +
- '<iframe src="http://cksource.com"></iframe>' +
- '</div>' +
- '</div>' +
- '</figure>' );
- expect( getModelData( model, { withoutSelection: true } ) )
- .to.equal( '<media url="http://ckeditor.com"></media>' );
- } );
- it( 'should not convert if there is no media class', () => {
- editor.setData( '<figure class="quote">My quote</figure>' );
- expect( getModelData( model, { withoutSelection: true } ) )
- .to.equal( '' );
- } );
- it( 'should not convert if there is no oembed wrapper inside #1', () => {
- editor.setData( '<figure class="media"></figure>' );
- expect( getModelData( model, { withoutSelection: true } ) )
- .to.equal( '' );
- } );
- it( 'should not convert if there is no oembed wrapper inside #2', () => {
- editor.setData( '<figure class="media">test</figure>' );
- expect( getModelData( model, { withoutSelection: true } ) )
- .to.equal( '' );
- } );
- it( 'should not convert in the wrong context', () => {
- model.schema.register( 'div', { inheritAllFrom: '$block' } );
- model.schema.addChildCheck( ( ctx, childDef ) => {
- if ( ctx.endsWith( '$root' ) && childDef.name == 'media' ) {
- return false;
- }
- } );
- editor.conversion.elementToElement( { model: 'div', view: 'div' } );
- editor.setData(
- '<div>' +
- '<figure class="media">' +
- '<div data-oembed-url="http://ckeditor.com">' +
- '<div class="ck-media__wrapper__aspect">' +
- '<iframe src="http://cksource.com"></iframe>' +
- '</div>' +
- '</div>' +
- '</figure>' +
- '</div>' );
- expect( getModelData( model, { withoutSelection: true } ) )
- .to.equal( '<div></div>' );
- } );
- it( 'should not convert if the oembed wrapper is already consumed', () => {
- editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
- const img = data.viewItem.getChild( 0 );
- conversionApi.consumable.consume( img, { name: true } );
- }, { priority: 'high' } );
- editor.setData(
- '<div>' +
- '<figure class="media">' +
- '<div data-oembed-url="http://ckeditor.com">' +
- '<div class="ck-media__wrapper__aspect">' +
- '<iframe src="http://cksource.com"></iframe>' +
- '</div>' +
- '</div>' +
- '</figure>' +
- '</div>' );
- expect( getModelData( model, { withoutSelection: true } ) )
- .to.equal( '' );
- } );
- it( 'should not convert if the figure is already consumed', () => {
- editor.data.upcastDispatcher.on( 'element:figure', ( evt, data, conversionApi ) => {
- conversionApi.consumable.consume( data.viewItem, { name: true, class: 'image' } );
- }, { priority: 'high' } );
- editor.setData( '<figure class="media"><div data-oembed-url="http://ckeditor.com"></div></figure>' );
- expect( getModelData( model, { withoutSelection: true } ) )
- .to.equal( '' );
- } );
- } );
- } );
- } );
- } );
- describe( 'conversion in the editing pipeline', () => {
- describe( 'semanticDataOutput=true', () => {
- beforeEach( () => {
- return VirtualTestEditor
- .create( {
- plugins: [ MediaEmbedEditing ],
- mediaEmbed: {
- media: mediaDefinitions,
- semanticDataOutput: true
- }
- } )
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- doc = model.document;
- view = editor.editing.view;
- } );
- } );
- test();
- } );
- describe( 'semanticDataOutput=false', () => {
- beforeEach( () => {
- return VirtualTestEditor
- .create( {
- plugins: [ MediaEmbedEditing ],
- mediaEmbed: {
- media: mediaDefinitions
- }
- } )
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- doc = model.document;
- view = editor.editing.view;
- } );
- } );
- test();
- } );
- function test() {
- describe( 'model to view', () => {
- it( 'should convert', () => {
- setModelData( model, '<media url="http://ckeditor.com"></media>' );
- expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget media" contenteditable="false">' +
- '<div class="ck-media__wrapper" data-oembed-url="http://ckeditor.com">' +
- '<div class="ck-media__wrapper__aspect">' +
- '<iframe src="http://ckeditor.com"></iframe>' +
- '</div>' +
- '</div>' +
- '</figure>'
- );
- } );
- it( 'should convert the url attribute change', () => {
- setModelData( model, '<media url="http://ckeditor.com"></media>' );
- const media = doc.getRoot().getChild( 0 );
- model.change( writer => {
- writer.setAttribute( 'url', 'http://cksource.com', media );
- } );
- expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget media" contenteditable="false">' +
- '<div class="ck-media__wrapper" data-oembed-url="http://cksource.com">' +
- '<div class="ck-media__wrapper__aspect">' +
- '<iframe src="http://cksource.com"></iframe>' +
- '</div>' +
- '</div>' +
- '</figure>'
- );
- } );
- it( 'should convert the url attribute removal', () => {
- setModelData( model, '<media url="http://ckeditor.com"></media>' );
- const media = doc.getRoot().getChild( 0 );
- model.change( writer => {
- writer.removeAttribute( 'url', media );
- } );
- expect( getViewData( view, { withoutSelection: true } ) )
- .to.equal(
- '<figure class="ck-widget media" contenteditable="false">' +
- '<div class="ck-media__wrapper">' +
- '<div class="ck-media__wrapper__aspect">' +
- '<p>No embeddable media found for given URL.</p>' +
- '</div>' +
- '</div>' +
- '</figure>'
- );
- } );
- it( 'should not convert the url attribute removal if is already consumed', () => {
- setModelData( model, '<media url="http://ckeditor.com"></media>' );
- const media = doc.getRoot().getChild( 0 );
- editor.editing.downcastDispatcher.on( 'attribute:url:media', ( evt, data, conversionApi ) => {
- conversionApi.consumable.consume( data.item, 'attribute:url' );
- }, { priority: 'high' } );
- model.change( writer => {
- writer.removeAttribute( 'url', media );
- } );
- expect( getViewData( view, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget media" contenteditable="false">' +
- '<div class="ck-media__wrapper" data-oembed-url="http://ckeditor.com">' +
- '<div class="ck-media__wrapper__aspect">' +
- '<iframe src="http://ckeditor.com"></iframe>' +
- '</div>' +
- '</div>' +
- '</figure>'
- );
- } );
- } );
- }
- } );
- } );
|