| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import {
- viewFigureToModel,
- createImageAttributeConverter,
- } from '../../src/image/converters';
- import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
- import { createImageViewElement } from '../../src/image/imageengine';
- import { toImageWidget } from '../../src/image/utils';
- import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
- import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
- import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
- import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
- import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- describe( 'Image converters', () => {
- let editor, model, document, viewDocument;
- beforeEach( () => {
- return VirtualTestEditor.create()
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- document = model.document;
- viewDocument = editor.editing.view;
- const schema = model.schema;
- schema.register( 'image', {
- allowWhere: '$block',
- allowAttributes: [ 'alt', 'src' ],
- isObject: true,
- isBlock: true
- } );
- buildModelConverter().for( editor.editing.modelToView )
- .fromElement( 'image' )
- .toElement( () => toImageWidget( createImageViewElement() ) );
- createImageAttributeConverter( [ editor.editing.modelToView ], 'src' );
- createImageAttributeConverter( [ editor.editing.modelToView ], 'alt' );
- } );
- } );
- describe( 'viewFigureToModel', () => {
- function expectModel( data ) {
- expect( getModelData( model, { withoutSelection: true } ) ).to.equal( data );
- }
- let dispatcher, schema, imgConverterCalled;
- beforeEach( () => {
- imgConverterCalled = false;
- schema = model.schema;
- schema.extend( '$text', { allowIn: 'image' } );
- dispatcher = editor.data.viewToModel;
- dispatcher.on( 'element:figure', viewFigureToModel() );
- dispatcher.on( 'element:img', ( evt, data, consumable, conversionApi ) => {
- if ( consumable.consume( data.input, { name: true, attribute: 'src' } ) ) {
- const image = conversionApi.writer.createElement( 'image', { src: data.input.getAttribute( 'src' ) } );
- conversionApi.writer.insert( image, data.position );
- data.output = ModelRange.createOn( image );
- imgConverterCalled = true;
- }
- } );
- } );
- it( 'should find img element among children and convert it using already defined converters', () => {
- editor.setData( '<figure class="image"><img src="foo.png" /></figure>' );
- expectModel( '<image src="foo.png"></image>' );
- expect( imgConverterCalled ).to.be.true;
- } );
- it( 'should convert non-img children in image context and append them to model image element', () => {
- buildViewConverter().for( editor.data.viewToModel ).fromElement( 'foo' ).toElement( 'foo' );
- buildViewConverter().for( editor.data.viewToModel ).fromElement( 'bar' ).toElement( 'bar' );
- schema.register( 'foo' );
- schema.register( 'bar' );
- schema.extend( 'foo', { allowIn: 'image' } );
- editor.setData( '<figure class="image">x<img src="foo.png" />y<foo></foo><bar></bar></figure>' );
- // Element bar not converted because schema does not allow it.
- expectModel( '<image src="foo.png">xy<foo></foo></image>' );
- } );
- it( 'should be possible to overwrite', () => {
- dispatcher.on( 'element:figure', ( evt, data, consumable, conversionApi ) => {
- consumable.consume( data.input, { name: true } );
- consumable.consume( data.input.getChild( 0 ), { name: true } );
- const element = conversionApi.writer.createElement( 'myImage', {
- data: {
- src: data.input.getChild( 0 ).getAttribute( 'src' )
- }
- } );
- conversionApi.writer.insert( element, data.position );
- data.output = ModelRange.createOn( element );
- }, { priority: 'high' } );
- editor.setData( '<figure class="image"><img src="foo.png" />xyz</figure>' );
- expectModel( '<myImage data="{"src":"foo.png"}"></myImage>' );
- } );
- // Test exactly what figure converter does, which is putting it's children element to image element.
- // If this has not been done, it means that figure converter was not used.
- it( 'should not convert if figure do not have class="image" attribute', () => {
- editor.setData( '<figure><img src="foo.png" />xyz</figure>' );
- // Default image converter will be fired.
- expectModel( '<image src="foo.png"></image>' );
- } );
- it( 'should not convert if there is no img element among children', () => {
- editor.setData( '<figure class="image">xyz</figure>' );
- // Figure converter outputs nothing and text is disallowed in root.
- expectModel( '' );
- } );
- it( 'should not convert if img element was not converted', () => {
- // Image element missing src attribute.
- editor.setData( '<figure class="image"><img alt="abc" />xyz</figure>' );
- // Figure converter outputs nothing and text is disallowed in root.
- expectModel( '' );
- } );
- } );
- describe( 'modelToViewAttributeConverter', () => {
- it( 'should convert adding attribute to image', () => {
- setModelData( model, '<image src=""></image>' );
- const image = document.getRoot().getChild( 0 );
- model.change( writer => {
- writer.setAttribute( 'alt', 'foo bar', image );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget image" contenteditable="false"><img alt="foo bar" src=""></img></figure>'
- );
- } );
- it( 'should convert removing attribute from image', () => {
- setModelData( model, '<image src="" alt="foo bar"></image>' );
- const image = document.getRoot().getChild( 0 );
- model.change( writer => {
- writer.removeAttribute( 'alt', image );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget image" contenteditable="false"><img src=""></img></figure>'
- );
- } );
- it( 'should convert change of attribute image', () => {
- setModelData( model, '<image src="" alt="foo bar"></image>' );
- const image = document.getRoot().getChild( 0 );
- model.change( writer => {
- writer.setAttribute( 'alt', 'baz quix', image );
- } );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget image" contenteditable="false"><img alt="baz quix" src=""></img></figure>'
- );
- } );
- it( 'should not set attribute if change was already consumed', () => {
- editor.editing.modelToView.on( 'attribute:alt:image', ( evt, data, consumable ) => {
- consumable.consume( data.item, 'attribute:alt' );
- }, { priority: 'high' } );
- setModelData( model, '<image src="" alt="foo bar"></image>' );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget image" contenteditable="false"><img src=""></img></figure>'
- );
- } );
- } );
- } );
|