r100897 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100896‎ | r100897 | r100898 >
Date:23:06, 26 October 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Replaced broken iterative implementation of getOffsetFromNode with the old (and fixed) recursive version.
Modified paths:
  • /trunk/parsers/wikidom/lib/hype/bases/es.DocumentNode.js (modified) (history)
  • /trunk/parsers/wikidom/tests/hype/es.DocumentModel.test.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/tests/hype/es.DocumentModel.test.js
@@ -52,7 +52,7 @@
5353 {
5454 'type': 'paragraph',
5555 'content': {
56 - 'text': 'a'
 56+ 'text': 'd'
5757 }
5858 },
5959 {
@@ -64,7 +64,7 @@
6565 'styles': ['bullet']
6666 },
6767 'content': {
68 - 'text': 'a'
 68+ 'text': 'e'
6969 }
7070 },
7171 {
@@ -73,7 +73,7 @@
7474 'styles': ['bullet', 'bullet']
7575 },
7676 'content': {
77 - 'text': 'b'
 77+ 'text': 'f'
7878 }
7979 },
8080 {
@@ -82,7 +82,7 @@
8383 'styles': ['number']
8484 },
8585 'content': {
86 - 'text': 'c'
 86+ 'text': 'g'
8787 }
8888 }
8989 ]
@@ -96,7 +96,7 @@
9797 {
9898 'type': 'paragraph',
9999 'content': {
100 - 'text': 'a'
 100+ 'text': 'h'
101101 }
102102 }
103103 ]
@@ -139,7 +139,7 @@
140140 // 8 - Beginning of paragraph
141141 { 'type': 'paragraph' },
142142 // 9 - Plain content
143 - 'a',
 143+ 'd',
144144 // 10 - End of paragraph
145145 { 'type': '/paragraph' },
146146 // 11 - Beginning of list
@@ -147,19 +147,19 @@
148148 // 12 - Beginning of bullet list item
149149 { 'type': 'listItem', 'attributes': { 'styles': ['bullet'] } },
150150 // 13 - Plain content
151 - 'a',
 151+ 'e',
152152 // 14 - End of item
153153 { 'type': '/listItem' },
154154 // 15 - Beginning of nested bullet list item
155155 { 'type': 'listItem', 'attributes': { 'styles': ['bullet', 'bullet'] } },
156156 // 16 - Plain content
157 - 'b',
 157+ 'f',
158158 // 17 - End of item
159159 { 'type': '/listItem' },
160160 // 18 - Beginning of numbered list item
161161 { 'type': 'listItem', 'attributes': { 'styles': ['number'] } },
162162 // 19 - Plain content
163 - 'c',
 163+ 'g',
164164 // 20 - End of item
165165 { 'type': '/listItem' },
166166 // 21 - End of list
@@ -173,7 +173,7 @@
174174 // 25 - Beginning of paragraph
175175 { 'type': 'paragraph' },
176176 // 26 - Plain content
177 - 'a',
 177+ 'h',
178178 // 27 - End of paragraph
179179 { 'type': '/paragraph' }
180180 ];
@@ -217,7 +217,7 @@
218218
219219 test( 'es.DocumentModel.getContent', 6, function() {
220220 var documentModel = es.DocumentModel.newFromPlainObject( obj );
221 -
 221+
222222 // Test 1
223223 deepEqual(
224224 documentModel[0].getContent( new es.Range( 1, 3 ) ),
@@ -257,7 +257,7 @@
258258 }
259259
260260 // Test 6
261 - deepEqual( documentModel[2].getContent(), ['a'], 'Content can be extracted from nodes' );
 261+ deepEqual( documentModel[2].getContent(), ['h'], 'Content can be extracted from nodes' );
262262 } );
263263
264264 test( 'es.DocumentModel.getIndexOfAnnotation', 3, function() {
Index: trunk/parsers/wikidom/lib/hype/bases/es.DocumentNode.js
@@ -37,53 +37,41 @@
3838 * This method is pretty expensive. If you need to get different slices of the same content, get
3939 * the content first, then slice it up locally.
4040 *
 41+ * TODO: Rewrite this method to not use recursion, because the function call overhead is expensive
 42+ *
4143 * @method
4244 * @param {es.DocumentModelNode} node Node to get offset of
4345 * @param {Boolean} [shallow] Do not iterate into child nodes of child nodes
4446 * @returns {Integer} Offset of node or -1 of node was not found
4547 */
4648 es.DocumentNode.prototype.getOffsetFromNode = function( node, shallow ) {
47 - var offset = 0;
 49+ var offset = 0,
 50+ i;
4851 if ( shallow ) {
4952 if ( this.length ) {
50 - for ( var i = 0; i < this.length; i++ ) {
 53+ for ( i = 0; i < this.length; i++ ) {
5154 if ( this[i] === node ) {
5255 return offset;
5356 }
5457 offset += this[i].getElementLength() + 1;
5558 }
 59+ return -1;
5660 }
5761 } else {
58 - var currentNode,
59 - iteration = [this, 0],
60 - iterations = [iteration];
61 - while ( iterations[0][0].length < iteration[0][1] ) {
62 - currentNode = iteration[0][iteration[1]];
63 - if ( currentNode === node ) {
64 - break;
65 - } else {
66 - if ( currentNode.length ) {
67 - // Include opening element when descending
68 - offset++;
69 - // Descend one level down
70 - iterations.push( [currentNode, 0] );
71 - iteration = iterations[iterations.length - 1];
72 - } else {
73 - // Include opening and closing when passing over
74 - offset += 2;
 62+ for ( i = 0; i < this.length; i++ ) {
 63+ if ( this[i] === node ) {
 64+ return offset;
 65+ }
 66+ if ( this[i].length ) {
 67+ var childOffset = this.getOffsetFromNode.call( this[i], node );
 68+ if ( childOffset !== -1 ) {
 69+ return offset + 1 + childOffset;
7570 }
7671 }
77 - iteration[1]++;
78 - if ( iteration[1] >= iteration[0].length ) {
79 - // Include closing element when ascending
80 - offset++;
81 - // Ascend one level up
82 - iterations.pop();
83 - iteration = iterations[iterations.length - 1];
84 - }
 72+ offset += this[i].getElementLength();
8573 }
 74+ return -1;
8675 }
87 - return offset;
8876 };
8977
9078 /**

Status & tagging log