Jelajahi Sumber

Changed: `view.TreeWalker` should always return `view.TextProxy`.

Szymon Cofalik 8 tahun lalu
induk
melakukan
f86c3b57b8

+ 1 - 1
packages/ckeditor5-engine/src/view/domconverter.js

@@ -1036,7 +1036,7 @@ export default class DomConverter {
 				// ViewContainerElement is found on a way to next ViewText node, so given `node` was first/last
 				// ViewContainerElement is found on a way to next ViewText node, so given `node` was first/last
 				// text node in its container element.
 				// text node in its container element.
 				return null;
 				return null;
-			} else if ( value.item.is( 'text' ) ) {
+			} else if ( value.item.is( 'textProxy' ) ) {
 				// Found a text node in the same container element.
 				// Found a text node in the same container element.
 				return value.item;
 				return value.item;
 			}
 			}

+ 4 - 2
packages/ckeditor5-engine/src/view/treewalker.js

@@ -236,7 +236,7 @@ export default class TreeWalker {
 				return this._next();
 				return this._next();
 			} else {
 			} else {
 				let charactersCount = node.data.length;
 				let charactersCount = node.data.length;
-				let item = node;
+				let item;
 
 
 				// If text stick out of walker range, we need to cut it and wrap by TextProxy.
 				// If text stick out of walker range, we need to cut it and wrap by TextProxy.
 				if ( node == this._boundaryEndParent ) {
 				if ( node == this._boundaryEndParent ) {
@@ -244,6 +244,7 @@ export default class TreeWalker {
 					item = new TextProxy( node, 0, charactersCount );
 					item = new TextProxy( node, 0, charactersCount );
 					position = Position.createAfter( item );
 					position = Position.createAfter( item );
 				} else {
 				} else {
+					item = new TextProxy( node, 0, node.data.length );
 					// If not just keep moving forward.
 					// If not just keep moving forward.
 					position.offset++;
 					position.offset++;
 				}
 				}
@@ -347,7 +348,7 @@ export default class TreeWalker {
 				return this._previous();
 				return this._previous();
 			} else {
 			} else {
 				let charactersCount = node.data.length;
 				let charactersCount = node.data.length;
-				let item = node;
+				let item;
 
 
 				// If text stick out of walker range, we need to cut it and wrap by TextProxy.
 				// If text stick out of walker range, we need to cut it and wrap by TextProxy.
 				if ( node == this._boundaryStartParent ) {
 				if ( node == this._boundaryStartParent ) {
@@ -357,6 +358,7 @@ export default class TreeWalker {
 					charactersCount = item.data.length;
 					charactersCount = item.data.length;
 					position = Position.createBefore( item );
 					position = Position.createBefore( item );
 				} else {
 				} else {
+					item = new TextProxy( node, 0, node.data.length );
 					// If not just keep moving backward.
 					// If not just keep moving backward.
 					position.offset--;
 					position.offset--;
 				}
 				}

+ 45 - 6
packages/ckeditor5-engine/tests/view/treewalker.js

@@ -1005,14 +1005,53 @@ describe( 'TreeWalker', () => {
 		const b = new AttributeElement( 'b', null, bar );
 		const b = new AttributeElement( 'b', null, bar );
 		const docFrag = new DocumentFragment( [ p, b ] );
 		const docFrag = new DocumentFragment( [ p, b ] );
 
 
-		const iterator = new TreeWalker( {
-			startPosition: new Position( docFrag, 0 ),
-			ignoreElementEnd: true
-		} );
+		const expected = [
+			{
+				type: 'elementStart',
+				item: p,
+				previousPosition: new Position( docFrag, 0 ),
+				nextPosition: new Position( p, 0 )
+			},
+			{
+				type: 'text',
+				text: 'foo',
+				previousPosition: new Position( p, 0 ),
+				nextPosition: new Position( p, 1 )
+			},
+			{
+				type: 'elementEnd',
+				item: p,
+				previousPosition: new Position( p, 1 ),
+				nextPosition: new Position( docFrag, 1 )
+			},
+			{
+				type: 'elementStart',
+				item: b,
+				previousPosition: new Position( docFrag, 1 ),
+				nextPosition: new Position( b, 0 )
+			},
+			{
+				type: 'text',
+				text: 'bar',
+				previousPosition: new Position( b, 0 ),
+				nextPosition: new Position( b, 1 )
+			},
+			{
+				type: 'elementEnd',
+				item: b,
+				previousPosition: new Position( b, 1 ),
+				nextPosition: new Position( docFrag, 2 )
+			}
+		];
+
+		const iterator = new TreeWalker( { boundaries: Range.createIn( docFrag ) } );
+		let i = 0;
 
 
-		const nodes = Array.from( iterator ).map( step => step.item );
+		for ( const value of iterator ) {
+			expectValue( value, expected[ i++ ] );
+		}
 
 
-		expect( nodes ).to.deep.equal( [ p, foo, b, bar ] );
+		expect( i ).to.equal( expected.length );
 	} );
 	} );
 
 
 	describe( 'skip', () => {
 	describe( 'skip', () => {