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

Commit 425399b

Browse files
authored
Merge pull request #876 from ckeditor/t/875
Fix: Empty `AttributeDelta` should not be added to batch. Closes #875.
2 parents 3831a59 + 40d90a4 commit 425399b

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/model/delta/attributedelta.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,10 @@ function changeItem( batch, doc, key, value, item ) {
165165
let range, operation;
166166

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

170169
if ( previousValue != value ) {
170+
batch.addDelta( delta );
171+
171172
if ( item.is( 'rootElement' ) ) {
172173
// If we change attributes of root element, we have to use `RootAttributeOperation`.
173174
operation = new RootAttributeOperation( item, key, previousValue, value, doc.version );
@@ -195,7 +196,6 @@ function changeItem( batch, doc, key, value, item ) {
195196
// into smaller parts.
196197
function changeRange( batch, doc, attributeKey, attributeValue, range ) {
197198
const delta = new AttributeDelta();
198-
batch.addDelta( delta );
199199

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

235235
function addOperation() {
236+
// Add delta to the batch only if there is at least operation in the delta. Add delta only once.
237+
if ( delta.operations.length === 0 ) {
238+
batch.addDelta( delta );
239+
}
240+
236241
let range = new Range( lastSplitPosition, position );
237242
const operation = new AttributeOperation( range, attributeKey, attributeValueBefore, attributeValue, doc.version );
238243

tests/model/delta/attributedelta.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,20 @@ describe( 'Batch', () => {
393393
} );
394394
} );
395395
} );
396+
397+
it( 'should not add empty delta to the batch', () => {
398+
let nodeA = new Element( 'p', { a: 1 } );
399+
let nodeB = new Element( 'p', { b: 2 } );
400+
root.insertChildren( 0, [ nodeA, nodeB ] );
401+
402+
batch.setAttribute( nodeA, 'a', 1 );
403+
404+
expect( batch.deltas.length ).to.equal( 0 );
405+
406+
batch.removeAttribute( Range.createIn( root ), 'x' );
407+
408+
expect( batch.deltas.length ).to.equal( 0 );
409+
} );
396410
} );
397411

398412
describe( 'AttributeDelta', () => {

0 commit comments

Comments
 (0)