Browse Source

Other: Detach `LiveRange` used to apply inline auto format.

Maciej Gołaszewski 8 years ago
parent
commit
74ff040461

+ 3 - 0
packages/ckeditor5-autoformat/src/inlineautoformatengine.js

@@ -195,6 +195,9 @@ export default class InlineAutoformatEngine {
 				// Apply format.
 				formatCallback( fixBatch, validRanges );
 
+				// Detach ranges used to apply Autoformat. Prevents memory leaks. #39
+				rangesToFormat.forEach( range => range.detach() );
+
 				// Remove delimiters.
 				for ( const range of rangesToRemove ) {
 					fixBatch.remove( range );

+ 5 - 2
packages/ckeditor5-autoformat/tests/inlineautoformatengine.js

@@ -141,6 +141,9 @@ describe( 'InlineAutoformatEngine', () => {
 		it( 'should detach removed ranges', () => {
 			const detachSpies = [];
 			const callback = fixBatch => testUtils.sinon.stub( fixBatch, 'remove' ).callsFake( saveDetachSpy );
+			testUtils.sinon.stub( editor.document.schema, 'getValidRanges' )
+				.callThrough()
+				.callsFake( ranges => ranges.map( saveDetachSpy ) );
 
 			new InlineAutoformatEngine( editor, /(\*)(.+?)(\*)/g, callback ); // eslint-disable-line no-new
 
@@ -150,8 +153,8 @@ describe( 'InlineAutoformatEngine', () => {
 				doc.batch().insert( doc.selection.getFirstPosition(), '*' );
 			} );
 
-			// There should be two ranges removed.
-			expect( detachSpies ).to.have.length( 2 );
+			// There should be two removed ranges and one range used to apply autoformat.
+			expect( detachSpies ).to.have.length( 3 );
 
 			for ( const spy of detachSpies ) {
 				testUtils.sinon.assert.calledOnce( spy );