| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import buildViewConverter from '../../src/conversion/buildviewconverter';
- import Model from '../../src/model/model';
- import ModelDocumentFragment from '../../src/model/documentfragment';
- import ModelElement from '../../src/model/element';
- import ModelTextProxy from '../../src/model/textproxy';
- import ModelRange from '../../src/model/range';
- import ModelWalker from '../../src/model/treewalker';
- import ViewDocumentFragment from '../../src/view/documentfragment';
- import ViewContainerElement from '../../src/view/containerelement';
- import ViewAttributeElement from '../../src/view/attributeelement';
- import ViewText from '../../src/view/text';
- import ViewMatcher from '../../src/view/matcher';
- import ViewConversionDispatcher from '../../src/conversion/viewconversiondispatcher';
- import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
- import { convertToModelFragment, convertText } from '../../src/conversion/view-to-model-converters';
- function modelAttributesToString( item ) {
- let result = '';
- for ( const attr of item.getAttributes() ) {
- result += ' ' + attr[ 0 ] + '="' + attr[ 1 ] + '"';
- }
- return result;
- }
- function modelToString( item ) {
- let result = '';
- if ( item instanceof ModelTextProxy ) {
- const attributes = modelAttributesToString( item );
- result = attributes ? '<$text' + attributes + '>' + item.data + '</$text>' : item.data;
- } else {
- const walker = new ModelWalker( { boundaries: ModelRange.createIn( item ), shallow: true } );
- for ( const value of walker ) {
- result += modelToString( value.item );
- }
- if ( item instanceof ModelElement ) {
- const attributes = modelAttributesToString( item );
- result = '<' + item.name + attributes + '>' + result + '</' + item.name + '>';
- }
- }
- return result;
- }
- const textAttributes = [ 'linkHref', 'linkTitle', 'bold', 'italic', 'style' ];
- const pAttributes = [ 'class', 'important', 'theme', 'decorated', 'size' ];
- describe( 'View converter builder', () => {
- let dispatcher, model, schema, context;
- beforeEach( () => {
- model = new Model();
- // `context` parameter for `.convert` calls.
- context = [ '$root' ];
- schema = model.schema;
- schema.register( 'paragraph', {
- inheritAllFrom: '$block',
- allowAttributes: pAttributes
- } );
- schema.register( 'div', {
- inheritAllFrom: '$block',
- allowAttributes: 'class'
- } );
- schema.register( 'customP', {
- inheritAllFrom: 'paragraph'
- } );
- schema.register( 'image', {
- inheritAllFrom: '$text',
- allowAttributes: 'src'
- } );
- schema.register( 'span', {
- inheritAllFrom: '$text',
- allowAttributes: 'transformer'
- } );
- // Yes, folks, we are building MEGATRON.
- schema.register( 'MEGATRON', {
- inheritAllFrom: '$text'
- } );
- schema.register( 'abcd', {
- inheritAllFrom: '$text'
- } );
- schema.extend( '$text', {
- allowAttributes: textAttributes,
- allowIn: [ '$root', 'span', 'abcd', 'MEGATRON' ]
- } );
- dispatcher = new ViewConversionDispatcher( model, { schema } );
- dispatcher.on( 'text', convertText() );
- } );
- it( 'should convert from view element to model element', () => {
- buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
- const conversionResult = dispatcher.convert( new ViewContainerElement( 'p', null, new ViewText( 'foo' ) ) );
- expect( modelToString( conversionResult ) ).to.equal( '<paragraph>foo</paragraph>' );
- } );
- it( 'should convert from view element to model element using creator function', () => {
- buildViewConverter().for( dispatcher )
- .fromElement( 'img' )
- .toElement( viewElement => new ModelElement( 'image', { src: viewElement.getAttribute( 'src' ) } ) );
- const conversionResult = dispatcher.convert( new ViewContainerElement( 'img', { src: 'foo.jpg' } ), context );
- expect( modelToString( conversionResult ) ).to.equal( '<image src="foo.jpg"></image>' );
- } );
- it( 'should convert to model element when element children are not allowed in parent (empty split elements should be removed)', () => {
- schema.register( 'section', {
- inheritAllFrom: '$block'
- } );
- buildViewConverter().for( dispatcher )
- .fromElement( 'p' )
- .toElement( 'paragraph' );
- buildViewConverter().for( dispatcher )
- .fromElement( 'section' )
- .toElement( 'section' );
- const input = new ViewContainerElement( 'p', null, [
- new ViewText( 'foo' ),
- new ViewContainerElement( 'section', null, [
- new ViewContainerElement( 'p', null, [
- new ViewText( 'abc' ),
- new ViewContainerElement( 'section' ),
- new ViewText( 'cde' ),
- ] )
- ] ),
- new ViewText( 'bar' ),
- ] );
- const conversionResult = dispatcher.convert( input );
- expect( modelToString( conversionResult ) ).to.equal(
- '<paragraph>foo</paragraph>' +
- '<paragraph>abc</paragraph>' +
- '<section></section>' +
- '<paragraph>cde</paragraph>' +
- '<paragraph>bar</paragraph>'
- );
- } );
- it( 'should convert from view element to model attribute', () => {
- buildViewConverter().for( dispatcher ).fromElement( 'strong' ).toAttribute( 'bold', true );
- const conversionResult = dispatcher.convert(
- new ViewAttributeElement( 'strong', null, new ViewText( 'foo' ) ), context
- );
- // Have to check root because result is a ModelText.
- expect( modelToString( conversionResult ) ).to.equal( '<$text bold="true">foo</$text>' );
- } );
- it( 'should convert from view element to model attributes using creator function', () => {
- buildViewConverter().for( dispatcher )
- .fromElement( 'a' )
- .toAttribute( viewElement => ( { key: 'linkHref', value: viewElement.getAttribute( 'href' ) } ) );
- const conversionResult = dispatcher.convert(
- new ViewAttributeElement( 'a', { href: 'foo.html' }, new ViewText( 'foo' ) ), context
- );
- // Have to check root because result is a ModelText.
- expect( modelToString( conversionResult ) ).to.equal( '<$text linkHref="foo.html">foo</$text>' );
- } );
- it( 'should convert from view attribute to model attribute', () => {
- buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
- buildViewConverter().for( dispatcher )
- .fromAttribute( 'class' )
- .toAttribute( viewElement => ( { key: 'class', value: viewElement.getAttribute( 'class' ) } ) );
- const conversionResult = dispatcher.convert(
- new ViewContainerElement( 'p', { class: 'myClass' }, new ViewText( 'foo' ) ), context
- );
- expect( modelToString( conversionResult ) ).to.equal( '<paragraph class="myClass">foo</paragraph>' );
- } );
- it( 'should convert from view attribute and key to model attribute', () => {
- schema.extend( 'paragraph', { allowAttributes: 'type' } );
- dispatcher.on( 'documentFragment', convertToModelFragment() );
- buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
- buildViewConverter().for( dispatcher ).fromAttribute( 'class', 'important' ).toAttribute( 'important', true );
- buildViewConverter().for( dispatcher ).fromAttribute( 'class', 'theme-nice' ).toAttribute( 'theme', 'nice' );
- buildViewConverter().for( dispatcher ).fromAttribute( 'data-type' ).toAttribute( 'type' );
- const viewStructure = new ViewDocumentFragment( [
- new ViewContainerElement( 'p', { class: 'important' }, new ViewText( 'foo' ) ),
- new ViewContainerElement( 'p', { class: 'important theme-nice' }, new ViewText( 'bar' ) ),
- new ViewContainerElement( 'p', { 'data-type': 'foo' }, new ViewText( 'xyz' ) )
- ] );
- const conversionResult = dispatcher.convert( viewStructure, context );
- expect( modelToString( conversionResult ) ).to.equal(
- '<paragraph important="true">foo</paragraph>' +
- '<paragraph important="true" theme="nice">bar</paragraph>' +
- '<paragraph type="foo">xyz</paragraph>'
- );
- } );
- it( 'should convert from multiple view entities to model attribute', () => {
- buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
- buildViewConverter().for( dispatcher )
- .fromElement( 'strong' )
- .fromElement( 'b' )
- .fromAttribute( 'class', 'bold' )
- .fromAttribute( 'style', { 'font-weight': 'bold' } )
- .toAttribute( 'bold', true );
- const viewElement = new ViewContainerElement( 'p', null, [
- new ViewAttributeElement( 'strong', null, new ViewText( 'aaa' ) ),
- new ViewAttributeElement( 'b', null, new ViewText( 'bbb' ) ),
- new ViewContainerElement( 'span', { class: 'bold' }, new ViewText( 'ccc' ) ),
- new ViewContainerElement( 'span', { style: 'font-weight:bold; font-size:20px' }, new ViewText( 'ddd' ) )
- ] );
- const conversionResult = dispatcher.convert( viewElement, context );
- expect( modelToString( conversionResult ) ).to.equal( '<paragraph><$text bold="true">aaabbbcccddd</$text></paragraph>' );
- } );
- it( 'should convert from pattern to marker', () => {
- buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
- buildViewConverter().for( dispatcher ).from( { attribute: { 'data-name': 'search' } } ).toMarker();
- const viewElement = new ViewContainerElement( 'p', null, [
- new ViewText( 'Fo' ),
- new ViewAttributeElement( 'marker', { 'data-name': 'search' } ),
- new ViewText( 'o ba' ),
- new ViewAttributeElement( 'marker', { 'data-name': 'search' } ),
- new ViewText( 'r' )
- ] );
- const conversionResult = dispatcher.convert( viewElement, context );
- const markerSearch = conversionResult.markers.get( 'search' );
- expect( conversionResult.markers.size ).to.equal( 1 );
- expect( markerSearch.start.path ).to.deep.equal( [ 0, 2 ] );
- expect( markerSearch.end.path ).to.deep.equal( [ 0, 6 ] );
- } );
- it( 'should convert from element to marker using creator function', () => {
- buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
- buildViewConverter().for( dispatcher ).fromElement( 'marker' ).toMarker( data => {
- return new ModelElement( '$marker', { 'data-name': data.getAttribute( 'class' ) } );
- } );
- const viewElement = new ViewContainerElement( 'p', null, [
- new ViewText( 'Fo' ),
- new ViewAttributeElement( 'marker', { 'class': 'search' } ),
- new ViewText( 'o ba' ),
- new ViewAttributeElement( 'marker', { 'class': 'search' } ),
- new ViewText( 'r' )
- ] );
- const conversionResult = dispatcher.convert( viewElement, context );
- const markerSearch = conversionResult.markers.get( 'search' );
- expect( conversionResult.markers.size ).to.equal( 1 );
- expect( markerSearch.start.path ).to.deep.equal( [ 0, 2 ] );
- expect( markerSearch.end.path ).to.deep.equal( [ 0, 6 ] );
- } );
- it( 'should convert from multiple view entities to marker', () => {
- buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
- buildViewConverter().for( dispatcher )
- .from( { attribute: { 'foo': 'marker' } } )
- .from( { attribute: { 'bar': 'marker' } } )
- .from( { attribute: { 'foo': 'marker', 'bar': 'marker' } } )
- .toMarker();
- const viewElement = new ViewContainerElement( 'p', null, [
- new ViewText( 'Fo' ),
- new ViewAttributeElement( 'span', { 'foo': 'marker', 'data-name': 'marker1' } ),
- new ViewText( 'o b' ),
- new ViewAttributeElement( 'span', { 'bar': 'marker', 'data-name': 'marker2' } ),
- new ViewText( 'a' ),
- new ViewAttributeElement( 'span', { 'foo': 'marker', 'bar': 'marker', 'data-name': 'marker3' } ),
- new ViewText( 'r' )
- ] );
- const conversionResult = dispatcher.convert( viewElement );
- const marker1 = conversionResult.markers.get( 'marker1' );
- const marker2 = conversionResult.markers.get( 'marker2' );
- const marker3 = conversionResult.markers.get( 'marker3' );
- expect( conversionResult.markers.size ).to.equal( 3 );
- expect( marker1.start.path ).to.deep.equal( marker1.end.path ).to.deep.equal( [ 0, 2 ] );
- expect( marker2.start.path ).to.deep.equal( marker2.end.path ).to.deep.equal( [ 0, 5 ] );
- expect( marker3.start.path ).to.deep.equal( marker3.end.path ).to.deep.equal( [ 0, 6 ] );
- } );
- it( 'should do nothing when there is no element matching to marker pattern', () => {
- buildViewConverter().for( dispatcher ).from( { class: 'color' } ).toMarker();
- const element = new ViewAttributeElement( 'span' );
- const result = dispatcher.convert( element );
- expect( result ).to.be.instanceof( ModelDocumentFragment );
- expect( result.childCount ).to.equal( 0 );
- } );
- it( 'should throw an error when view element in not valid to convert to marker', () => {
- buildViewConverter().for( dispatcher ).fromElement( 'marker' ).toMarker();
- const element = new ViewAttributeElement( 'marker', { class: 'search' } );
- expect( () => {
- dispatcher.convert( element, context );
- } ).to.throw( CKEditorError, /^build-view-converter-invalid-marker/ );
- } );
- it( 'should throw an error when model element returned by creator has not valid name', () => {
- buildViewConverter().for( dispatcher ).fromElement( 'marker' ).toMarker( () => {
- return new ModelElement( 'element', { 'data-name': 'search' } );
- } );
- const element = new ViewAttributeElement( 'marker', { 'data-name': 'search' } );
- expect( () => {
- dispatcher.convert( element, context );
- } ).to.throw( CKEditorError, /^build-view-converter-invalid-marker/ );
- } );
- it( 'should throw an error when model element returned by creator has not valid data-name attribute', () => {
- buildViewConverter().for( dispatcher ).fromElement( 'marker' ).toMarker( () => {
- return new ModelElement( '$marker', { 'foo': 'search' } );
- } );
- const element = new ViewAttributeElement( 'marker', { 'data-name': 'search' } );
- expect( () => {
- dispatcher.convert( element, context );
- } ).to.throw( CKEditorError, /^build-view-converter-invalid-marker/ );
- } );
- it( 'should convert from pattern to model element', () => {
- buildViewConverter().for( dispatcher ).from(
- { name: 'span', class: 'megatron', attribute: { head: 'megatron', body: 'megatron', legs: 'megatron' } }
- ).toElement( 'MEGATRON' );
- // Adding callbacks later so they are called later. MEGATRON callback is more important.
- buildViewConverter().for( dispatcher ).fromElement( 'span' ).toElement( 'span' );
- buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
- let result;
- // Not quite megatron.
- result = dispatcher.convert(
- new ViewContainerElement( 'span', { class: 'megatron' }, new ViewText( 'foo' ) ), context
- );
- expect( modelToString( result ) ).to.equal( '<span>foo</span>' );
- // Almost a megatron. Missing a head.
- result = dispatcher.convert(
- new ViewContainerElement( 'span', { class: 'megatron', body: 'megatron', legs: 'megatron' }, new ViewText( 'foo' ) ),
- context
- );
- expect( modelToString( result ) ).to.equal( '<span>foo</span>' );
- // This would be a megatron but is a paragraph.
- result = dispatcher.convert(
- new ViewContainerElement(
- 'p',
- { class: 'megatron', body: 'megatron', legs: 'megatron', head: 'megatron' },
- new ViewText( 'foo' )
- ),
- context
- );
- expect( modelToString( result ) ).to.equal( '<paragraph>foo</paragraph>' );
- // At last we have a megatron!
- result = dispatcher.convert(
- new ViewContainerElement(
- 'span',
- { class: 'megatron', body: 'megatron', legs: 'megatron', head: 'megatron' },
- new ViewText( 'foo' )
- ),
- context
- );
- expect( modelToString( result ) ).to.equal( '<MEGATRON>foo</MEGATRON>' );
- } );
- it( 'should convert from pattern to model attribute', () => {
- buildViewConverter().for( dispatcher ).fromElement( 'span' ).toElement( 'span' );
- // This time without name so default span converter will convert children.
- buildViewConverter().for( dispatcher )
- .from( { class: 'megatron', attribute: { head: 'megatron', body: 'megatron', legs: 'megatron' } } )
- .toAttribute( 'transformer', 'megatron' );
- const viewElement = new ViewContainerElement(
- 'span',
- { class: 'megatron', body: 'megatron', legs: 'megatron', head: 'megatron' },
- new ViewText( 'foo' )
- );
- const conversionResult = dispatcher.convert( viewElement, context );
- expect( modelToString( conversionResult ) ).to.equal( '<span transformer="megatron">foo</span>' );
- } );
- it( 'should return model document fragment when converting attributes on text', () => {
- buildViewConverter().for( dispatcher ).fromElement( 'strong' ).toAttribute( 'bold', true );
- const viewElement = new ViewAttributeElement( 'strong', null, new ViewText( 'foo' ) );
- const conversionResult = dispatcher.convert( viewElement, context );
- expect( conversionResult.is( 'documentFragment' ) ).to.be.true;
- } );
- it( 'should set different priorities for `toElement` and `toAttribute` conversion', () => {
- buildViewConverter().for( dispatcher )
- .fromAttribute( 'class' )
- .toAttribute( viewElement => ( { key: 'class', value: viewElement.getAttribute( 'class' ) } ) );
- buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
- const conversionResult = dispatcher.convert(
- new ViewContainerElement( 'p', { class: 'myClass' }, new ViewText( 'foo' ) ), context
- );
- // Element converter was fired first even though attribute converter was added first.
- expect( modelToString( conversionResult ) ).to.equal( '<paragraph class="myClass">foo</paragraph>' );
- } );
- it( 'should overwrite default priorities for converters', () => {
- buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
- buildViewConverter().for( dispatcher )
- .fromAttribute( 'class' )
- .toAttribute( viewElement => ( { key: 'class', value: viewElement.getAttribute( 'class' ) } ) );
- let result;
- result = dispatcher.convert(
- new ViewContainerElement( 'p', { class: 'myClass' }, new ViewText( 'foo' ) ), context
- );
- expect( modelToString( result ) ).to.equal( '<paragraph class="myClass">foo</paragraph>' );
- buildViewConverter().for( dispatcher )
- .from( { name: 'p', class: 'myClass' } ).withPriority( 'high' )
- .toElement( 'customP' );
- result = dispatcher.convert(
- new ViewContainerElement( 'p', { class: 'myClass' }, new ViewText( 'foo' ) ), context
- );
- expect( modelToString( result ) ).to.equal( '<customP>foo</customP>' );
- } );
- it( 'should overwrite default consumed values', () => {
- // Converter (1).
- buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
- // Converter (2).
- buildViewConverter().for( dispatcher )
- .from( { name: 'p', class: 'decorated' } ).consuming( { class: 'decorated' } )
- .toAttribute( 'decorated', true );
- // Converter (3).
- buildViewConverter().for( dispatcher )
- .fromAttribute( 'class', 'small' ).consuming( { class: 'small' } )
- .toAttribute( 'size', 'small' );
- const viewElement = new ViewContainerElement( 'p', { class: 'decorated small' }, new ViewText( 'foo' ) );
- const conversionResult = dispatcher.convert( viewElement, context );
- // P element and it's children got converted by the converter (1) and the converter (1) got fired
- // because P name was not consumed in converter (2). Converter (3) could consume class="small" because
- // only class="decorated" was consumed in converter (2).
- expect( modelToString( conversionResult ) ).to.equal( '<paragraph decorated="true" size="small">foo</paragraph>' );
- } );
- it( 'should convert from matcher instance to model', () => {
- // Universal class converter, synonymous to .fromAttribute( 'class' ).
- buildViewConverter().for( dispatcher )
- .from( new ViewMatcher( { class: /.*/ } ) )
- .toAttribute( viewElement => ( { key: 'class', value: viewElement.getAttribute( 'class' ) } ) );
- // Universal element converter.
- buildViewConverter().for( dispatcher )
- .from( new ViewMatcher( { name: /.*/ } ) )
- .toElement( viewElement => new ModelElement( viewElement.name ) );
- const viewStructure = new ViewContainerElement( 'div', { class: 'myClass' }, [
- new ViewContainerElement( 'abcd', null, new ViewText( 'foo' ) )
- ] );
- const conversionResult = dispatcher.convert( viewStructure, context );
- expect( modelToString( conversionResult ) ).to.equal( '<div class="myClass"><abcd>foo</abcd></div>' );
- } );
- it( 'should filter out structure that is wrong with schema - elements', () => {
- buildViewConverter().for( dispatcher ).fromElement( 'div' ).toElement( 'div' );
- buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
- // Disallow $root>div.
- schema.addChildCheck( ( ctx, childDef ) => {
- if ( childDef.name == 'div' && ctx.endsWith( '$root' ) ) {
- return false;
- }
- } );
- dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
- const viewElement = new ViewContainerElement( 'div', null,
- new ViewContainerElement( 'p', null,
- new ViewText( 'foo' )
- )
- );
- const conversionResult = dispatcher.convert( viewElement, context );
- expect( modelToString( conversionResult ) ).to.equal( '<paragraph>foo</paragraph>' );
- } );
- it( 'should filter out structure that is wrong with schema - attributes', () => {
- buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
- buildViewConverter().for( dispatcher ).fromElement( 'strong' ).toAttribute( 'bold', true );
- // Disallow bold in paragraph>$text.
- schema.addAttributeCheck( ( ctx, attributeName ) => {
- if ( ctx.endsWith( 'paragraph $text' ) && attributeName == 'bold' ) {
- return false;
- }
- } );
- dispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
- const viewElement = new ViewContainerElement( 'p', null,
- new ViewAttributeElement( 'strong', null,
- new ViewText( 'foo' )
- )
- );
- const conversionResult = dispatcher.convert( viewElement, context );
- expect( modelToString( conversionResult ) ).to.equal( '<paragraph>foo</paragraph>' );
- } );
- it( 'should not set attribute when it is not allowed', () => {
- buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
- buildViewConverter().for( dispatcher ).fromElement( 'u' ).toAttribute( 'underscore', true );
- const viewElement = new ViewContainerElement( 'p', null,
- new ViewAttributeElement( 'u', null,
- new ViewText( 'foo' )
- )
- );
- const conversionResult = dispatcher.convert( viewElement, context );
- expect( modelToString( conversionResult ) ).to.equal( '<paragraph>foo</paragraph>' );
- } );
- it( 'should stop to element conversion if creating function returned null', () => {
- buildViewConverter()
- .for( dispatcher )
- .fromElement( 'p' )
- .toElement( viewElement => {
- return viewElement.hasAttribute( 'stop' ) ? null : new ModelElement( 'paragraph' );
- } );
- const viewElement = new ViewContainerElement( 'p' );
- let conversionResult = dispatcher.convert( viewElement, context );
- expect( modelToString( conversionResult ) ).to.equal( '<paragraph></paragraph>' );
- viewElement.setAttribute( 'stop', true );
- conversionResult = dispatcher.convert( viewElement, context );
- expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
- expect( conversionResult.childCount ).to.equal( 0 );
- } );
- it( 'should stop to attribute conversion if creating function returned null', () => {
- schema.extend( 'paragraph', { allowAttributes: 'type' } );
- buildViewConverter().for( dispatcher ).fromElement( 'p' ).toElement( 'paragraph' );
- buildViewConverter().for( dispatcher ).fromAttribute( 'data-type' ).toAttribute( viewElement => {
- const value = viewElement.getAttribute( 'data-type' );
- return value == 'stop' ? null : { key: 'type', value };
- } );
- const viewElement = new ViewContainerElement( 'p', { 'data-type': 'foo' } );
- let conversionResult = dispatcher.convert( viewElement, context );
- expect( modelToString( conversionResult ) ).to.equal( '<paragraph type="foo"></paragraph>' );
- viewElement.setAttribute( 'data-type', 'stop' );
- conversionResult = dispatcher.convert( viewElement, context );
- expect( modelToString( conversionResult ) ).to.equal( '<paragraph></paragraph>' );
- } );
- } );
|