| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- /**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
- import ImageCaptionEngine from '../../src/imagecaption/imagecaptionengine';
- import ImageEngine from '../../src/image/imageengine';
- import UndoEngine from '@ckeditor/ckeditor5-undo/src/undoengine';
- import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
- import ViewAttributeElement from '@ckeditor/ckeditor5-engine/src/view/attributeelement';
- import ViewPosition from '@ckeditor/ckeditor5-engine/src/view/position';
- import viewWriter from '@ckeditor/ckeditor5-engine/src/view/writer';
- import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
- import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
- import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
- import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
- import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
- describe( 'ImageCaptionEngine', () => {
- let editor, doc, viewDocument;
- beforeEach( () => {
- return VirtualTestEditor
- .create( {
- plugins: [ ImageCaptionEngine, ImageEngine, UndoEngine, Paragraph ]
- } )
- .then( newEditor => {
- editor = newEditor;
- doc = editor.document;
- viewDocument = editor.editing.view;
- doc.schema.registerItem( 'widget' );
- doc.schema.allow( { name: 'widget', inside: '$root' } );
- doc.schema.allow( { name: 'caption', inside: 'widget' } );
- doc.schema.allow( { name: '$inline', inside: 'widget' } );
- buildViewConverter()
- .for( editor.data.viewToModel )
- .fromElement( 'widget' )
- .toElement( 'widget' );
- buildModelConverter()
- .for( editor.data.modelToView, editor.editing.modelToView )
- .fromElement( 'widget' )
- .toElement( 'widget' );
- } );
- } );
- it( 'should be loaded', () => {
- expect( editor.plugins.get( ImageCaptionEngine ) ).to.be.instanceOf( ImageCaptionEngine );
- } );
- it( 'should set proper schema rules', () => {
- expect( doc.schema.check( { name: 'caption', iniside: 'image' } ) ).to.be.true;
- expect( doc.schema.check( { name: '$inline', inside: 'caption' } ) ).to.be.true;
- expect( doc.schema.itemExtends( 'caption', '$block' ) ).to.be.true;
- expect( doc.schema.limits.has( 'caption' ) );
- } );
- describe( 'data pipeline', () => {
- describe( 'view to model', () => {
- it( 'should convert figcaption inside image figure', () => {
- editor.setData( '<figure class="image"><img src="foo.png"/><figcaption>foo bar</figcaption></figure>' );
- expect( getModelData( doc, { withoutSelection: true } ) )
- .to.equal( '<image src="foo.png"><caption>foo bar</caption></image>' );
- } );
- it( 'should add empty caption if there is no figcaption', () => {
- editor.setData( '<figure class="image"><img src="foo.png"/></figure>' );
- expect( getModelData( doc, { withoutSelection: true } ) )
- .to.equal( '<image src="foo.png"><caption></caption></image>' );
- } );
- it( 'should not convert figcaption inside other elements than image', () => {
- editor.setData( '<widget><figcaption>foobar</figcaption></widget>' );
- expect( getModelData( doc, { withoutSelection: true } ) )
- .to.equal( '<widget>foobar</widget>' );
- } );
- } );
- describe( 'model to view', () => {
- it( 'should convert caption element to figcaption', () => {
- setModelData( doc, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
- expect( editor.getData() ).to.equal(
- '<figure class="image"><img src="img.png"><figcaption>Foo bar baz.</figcaption></figure>'
- );
- } );
- it( 'should not convert caption to figcaption if it\'s empty', () => {
- setModelData( doc, '<image src="img.png"><caption></caption></image>' );
- expect( editor.getData() ).to.equal( '<figure class="image"><img src="img.png"></figure>' );
- } );
- it( 'should not convert caption from other elements', () => {
- setModelData( doc, '<widget>foo bar<caption></caption></widget>' );
- expect( editor.getData() ).to.equal( '<widget>foo bar</widget>' );
- } );
- } );
- } );
- describe( 'editing pipeline', () => {
- describe( 'model to view', () => {
- it( 'should convert caption element to figcaption contenteditable', () => {
- setModelData( doc, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget image" contenteditable="false">' +
- '<img src="img.png"></img>' +
- '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
- 'Foo bar baz.' +
- '</figcaption>' +
- '</figure>'
- );
- } );
- it( 'should convert caption to element with proper CSS class if it\'s empty', () => {
- setModelData( doc, '<paragraph>foo</paragraph><image src="img.png"><caption></caption></image>' );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<p>foo</p>' +
- '<figure class="ck-widget image" contenteditable="false">' +
- '<img src="img.png"></img>' +
- '<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
- 'contenteditable="true" data-placeholder="Enter image caption">' +
- '</figcaption>' +
- '</figure>'
- );
- } );
- it( 'should not convert caption from other elements', () => {
- setModelData( doc, '<widget>foo bar<caption></caption></widget>' );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal( '<widget>foo bar</widget>' );
- } );
- it( 'should not convert when element is already consumed', () => {
- editor.editing.modelToView.on(
- 'insert:caption',
- ( evt, data, consumable, conversionApi ) => {
- consumable.consume( data.item, 'insert' );
- const imageFigure = conversionApi.mapper.toViewElement( data.range.start.parent );
- const viewElement = new ViewAttributeElement( 'span' );
- const viewPosition = ViewPosition.createAt( imageFigure, 'end' );
- conversionApi.mapper.bindElements( data.item, viewElement );
- viewWriter.insert( viewPosition, viewElement );
- },
- { priority: 'high' }
- );
- setModelData( doc, '<image src="img.png"><caption>Foo bar baz.</caption></image>' );
- expect( getViewData( viewDocument, { withoutSelection: true } ) ).to.equal(
- '<figure class="ck-widget image" contenteditable="false"><img src="img.png"></img><span></span>Foo bar baz.</figure>'
- );
- } );
- it( 'should show caption when something is inserted inside', () => {
- setModelData( doc, '<paragraph>foo</paragraph><image src="img.png"><caption></caption></image>' );
- const image = doc.getRoot().getChild( 1 );
- const caption = image.getChild( 0 );
- doc.enqueueChanges( () => {
- const batch = doc.batch();
- batch.insertText( 'foo bar', caption );
- } );
- expect( getViewData( viewDocument ) ).to.equal(
- '<p>{}foo</p>' +
- '<figure class="ck-widget image" contenteditable="false">' +
- '<img src="img.png"></img>' +
- '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
- 'foo bar' +
- '</figcaption>' +
- '</figure>'
- );
- } );
- it( 'should hide when everything is removed from caption', () => {
- setModelData( doc, '<paragraph>foo</paragraph><image src="img.png"><caption>foo bar baz</caption></image>' );
- const image = doc.getRoot().getChild( 1 );
- const caption = image.getChild( 0 );
- doc.enqueueChanges( () => {
- const batch = doc.batch();
- batch.remove( ModelRange.createIn( caption ) );
- } );
- expect( getViewData( viewDocument ) ).to.equal(
- '<p>{}foo</p>' +
- '<figure class="ck-widget image" contenteditable="false">' +
- '<img src="img.png"></img>' +
- '<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
- 'contenteditable="true" data-placeholder="Enter image caption">' +
- '</figcaption>' +
- '</figure>'
- );
- } );
- it( 'should show when not everything is removed from caption', () => {
- setModelData( doc, '<paragraph>foo</paragraph><image src="img.png"><caption>foo bar baz</caption></image>' );
- const image = doc.getRoot().getChild( 1 );
- const caption = image.getChild( 0 );
- doc.enqueueChanges( () => {
- const batch = doc.batch();
- batch.remove( ModelRange.createFromParentsAndOffsets( caption, 0, caption, 8 ) );
- } );
- expect( getViewData( viewDocument ) ).to.equal(
- '<p>{}foo</p>' +
- '<figure class="ck-widget image" contenteditable="false">' +
- '<img src="img.png"></img>' +
- '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">baz</figcaption>' +
- '</figure>'
- );
- } );
- } );
- } );
- describe( 'inserting image to the document', () => {
- it( 'should add caption element if image does not have it', () => {
- const batch = doc.batch();
- doc.enqueueChanges( () => {
- batch.insertElement( 'image', { src: '', alt: '' }, doc.getRoot() );
- } );
- expect( getModelData( doc ) ).to.equal(
- '[<image alt="" src=""><caption></caption></image>]<paragraph></paragraph>'
- );
- expect( getViewData( viewDocument ) ).to.equal(
- '[<figure class="ck-widget image" contenteditable="false">' +
- '<img alt="" src=""></img>' +
- '<figcaption class="ck-editable ck-placeholder" ' +
- 'contenteditable="true" data-placeholder="Enter image caption">' +
- '</figcaption>' +
- '</figure>]' +
- '<p></p>'
- );
- } );
- it( 'should not add caption element if image already have it', () => {
- const caption = new ModelElement( 'caption', null, 'foo bar' );
- const image = new ModelElement( 'image', { src: '', alt: '' }, caption );
- const batch = doc.batch();
- doc.enqueueChanges( () => {
- batch.insert( image, doc.getRoot() );
- } );
- expect( getModelData( doc ) ).to.equal(
- '[<image alt="" src=""><caption>foo bar</caption></image>]<paragraph></paragraph>'
- );
- expect( getViewData( viewDocument ) ).to.equal(
- '[<figure class="ck-widget image" contenteditable="false">' +
- '<img alt="" src=""></img>' +
- '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
- 'foo bar' +
- '</figcaption>' +
- '</figure>]' +
- '<p></p>'
- );
- } );
- it( 'should not add caption element twice', () => {
- const image = new ModelElement( 'image', { src: '', alt: '' } );
- const caption = new ModelElement( 'caption' );
- const batch = doc.batch();
- doc.enqueueChanges( () => {
- // Since we are adding an empty image, this should trigger caption fixer.
- batch.insert( image, doc.getRoot() );
- // Add caption just after the image is inserted, in same batch and enqueue changes block.
- batch.insert( caption, image );
- } );
- // Check whether caption fixer added redundant caption.
- expect( getModelData( doc ) ).to.equal(
- '[<image alt="" src=""><caption></caption></image>]<paragraph></paragraph>'
- );
- expect( getViewData( viewDocument ) ).to.equal(
- '[<figure class="ck-widget image" contenteditable="false">' +
- '<img alt="" src=""></img>' +
- '<figcaption class="ck-editable ck-placeholder" ' +
- 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
- '</figure>]' +
- '<p></p>'
- );
- } );
- it( 'should do nothing for other changes than insert', () => {
- setModelData( doc, '<image src=""><caption>foo bar</caption></image>' );
- const image = doc.getRoot().getChild( 0 );
- const batch = doc.batch();
- doc.enqueueChanges( () => {
- batch.setAttribute( 'alt', 'alt text', image );
- } );
- expect( getModelData( doc, { withoutSelection: true } ) ).to.equal(
- '<image alt="alt text" src=""><caption>foo bar</caption></image>'
- );
- } );
- } );
- describe( 'editing view', () => {
- it( 'image should have empty figcaption element when is selected', () => {
- setModelData( doc, '<paragraph>foo</paragraph>[<image src=""><caption></caption></image>]' );
- expect( getViewData( viewDocument ) ).to.equal(
- '<p>foo</p>' +
- '[<figure class="ck-widget image" contenteditable="false">' +
- '<img src=""></img>' +
- '<figcaption class="ck-editable ck-placeholder" contenteditable="true" data-placeholder="Enter image caption">' +
- '</figcaption>' +
- '</figure>]'
- );
- } );
- it( 'image should have empty figcaption element with hidden class when not selected', () => {
- setModelData( doc, '<paragraph>[]foo</paragraph><image src=""><caption></caption></image>' );
- expect( getViewData( viewDocument ) ).to.equal(
- '<p>{}foo</p>' +
- '<figure class="ck-widget image" contenteditable="false">' +
- '<img src=""></img>' +
- '<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
- 'contenteditable="true" data-placeholder="Enter image caption">' +
- '</figcaption>' +
- '</figure>'
- );
- } );
- it( 'should not add additional figcaption if one is already present', () => {
- setModelData( doc, '<paragraph>foo</paragraph>[<image src=""><caption>foo bar</caption></image>]' );
- expect( getViewData( viewDocument ) ).to.equal(
- '<p>foo</p>' +
- '[<figure class="ck-widget image" contenteditable="false">' +
- '<img src=""></img>' +
- '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">foo bar</figcaption>' +
- '</figure>]'
- );
- } );
- it( 'should add hidden class to figcaption when caption is empty and image is no longer selected', () => {
- setModelData( doc, '<paragraph>foo</paragraph>[<image src=""><caption></caption></image>]' );
- doc.enqueueChanges( () => {
- doc.selection.removeAllRanges();
- } );
- expect( getViewData( viewDocument ) ).to.equal(
- '<p>{}foo</p>' +
- '<figure class="ck-widget image" contenteditable="false">' +
- '<img src=""></img>' +
- '<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
- 'contenteditable="true" data-placeholder="Enter image caption">' +
- '</figcaption>' +
- '</figure>'
- );
- } );
- it( 'should not remove figcaption when selection is inside it even when it is empty', () => {
- setModelData( doc, '<image src=""><caption>[foo bar]</caption></image>' );
- doc.enqueueChanges( () => {
- doc.batch().remove( doc.selection.getFirstRange() );
- } );
- expect( getViewData( viewDocument ) ).to.equal(
- '<figure class="ck-widget image" contenteditable="false">' +
- '<img src=""></img>' +
- '<figcaption class="ck-editable ck-placeholder" contenteditable="true" data-placeholder="Enter image caption">' +
- '[]' +
- '</figcaption>' +
- '</figure>'
- );
- } );
- it( 'should not remove figcaption when selection is moved from it to its image', () => {
- setModelData( doc, '<image src=""><caption>[foo bar]</caption></image>' );
- const image = doc.getRoot().getChild( 0 );
- doc.enqueueChanges( () => {
- doc.batch().remove( doc.selection.getFirstRange() );
- doc.selection.setRanges( [ ModelRange.createOn( image ) ] );
- } );
- expect( getViewData( viewDocument ) ).to.equal(
- '[<figure class="ck-widget image" contenteditable="false">' +
- '<img src=""></img>' +
- '<figcaption class="ck-editable ck-placeholder" ' +
- 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
- '</figure>]'
- );
- } );
- it( 'should not remove figcaption when selection is moved from it to other image', () => {
- setModelData( doc, '<image src=""><caption>[foo bar]</caption></image><image src=""><caption></caption></image>' );
- const image = doc.getRoot().getChild( 1 );
- doc.enqueueChanges( () => {
- doc.selection.setRanges( [ ModelRange.createOn( image ) ] );
- } );
- expect( getViewData( viewDocument ) ).to.equal(
- '<figure class="ck-widget image" contenteditable="false">' +
- '<img src=""></img>' +
- '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">foo bar</figcaption>' +
- '</figure>' +
- '[<figure class="ck-widget image" contenteditable="false">' +
- '<img src=""></img>' +
- '<figcaption class="ck-editable ck-placeholder" ' +
- 'contenteditable="true" data-placeholder="Enter image caption"></figcaption>' +
- '</figure>]'
- );
- } );
- describe( 'undo/redo integration', () => {
- it( 'should create view element after redo', () => {
- setModelData( doc, '<paragraph>foo</paragraph><image src=""><caption>[foo bar baz]</caption></image>' );
- const modelRoot = doc.getRoot();
- const modelImage = modelRoot.getChild( 1 );
- const modelCaption = modelImage.getChild( 0 );
- // Remove text and selection from caption.
- doc.enqueueChanges( () => {
- const batch = doc.batch();
- batch.remove( ModelRange.createIn( modelCaption ) );
- doc.selection.removeAllRanges();
- } );
- // Check if there is no figcaption in the view.
- expect( getViewData( viewDocument ) ).to.equal(
- '<p>{}foo</p>' +
- '<figure class="ck-widget image" contenteditable="false">' +
- '<img src=""></img>' +
- '<figcaption class="ck-editable ck-hidden ck-placeholder" ' +
- 'contenteditable="true" data-placeholder="Enter image caption">' +
- '</figcaption>' +
- '</figure>'
- );
- editor.execute( 'undo' );
- // Check if figcaption is back with contents.
- expect( getViewData( viewDocument ) ).to.equal(
- '<p>foo</p>' +
- '<figure class="ck-widget image" contenteditable="false">' +
- '<img src=""></img>' +
- '<figcaption class="ck-editable" contenteditable="true" data-placeholder="Enter image caption">' +
- '{foo bar baz}' +
- '</figcaption>' +
- '</figure>'
- );
- } );
- it( 'undo should work after inserting the image', () => {
- const image = new ModelElement( 'image' );
- image.setAttribute( 'src', '/foo.png' );
- setModelData( doc, '<paragraph>foo[]</paragraph>' );
- doc.enqueueChanges( () => {
- const batch = doc.batch();
- batch.insert( image, doc.getRoot() );
- } );
- expect( getModelData( doc ) ).to.equal( '<image src="/foo.png"><caption></caption></image><paragraph>foo[]</paragraph>' );
- editor.execute( 'undo' );
- expect( getModelData( doc ) ).to.equal( '<paragraph>foo[]</paragraph>' );
- } );
- } );
- } );
- } );
|