Sfoglia il codice sorgente

Made getData options flat.

Oskar Wróbel 5 anni fa
parent
commit
5b8a7b19ce

+ 15 - 12
packages/ckeditor5-engine/src/controller/datacontroller.js

@@ -146,16 +146,19 @@ export default class DataController {
 	 * Returns the model's data converted by downcast dispatchers attached to {@link #downcastDispatcher} and
 	 * formatted by the {@link #processor data processor}.
 	 *
-	 * @param {Object} [options]
+	 * @param {Object} [options] Additional configuration for the retrieved data. `DataController` provides two optional
+	 * properties: `root` and `trim`. Other properties of this object are specified by various editor features.
 	 * @param {String} [options.rootName='main'] Root name.
 	 * @param {String} [options.trim='empty'] Whether returned data should be trimmed. This option is set to `empty` by default,
 	 * which means whenever editor content is considered empty, an empty string will be returned. To turn off trimming completely
 	 * use `'none'`. In such cases exact content will be returned (for example `<p>&nbsp;</p>` for an empty editor).
-	 * @param {Object} [options.conversionOptions] Additional, custom configuration passed to the conversion process.
 	 * @returns {String} Output data.
 	 */
-	get( options ) {
-		const { rootName = 'main', trim = 'empty', conversionOptions } = options || {};
+	get( options = {} ) {
+		const { rootName = 'main', trim = 'empty' } = options;
+
+		delete options.rootName;
+		delete options.trim;
 
 		if ( !this._checkIfRootsExists( [ rootName ] ) ) {
 			/**
@@ -178,7 +181,7 @@ export default class DataController {
 			return '';
 		}
 
-		return this.stringify( root, conversionOptions );
+		return this.stringify( root, options );
 	}
 
 	/**
@@ -188,12 +191,12 @@ export default class DataController {
 	 *
 	 * @param {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment} modelElementOrFragment
 	 * Element whose content will be stringified.
-	 * @param {Object} [conversionOptions] Additional, custom configuration passed to the conversion process.
+	 * @param {Object} [options] Additional configuration passed to the conversion process.
 	 * @returns {String} Output data.
 	 */
-	stringify( modelElementOrFragment, conversionOptions ) {
+	stringify( modelElementOrFragment, options ) {
 		// Model -> view.
-		const viewDocumentFragment = this.toView( modelElementOrFragment, conversionOptions );
+		const viewDocumentFragment = this.toView( modelElementOrFragment, options );
 
 		// View -> data.
 		return this.processor.toData( viewDocumentFragment );
@@ -207,10 +210,10 @@ 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} [conversionOptions] Additional, custom configuration passed to the conversion process.
+	 * @param {Object} [options] Additional configuration passed to the conversion process.
 	 * @returns {module:engine/view/documentfragment~DocumentFragment} Output view DocumentFragment.
 	 */
-	toView( modelElementOrFragment, conversionOptions ) {
+	toView( modelElementOrFragment, options ) {
 		const viewDocument = this.viewDocument;
 		const viewWriter = this._viewWriter;
 
@@ -224,7 +227,7 @@ export default class DataController {
 		this.mapper.bindElements( modelElementOrFragment, viewDocumentFragment );
 
 		// We have no view controller and rendering to DOM in DataController so view.change() block is not used here.
-		this.downcastDispatcher.convertInsert( modelRange, viewWriter, conversionOptions );
+		this.downcastDispatcher.convertInsert( modelRange, viewWriter, options );
 
 		if ( !modelElementOrFragment.is( 'documentFragment' ) ) {
 			// Then, if a document element is converted, convert markers.
@@ -232,7 +235,7 @@ export default class DataController {
 			const markers = _getMarkersRelativeToElement( modelElementOrFragment );
 
 			for ( const [ name, range ] of markers ) {
-				this.downcastDispatcher.convertMarkerAdd( name, range, viewWriter, conversionOptions );
+				this.downcastDispatcher.convertMarkerAdd( name, range, viewWriter, options );
 			}
 		}
 

+ 26 - 9
packages/ckeditor5-engine/tests/controller/datacontroller.js

@@ -455,7 +455,7 @@ describe( 'DataController', () => {
 			}, /datacontroller-get-non-existent-root:/ );
 		} );
 
-		it( 'should allow to provide additional conversion options - insert conversion', () => {
+		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 ) => {
@@ -472,11 +472,11 @@ describe( 'DataController', () => {
 
 			setData( model, '<paragraph>foo</paragraph>' );
 
-			expect( data.get( { conversionOptions: { attributeValue: 'foo' } } ) ).to.equal( '<p attribute="foo">foo</p>' );
-			expect( data.get( { conversionOptions: { attributeValue: 'bar' } } ) ).to.equal( '<p attribute="bar">foo</p>' );
+			expect( data.get( { attributeValue: 'foo' } ) ).to.equal( '<p attribute="foo">foo</p>' );
+			expect( data.get( { attributeValue: 'bar' } ) ).to.equal( '<p attribute="bar">foo</p>' );
 		} );
 
-		it( 'should allow to provide additional conversion options - attribute conversion', () => {
+		it( 'should allow to provide additional options for retrieving data - attribute conversion', () => {
 			schema.register( 'paragraph', { inheritAllFrom: '$block', allowAttributes: [ 'foo' ] } );
 			downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
 
@@ -494,11 +494,11 @@ describe( 'DataController', () => {
 			setData( model, '<paragraph>f<$text foo="a">o</$text>ob<$text foo="b">a</$text>r</paragraph>' );
 
 			expect( data.get() ).to.equal( '<p>f<a>o</a>ob<b>a</b>r</p>' );
-			expect( data.get( { conversionOptions: { skipAttribute: 'a' } } ) ).to.equal( '<p>foob<b>a</b>r</p>' );
-			expect( data.get( { conversionOptions: { skipAttribute: 'b' } } ) ).to.equal( '<p>f<a>o</a>obar</p>' );
+			expect( data.get( { skipAttribute: 'a' } ) ).to.equal( '<p>foob<b>a</b>r</p>' );
+			expect( data.get( { skipAttribute: 'b' } ) ).to.equal( '<p>f<a>o</a>obar</p>' );
 		} );
 
-		it( 'should allow to provide additional conversion options - addMarker conversion', () => {
+		it( 'should allow to provide additional options for retrieving data - addMarker conversion', () => {
 			schema.register( 'paragraph', { inheritAllFrom: '$block' } );
 			downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
 
@@ -527,8 +527,25 @@ describe( 'DataController', () => {
 				} );
 			} );
 
-			expect( data.get( { conversionOptions: { skipMarker: false } } ) ).to.equal( '<p>f<marker>o</marker>o</p>' );
-			expect( data.get( { conversionOptions: { skipMarker: true } } ) ).to.equal( '<p>foo</p>' );
+			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 provide additional options for retrieving data without basic rootName and trim properties', () => {
+			downcastHelpers.elementToElement( { model: 'paragraph', view: 'p' } );
+			schema.register( 'paragraph', { inheritAllFrom: '$block' } );
+
+			const spy = sinon.spy();
+
+			data.downcastDispatcher.on( 'insert:paragraph', ( evt, data, conversionApi ) => {
+				spy( conversionApi.options );
+			}, { priority: 'high' } );
+
+			setData( model, '<paragraph>foo</paragraph>' );
+
+			data.get( { rootName: 'main', trim: 'empty', foo: 'bar' } );
+
+			expect( spy.lastCall.args[ 0 ] ).to.deep.equal( { foo: 'bar' } );
 		} );
 	} );