瀏覽代碼

Consider only text insertions when incrementing buffer size.

Krzysztof Krztoń 8 年之前
父節點
當前提交
c5b1ca92fa
共有 2 個文件被更改,包括 12 次插入54 次删除
  1. 7 32
      packages/ckeditor5-typing/src/inputcommand.js
  2. 5 22
      packages/ckeditor5-typing/tests/inputcommand.js

+ 7 - 32
packages/ckeditor5-typing/src/inputcommand.js

@@ -8,7 +8,6 @@
  */
 
 import Command from '@ckeditor/ckeditor5-core/src/command/command';
-import TreeWalker from '@ckeditor/ckeditor5-engine/src/model/treewalker';
 import ChangeBuffer from './changebuffer';
 
 /**
@@ -56,9 +55,9 @@ export default class InputCommand extends Command {
 	}
 
 	/**
-	 * Executes the input command. It replaces the content within the given range with given text. Replacing is a two
-	 * step process, first content within range is removed and then new text is inserted on the beginning
-	 * of the range (which after removal is a collapsed range).
+	 * Executes the input command. It replaces the content within the given range with the given text.
+	 * Replacing is a two step process, first content within the range is removed and then new text is inserted
+	 * on the beginning of the range (which after removal is a collapsed range).
 	 *
 	 * @param {Object} [options] The command options.
 	 * @param {String} [options.text=''] Text to be inserted.
@@ -72,19 +71,18 @@ export default class InputCommand extends Command {
 		const range = options.range || doc.selection.getFirstRange();
 		const text = options.text || '';
 		const selectionAnchor = options.selectionAnchor;
-		let textLength = 0;
+		let textInsertions = 0;
 
 		if ( range ) {
 			doc.enqueueChanges( () => {
 				const isCollapsedRange = range.isCollapsed;
 
 				if ( !isCollapsedRange ) {
-					textLength -= this._getTextWithinRange( range ).length;
 					this._buffer.batch.remove( range );
 				}
 
 				if ( text ) {
-					textLength += text.length;
+					textInsertions = text.length;
 					this._buffer.batch.weakInsert( range.start, text );
 				}
 
@@ -92,34 +90,11 @@ export default class InputCommand extends Command {
 					this.editor.data.model.selection.collapse( selectionAnchor );
 				} else if ( isCollapsedRange ) {
 					// If range was collapsed just shift the selection by the number of inserted characters.
-					this.editor.data.model.selection.collapse( range.start.getShiftedBy( textLength ) );
+					this.editor.data.model.selection.collapse( range.start.getShiftedBy( textInsertions ) );
 				}
 
-				this._buffer.input( Math.max( textLength, 0 ) );
+				this._buffer.input( textInsertions );
 			} );
 		}
 	}
-
-	/**
-	 * Returns text within given range.
-	 *
-	 * @protected
-	 * @param {module:engine/model/range~Range} range Range from which text will be extracted.
-	 * @returns {String} Text within the given range.
-	 */
-	_getTextWithinRange( range ) {
-		let txt = '';
-		const treeWalker = new TreeWalker( { boundaries: range } );
-
-		for ( let value of treeWalker ) {
-			let node = value.item;
-			let nodeText = node.data;
-
-			if ( nodeText ) {
-				txt += nodeText;
-			}
-		}
-
-		return txt;
-	}
 }

+ 5 - 22
packages/ckeditor5-typing/tests/inputcommand.js

@@ -88,7 +88,7 @@ describe( 'InputCommand', () => {
 			} );
 
 			expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>rab[]r</p>' );
-			expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 0 );
+			expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 3 );
 		} );
 
 		it( 'replaces text for range within single element in the middle', () => {
@@ -100,7 +100,7 @@ describe( 'InputCommand', () => {
 			} );
 
 			expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>fobazz[]r</p>' );
-			expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 1 );
+			expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 4 );
 		} );
 
 		it( 'replaces text for range within single element on the end', () => {
@@ -112,7 +112,7 @@ describe( 'InputCommand', () => {
 			} );
 
 			expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>foobazzz[]</p>' );
-			expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 2 );
+			expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 3 );
 		} );
 
 		it( 'replaces text for range within multiple elements', () => {
@@ -124,7 +124,7 @@ describe( 'InputCommand', () => {
 			} );
 
 			expect( getData( doc, { selection: true } ) ).to.be.equal( '<h1>Funny c[</h1><p>]ar</p>' );
-			expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 3 );
+			expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 6 );
 		} );
 
 		it( 'uses current selection when range is not given', () => {
@@ -135,7 +135,7 @@ describe( 'InputCommand', () => {
 			} );
 
 			expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>foobaz[]</p>' );
-			expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 0 );
+			expect( editor.commands.get( 'input' ).buffer.size ).to.be.equal( 2 );
 		} );
 
 		it( 'only removes content when text is not given', () => {
@@ -167,23 +167,6 @@ describe( 'InputCommand', () => {
 		} );
 	} );
 
-	describe( '_getTextWithinRange', () => {
-		it( 'returns empty text for collapsed selection', () => {
-			setData( doc, '<p>[]foo</p>' );
-			expect( editor.commands.get( 'input' )._getTextWithinRange( editor.document.selection.getFirstRange() ) ).to.be.empty;
-		} );
-
-		it( 'returns valid text for selection within single element', () => {
-			setData( doc, '<p>[fooBA]R</p>' );
-			expect( editor.commands.get( 'input' )._getTextWithinRange( editor.document.selection.getFirstRange() ) ).to.be.equal( 'fooBA' );
-		} );
-
-		it( 'returns valid text for selection within mulitple elements', () => {
-			setData( doc, '<h1>fo[o</h1><p>BARb]az</p>' );
-			expect( editor.commands.get( 'input' )._getTextWithinRange( editor.document.selection.getFirstRange() ) ).to.be.equal( 'oBARb' );
-		} );
-	} );
-
 	describe( 'destroy', () => {
 		it( 'should destroy change buffer', () => {
 			const command = editor.commands.get( 'input' );