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 #876 from ckeditor/t/875
Browse files Browse the repository at this point in the history
Fix: Empty `AttributeDelta` should not be added to batch. Closes #875.
  • Loading branch information
Reinmar authored Mar 17, 2017
2 parents 3831a59 + 40d90a4 commit 425399b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/model/delta/attributedelta.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,10 @@ function changeItem( batch, doc, key, value, item ) {
let range, operation;

const delta = item.is( 'rootElement' ) ? new RootAttributeDelta() : new AttributeDelta();
batch.addDelta( delta );

if ( previousValue != value ) {
batch.addDelta( delta );

if ( item.is( 'rootElement' ) ) {
// If we change attributes of root element, we have to use `RootAttributeOperation`.
operation = new RootAttributeOperation( item, key, previousValue, value, doc.version );
Expand Down Expand Up @@ -195,7 +196,6 @@ function changeItem( batch, doc, key, value, item ) {
// into smaller parts.
function changeRange( batch, doc, attributeKey, attributeValue, range ) {
const delta = new AttributeDelta();
batch.addDelta( delta );

// Position of the last split, the beginning of the new range.
let lastSplitPosition = range.start;
Expand Down Expand Up @@ -233,6 +233,11 @@ function changeRange( batch, doc, attributeKey, attributeValue, range ) {
}

function addOperation() {
// Add delta to the batch only if there is at least operation in the delta. Add delta only once.
if ( delta.operations.length === 0 ) {
batch.addDelta( delta );
}

let range = new Range( lastSplitPosition, position );
const operation = new AttributeOperation( range, attributeKey, attributeValueBefore, attributeValue, doc.version );

Expand Down
14 changes: 14 additions & 0 deletions tests/model/delta/attributedelta.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,20 @@ describe( 'Batch', () => {
} );
} );
} );

it( 'should not add empty delta to the batch', () => {
let nodeA = new Element( 'p', { a: 1 } );
let nodeB = new Element( 'p', { b: 2 } );
root.insertChildren( 0, [ nodeA, nodeB ] );

batch.setAttribute( nodeA, 'a', 1 );

expect( batch.deltas.length ).to.equal( 0 );

batch.removeAttribute( Range.createIn( root ), 'x' );

expect( batch.deltas.length ).to.equal( 0 );
} );
} );

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

0 comments on commit 425399b

Please sign in to comment.