Skip to content

Commit

Permalink
Merge pull request #9295 from ckeditor/i/9293
Browse files Browse the repository at this point in the history
Fix (engine): `DataController#toView()` should have a default value for the `options` parameter. Closes #9293.
  • Loading branch information
pomek authored Mar 18, 2021
2 parents b58f7fa + 68ce484 commit f77a3d5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
6 changes: 3 additions & 3 deletions packages/ckeditor5-engine/src/controller/datacontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand All @@ -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;

Expand Down
64 changes: 35 additions & 29 deletions packages/ckeditor5-engine/tests/controller/datacontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,41 +376,34 @@ describe( 'DataController', () => {
} );

describe( 'get()', () => {
it( 'should get paragraph with text', () => {
beforeEach( () => {
schema.register( 'paragraph', { inheritAllFrom: '$block' } );
setData( model, '<paragraph>foo</paragraph>' );

downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
} );

it( 'should get paragraph with text', () => {
setData( model, '<paragraph>foo</paragraph>' );

expect( data.get() ).to.equal( '<p>foo</p>' );
expect( data.get( { trim: 'empty' } ) ).to.equal( '<p>foo</p>' );
} );

it( 'should trim empty paragraph by default', () => {
schema.register( 'paragraph', { inheritAllFrom: '$block' } );
setData( model, '<paragraph></paragraph>' );

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, '<paragraph></paragraph>' );

downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );

expect( data.get( { trim: 'none' } ) ).to.equal( '<p>&nbsp;</p>' );
} );

it( 'should get two paragraphs', () => {
schema.register( 'paragraph', { inheritAllFrom: '$block' } );
setData( model, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );

downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );

expect( data.get() ).to.equal( '<p>foo</p><p>bar</p>' );
expect( data.get( { trim: 'empty' } ) ).to.equal( '<p>foo</p><p>bar</p>' );
} );
Expand All @@ -424,34 +417,27 @@ describe( 'DataController', () => {
} );

it( 'should get paragraphs without bold', () => {
schema.register( 'paragraph', { inheritAllFrom: '$block' } );
setData( model, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );

downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );

expect( data.get() ).to.equal( '<p>foobar</p>' );
expect( data.get( { trim: 'empty' } ) ).to.equal( '<p>foobar</p>' );
} );

it( 'should get paragraphs with bold', () => {
schema.register( 'paragraph', { inheritAllFrom: '$block' } );
setData( model, '<paragraph>foo<$text bold="true">bar</$text></paragraph>' );

downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } );

expect( data.get() ).to.equal( '<p>foo<strong>bar</strong></p>' );
expect( data.get( { trim: 'empty' } ) ).to.equal( '<p>foo<strong>bar</strong></p>' );
} );

it( 'should get root name as a parameter', () => {
schema.register( 'paragraph', { inheritAllFrom: '$block' } );
schema.extend( '$text', { allowIn: '$root' } );

setData( model, '<paragraph>foo</paragraph>', { rootName: 'main' } );
setData( model, 'Bar', { rootName: 'title' } );

downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
downcastHelpers.attributeToElement( { model: 'bold', view: 'strong' } );

expect( data.get() ).to.equal( '<p>foo</p>' );
Expand All @@ -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' );

Expand All @@ -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 ) => {
Expand All @@ -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;
Expand Down Expand Up @@ -540,6 +521,15 @@ describe( 'DataController', () => {
expect( data.get( { skipMarker: false } ) ).to.equal( '<p>f<marker>o</marker>o</p>' );
expect( data.get( { skipMarker: true } ) ).to.equal( '<p>foo</p>' );
} );

it( 'should pass default options value to converters', () => {
data.downcastDispatcher.on( 'insert:paragraph', ( evt, data, conversionApi ) => {
expect( conversionApi.options ).to.deep.equal( {} );
} );

setData( model, '<paragraph>foo</paragraph>' );
data.get();
} );
} );

describe( 'stringify()', () => {
Expand Down Expand Up @@ -573,15 +563,20 @@ describe( 'DataController', () => {
}, { priority: 'high' } );

const modelDocumentFragment = parseModel( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', 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( '<paragraph>foo</paragraph><paragraph>bar</paragraph>', schema );
data.stringify( modelDocumentFragment );
} );
} );

describe( 'toView()', () => {
Expand Down Expand Up @@ -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, '<paragraph>foo</paragraph>' );

data.toView( root );
} );
} );

describe( 'destroy()', () => {
Expand Down

0 comments on commit f77a3d5

Please sign in to comment.