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

Commit 5f24ae8

Browse files
authored
Merge pull request #40 from ckeditor/t/39
Fix: LiveRanges used by InlineAutoFormatEngine are now properly detached. Closes #39.
2 parents dcb1c8f + bd68e72 commit 5f24ae8

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/inlineautoformatengine.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,16 @@ export default class InlineAutoformatEngine {
195195
// Apply format.
196196
formatCallback( fixBatch, validRanges );
197197

198+
// Detach ranges used to apply Autoformat. Prevents memory leaks. #39
199+
rangesToFormat.forEach( range => range.detach() );
200+
198201
// Remove delimiters.
199202
for ( const range of rangesToRemove ) {
200203
fixBatch.remove( range );
204+
205+
// Prevents memory leaks.
206+
// https://github.com/ckeditor/ckeditor5-autoformat/issues/39
207+
range.detach();
201208
}
202209
} );
203210
} );

tests/inlineautoformatengine.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,32 @@ describe( 'InlineAutoformatEngine', () => {
137137

138138
sinon.assert.notCalled( formatSpy );
139139
} );
140+
141+
it( 'should detach removed ranges', () => {
142+
const detachSpies = [];
143+
const callback = fixBatch => testUtils.sinon.stub( fixBatch, 'remove' ).callsFake( saveDetachSpy );
144+
testUtils.sinon.stub( editor.document.schema, 'getValidRanges' )
145+
.callThrough()
146+
.callsFake( ranges => ranges.map( saveDetachSpy ) );
147+
148+
new InlineAutoformatEngine( editor, /(\*)(.+?)(\*)/g, callback ); // eslint-disable-line no-new
149+
150+
setData( doc, '<paragraph>*foobar[]</paragraph>' );
151+
152+
doc.enqueueChanges( () => {
153+
doc.batch().insert( doc.selection.getFirstPosition(), '*' );
154+
} );
155+
156+
// There should be two removed ranges and one range used to apply autoformat.
157+
expect( detachSpies ).to.have.length( 3 );
158+
159+
for ( const spy of detachSpies ) {
160+
testUtils.sinon.assert.calledOnce( spy );
161+
}
162+
163+
function saveDetachSpy( range ) {
164+
detachSpies.push( testUtils.sinon.spy( range, 'detach' ) );
165+
}
166+
} );
140167
} );
141168
} );

0 commit comments

Comments
 (0)