Parcourir la source

Fixed: Attributes were incorrectly set on an element's children during upcast.

Szymon Cofalik il y a 7 ans
Parent
commit
e11a9babad

+ 1 - 1
packages/ckeditor5-engine/src/conversion/upcast-converters.js

@@ -514,7 +514,7 @@ function _setAttributeOn( modelRange, modelAttribute, conversionApi ) {
 	let result = false;
 
 	// Set attribute on each item in range according to Schema.
-	for ( const node of Array.from( modelRange.getItems() ) ) {
+	for ( const node of Array.from( modelRange.getItems( { shallow: true } ) ) ) {
 		if ( conversionApi.schema.checkAttribute( node, modelAttribute.key ) ) {
 			conversionApi.writer.setAttribute( modelAttribute.key, modelAttribute.value, node );
 

+ 22 - 0
packages/ckeditor5-engine/tests/conversion/conversion.js

@@ -604,6 +604,28 @@ describe( 'Conversion', () => {
 
 				test( '<img src="foo.jpg"></img>', '<image source="foo.jpg"></image>' );
 			} );
+
+			// #1443.
+			it( 'should not set attributes on the element\'s children', () => {
+				schema.register( 'div', {
+					inheritAllFrom: '$root',
+					allowWhere: '$block',
+					isLimit: true,
+					allowAttributes: [ 'border', 'shade' ]
+				} );
+
+				conversion.elementToElement(
+					{ model: 'div', view: 'div' }
+				);
+
+				conversion.attributeToAttribute( { model: 'border', view: { key: 'class', value: 'border' } } );
+				conversion.attributeToAttribute( { model: 'shade', view: { key: 'class', value: 'shade' } } );
+
+				test(
+					'<div class="border"><div class="shade"></div></div>',
+					'<div border="border"><div shade="shade"></div></div>'
+				);
+			} );
 		} );
 
 		function test( input, expectedModel, expectedView = null ) {

+ 27 - 0
packages/ckeditor5-engine/tests/conversion/upcast-converters.js

@@ -553,6 +553,33 @@ describe( 'upcast-helpers', () => {
 				'<image styled="true"></image>'
 			);
 		} );
+
+		// #1443.
+		it( 'should not set attributes on the element\'s children', () => {
+			schema.register( 'div', {
+				inheritAllFrom: '$root',
+				allowWhere: '$block',
+				isLimit: true,
+				allowAttributes: [ 'border', 'shade' ]
+			} );
+
+			conversion.for( 'upcast' ).add( upcastElementToElement( { view: 'div', model: 'div' } ) );
+
+			const shadeHelper = upcastAttributeToAttribute( { view: { key: 'class', value: 'shade' }, model: 'shade' } );
+			const borderHelper = upcastAttributeToAttribute( { view: { key: 'class', value: 'border' }, model: 'border' } );
+
+			conversion.for( 'upcast' ).add( shadeHelper );
+			conversion.for( 'upcast' ).add( borderHelper );
+
+			expectResult(
+				new ViewContainerElement(
+					'div',
+					{ class: 'border' },
+					new ViewContainerElement( 'div', { class: 'shade' } )
+				),
+				'<div border="border"><div shade="shade"></div></div>'
+			);
+		} );
 	} );
 
 	describe( 'upcastElementToMarker', () => {