r103166 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103165‎ | r103166 | r103167 >
Date:13:23, 15 November 2011
Author:catrope
Status:deferred
Tags:
Comment:
Followup r103161: make reverse work
Modified paths:
  • /trunk/extensions/VisualEditor/modules/es/bases/es.DocumentBranchNode.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/es/bases/es.DocumentBranchNode.js
@@ -45,7 +45,18 @@
4646 * @param {Boolean} [reverse] Whether to iterate backwards
4747 */
4848 es.DocumentBranchNode.prototype.traverseLeafNodes = function( callback, from, reverse ) {
49 - var indexStack = [], index = 0, node = this, childNode, callbackResult, n, p, i;
 49+ // Stack of indices that lead from this to node
 50+ var indexStack = [],
 51+ // Node whose children we're currently traversing
 52+ node = this,
 53+ // Index of the child node we're currently visiting
 54+ index = reverse ? node.children.length - 1 : 0,
 55+ // Shortcut for node.children[index]
 56+ childNode,
 57+ // Result of the last invocation of the callback
 58+ callbackResult,
 59+ // Variables for the loop that builds indexStack if from is specified
 60+ n, p, i;
5061
5162 if ( from !== undefined ) {
5263 // Reverse-engineer the index stack by starting at from and
@@ -74,6 +85,13 @@
7586 // Set up the variables such that from will be visited next
7687 index = indexStack.pop();
7788 node = from.getParent(); // from is a descendant of this so its parent exists
 89+
 90+ // If we're going in reverse, then we still need to visit from if it's
 91+ // a leaf node, but we should not descend into it
 92+ // So if from is not a leaf node, skip it now
 93+ if ( reverse && from.hasChildren() ) {
 94+ index--;
 95+ }
7896 }
7997
8098 while ( true ) {
@@ -84,7 +102,7 @@
85103 node = node.getParent();
86104 index = indexStack.pop();
87105 // Move to the next child
88 - index++;
 106+ reverse ? index-- : index++;
89107 continue;
90108 } else {
91109 // We can't move up any more, so we're done
@@ -97,8 +115,8 @@
98116 node = childNode;
99117 // Push our current index onto the stack
100118 indexStack.push( index );
101 - // Set the current index to zero
102 - index = 0;
 119+ // Set the current index to the first element we're visiting
 120+ index = reverse ? node.children.length - 1 : 0;
103121 } else {
104122 // This is a leaf node, visit it
105123 callbackResult = callback( childNode ); // TODO what is index?
@@ -107,7 +125,7 @@
108126 return;
109127 }
110128 // Move to the next child
111 - index++;
 129+ reverse ? index-- : index++;
112130 }
113131 }
114132 };

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r103161Rewrite traverseLeafNodes() with an iterative traversal, and add support for ...catrope12:55, 15 November 2011

Status & tagging log