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 #1372 from ckeditor/t/1369
Browse files Browse the repository at this point in the history
Fixed: conversion.downcast-converters.changeAttribute should not consume if element creators returned null. Closes #1369.
  • Loading branch information
jodator committed Mar 21, 2018
2 parents 4c38683 + 849cef9 commit 6866256
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/conversion/downcast-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,13 @@ export function changeAttribute( attributeCreator ) {
attributeCreator = attributeCreator || ( ( value, data ) => ( { value, key: data.attributeKey } ) );

return ( evt, data, conversionApi ) => {
const oldAttribute = attributeCreator( data.attributeOldValue, data );
const newAttribute = attributeCreator( data.attributeNewValue, data );

if ( !oldAttribute && !newAttribute ) {
return;
}

if ( !conversionApi.consumable.consume( data.item, evt.name ) ) {
return;
}
Expand All @@ -662,8 +669,6 @@ export function changeAttribute( attributeCreator ) {
const viewWriter = conversionApi.writer;

// First remove the old attribute if there was one.
const oldAttribute = attributeCreator( data.attributeOldValue, data );

if ( data.attributeOldValue !== null && oldAttribute ) {
if ( oldAttribute.key == 'class' ) {
const classes = Array.isArray( oldAttribute.value ) ? oldAttribute.value : [ oldAttribute.value ];
Expand All @@ -682,9 +687,7 @@ export function changeAttribute( attributeCreator ) {
}
}

// Then, if conversion was successful, set the new attribute.
const newAttribute = attributeCreator( data.attributeNewValue, data );

// Then set the new attribute.
if ( data.attributeNewValue !== null && newAttribute ) {
if ( newAttribute.key == 'class' ) {
const classes = Array.isArray( newAttribute.value ) ? newAttribute.value : [ newAttribute.value ];
Expand Down
18 changes: 17 additions & 1 deletion tests/conversion/downcast-converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ describe( 'downcast-converters', () => {
} );
} );

describe( 'setAttribute', () => {
describe( 'changeAttribute', () => {
it( 'should convert attribute insert/change/remove on a model node', () => {
const modelElement = new ModelElement( 'paragraph', { class: 'foo' }, new ModelText( 'foobar' ) );

Expand Down Expand Up @@ -717,6 +717,22 @@ describe( 'downcast-converters', () => {
// No attribute set.
expect( viewToString( viewRoot ) ).to.equal( '<div><p>foobar</p></div>' );
} );

it( 'should not convert or consume if element creator returned null', () => {
const callback = sinon.stub().returns( null );

dispatcher.on( 'attribute:class', changeAttribute( callback ) );

const modelElement = new ModelElement( 'paragraph', { class: 'foo' }, new ModelText( 'foobar' ) );

model.change( writer => {
writer.insert( modelElement, modelRootStart );
} );

expect( viewToString( viewRoot ) ).to.equal( '<div><p class="foo">foobar</p></div>' );

sinon.assert.called( callback );
} );
} );

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

0 comments on commit 6866256

Please sign in to comment.