| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- /**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* bender-tags: treecontroller */
- 'use strict';
- import ViewConversionDispatcher from '/ckeditor5/engine/treecontroller/viewconversiondispatcher.js';
- import ViewContainerElement from '/ckeditor5/engine/treeview/element.js';
- import ViewDocumentFragment from '/ckeditor5/engine/treeview/documentfragment.js';
- import ViewText from '/ckeditor5/engine/treeview/text.js';
- describe( 'ViewConversionDispatcher', () => {
- describe( 'constructor', () => {
- it( 'should create ViewConversionDispatcher with passed api', () => {
- const apiObj = {};
- const dispatcher = new ViewConversionDispatcher( { apiObj } );
- expect( dispatcher.conversionApi.apiObj ).to.equal( apiObj );
- expect( dispatcher.conversionApi ).to.have.property( 'convertItem' ).that.is.instanceof( Function );
- expect( dispatcher.conversionApi ).to.have.property( 'convertChildren' ).that.is.instanceof( Function );
- } );
- } );
- describe( 'convert', () => {
- let dispatcher;
- beforeEach( () => {
- dispatcher = new ViewConversionDispatcher();
- } );
- it( 'should fire viewCleanup event on converted view part', () => {
- sinon.spy( dispatcher, 'fire' );
- const viewP = new ViewContainerElement( 'p' );
- dispatcher.convert( viewP );
- expect( dispatcher.fire.calledWith( 'viewCleanup', viewP ) ).to.be.true;
- } );
- it( 'should fire proper events', () => {
- const viewText = new ViewText( 'foobar' );
- const viewElement = new ViewContainerElement( 'p', null, viewText );
- const viewFragment = new ViewDocumentFragment( viewElement );
- sinon.spy( dispatcher, 'fire' );
- dispatcher.convert( viewText );
- dispatcher.convert( viewElement );
- dispatcher.convert( viewFragment );
- expect( dispatcher.fire.calledWith( 'text' ) ).to.be.true;
- expect( dispatcher.fire.calledWith( 'element:p' ) ).to.be.true;
- expect( dispatcher.fire.calledWith( 'documentFragment' ) ).to.be.true;
- } );
- it( 'should convert ViewText', () => {
- const viewText = new ViewText( 'foobar' );
- dispatcher.on( 'text', ( evt, data, consumable, conversionApi ) => {
- const result = {
- eventName: evt.name,
- input: data.input,
- // Check whether additional data has been passed.
- foo: data.foo
- };
- // Check whether consumable has appropriate value to consume.
- expect( consumable.consume( data.input ) ).to.be.true;
- // Check whether conversionApi of `dispatcher` has been passed.
- expect( conversionApi ).to.equal( dispatcher.conversionApi );
- // Set conversion result to `output` property of `data`.
- // Later we will check if it was returned by `convert` method.
- data.output = result;
- } );
- // Use `additionalData` parameter to check if it was passed to the event.
- const result = dispatcher.convert( viewText, { foo: 'bar' } );
- // Check conversion result.
- expect( result ).to.deep.equal( {
- eventName: 'text',
- input: viewText,
- foo: 'bar'
- } );
- } );
- it( 'should convert ViewContainerElement', () => {
- const viewElement = new ViewContainerElement( 'p', { attrKey: 'attrValue' } );
- dispatcher.on( 'element', ( evt, data, consumable, conversionApi ) => {
- const result = {
- eventName: evt.name,
- input: data.input,
- // Check whether additional data has been passed.
- foo: data.foo
- };
- // Check whether consumable has appropriate value to consume.
- expect( consumable.consume( data.input, { name: true } ) ).to.be.true;
- expect( consumable.consume( data.input, { attribute: 'attrKey' } ) ).to.be.true;
- // Check whether conversionApi of `dispatcher` has been passed.
- expect( conversionApi ).to.equal( dispatcher.conversionApi );
- // Set conversion result to `output` property of `data`.
- // Later we will check if it was returned by `convert` method.
- data.output = result;
- } );
- // Use `additionalData` parameter to check if it was passed to the event.
- const result = dispatcher.convert( viewElement, { foo: 'bar' } );
- // Check conversion result.
- expect( result ).to.deep.equal( {
- eventName: 'element:p',
- input: viewElement,
- foo: 'bar'
- } );
- } );
- it( 'should convert ViewDocumentFragment', () => {
- const viewFragment = new ViewDocumentFragment();
- dispatcher.on( 'documentFragment', ( evt, data, consumable, conversionApi ) => {
- const result = {
- eventName: evt.name,
- input: data.input,
- // Check whether additional data has been passed.
- foo: data.foo
- };
- // Check whether consumable has appropriate value to consume.
- expect( consumable.consume( data.input ) ).to.be.true;
- // Check whether conversionApi of `dispatcher` has been passed.
- expect( conversionApi ).to.equal( dispatcher.conversionApi );
- // Set conversion result to `output` property of `data`.
- // Later we will check if it was returned by `convert` method.
- data.output = result;
- } );
- // Use `additionalData` parameter to check if it was passed to the event.
- const result = dispatcher.convert( viewFragment, { foo: 'bar' } );
- // Check conversion result.
- expect( result ).to.deep.equal( {
- eventName: 'documentFragment',
- input: viewFragment,
- foo: 'bar'
- } );
- } );
- } );
- describe( 'conversionApi#convertItem', () => {
- it( 'should convert view elements and view text', () => {
- const dispatcher = new ViewConversionDispatcher();
- const viewFragment = new ViewDocumentFragment( [
- new ViewContainerElement( 'p' ), new ViewText( 'foobar' )
- ] );
- dispatcher.on( 'text', ( evt, data ) => {
- data.output = { text: data.input.data };
- } );
- dispatcher.on( 'element:p', ( evt, data ) => {
- data.output = { name: 'p' };
- } );
- dispatcher.on( 'documentFragment', ( evt, data, consumable, conversionApi ) => {
- data.output = [];
- for ( let child of data.input.getChildren() ) {
- data.output.push( conversionApi.convertItem( child ) );
- }
- } );
- const result = dispatcher.convert( viewFragment );
- expect( result ).to.deep.equal( [
- { name: 'p' },
- { text: 'foobar' }
- ] );
- } );
- } );
- describe( 'conversionApi#convertChildren', () => {
- it( 'should fire proper events for all children of passed view part', () => {
- const dispatcher = new ViewConversionDispatcher();
- const viewFragment = new ViewDocumentFragment( [
- new ViewContainerElement( 'p' ), new ViewText( 'foobar' )
- ] );
- dispatcher.on( 'text', ( evt, data ) => {
- data.output = { text: data.input.data };
- } );
- dispatcher.on( 'element:p', ( evt, data ) => {
- data.output = { name: 'p' };
- } );
- dispatcher.on( 'documentFragment', ( evt, data, consumable, conversionApi ) => {
- data.output = conversionApi.convertChildren( data.input );
- } );
- const result = dispatcher.convert( viewFragment );
- expect( result ).to.deep.equal( [
- { name: 'p' },
- { text: 'foobar' }
- ] );
- } );
- } );
- } );
|