2012-07-19 00:11:26 +00:00
|
|
|
/**
|
|
|
|
* VisualEditor BranchNode class.
|
2012-07-19 21:25:16 +00:00
|
|
|
*
|
2012-07-19 00:11:26 +00:00
|
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2011-11-03 21:48:40 +00:00
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Mixin for branch nodes.
|
Object management: Object create/inherit/clone utilities
* For the most common case:
- replace ve.extendClass with ve.inheritClass (chose slightly
different names to detect usage of the old/new one, and I
like 'inherit' better).
- move it up to below the constructor, see doc block for why.
* Cases where more than 2 arguments were passed to
ve.extendClass are handled differently depending on the case.
In case of a longer inheritance tree, the other arguments
could be omitted (like in "ve.ce.FooBar, ve.FooBar,
ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar,
because ve.ce.FooBar inherits from ve.Bar).
In the case of where it previously had two mixins with
ve.extendClass(), either one becomes inheritClass and one
a mixin, both to mixinClass().
No visible changes should come from this commit as the
instances still all have the same visible properties in the
end. No more or less than before.
* Misc.:
- Be consistent in calling parent constructors in the
same order as the inheritance.
- Add missing @extends and @param documentation.
- Replace invalid {Integer} type hint with {Number}.
- Consistent doc comments order:
@class, @abstract, @constructor, @extends, @params.
- Fix indentation errors
A fairly common mistake was a superfluous space before the
identifier on the assignment line directly below the
documentation comment.
$ ack "^ [^*]" --js modules/ve
- Typo "Inhertiance" -> "Inheritance".
- Replacing the other confusing comment "Inheritance" (inside
the constructor) with "Parent constructor".
- Add missing @abstract for ve.ui.Tool.
- Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js
- Add function names to all @constructor functions. Now that we
have inheritance it is important and useful to have these
functions not be anonymous.
Example of debug shot: http://cl.ly/image/1j3c160w3D45
Makes the difference between
< documentNode;
> ve_dm_DocumentNode
...
: ve_dm_BranchNode
...
: ve_dm_Node
...
: ve_dm_Node
...
: Object
...
without names (current situation):
< documentNode;
> Object
...
: Object
...
: Object
...
: Object
...
: Object
...
though before this commit, it really looks like this
(flattened since ve.extendClass really did a mixin):
< documentNode;
> Object
...
...
...
Pattern in Sublime (case-sensitive) to find nameless
constructor functions:
"^ve\..*\.([A-Z])([^\.]+) = function \("
Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
|
|
|
* Extenders are expected to inherit from ve.Node.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
|
|
|
* Branch nodes are immutable, which is why there are no methods for adding or removing children.
|
|
|
|
* DataModel classes will add this functionality, and other subclasses will implement behavior that
|
|
|
|
* mimcs changes made to data model nodes.
|
|
|
|
*
|
Object management: Object create/inherit/clone utilities
* For the most common case:
- replace ve.extendClass with ve.inheritClass (chose slightly
different names to detect usage of the old/new one, and I
like 'inherit' better).
- move it up to below the constructor, see doc block for why.
* Cases where more than 2 arguments were passed to
ve.extendClass are handled differently depending on the case.
In case of a longer inheritance tree, the other arguments
could be omitted (like in "ve.ce.FooBar, ve.FooBar,
ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar,
because ve.ce.FooBar inherits from ve.Bar).
In the case of where it previously had two mixins with
ve.extendClass(), either one becomes inheritClass and one
a mixin, both to mixinClass().
No visible changes should come from this commit as the
instances still all have the same visible properties in the
end. No more or less than before.
* Misc.:
- Be consistent in calling parent constructors in the
same order as the inheritance.
- Add missing @extends and @param documentation.
- Replace invalid {Integer} type hint with {Number}.
- Consistent doc comments order:
@class, @abstract, @constructor, @extends, @params.
- Fix indentation errors
A fairly common mistake was a superfluous space before the
identifier on the assignment line directly below the
documentation comment.
$ ack "^ [^*]" --js modules/ve
- Typo "Inhertiance" -> "Inheritance".
- Replacing the other confusing comment "Inheritance" (inside
the constructor) with "Parent constructor".
- Add missing @abstract for ve.ui.Tool.
- Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js
- Add function names to all @constructor functions. Now that we
have inheritance it is important and useful to have these
functions not be anonymous.
Example of debug shot: http://cl.ly/image/1j3c160w3D45
Makes the difference between
< documentNode;
> ve_dm_DocumentNode
...
: ve_dm_BranchNode
...
: ve_dm_Node
...
: ve_dm_Node
...
: Object
...
without names (current situation):
< documentNode;
> Object
...
: Object
...
: Object
...
: Object
...
: Object
...
though before this commit, it really looks like this
(flattened since ve.extendClass really did a mixin):
< documentNode;
> Object
...
...
...
Pattern in Sublime (case-sensitive) to find nameless
constructor functions:
"^ve\..*\.([A-Z])([^\.]+) = function \("
Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
|
|
|
* @mixin
|
2011-11-03 21:48:40 +00:00
|
|
|
* @abstract
|
|
|
|
* @constructor
|
2012-06-20 01:20:28 +00:00
|
|
|
* @param {ve.Node[]} children Array of children to add
|
2011-11-03 21:48:40 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.BranchNode = function ( children ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
this.children = ve.isArray( children ) ? children : [];
|
2011-11-03 21:48:40 +00:00
|
|
|
};
|
|
|
|
|
2011-11-10 19:26:02 +00:00
|
|
|
/**
|
|
|
|
* Checks if this node has child nodes.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-10 19:26:02 +00:00
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @see {ve.Node.prototype.hasChildren}
|
2011-11-10 19:26:02 +00:00
|
|
|
* @returns {Boolean} Whether this node has children
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.BranchNode.prototype.hasChildren = function () {
|
2011-11-10 19:26:02 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2011-11-03 21:48:40 +00:00
|
|
|
/**
|
|
|
|
* Gets a list of child nodes.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-03 21:48:40 +00:00
|
|
|
* @method
|
2012-06-20 01:20:28 +00:00
|
|
|
* @returns {ve.Node[]} List of child nodes
|
2011-11-03 21:48:40 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.BranchNode.prototype.getChildren = function () {
|
2011-11-03 21:48:40 +00:00
|
|
|
return this.children;
|
|
|
|
};
|
|
|
|
|
2011-11-15 16:24:33 +00:00
|
|
|
/**
|
|
|
|
* Gets the index of a given child node.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-15 16:24:33 +00:00
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @param {ve.dm.Node} node Child node to find index of
|
Object management: Object create/inherit/clone utilities
* For the most common case:
- replace ve.extendClass with ve.inheritClass (chose slightly
different names to detect usage of the old/new one, and I
like 'inherit' better).
- move it up to below the constructor, see doc block for why.
* Cases where more than 2 arguments were passed to
ve.extendClass are handled differently depending on the case.
In case of a longer inheritance tree, the other arguments
could be omitted (like in "ve.ce.FooBar, ve.FooBar,
ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar,
because ve.ce.FooBar inherits from ve.Bar).
In the case of where it previously had two mixins with
ve.extendClass(), either one becomes inheritClass and one
a mixin, both to mixinClass().
No visible changes should come from this commit as the
instances still all have the same visible properties in the
end. No more or less than before.
* Misc.:
- Be consistent in calling parent constructors in the
same order as the inheritance.
- Add missing @extends and @param documentation.
- Replace invalid {Integer} type hint with {Number}.
- Consistent doc comments order:
@class, @abstract, @constructor, @extends, @params.
- Fix indentation errors
A fairly common mistake was a superfluous space before the
identifier on the assignment line directly below the
documentation comment.
$ ack "^ [^*]" --js modules/ve
- Typo "Inhertiance" -> "Inheritance".
- Replacing the other confusing comment "Inheritance" (inside
the constructor) with "Parent constructor".
- Add missing @abstract for ve.ui.Tool.
- Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js
- Add function names to all @constructor functions. Now that we
have inheritance it is important and useful to have these
functions not be anonymous.
Example of debug shot: http://cl.ly/image/1j3c160w3D45
Makes the difference between
< documentNode;
> ve_dm_DocumentNode
...
: ve_dm_BranchNode
...
: ve_dm_Node
...
: ve_dm_Node
...
: Object
...
without names (current situation):
< documentNode;
> Object
...
: Object
...
: Object
...
: Object
...
: Object
...
though before this commit, it really looks like this
(flattened since ve.extendClass really did a mixin):
< documentNode;
> Object
...
...
...
Pattern in Sublime (case-sensitive) to find nameless
constructor functions:
"^ve\..*\.([A-Z])([^\.]+) = function \("
Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
|
|
|
* @returns {Number} Index of child node or -1 if node was not found
|
2011-11-15 16:24:33 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.BranchNode.prototype.indexOf = function ( node ) {
|
2012-08-11 08:14:56 +00:00
|
|
|
return ve.indexOf( node, this.children );
|
2011-11-15 16:24:33 +00:00
|
|
|
};
|
|
|
|
|
2012-06-20 01:20:28 +00:00
|
|
|
/**
|
|
|
|
* Sets the root node this node is a descendent of.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @see {ve.Node.prototype.setRoot}
|
|
|
|
* @param {ve.Node} root Node to use as root
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.BranchNode.prototype.setRoot = function ( root ) {
|
2012-07-19 03:40:49 +00:00
|
|
|
if ( root === this.root ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
// Nothing to do, don't recurse into all descendants
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.root = root;
|
|
|
|
for ( var i = 0; i < this.children.length; i++ ) {
|
|
|
|
this.children[i].setRoot( root );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the document this node is a part of.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @see {ve.Node.prototype.setDocument}
|
|
|
|
* @param {ve.Document} root Node to use as root
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.BranchNode.prototype.setDocument = function ( doc ) {
|
2012-07-19 03:40:49 +00:00
|
|
|
if ( doc === this.doc ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
// Nothing to do, don't recurse into all descendants
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.doc = doc;
|
|
|
|
for ( var i = 0; i < this.children.length; i++ ) {
|
|
|
|
this.children[i].setDocument( doc );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the node at a given offset.
|
|
|
|
*
|
|
|
|
* This method is pretty expensive. If you need to get different slices of the same content, get
|
|
|
|
* the content first, then slice it up locally.
|
|
|
|
*
|
|
|
|
* TODO: Rewrite this method to not use recursion, because the function call overhead is expensive
|
|
|
|
*
|
|
|
|
* @method
|
Object management: Object create/inherit/clone utilities
* For the most common case:
- replace ve.extendClass with ve.inheritClass (chose slightly
different names to detect usage of the old/new one, and I
like 'inherit' better).
- move it up to below the constructor, see doc block for why.
* Cases where more than 2 arguments were passed to
ve.extendClass are handled differently depending on the case.
In case of a longer inheritance tree, the other arguments
could be omitted (like in "ve.ce.FooBar, ve.FooBar,
ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar,
because ve.ce.FooBar inherits from ve.Bar).
In the case of where it previously had two mixins with
ve.extendClass(), either one becomes inheritClass and one
a mixin, both to mixinClass().
No visible changes should come from this commit as the
instances still all have the same visible properties in the
end. No more or less than before.
* Misc.:
- Be consistent in calling parent constructors in the
same order as the inheritance.
- Add missing @extends and @param documentation.
- Replace invalid {Integer} type hint with {Number}.
- Consistent doc comments order:
@class, @abstract, @constructor, @extends, @params.
- Fix indentation errors
A fairly common mistake was a superfluous space before the
identifier on the assignment line directly below the
documentation comment.
$ ack "^ [^*]" --js modules/ve
- Typo "Inhertiance" -> "Inheritance".
- Replacing the other confusing comment "Inheritance" (inside
the constructor) with "Parent constructor".
- Add missing @abstract for ve.ui.Tool.
- Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js
- Add function names to all @constructor functions. Now that we
have inheritance it is important and useful to have these
functions not be anonymous.
Example of debug shot: http://cl.ly/image/1j3c160w3D45
Makes the difference between
< documentNode;
> ve_dm_DocumentNode
...
: ve_dm_BranchNode
...
: ve_dm_Node
...
: ve_dm_Node
...
: Object
...
without names (current situation):
< documentNode;
> Object
...
: Object
...
: Object
...
: Object
...
: Object
...
though before this commit, it really looks like this
(flattened since ve.extendClass really did a mixin):
< documentNode;
> Object
...
...
...
Pattern in Sublime (case-sensitive) to find nameless
constructor functions:
"^ve\..*\.([A-Z])([^\.]+) = function \("
Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
|
|
|
* @param {Number} offset Offset get node for
|
2012-06-20 01:20:28 +00:00
|
|
|
* @param {Boolean} [shallow] Do not iterate into child nodes of child nodes
|
|
|
|
* @returns {ve.Node|null} Node at offset, or null if non was found
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.BranchNode.prototype.getNodeFromOffset = function ( offset, shallow ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
if ( offset === 0 ) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
// TODO a lot of logic is duplicated in selectNodes(), abstract that into a traverser or something
|
|
|
|
if ( this.children.length ) {
|
2012-08-02 18:46:13 +00:00
|
|
|
var i, length, nodeLength, childNode,
|
|
|
|
nodeOffset = 0;
|
|
|
|
for ( i = 0, length = this.children.length; i < length; i++ ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
childNode = this.children[i];
|
2012-07-19 03:40:49 +00:00
|
|
|
if ( offset === nodeOffset ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
// The requested offset is right before childNode,
|
|
|
|
// so it's not inside any of this's children, but inside this
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
nodeLength = childNode.getOuterLength();
|
|
|
|
if ( offset >= nodeOffset && offset < nodeOffset + nodeLength ) {
|
|
|
|
if ( !shallow && childNode.hasChildren() && childNode.getChildren().length ) {
|
|
|
|
return this.getNodeFromOffset.call( childNode, offset - nodeOffset - 1 );
|
|
|
|
} else {
|
|
|
|
return childNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nodeOffset += nodeLength;
|
|
|
|
}
|
2012-07-19 03:40:49 +00:00
|
|
|
if ( offset === nodeOffset ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
// The requested offset is right before this.children[i],
|
|
|
|
// so it's not inside any of this's children, but inside this
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the content offset of a node.
|
|
|
|
*
|
|
|
|
* TODO: Rewrite this method to not use recursion, because the function call overhead is expensive
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.Node} node Node to get offset of
|
Object management: Object create/inherit/clone utilities
* For the most common case:
- replace ve.extendClass with ve.inheritClass (chose slightly
different names to detect usage of the old/new one, and I
like 'inherit' better).
- move it up to below the constructor, see doc block for why.
* Cases where more than 2 arguments were passed to
ve.extendClass are handled differently depending on the case.
In case of a longer inheritance tree, the other arguments
could be omitted (like in "ve.ce.FooBar, ve.FooBar,
ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar,
because ve.ce.FooBar inherits from ve.Bar).
In the case of where it previously had two mixins with
ve.extendClass(), either one becomes inheritClass and one
a mixin, both to mixinClass().
No visible changes should come from this commit as the
instances still all have the same visible properties in the
end. No more or less than before.
* Misc.:
- Be consistent in calling parent constructors in the
same order as the inheritance.
- Add missing @extends and @param documentation.
- Replace invalid {Integer} type hint with {Number}.
- Consistent doc comments order:
@class, @abstract, @constructor, @extends, @params.
- Fix indentation errors
A fairly common mistake was a superfluous space before the
identifier on the assignment line directly below the
documentation comment.
$ ack "^ [^*]" --js modules/ve
- Typo "Inhertiance" -> "Inheritance".
- Replacing the other confusing comment "Inheritance" (inside
the constructor) with "Parent constructor".
- Add missing @abstract for ve.ui.Tool.
- Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js
- Add function names to all @constructor functions. Now that we
have inheritance it is important and useful to have these
functions not be anonymous.
Example of debug shot: http://cl.ly/image/1j3c160w3D45
Makes the difference between
< documentNode;
> ve_dm_DocumentNode
...
: ve_dm_BranchNode
...
: ve_dm_Node
...
: ve_dm_Node
...
: Object
...
without names (current situation):
< documentNode;
> Object
...
: Object
...
: Object
...
: Object
...
: Object
...
though before this commit, it really looks like this
(flattened since ve.extendClass really did a mixin):
< documentNode;
> Object
...
...
...
Pattern in Sublime (case-sensitive) to find nameless
constructor functions:
"^ve\..*\.([A-Z])([^\.]+) = function \("
Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
|
|
|
* @returns {Number} Offset of node or -1 of node was not found
|
2012-06-20 01:20:28 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.BranchNode.prototype.getOffsetFromNode = function ( node ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
if ( node === this ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ( this.children.length ) {
|
2012-08-02 18:46:13 +00:00
|
|
|
var i, length, childOffset, childNode,
|
|
|
|
offset = 0;
|
|
|
|
for ( i = 0, length = this.children.length; i < length; i++ ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
childNode = this.children[i];
|
|
|
|
if ( childNode === node ) {
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
if ( childNode.canHaveChildren() && childNode.getChildren().length ) {
|
2012-08-02 18:46:13 +00:00
|
|
|
childOffset = this.getOffsetFromNode.call( childNode, node );
|
2012-06-20 01:20:28 +00:00
|
|
|
if ( childOffset !== -1 ) {
|
|
|
|
return offset + 1 + childOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
offset += childNode.getOuterLength();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
2011-11-15 01:31:46 +00:00
|
|
|
/**
|
|
|
|
* Traverse leaf nodes depth first.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-15 01:31:46 +00:00
|
|
|
* Callback functions are expected to accept a node and index argument. If a callback returns false,
|
|
|
|
* iteration will stop.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-15 01:31:46 +00:00
|
|
|
* @param {Function} callback Function to execute for each leaf node
|
2012-02-06 23:50:56 +00:00
|
|
|
* @param {ve.Node} [from] Node to start at. Must be a descendant of this node
|
2011-11-15 01:31:46 +00:00
|
|
|
* @param {Boolean} [reverse] Whether to iterate backwards
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.BranchNode.prototype.traverseLeafNodes = function ( callback, from, reverse ) {
|
2011-11-15 13:23:04 +00:00
|
|
|
// Stack of indices that lead from this to node
|
2012-07-20 00:24:54 +00:00
|
|
|
var indexStack = [],
|
2011-11-15 13:23:04 +00:00
|
|
|
// Node whose children we're currently traversing
|
|
|
|
node = this,
|
|
|
|
// Index of the child node we're currently visiting
|
|
|
|
index = reverse ? node.children.length - 1 : 0,
|
|
|
|
// Shortcut for node.children[index]
|
|
|
|
childNode,
|
|
|
|
// Result of the last invocation of the callback
|
|
|
|
callbackResult,
|
|
|
|
// Variables for the loop that builds indexStack if from is specified
|
|
|
|
n, p, i;
|
2011-11-15 12:55:31 +00:00
|
|
|
|
|
|
|
if ( from !== undefined ) {
|
|
|
|
// Reverse-engineer the index stack by starting at from and
|
|
|
|
// working our way up until we reach this
|
|
|
|
n = from;
|
|
|
|
while ( n !== this ) {
|
|
|
|
p = n.getParent();
|
|
|
|
if ( !p ) {
|
|
|
|
// n is a root node and we haven't reached this
|
|
|
|
// That means from isn't a descendant of this
|
2012-08-08 17:48:53 +00:00
|
|
|
throw new Error( 'from parameter passed to traverseLeafNodes() must be a descendant' );
|
2011-11-15 12:55:31 +00:00
|
|
|
}
|
|
|
|
// Find the index of n in p
|
2011-11-16 12:51:31 +00:00
|
|
|
i = p.indexOf( n );
|
2011-11-15 12:55:31 +00:00
|
|
|
if ( i === -1 ) {
|
|
|
|
// This isn't supposed to be possible
|
2012-08-08 17:48:53 +00:00
|
|
|
throw new Error( 'Tree corruption detected: node isn\'t in its parent\'s children array' );
|
2011-11-15 12:55:31 +00:00
|
|
|
}
|
|
|
|
indexStack.push( i );
|
|
|
|
// Move up
|
|
|
|
n = p;
|
|
|
|
}
|
|
|
|
// We've built the indexStack in reverse order, so reverse it
|
|
|
|
indexStack = indexStack.reverse();
|
|
|
|
|
|
|
|
// Set up the variables such that from will be visited next
|
|
|
|
index = indexStack.pop();
|
|
|
|
node = from.getParent(); // from is a descendant of this so its parent exists
|
2011-11-15 13:23:04 +00:00
|
|
|
|
|
|
|
// If we're going in reverse, then we still need to visit from if it's
|
|
|
|
// a leaf node, but we should not descend into it
|
|
|
|
// So if from is not a leaf node, skip it now
|
2012-06-20 01:20:28 +00:00
|
|
|
if ( reverse && from.canHaveChildren() ) {
|
2011-11-15 13:23:04 +00:00
|
|
|
index--;
|
|
|
|
}
|
2011-11-15 12:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while ( true ) {
|
|
|
|
childNode = node.children[index];
|
|
|
|
if ( childNode === undefined ) {
|
|
|
|
if ( indexStack.length > 0 ) {
|
|
|
|
// We're done traversing the current node, move back out of it
|
|
|
|
node = node.getParent();
|
|
|
|
index = indexStack.pop();
|
|
|
|
// Move to the next child
|
2011-11-15 18:42:34 +00:00
|
|
|
index += reverse ? -1 : 1;
|
2011-11-15 12:55:31 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
// We can't move up any more, so we're done
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-20 01:20:28 +00:00
|
|
|
if ( childNode.canHaveChildren() ) {
|
2011-11-15 12:55:31 +00:00
|
|
|
// Descend into this node
|
|
|
|
node = childNode;
|
|
|
|
// Push our current index onto the stack
|
|
|
|
indexStack.push( index );
|
2011-11-15 13:23:04 +00:00
|
|
|
// Set the current index to the first element we're visiting
|
|
|
|
index = reverse ? node.children.length - 1 : 0;
|
2011-11-15 11:12:06 +00:00
|
|
|
} else {
|
2011-11-15 12:55:31 +00:00
|
|
|
// This is a leaf node, visit it
|
|
|
|
callbackResult = callback( childNode ); // TODO what is index?
|
2011-11-15 11:12:06 +00:00
|
|
|
if ( callbackResult === false ) {
|
|
|
|
// The callback is telling us to stop
|
|
|
|
return;
|
|
|
|
}
|
2011-11-15 12:55:31 +00:00
|
|
|
// Move to the next child
|
2011-11-15 18:42:34 +00:00
|
|
|
index += reverse ? -1 : 1;
|
2011-11-15 11:12:06 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-15 01:31:46 +00:00
|
|
|
};
|