r100998 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100997‎ | r100998 | r100999 >
Date:16:24, 27 October 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Simplified getRangeOfNode, added deep functionality to it, and cleaned up some comments
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
@@ -13,19 +13,25 @@
1414 * Gets the range within this node that a given child node covers.
1515 *
1616 * @method
17 - * @param {es.ModelNode} node
 17+ * @param {es.ModelNode} node Node to get range for
 18+ * @param {Boolean} [shallow] Do not iterate into child nodes of child nodes
 19+ * @returns {es.Range|null} Range of node or null if node was not found
1820 */
19 -es.DocumentNode.prototype.getRangeFromNode = function( node ) {
 21+es.DocumentNode.prototype.getRangeFromNode = function( node, shallow ) {
2022 if ( this.length ) {
21 - var i = 0,
22 - length = this.length,
23 - left = 0;
24 - while ( i < length ) {
 23+ for ( var i = 0, length = this.length, left = 0; i < length; i++ ) {
2524 if ( this[i] === node ) {
2625 return new es.Range( left, left + this[i].getElementLength() );
2726 }
 27+ if ( !shallow && this[i].length ) {
 28+ var range = this[i].getRangeFromNode( node );
 29+ if ( range !== null ) {
 30+ // Include opening of parent
 31+ left++;
 32+ return es.Range.newFromTranslatedRange( range, left );
 33+ }
 34+ }
2835 left += this[i].getElementLength();
29 - i++;
3036 }
3137 }
3238 return null;
@@ -47,7 +53,7 @@
4854 es.DocumentNode.prototype.getOffsetFromNode = function( node, shallow ) {
4955 if ( this.length ) {
5056 var offset = 0;
51 - for ( var i = 0; i < this.length; i++ ) {
 57+ for ( var i = 0, length = this.length; i < length; i++ ) {
5258 if ( this[i] === node ) {
5359 return offset;
5460 }
@@ -72,7 +78,7 @@
7379 * TODO: Rewrite this method to not use recursion, because the function call overhead is expensive
7480 *
7581 * @method
76 - * @param {Integer} offset Offset within this node to look for child node in
 82+ * @param {Integer} offset Offset get node for
7783 * @param {Boolean} [shallow] Do not iterate into child nodes of child nodes
7884 * @returns {es.DocumentModelNode|null} Node at offset, or null if non was found
7985 */
@@ -100,9 +106,8 @@
101107 *
102108 * @method
103109 * @param {es.Range} range Range to select nodes within
104 - * @returns {Object} Object with 'on' and 'off' properties, 'on' being a list of objects with 'node'
105 - * and 'range' properties describing nodes which are covered by the range and the range within the
106 - * node that is covered, and 'off' being a list of nodes that are not covered by the range
 110+ * @returns {Array} List of objects with 'node' and 'range' properties describing nodes which are
 111+ * covered by the range and the range within the node that is covered
107112 */
108113 es.DocumentNode.prototype.selectNodes = function( range ) {
109114 range.normalize();

Status & tagging log