| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- /**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import ViewConversionDispatcher from '../../src/conversion/viewconversiondispatcher';
- import ViewContainerElement from '../../src/view/containerelement';
- import ViewDocumentFragment from '../../src/view/documentfragment';
- import ViewText from '../../src/view/text';
- 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 { convertToModelFragment, convertText } from '../../src/conversion/view-to-model-converters';
- describe( 'view-to-model-converters', () => {
- let dispatcher, schema, additionalData, model;
- beforeEach( () => {
- model = new Model();
- schema = model.schema;
- schema.register( 'paragraph', { inheritAllFrom: '$block' } );
- schema.extend( '$text', { allowIn: '$root' } );
- additionalData = { context: [ '$root' ] };
- dispatcher = new ViewConversionDispatcher( model, { schema } );
- } );
- describe( 'convertText', () => {
- it( 'should return converter converting ViewText to ModelText', () => {
- const viewText = new ViewText( 'foobar' );
- dispatcher.on( 'text', convertText() );
- const conversionResult = dispatcher.convert( viewText, additionalData );
- 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, consumable ) => {
- if ( consumable.consume( data.input ) ) {
- data.output = new ModelText( data.input.data.replace( /fuck/gi, '****' ) );
- }
- } );
- const conversionResult = dispatcher.convert( viewText, additionalData );
- 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.on( 'checkChild', ( evt, args ) => {
- const ctx = args[ 0 ];
- const child = args[ 1 ];
- const childRule = schema.getRule( child );
- if ( childRule.name == '$text' && ctx.matchEnd( '$root' ) ) {
- evt.stop();
- evt.return = false;
- }
- }, { priority: 'high' } );
- const viewText = new ViewText( 'foobar' );
- dispatcher.on( 'text', convertText() );
- let conversionResult = dispatcher.convert( viewText, additionalData );
- expect( conversionResult ).to.be.instanceof( ModelDocumentFragment );
- expect( conversionResult.childCount ).to.equal( 0 );
- conversionResult = dispatcher.convert( viewText, { context: [ '$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 = dispatcher.convert( viewText, additionalData );
- 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 = dispatcher.convert( viewFragment, additionalData );
- 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, consumable, conversionApi ) => {
- if ( consumable.consume( data.input, { name: true } ) ) {
- data.output = new ModelElement( 'paragraph' );
- data.context.push( data.output );
- data.output.appendChildren( conversionApi.convertChildren( data.input, consumable, data ) );
- data.context.pop();
- }
- } );
- const conversionResult = dispatcher.convert( viewP, additionalData );
- 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' );
- } );
- } );
- } );
|