Index: trunk/parsers/wikidom/tests/hype/es.DocumentModel.test.js |
— | — | @@ -52,7 +52,7 @@ |
53 | 53 | { |
54 | 54 | 'type': 'paragraph', |
55 | 55 | 'content': { |
56 | | - 'text': 'a' |
| 56 | + 'text': 'd' |
57 | 57 | } |
58 | 58 | }, |
59 | 59 | { |
— | — | @@ -64,7 +64,7 @@ |
65 | 65 | 'styles': ['bullet'] |
66 | 66 | }, |
67 | 67 | 'content': { |
68 | | - 'text': 'a' |
| 68 | + 'text': 'e' |
69 | 69 | } |
70 | 70 | }, |
71 | 71 | { |
— | — | @@ -73,7 +73,7 @@ |
74 | 74 | 'styles': ['bullet', 'bullet'] |
75 | 75 | }, |
76 | 76 | 'content': { |
77 | | - 'text': 'b' |
| 77 | + 'text': 'f' |
78 | 78 | } |
79 | 79 | }, |
80 | 80 | { |
— | — | @@ -82,7 +82,7 @@ |
83 | 83 | 'styles': ['number'] |
84 | 84 | }, |
85 | 85 | 'content': { |
86 | | - 'text': 'c' |
| 86 | + 'text': 'g' |
87 | 87 | } |
88 | 88 | } |
89 | 89 | ] |
— | — | @@ -96,7 +96,7 @@ |
97 | 97 | { |
98 | 98 | 'type': 'paragraph', |
99 | 99 | 'content': { |
100 | | - 'text': 'a' |
| 100 | + 'text': 'h' |
101 | 101 | } |
102 | 102 | } |
103 | 103 | ] |
— | — | @@ -139,7 +139,7 @@ |
140 | 140 | // 8 - Beginning of paragraph |
141 | 141 | { 'type': 'paragraph' }, |
142 | 142 | // 9 - Plain content |
143 | | - 'a', |
| 143 | + 'd', |
144 | 144 | // 10 - End of paragraph |
145 | 145 | { 'type': '/paragraph' }, |
146 | 146 | // 11 - Beginning of list |
— | — | @@ -147,19 +147,19 @@ |
148 | 148 | // 12 - Beginning of bullet list item |
149 | 149 | { 'type': 'listItem', 'attributes': { 'styles': ['bullet'] } }, |
150 | 150 | // 13 - Plain content |
151 | | - 'a', |
| 151 | + 'e', |
152 | 152 | // 14 - End of item |
153 | 153 | { 'type': '/listItem' }, |
154 | 154 | // 15 - Beginning of nested bullet list item |
155 | 155 | { 'type': 'listItem', 'attributes': { 'styles': ['bullet', 'bullet'] } }, |
156 | 156 | // 16 - Plain content |
157 | | - 'b', |
| 157 | + 'f', |
158 | 158 | // 17 - End of item |
159 | 159 | { 'type': '/listItem' }, |
160 | 160 | // 18 - Beginning of numbered list item |
161 | 161 | { 'type': 'listItem', 'attributes': { 'styles': ['number'] } }, |
162 | 162 | // 19 - Plain content |
163 | | - 'c', |
| 163 | + 'g', |
164 | 164 | // 20 - End of item |
165 | 165 | { 'type': '/listItem' }, |
166 | 166 | // 21 - End of list |
— | — | @@ -173,7 +173,7 @@ |
174 | 174 | // 25 - Beginning of paragraph |
175 | 175 | { 'type': 'paragraph' }, |
176 | 176 | // 26 - Plain content |
177 | | - 'a', |
| 177 | + 'h', |
178 | 178 | // 27 - End of paragraph |
179 | 179 | { 'type': '/paragraph' } |
180 | 180 | ]; |
— | — | @@ -217,7 +217,7 @@ |
218 | 218 | |
219 | 219 | test( 'es.DocumentModel.getContent', 6, function() { |
220 | 220 | var documentModel = es.DocumentModel.newFromPlainObject( obj ); |
221 | | - |
| 221 | + |
222 | 222 | // Test 1 |
223 | 223 | deepEqual( |
224 | 224 | documentModel[0].getContent( new es.Range( 1, 3 ) ), |
— | — | @@ -257,7 +257,7 @@ |
258 | 258 | } |
259 | 259 | |
260 | 260 | // 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' ); |
262 | 262 | } ); |
263 | 263 | |
264 | 264 | test( 'es.DocumentModel.getIndexOfAnnotation', 3, function() { |
Index: trunk/parsers/wikidom/lib/hype/bases/es.DocumentNode.js |
— | — | @@ -37,53 +37,41 @@ |
38 | 38 | * This method is pretty expensive. If you need to get different slices of the same content, get |
39 | 39 | * the content first, then slice it up locally. |
40 | 40 | * |
| 41 | + * TODO: Rewrite this method to not use recursion, because the function call overhead is expensive |
| 42 | + * |
41 | 43 | * @method |
42 | 44 | * @param {es.DocumentModelNode} node Node to get offset of |
43 | 45 | * @param {Boolean} [shallow] Do not iterate into child nodes of child nodes |
44 | 46 | * @returns {Integer} Offset of node or -1 of node was not found |
45 | 47 | */ |
46 | 48 | es.DocumentNode.prototype.getOffsetFromNode = function( node, shallow ) { |
47 | | - var offset = 0; |
| 49 | + var offset = 0, |
| 50 | + i; |
48 | 51 | if ( shallow ) { |
49 | 52 | if ( this.length ) { |
50 | | - for ( var i = 0; i < this.length; i++ ) { |
| 53 | + for ( i = 0; i < this.length; i++ ) { |
51 | 54 | if ( this[i] === node ) { |
52 | 55 | return offset; |
53 | 56 | } |
54 | 57 | offset += this[i].getElementLength() + 1; |
55 | 58 | } |
| 59 | + return -1; |
56 | 60 | } |
57 | 61 | } 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; |
75 | 70 | } |
76 | 71 | } |
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(); |
85 | 73 | } |
| 74 | + return -1; |
86 | 75 | } |
87 | | - return offset; |
88 | 76 | }; |
89 | 77 | |
90 | 78 | /** |