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

Commit dfa0b39

Browse files
authored
Merge pull request #1444 from ckeditor/t/1443
Fix: Attributes were incorrectly set on an element's children during upcast. Closes #1443.
2 parents ec70448 + d224f33 commit dfa0b39

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

src/conversion/upcast-converters.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ function _setAttributeOn( modelRange, modelAttribute, conversionApi ) {
514514
let result = false;
515515

516516
// Set attribute on each item in range according to Schema.
517-
for ( const node of Array.from( modelRange.getItems() ) ) {
517+
for ( const node of Array.from( modelRange.getItems( { shallow: true } ) ) ) {
518518
if ( conversionApi.schema.checkAttribute( node, modelAttribute.key ) ) {
519519
conversionApi.writer.setAttribute( modelAttribute.key, modelAttribute.value, node );
520520

tests/conversion/conversion.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,28 @@ describe( 'Conversion', () => {
604604

605605
test( '<img src="foo.jpg"></img>', '<image source="foo.jpg"></image>' );
606606
} );
607+
608+
// #1443.
609+
it( 'should not set attributes on the element\'s children', () => {
610+
schema.register( 'div', {
611+
inheritAllFrom: '$root',
612+
allowWhere: '$block',
613+
isLimit: true,
614+
allowAttributes: [ 'border', 'shade' ]
615+
} );
616+
617+
conversion.elementToElement(
618+
{ model: 'div', view: 'div' }
619+
);
620+
621+
conversion.attributeToAttribute( { model: 'border', view: { key: 'class', value: 'border' } } );
622+
conversion.attributeToAttribute( { model: 'shade', view: { key: 'class', value: 'shade' } } );
623+
624+
test(
625+
'<div class="border"><div class="shade"></div></div>',
626+
'<div border="border"><div shade="shade"></div></div>'
627+
);
628+
} );
607629
} );
608630

609631
function test( input, expectedModel, expectedView = null ) {

tests/conversion/upcast-converters.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,33 @@ describe( 'upcast-helpers', () => {
553553
'<image styled="true"></image>'
554554
);
555555
} );
556+
557+
// #1443.
558+
it( 'should not set attributes on the element\'s children', () => {
559+
schema.register( 'div', {
560+
inheritAllFrom: '$root',
561+
allowWhere: '$block',
562+
isLimit: true,
563+
allowAttributes: [ 'border', 'shade' ]
564+
} );
565+
566+
conversion.for( 'upcast' ).add( upcastElementToElement( { view: 'div', model: 'div' } ) );
567+
568+
const shadeHelper = upcastAttributeToAttribute( { view: { key: 'class', value: 'shade' }, model: 'shade' } );
569+
const borderHelper = upcastAttributeToAttribute( { view: { key: 'class', value: 'border' }, model: 'border' } );
570+
571+
conversion.for( 'upcast' ).add( shadeHelper );
572+
conversion.for( 'upcast' ).add( borderHelper );
573+
574+
expectResult(
575+
new ViewContainerElement(
576+
'div',
577+
{ class: 'border' },
578+
new ViewContainerElement( 'div', { class: 'shade' } )
579+
),
580+
'<div border="border"><div shade="shade"></div></div>'
581+
);
582+
} );
556583
} );
557584

558585
describe( 'upcastElementToMarker', () => {

0 commit comments

Comments
 (0)