Index: trunk/parsers/wikidom/lib/hype/bases/es.DocumentNode.js |
— | — | @@ -13,19 +13,25 @@ |
14 | 14 | * Gets the range within this node that a given child node covers. |
15 | 15 | * |
16 | 16 | * @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 |
18 | 20 | */ |
19 | | -es.DocumentNode.prototype.getRangeFromNode = function( node ) { |
| 21 | +es.DocumentNode.prototype.getRangeFromNode = function( node, shallow ) { |
20 | 22 | 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++ ) { |
25 | 24 | if ( this[i] === node ) { |
26 | 25 | return new es.Range( left, left + this[i].getElementLength() ); |
27 | 26 | } |
| 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 | + } |
28 | 35 | left += this[i].getElementLength(); |
29 | | - i++; |
30 | 36 | } |
31 | 37 | } |
32 | 38 | return null; |
— | — | @@ -47,7 +53,7 @@ |
48 | 54 | es.DocumentNode.prototype.getOffsetFromNode = function( node, shallow ) { |
49 | 55 | if ( this.length ) { |
50 | 56 | var offset = 0; |
51 | | - for ( var i = 0; i < this.length; i++ ) { |
| 57 | + for ( var i = 0, length = this.length; i < length; i++ ) { |
52 | 58 | if ( this[i] === node ) { |
53 | 59 | return offset; |
54 | 60 | } |
— | — | @@ -72,7 +78,7 @@ |
73 | 79 | * TODO: Rewrite this method to not use recursion, because the function call overhead is expensive |
74 | 80 | * |
75 | 81 | * @method |
76 | | - * @param {Integer} offset Offset within this node to look for child node in |
| 82 | + * @param {Integer} offset Offset get node for |
77 | 83 | * @param {Boolean} [shallow] Do not iterate into child nodes of child nodes |
78 | 84 | * @returns {es.DocumentModelNode|null} Node at offset, or null if non was found |
79 | 85 | */ |
— | — | @@ -100,9 +106,8 @@ |
101 | 107 | * |
102 | 108 | * @method |
103 | 109 | * @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 |
107 | 112 | */ |
108 | 113 | es.DocumentNode.prototype.selectNodes = function( range ) { |
109 | 114 | range.normalize(); |