浏览代码

Merge pull request #40 from ckeditor/t/39

Fix: LiveRanges used by InlineAutoFormatEngine are now properly detached. Closes #39.
Szymon Kupś 8 年之前
父节点
当前提交
9e930f9800

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

@@ -195,9 +195,16 @@ 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 );
+
+					// Prevents memory leaks.
+					// https://github.com/ckeditor/ckeditor5-autoformat/issues/39
+					range.detach();
 				}
 			} );
 		} );

+ 27 - 0
packages/ckeditor5-autoformat/tests/inlineautoformatengine.js

@@ -137,5 +137,32 @@ describe( 'InlineAutoformatEngine', () => {
 
 			sinon.assert.notCalled( formatSpy );
 		} );
+
+		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
+
+			setData( doc, '<paragraph>*foobar[]</paragraph>' );
+
+			doc.enqueueChanges( () => {
+				doc.batch().insert( doc.selection.getFirstPosition(), '*' );
+			} );
+
+			// 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 );
+			}
+
+			function saveDetachSpy( range ) {
+				detachSpies.push( testUtils.sinon.spy( range, 'detach' ) );
+			}
+		} );
 	} );
 } );