r101443 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r101442‎ | r101443 | r101444 >
Date:00:13, 1 November 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Added shallow argument to selectNodes method
Modified paths:
  • /trunk/parsers/wikidom/lib/hype/bases/es.DocumentNode.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/hype/bases/es.DocumentNode.js
@@ -102,27 +102,32 @@
103103 *
104104 * @method
105105 * @param {es.Range} range Range to select nodes within
 106+ * @param {Boolean} [shallow] Do not iterate into child nodes of child nodes
106107 * @returns {Array} List of objects with 'node' and 'range' properties describing nodes which are
107108 * covered by the range and the range within the node that is covered
108109 */
109 -es.DocumentNode.prototype.selectNodes = function( range ) {
110 - var nodes = [], i, left, right, start, end, startInside, endInside;
 110+es.DocumentNode.prototype.selectNodes = function( range, shallow ) {
111111 range.normalize();
112 - start = range.start;
113 - end = range.end;
 112+ var nodes = [],
 113+ i,
 114+ left,
 115+ right,
 116+ start = range.start,
 117+ end = range.end,
 118+ startInside,
 119+ endInside;
114120
115121 if ( start < 0 ) {
116122 throw 'The start offset of the range is negative';
117123 }
118124
119 -
120 - if ( this.length == 0 ) {
 125+ if ( this.length === 0 ) {
121126 // Special case: this node doesn't have any children
122127 // The return value is simply the range itself, if it is not out of bounds
123128 if ( end > this.getContentLength() ) {
124129 throw 'The end offset of the range is past the end of the node';
125130 }
126 - return [ { 'node': this, 'range': new es.Range( start, end ) } ];
 131+ return [{ 'node': this, 'range': new es.Range( start, end ) }];
127132 }
128133
129134 // This node has children, loop over them
@@ -153,7 +158,7 @@
154159 startInside = start >= left && start <= right; // is the start inside this[i]?
155160 endInside = end >= left && end <= right; // is the end inside this[i]?
156161
157 - if ( startInside && endInside ) {
 162+ if ( !shallow && startInside && endInside ) {
158163 // The range is entirely inside this[i]
159164 // Recurse into this[i]
160165 // Since the start and end are both inside this[i], we know for sure that we're done, so return
@@ -182,7 +187,7 @@
183188
184189 // If we got here, that means that at least some part of the range is out of bounds
185190 // This is an error
186 - if ( nodes.length == 0 ) {
 191+ if ( nodes.length === 0 ) {
187192 throw 'The start offset of the range is past the end of the node';
188193 } else {
189194 // Apparently the start was inside this node, but the end wasn't

Follow-up revisions

RevisionCommit summaryAuthorDate
r101459Followup r101443, that code can't possibly be right. Account for the case whe...catrope08:47, 1 November 2011

Status & tagging log