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

Changed: empty AttributeDeltas should not be added to batch. Fixes #875. #876

Merged
merged 1 commit into from
Mar 17, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/model/delta/attributedelta.js
Original file line number Diff line number Diff line change
@@ -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 );
@@ -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;
@@ -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 );

14 changes: 14 additions & 0 deletions tests/model/delta/attributedelta.js
Original file line number Diff line number Diff line change
@@ -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', () => {