| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import Conversion from '../../src/conversion/conversion';
- import UpcastDispatcher from '../../src/conversion/upcastdispatcher';
- import ViewContainerElement from '../../src/view/containerelement';
- import ViewDocumentFragment from '../../src/view/documentfragment';
- import ViewText from '../../src/view/text';
- import ViewUIElement from '../../src/view/uielement';
- import ViewAttributeElement from '../../src/view/attributeelement';
- import Model from '../../src/model/model';
- import ModelDocumentFragment from '../../src/model/documentfragment';
- import ModelElement from '../../src/model/element';
- import ModelText from '../../src/model/text';
- import ModelRange from '../../src/model/range';
- import ModelPosition from '../../src/model/position';
- import {
- upcastElementToElement, upcastElementToAttribute, upcastAttributeToAttribute, upcastElementToMarker,
- convertToModelFragment, convertText
- } from '../../src/conversion/upcast-converters';
- import { stringify } from '../../src/dev-utils/model';
- describe( 'upcast-helpers', () => {
- let upcastDispatcher, model, schema, conversion;
- beforeEach( () => {
- model = new Model();
- schema = model.schema;
- schema.extend( '$text', {
- allowIn: '$root',
- allowAttributes: [ 'bold' ]
- } );
- schema.register( 'paragraph', {
- inheritAllFrom: '$block'
- } );
- upcastDispatcher = new UpcastDispatcher( { schema } );
- upcastDispatcher.on( 'text', convertText() );
- upcastDispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
- upcastDispatcher.on( 'documentFragment', convertToModelFragment(), { priority: 'lowest' } );
- conversion = new Conversion();
- conversion.register( 'upcast', [ upcastDispatcher ] );
- } );
- describe( 'upcastElementToElement', () => {
- it( 'config.view is a string', () => {
- const helper = upcastElementToElement( { view: 'p', model: 'paragraph' } );
- conversion.for( 'upcast' ).add( helper );
- expectResult( new ViewContainerElement( 'p' ), '<paragraph></paragraph>' );
- } );
- it( 'can be overwritten using priority', () => {
- schema.register( 'p', {
- inheritAllFrom: '$block'
- } );
- const helperA = upcastElementToElement( { view: 'p', model: 'p' } );
- const helperB = upcastElementToElement( { view: 'p', model: 'paragraph', priority: 'high' } );
- conversion.for( 'upcast' ).add( helperA ).add( helperB );
- expectResult( new ViewContainerElement( 'p' ), '<paragraph></paragraph>' );
- } );
- it( 'config.view is an object', () => {
- schema.register( 'fancyParagraph', {
- inheritAllFrom: '$block'
- } );
- const helperFancy = upcastElementToElement( {
- view: {
- name: 'p',
- class: 'fancy'
- },
- model: 'fancyParagraph',
- } );
- conversion.for( 'upcast' ).add( helperFancy );
- expectResult( new ViewContainerElement( 'p', { class: 'fancy' } ), '<fancyParagraph></fancyParagraph>' );
- } );
- it( 'config.model is a function', () => {
- schema.register( 'heading', {
- inheritAllFrom: '$block',
- allowAttributes: [ 'level' ]
- } );
- const helper = upcastElementToElement( {
- view: {
- name: 'p',
- class: 'heading'
- },
- model: ( viewElement, modelWriter ) => {
- return modelWriter.createElement( 'heading', { level: viewElement.getAttribute( 'data-level' ) } );
- }
- } );
- conversion.for( 'upcast' ).add( helper );
- expectResult( new ViewContainerElement( 'p', { class: 'heading', 'data-level': 2 } ), '<heading level="2"></heading>' );
- } );
- it( 'should fire conversion of the element children', () => {
- const helper = upcastElementToElement( { view: 'p', model: 'paragraph' } );
- conversion.for( 'upcast' ).add( helper );
- expectResult( new ViewContainerElement( 'p', null, new ViewText( 'foo' ) ), '<paragraph>foo</paragraph>' );
- } );
- it( 'should not insert a model element if it is not allowed by schema', () => {
- const helper = upcastElementToElement( { view: 'h2', model: 'heading' } );
- conversion.for( 'upcast' ).add( helper );
- expectResult( new ViewContainerElement( 'h2' ), '' );
- } );
- it( 'should auto-break elements', () => {
- schema.register( 'heading', {
- inheritAllFrom: '$block'
- } );
- const helperParagraph = upcastElementToElement( { view: 'p', model: 'paragraph' } );
- const helperHeading = upcastElementToElement( { view: 'h2', model: 'heading' } );
- conversion.for( 'upcast' ).add( helperParagraph ).add( helperHeading );
- expectResult(
- new ViewContainerElement( 'p', null, [
- new ViewText( 'Foo' ),
- new ViewContainerElement( 'h2', null, new ViewText( 'Xyz' ) ),
- new ViewText( 'Bar' )
- ] ),
- '<paragraph>Foo</paragraph><heading>Xyz</heading><paragraph>Bar</paragraph>'
- );
- } );
- it( 'should not do anything if returned model element is null', () => {
- const helperA = upcastElementToElement( { view: 'p', model: 'paragraph' } );
- const helperB = upcastElementToElement( { view: 'p', model: () => null, priority: 'high' } );
- conversion.for( 'upcast' ).add( helperA ).add( helperB );
- expectResult( new ViewContainerElement( 'p' ), '<paragraph></paragraph>' );
- } );
- } );
- describe( 'upcastElementToAttribute', () => {
- it( 'config.view is string', () => {
- const helper = upcastElementToAttribute( { view: 'strong', model: 'bold' } );
- conversion.for( 'upcast' ).add( helper );
- expectResult(
- new ViewAttributeElement( 'strong', null, new ViewText( 'foo' ) ),
- '<$text bold="true">foo</$text>'
- );
- } );
- it( 'can be overwritten using priority', () => {
- const helperA = upcastElementToAttribute( { view: 'strong', model: 'strong' } );
- const helperB = upcastElementToAttribute( { view: 'strong', model: 'bold', priority: 'high' } );
- conversion.for( 'upcast' ).add( helperA ).add( helperB );
- expectResult(
- new ViewAttributeElement( 'strong', null, new ViewText( 'foo' ) ),
- '<$text bold="true">foo</$text>'
- );
- } );
- it( 'config.view is an object', () => {
- const helper = upcastElementToAttribute( {
- view: {
- name: 'span',
- class: 'bold'
- },
- model: 'bold'
- } );
- conversion.for( 'upcast' ).add( helper );
- expectResult(
- new ViewAttributeElement( 'span', { class: 'bold' }, new ViewText( 'foo' ) ),
- '<$text bold="true">foo</$text>'
- );
- } );
- it( 'model attribute value is given', () => {
- schema.extend( '$text', {
- allowAttributes: [ 'styled' ]
- } );
- const helper = upcastElementToAttribute( {
- view: {
- name: 'span',
- class: [ 'styled', 'styled-dark' ]
- },
- model: {
- key: 'styled',
- value: 'dark'
- }
- } );
- conversion.for( 'upcast' ).add( helper );
- expectResult(
- new ViewAttributeElement( 'span', { class: 'styled styled-dark' }, new ViewText( 'foo' ) ),
- '<$text styled="dark">foo</$text>'
- );
- } );
- it( 'model attribute value is a function', () => {
- schema.extend( '$text', {
- allowAttributes: [ 'fontSize' ]
- } );
- const helper = upcastElementToAttribute( {
- view: {
- name: 'span',
- style: {
- 'font-size': /[\s\S]+/
- }
- },
- model: {
- key: 'fontSize',
- value: viewElement => {
- const fontSize = viewElement.getStyle( 'font-size' );
- const value = fontSize.substr( 0, fontSize.length - 2 );
- if ( value <= 10 ) {
- return 'small';
- } else if ( value > 12 ) {
- return 'big';
- }
- return null;
- }
- }
- } );
- conversion.for( 'upcast' ).add( helper );
- expectResult(
- new ViewAttributeElement( 'span', { style: 'font-size:9px' }, new ViewText( 'foo' ) ),
- '<$text fontSize="small">foo</$text>'
- );
- expectResult(
- new ViewAttributeElement( 'span', { style: 'font-size:12px' }, new ViewText( 'foo' ) ),
- 'foo'
- );
- expectResult(
- new ViewAttributeElement( 'span', { style: 'font-size:14px' }, new ViewText( 'foo' ) ),
- '<$text fontSize="big">foo</$text>'
- );
- } );
- it( 'should not set an attribute if it is not allowed by schema', () => {
- const helper = upcastElementToAttribute( { view: 'em', model: 'italic' } );
- conversion.for( 'upcast' ).add( helper );
- expectResult(
- new ViewAttributeElement( 'em', null, new ViewText( 'foo' ) ),
- 'foo'
- );
- } );
- it( 'should not do anything if returned model attribute is null', () => {
- const helperA = upcastElementToAttribute( { view: 'strong', model: 'bold' } );
- const helperB = upcastElementToAttribute( {
- view: 'strong',
- model: {
- key: 'bold',
- value: () => null
- },
- priority: 'high'
- } );
- conversion.for( 'upcast' ).add( helperA ).add( helperB );
- expectResult(
- new ViewAttributeElement( 'strong', null, new ViewText( 'foo' ) ),
- '<$text bold="true">foo</$text>'
- );
- } );
- } );
- describe( 'upcastAttributeToAttribute', () => {
- beforeEach( () => {
- conversion.for( 'upcast' ).add( upcastElementToElement( { view: 'img', model: 'image' } ) );
- schema.register( 'image', {
- inheritAllFrom: '$block'
- } );
- } );
- it( 'config.view is a string', () => {
- schema.extend( 'image', {
- allowAttributes: [ 'source' ]
- } );
- const helper = upcastAttributeToAttribute( { view: 'src', model: 'source' } );
- conversion.for( 'upcast' ).add( helper );
- expectResult(
- new ViewAttributeElement( 'img', { src: 'foo.jpg' } ),
- '<image source="foo.jpg"></image>'
- );
- } );
- it( 'config.view has only key set', () => {
- schema.extend( 'image', {
- allowAttributes: [ 'source' ]
- } );
- const helper = upcastAttributeToAttribute( { view: { key: 'src' }, model: 'source' } );
- conversion.for( 'upcast' ).add( helper );
- expectResult(
- new ViewAttributeElement( 'img', { src: 'foo.jpg' } ),
- '<image source="foo.jpg"></image>'
- );
- } );
- it( 'can be overwritten using priority', () => {
- schema.extend( 'image', {
- allowAttributes: [ 'src', 'source' ]
- } );
- const helperA = upcastAttributeToAttribute( { view: { key: 'src' }, model: 'src' } );
- const helperB = upcastAttributeToAttribute( { view: { key: 'src' }, model: 'source', priority: 'normal' } );
- conversion.for( 'upcast' ).add( helperA ).add( helperB );
- expectResult(
- new ViewAttributeElement( 'img', { src: 'foo.jpg' } ),
- '<image source="foo.jpg"></image>'
- );
- } );
- it( 'config.view has value set', () => {
- schema.extend( 'image', {
- allowAttributes: [ 'styled' ]
- } );
- const helper = upcastAttributeToAttribute( {
- view: {
- key: 'data-style',
- value: /[\s\S]*/
- },
- model: 'styled'
- } );
- conversion.for( 'upcast' ).add( helper );
- expectResult(
- new ViewAttributeElement( 'img', { 'data-style': 'dark' } ),
- '<image styled="dark"></image>'
- );
- } );
- it( 'model attribute value is a string', () => {
- schema.extend( 'image', {
- allowAttributes: [ 'styled' ]
- } );
- const helper = upcastAttributeToAttribute( {
- view: {
- name: 'img',
- key: 'class',
- value: 'styled-dark'
- },
- model: {
- key: 'styled',
- value: 'dark'
- }
- } );
- conversion.for( 'upcast' )
- .add( helper )
- .add( upcastElementToElement( { view: 'p', model: 'paragraph' } ) );
- expectResult(
- new ViewContainerElement( 'img', { class: 'styled-dark' } ),
- '<image styled="dark"></image>'
- );
- expectResult(
- new ViewContainerElement( 'img', { class: 'styled-xxx' } ),
- '<image></image>'
- );
- expectResult(
- new ViewContainerElement( 'p', { class: 'styled-dark' } ),
- '<paragraph></paragraph>'
- );
- } );
- it( 'model attribute value is a function', () => {
- schema.extend( 'image', {
- allowAttributes: [ 'styled' ]
- } );
- const helper = upcastAttributeToAttribute( {
- view: {
- key: 'class',
- value: /styled-[\S]+/
- },
- model: {
- key: 'styled',
- value: viewElement => {
- const regexp = /styled-([\S]+)/;
- const match = viewElement.getAttribute( 'class' ).match( regexp );
- return match[ 1 ];
- }
- }
- } );
- conversion.for( 'upcast' ).add( helper );
- expectResult(
- new ViewAttributeElement( 'img', { 'class': 'styled-dark' } ),
- '<image styled="dark"></image>'
- );
- } );
- it( 'should not set an attribute if it is not allowed by schema', () => {
- const helper = upcastAttributeToAttribute( { view: 'src', model: 'source' } );
- conversion.for( 'upcast' ).add( helper );
- expectResult(
- new ViewAttributeElement( 'img', { src: 'foo.jpg' } ),
- '<image></image>'
- );
- } );
- it( 'should not do anything if returned model attribute is null', () => {
- schema.extend( 'image', {
- allowAttributes: [ 'styled' ]
- } );
- const helperA = upcastAttributeToAttribute( {
- view: {
- key: 'class',
- value: 'styled'
- },
- model: {
- key: 'styled',
- value: true
- }
- } );
- const helperB = upcastAttributeToAttribute( {
- view: {
- key: 'class',
- value: 'styled'
- },
- model: {
- key: 'styled',
- value: () => null
- }
- } );
- conversion.for( 'upcast' ).add( helperA ).add( helperB );
- expectResult(
- new ViewAttributeElement( 'img', { class: 'styled' } ),
- '<image styled="true"></image>'
- );
- } );
- } );
- describe( 'upcastElementToMarker', () => {
- it( 'config.view is a string', () => {
- const helper = upcastElementToMarker( { view: 'marker-search', model: 'search' } );
- conversion.for( 'upcast' ).add( helper );
- const frag = new ViewDocumentFragment( [
- new ViewText( 'fo' ),
- new ViewUIElement( 'marker-search' ),
- new ViewText( 'oba' ),
- new ViewUIElement( 'marker-search' ),
- new ViewText( 'r' )
- ] );
- const marker = { name: 'search', start: [ 2 ], end: [ 5 ] };
- expectResult( frag, 'foobar', marker );
- } );
- it( 'can be overwritten using priority', () => {
- const helperA = upcastElementToMarker( { view: 'marker-search', model: 'search-result' } );
- const helperB = upcastElementToMarker( { view: 'marker-search', model: 'search', priority: 'high' } );
- conversion.for( 'upcast' ).add( helperA ).add( helperB );
- const frag = new ViewDocumentFragment( [
- new ViewText( 'fo' ),
- new ViewUIElement( 'marker-search' ),
- new ViewText( 'oba' ),
- new ViewUIElement( 'marker-search' ),
- new ViewText( 'r' )
- ] );
- const marker = { name: 'search', start: [ 2 ], end: [ 5 ] };
- expectResult( frag, 'foobar', marker );
- } );
- it( 'config.view is an object', () => {
- const helper = upcastElementToMarker( {
- view: {
- name: 'span',
- 'data-marker': 'search'
- },
- model: 'search'
- } );
- conversion.for( 'upcast' ).add( helper );
- const frag = new ViewDocumentFragment( [
- new ViewText( 'f' ),
- new ViewUIElement( 'span', { 'data-marker': 'search' } ),
- new ViewText( 'oob' ),
- new ViewUIElement( 'span', { 'data-marker': 'search' } ),
- new ViewText( 'ar' )
- ] );
- const marker = { name: 'search', start: [ 1 ], end: [ 4 ] };
- expectResult( frag, 'foobar', marker );
- } );
- it( 'config.model is a function', () => {
- const helper = upcastElementToMarker( {
- view: 'comment',
- model: viewElement => 'comment:' + viewElement.getAttribute( 'data-comment-id' )
- } );
- conversion.for( 'upcast' ).add( helper );
- const frag = new ViewDocumentFragment( [
- new ViewText( 'foo' ),
- new ViewUIElement( 'comment', { 'data-comment-id': 4 } ),
- new ViewText( 'b' ),
- new ViewUIElement( 'comment', { 'data-comment-id': 4 } ),
- new ViewText( 'ar' )
- ] );
- const marker = { name: 'comment:4', start: [ 3 ], end: [ 4 ] };
- expectResult( frag, 'foobar', marker );
- } );
- it( 'marker is in a block element', () => {
- conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'paragraph', view: 'p' } ) );
- const helper = upcastElementToMarker( { view: 'marker-search', model: 'search' } );
- conversion.for( 'upcast' ).add( helper );
- const element = new ViewContainerElement( 'p', null, [
- new ViewText( 'fo' ),
- new ViewUIElement( 'marker-search' ),
- new ViewText( 'oba' ),
- new ViewUIElement( 'marker-search' ),
- new ViewText( 'r' )
- ] );
- const marker = { name: 'search', start: [ 0, 2 ], end: [ 0, 5 ] };
- expectResult( element, '<paragraph>foobar</paragraph>', marker );
- } );
- } );
- function expectResult( viewToConvert, modelString, marker ) {
- const conversionResult = model.change( writer => upcastDispatcher.convert( viewToConvert, writer ) );
- if ( marker ) {
- expect( conversionResult.markers.has( marker.name ) ).to.be.true;
- const convertedMarker = conversionResult.markers.get( marker.name );
- expect( convertedMarker.start.path ).to.deep.equal( marker.start );
- expect( convertedMarker.end.path ).to.deep.equal( marker.end );
- }
- expect( stringify( conversionResult ) ).to.equal( modelString );
- }
- } );
- describe( 'upcast-converters', () => {
- let dispatcher, schema, context, model;
- beforeEach( () => {
- model = new Model();
- schema = model.schema;
- schema.register( 'paragraph', { inheritAllFrom: '$block' } );
- schema.extend( '$text', { allowIn: '$root' } );
- context = [ '$root' ];
- dispatcher = new UpcastDispatcher( { schema } );
- } );
- describe( 'convertText()', () => {
- it( 'should return converter converting ViewText to ModelText', () => {
- const viewText = new ViewText( 'foobar' );
- dispatcher.on( 'text', convertText() );
- const conversionResult = model.change( writer => dispatcher.convert( viewText, writer ) );
- expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
- expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
- expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
- } );
- it( 'should not convert already consumed texts', () => {
- const viewText = new ViewText( 'foofuckbafuckr' );
- // Default converter for elements. Returns just converted children. Added with lowest priority.
- dispatcher.on( 'text', convertText(), { priority: 'lowest' } );
- // Added with normal priority. Should make the above converter not fire.
- dispatcher.on( 'text', ( evt, data, conversionApi ) => {
- if ( conversionApi.consumable.consume( data.viewItem ) ) {
- const text = conversionApi.writer.createText( data.viewItem.data.replace( /fuck/gi, '****' ) );
- conversionApi.writer.insert( text, data.modelCursor );
- data.modelRange = ModelRange.createFromPositionAndShift( data.modelCursor, text.offsetSize );
- data.modelCursor = data.modelRange.end;
- }
- } );
- const conversionResult = model.change( writer => dispatcher.convert( viewText, writer, context ) );
- expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
- expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
- expect( conversionResult.getChild( 0 ).data ).to.equal( 'foo****ba****r' );
- } );
- it( 'should not convert text if it is wrong with schema', () => {
- schema.addChildCheck( ( ctx, childDef ) => {
- if ( childDef.name == '$text' && ctx.endsWith( '$root' ) ) {
- return false;
- }
- } );
- const viewText = new ViewText( 'foobar' );
- dispatcher.on( 'text', convertText() );
- let conversionResult = model.change( writer => dispatcher.convert( viewText, writer, context ) );
- expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
- expect( conversionResult.childCount ).to.equal( 0 );
- conversionResult = model.change( writer => dispatcher.convert( viewText, writer, [ '$block' ] ) );
- expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
- expect( conversionResult.childCount ).to.equal( 1 );
- expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
- expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
- } );
- it( 'should support unicode', () => {
- const viewText = new ViewText( 'நிலைக்கு' );
- dispatcher.on( 'text', convertText() );
- const conversionResult = model.change( writer => dispatcher.convert( viewText, writer, context ) );
- expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
- expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelText );
- expect( conversionResult.getChild( 0 ).data ).to.equal( 'நிலைக்கு' );
- } );
- } );
- describe( 'convertToModelFragment()', () => {
- it( 'should return converter converting whole ViewDocumentFragment to ModelDocumentFragment', () => {
- const viewFragment = new ViewDocumentFragment( [
- new ViewContainerElement( 'p', null, new ViewText( 'foo' ) ),
- new ViewText( 'bar' )
- ] );
- // To get any meaningful results we have to actually convert something.
- dispatcher.on( 'text', convertText() );
- // This way P element won't be converted per-se but will fire converting it's children.
- dispatcher.on( 'element', convertToModelFragment() );
- dispatcher.on( 'documentFragment', convertToModelFragment() );
- const conversionResult = model.change( writer => dispatcher.convert( viewFragment, writer, context ) );
- expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
- expect( conversionResult.maxOffset ).to.equal( 6 );
- expect( conversionResult.getChild( 0 ).data ).to.equal( 'foobar' );
- } );
- it( 'should not convert already consumed (converted) changes', () => {
- const viewP = new ViewContainerElement( 'p', null, new ViewText( 'foo' ) );
- // To get any meaningful results we have to actually convert something.
- dispatcher.on( 'text', convertText() );
- // Default converter for elements. Returns just converted children. Added with lowest priority.
- dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
- // Added with normal priority. Should make the above converter not fire.
- dispatcher.on( 'element:p', ( evt, data, conversionApi ) => {
- if ( conversionApi.consumable.consume( data.viewItem, { name: true } ) ) {
- const paragraph = conversionApi.writer.createElement( 'paragraph' );
- conversionApi.writer.insert( paragraph, data.modelCursor );
- conversionApi.convertChildren( data.viewItem, ModelPosition.createAt( paragraph ) );
- data.modelRange = ModelRange.createOn( paragraph );
- data.modelCursor = data.modelRange.end;
- }
- } );
- const conversionResult = model.change( writer => dispatcher.convert( viewP, writer, context ) );
- expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
- expect( conversionResult.getChild( 0 ) ).to.be.instanceof( ModelElement );
- expect( conversionResult.getChild( 0 ).name ).to.equal( 'paragraph' );
- expect( conversionResult.getChild( 0 ).maxOffset ).to.equal( 3 );
- expect( conversionResult.getChild( 0 ).getChild( 0 ).data ).to.equal( 'foo' );
- } );
- it( 'should forward correct modelCursor', () => {
- const spy = sinon.spy();
- const view = new ViewDocumentFragment( [
- new ViewContainerElement( 'div', null, [ new ViewText( 'abc' ), new ViewContainerElement( 'foo' ) ] ),
- new ViewContainerElement( 'bar' )
- ] );
- const position = ModelPosition.createAt( new ModelElement( 'element' ) );
- dispatcher.on( 'documentFragment', convertToModelFragment() );
- dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
- dispatcher.on( 'element:foo', ( evt, data ) => {
- // Be sure that current cursor is not the same as custom.
- expect( data.modelCursor ).to.not.equal( position );
- // Set custom cursor as a result of docFrag last child conversion.
- // This cursor should be forwarded by a documentFragment converter.
- data.modelCursor = position;
- // Be sure that callback was fired.
- spy();
- } );
- dispatcher.on( 'element:bar', ( evt, data ) => {
- expect( data.modelCursor ).to.equal( position );
- spy();
- } );
- model.change( writer => dispatcher.convert( view, writer ) );
- sinon.assert.calledTwice( spy );
- } );
- } );
- } );
|