Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1444 from ckeditor/t/1443
Browse files Browse the repository at this point in the history
Fix: Attributes were incorrectly set on an element's children during upcast. Closes #1443.
  • Loading branch information
Reinmar authored Jun 25, 2018
2 parents ec70448 + d224f33 commit dfa0b39
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/conversion/upcast-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
22 changes: 22 additions & 0 deletions tests/conversion/conversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
27 changes: 27 additions & 0 deletions tests/conversion/upcast-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit dfa0b39

Please sign in to comment.