diff --git a/packages/ckeditor5-engine/src/controller/datacontroller.js b/packages/ckeditor5-engine/src/controller/datacontroller.js index 6090707dd9f..358637a3ea4 100644 --- a/packages/ckeditor5-engine/src/controller/datacontroller.js +++ b/packages/ckeditor5-engine/src/controller/datacontroller.js @@ -208,7 +208,7 @@ export default class DataController { * @param {Object} [options] Additional configuration passed to the conversion process. * @returns {String} Output data. */ - stringify( modelElementOrFragment, options ) { + stringify( modelElementOrFragment, options = {} ) { // Model -> view. const viewDocumentFragment = this.toView( modelElementOrFragment, options ); @@ -224,11 +224,11 @@ export default class DataController { * * @param {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment} modelElementOrFragment * Element or document fragment whose content will be converted. - * @param {Object} [options] Additional configuration that will be available through + * @param {Object} [options={}] Additional configuration that will be available through * {@link module:engine/conversion/downcastdispatcher~DowncastConversionApi#options} during the conversion process. * @returns {module:engine/view/documentfragment~DocumentFragment} Output view DocumentFragment. */ - toView( modelElementOrFragment, options ) { + toView( modelElementOrFragment, options = {} ) { const viewDocument = this.viewDocument; const viewWriter = this._viewWriter; diff --git a/packages/ckeditor5-engine/tests/controller/datacontroller.js b/packages/ckeditor5-engine/tests/controller/datacontroller.js index e31176bde86..9ba1ccea34e 100644 --- a/packages/ckeditor5-engine/tests/controller/datacontroller.js +++ b/packages/ckeditor5-engine/tests/controller/datacontroller.js @@ -376,41 +376,34 @@ describe( 'DataController', () => { } ); describe( 'get()', () => { - it( 'should get paragraph with text', () => { + beforeEach( () => { schema.register( 'paragraph', { inheritAllFrom: '$block' } ); - setData( model, 'foo' ); - downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } ); + } ); + + it( 'should get paragraph with text', () => { + setData( model, 'foo' ); expect( data.get() ).to.equal( '

foo

' ); expect( data.get( { trim: 'empty' } ) ).to.equal( '

foo

' ); } ); it( 'should trim empty paragraph by default', () => { - schema.register( 'paragraph', { inheritAllFrom: '$block' } ); setData( model, '' ); - downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } ); - expect( data.get() ).to.equal( '' ); expect( data.get( { trim: 'empty' } ) ).to.equal( '' ); } ); it( 'should get empty paragraph (with trim=none)', () => { - schema.register( 'paragraph', { inheritAllFrom: '$block' } ); setData( model, '' ); - downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } ); - expect( data.get( { trim: 'none' } ) ).to.equal( '

 

' ); } ); it( 'should get two paragraphs', () => { - schema.register( 'paragraph', { inheritAllFrom: '$block' } ); setData( model, 'foobar' ); - downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } ); - expect( data.get() ).to.equal( '

foo

bar

' ); expect( data.get( { trim: 'empty' } ) ).to.equal( '

foo

bar

' ); } ); @@ -424,20 +417,15 @@ describe( 'DataController', () => { } ); it( 'should get paragraphs without bold', () => { - schema.register( 'paragraph', { inheritAllFrom: '$block' } ); setData( model, 'foo<$text bold="true">bar' ); - downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } ); - expect( data.get() ).to.equal( '

foobar

' ); expect( data.get( { trim: 'empty' } ) ).to.equal( '

foobar

' ); } ); it( 'should get paragraphs with bold', () => { - schema.register( 'paragraph', { inheritAllFrom: '$block' } ); setData( model, 'foo<$text bold="true">bar' ); - downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } ); downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } ); expect( data.get() ).to.equal( '

foobar

' ); @@ -445,13 +433,11 @@ describe( 'DataController', () => { } ); it( 'should get root name as a parameter', () => { - schema.register( 'paragraph', { inheritAllFrom: '$block' } ); schema.extend( '$text', { allowIn: '$root' } ); setData( model, 'foo', { rootName: 'main' } ); setData( model, 'Bar', { rootName: 'title' } ); - downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } ); downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } ); expect( data.get() ).to.equal( '

foo

' ); @@ -466,8 +452,6 @@ describe( 'DataController', () => { } ); it( 'should allow to provide additional options for retrieving data - insert conversion', () => { - schema.register( 'paragraph', { inheritAllFrom: '$block' } ); - data.downcastDispatcher.on( 'insert:paragraph', ( evt, data, conversionApi ) => { conversionApi.consumable.consume( data.item, 'insert' ); @@ -487,7 +471,7 @@ describe( 'DataController', () => { } ); it( 'should allow to provide additional options for retrieving data - attribute conversion', () => { - schema.register( 'paragraph', { inheritAllFrom: '$block', allowAttributes: [ 'foo' ] } ); + schema.extend( 'paragraph', { allowAttributes: [ 'foo' ] } ); downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } ); data.downcastDispatcher.on( 'attribute:foo', ( evt, data, conversionApi ) => { @@ -509,9 +493,6 @@ describe( 'DataController', () => { } ); it( 'should allow to provide additional options for retrieving data - addMarker conversion', () => { - schema.register( 'paragraph', { inheritAllFrom: '$block' } ); - downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } ); - data.downcastDispatcher.on( 'addMarker', ( evt, data, conversionApi ) => { if ( conversionApi.options.skipMarker ) { return; @@ -540,6 +521,15 @@ describe( 'DataController', () => { expect( data.get( { skipMarker: false } ) ).to.equal( '

foo

' ); expect( data.get( { skipMarker: true } ) ).to.equal( '

foo

' ); } ); + + it( 'should pass default options value to converters', () => { + data.downcastDispatcher.on( 'insert:paragraph', ( evt, data, conversionApi ) => { + expect( conversionApi.options ).to.deep.equal( {} ); + } ); + + setData( model, 'foo' ); + data.get(); + } ); } ); describe( 'stringify()', () => { @@ -573,15 +563,20 @@ describe( 'DataController', () => { }, { priority: 'high' } ); const modelDocumentFragment = parseModel( 'foobar', schema ); - const options = { foo: 'bar' }; - data.stringify( modelDocumentFragment ); - expect( spy.lastCall.args[ 0 ] ).to.not.equal( options ); - data.stringify( modelDocumentFragment, options ); expect( spy.lastCall.args[ 0 ] ).to.equal( options ); } ); + + it( 'should pass default options value to converters', () => { + data.downcastDispatcher.on( 'insert:paragraph', ( evt, data, conversionApi ) => { + expect( conversionApi.options ).to.deep.equal( {} ); + } ); + + const modelDocumentFragment = parseModel( 'foobar', schema ); + data.stringify( modelDocumentFragment ); + } ); } ); describe( 'toView()', () => { @@ -724,6 +719,17 @@ describe( 'DataController', () => { expect( spy.firstCall.args[ 0 ] ).to.equal( options ); expect( spy.lastCall.args[ 0 ] ).to.equal( options ); } ); + + it( 'should pass default options value to converters', () => { + data.downcastDispatcher.on( 'insert:paragraph', ( evt, data, conversionApi ) => { + expect( conversionApi.options ).to.deep.equal( {} ); + } ); + + const root = model.document.getRoot(); + setData( model, 'foo' ); + + data.toView( root ); + } ); } ); describe( 'destroy()', () => {