|
|
@@ -93,6 +93,10 @@ export default class TreeWalker {
|
|
|
this._visitedParent = this.position.parent;
|
|
|
}
|
|
|
|
|
|
+ [ Symbol.iterator ]() {
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Makes a step forward in tree model. Moves the {@link #position} to the next position and returns the encountered value.
|
|
|
*
|
|
|
@@ -101,6 +105,7 @@ export default class TreeWalker {
|
|
|
* @returns {core.treeModel.TreeWalkerItem} return.value Information about taken step.
|
|
|
*/
|
|
|
next() {
|
|
|
+ const previousPosition = this.position;
|
|
|
const position = Position.createFromPosition( this.position );
|
|
|
const parent = this._visitedParent;
|
|
|
|
|
|
@@ -123,7 +128,7 @@ export default class TreeWalker {
|
|
|
|
|
|
this._visitedParent = node;
|
|
|
|
|
|
- return formatReturnValue( 'ELEMENT_START', node );
|
|
|
+ return formatReturnValue( 'ELEMENT_START', node, previousPosition, position, 1 );
|
|
|
} else if ( node instanceof CharacterProxy ) {
|
|
|
if ( this.mergeCharacters ) {
|
|
|
let charactersCount = node._nodeListText.text.length - node._index;
|
|
|
@@ -139,12 +144,12 @@ export default class TreeWalker {
|
|
|
position.offset = offset;
|
|
|
this.position = position;
|
|
|
|
|
|
- return formatReturnValue( 'TEXT', textFragment );
|
|
|
+ return formatReturnValue( 'TEXT', textFragment, previousPosition, position, charactersCount );
|
|
|
} else {
|
|
|
position.offset++;
|
|
|
this.position = position;
|
|
|
|
|
|
- return formatReturnValue( 'CHARACTER', node );
|
|
|
+ return formatReturnValue( 'CHARACTER', node, previousPosition, position, 1 );
|
|
|
}
|
|
|
} else {
|
|
|
position.path.pop();
|
|
|
@@ -153,7 +158,7 @@ export default class TreeWalker {
|
|
|
|
|
|
this._visitedParent = parent.parent;
|
|
|
|
|
|
- return formatReturnValue( 'ELEMENT_END', parent );
|
|
|
+ return formatReturnValue( 'ELEMENT_END', parent, previousPosition, position );
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -165,6 +170,7 @@ export default class TreeWalker {
|
|
|
* @returns {core.treeModel.TreeWalkerItem} return.value Information about taken step.
|
|
|
*/
|
|
|
previous() {
|
|
|
+ const previousPosition = this.position;
|
|
|
const position = Position.createFromPosition( this.position );
|
|
|
const parent = this._visitedParent;
|
|
|
|
|
|
@@ -188,7 +194,7 @@ export default class TreeWalker {
|
|
|
|
|
|
this._visitedParent = node;
|
|
|
|
|
|
- return formatReturnValue( 'ELEMENT_END', node );
|
|
|
+ return formatReturnValue( 'ELEMENT_END', node, position, previousPosition );
|
|
|
} else if ( node instanceof CharacterProxy ) {
|
|
|
if ( this.mergeCharacters ) {
|
|
|
let charactersCount = node._index + 1;
|
|
|
@@ -204,12 +210,12 @@ export default class TreeWalker {
|
|
|
position.offset = offset;
|
|
|
this.position = position;
|
|
|
|
|
|
- return formatReturnValue( 'TEXT', textFragment );
|
|
|
+ return formatReturnValue( 'TEXT', textFragment, position, previousPosition, charactersCount );
|
|
|
} else {
|
|
|
position.offset--;
|
|
|
this.position = position;
|
|
|
|
|
|
- return formatReturnValue( 'CHARACTER', node );
|
|
|
+ return formatReturnValue( 'CHARACTER', node, position, previousPosition, 1 );
|
|
|
}
|
|
|
} else {
|
|
|
position.path.pop();
|
|
|
@@ -217,17 +223,20 @@ export default class TreeWalker {
|
|
|
|
|
|
this._visitedParent = parent.parent;
|
|
|
|
|
|
- return formatReturnValue( 'ELEMENT_START', parent );
|
|
|
+ return formatReturnValue( 'ELEMENT_START', parent, position, previousPosition, 1 );
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-function formatReturnValue( type, item ) {
|
|
|
+function formatReturnValue( type, item, previousPosition, nextPosition, length ) {
|
|
|
return {
|
|
|
done: false,
|
|
|
value: {
|
|
|
type: type,
|
|
|
- item: item
|
|
|
+ item: item,
|
|
|
+ previousPosition: previousPosition,
|
|
|
+ nextPosition: nextPosition,
|
|
|
+ length: length
|
|
|
}
|
|
|
};
|
|
|
}
|
|
|
@@ -247,4 +256,7 @@ function formatReturnValue( type, item ) {
|
|
|
* @typedef {Object} core.treeModel.TreeWalkerItem
|
|
|
* @property {treeModel.TreeWalkerItemType} type
|
|
|
* @property {treeModel.Node|treeModel.TextFragment} item Value between old and new position of {@link core.treeModel.TreeWalker}.
|
|
|
+ * @property {treeModel.Position} positionBefore Position before item.
|
|
|
+ * @property {Number} [length] Length of the item. For `'ELEMENT_START'` and `'CHARACTER'` it is 1. For `'TEXT'` it is
|
|
|
+ * the length of the text. For `'ELEMENT_END'` it is undefined.
|
|
|
*/
|