Index: trunk/parsers/wikidom/tests/hype/es.DocumentModel.test.js |
— | — | @@ -205,5 +205,21 @@ |
206 | 206 | |
207 | 207 | deepEqual( documentModel.getData(), data, 'Flattening plain objects results in correct data' ); |
208 | 208 | deepEqual( documentModel.slice( 0 ), tree, 'Nodes contain correct lengths' ); |
209 | | - deepEqual( documentModel[2].getContent(), ['a'], 'Content can be extracted from document' ); |
| 209 | + deepEqual( |
| 210 | + documentModel[0].getContent( new es.Range( 1, 3 ) ), |
| 211 | + [ |
| 212 | + ['b', { 'type': 'bold', 'hash': '#bold' }], |
| 213 | + ['c', { 'type': 'italic', 'hash': '#italic' }] |
| 214 | + ], |
| 215 | + 'Content can be extracted from nodes using relative ranges' |
| 216 | + ); |
| 217 | + deepEqual( |
| 218 | + documentModel[0].getContent( new es.Range( 0, 2 ) ), |
| 219 | + [ |
| 220 | + 'a', |
| 221 | + ['b', { 'type': 'bold', 'hash': '#bold' }], |
| 222 | + ], |
| 223 | + 'Content can be extracted from nodes using relative ranges' |
| 224 | + ); |
| 225 | + deepEqual( documentModel[2].getContent(), ['a'], 'Content can be extracted from nodes' ); |
210 | 226 | } ); |
Index: trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js |
— | — | @@ -514,15 +514,14 @@ |
515 | 515 | es.DocumentModel.prototype.getContent = function( node, range ) { |
516 | 516 | if ( range ) { |
517 | 517 | range.normalize(); |
518 | | - } else { |
519 | | - range = { |
520 | | - 'start': 0, |
521 | | - 'end': this.contentLength |
522 | | - }; |
523 | 518 | } |
524 | 519 | var offset = this.getOffsetFromNode( node ); |
525 | 520 | if ( offset !== -1 ) { |
526 | | - return this.data.slice( offset + 1, offset + node.getContentLength() + 1 ); |
| 521 | + offset++; |
| 522 | + var length = node.getContentLength(), |
| 523 | + right = range ? Math.min( range.end, length ) : length, |
| 524 | + left = range ? range.start : 0; |
| 525 | + return this.data.slice( offset + left, offset + right ); |
527 | 526 | } |
528 | 527 | return null; |
529 | 528 | }; |
Index: trunk/parsers/wikidom/lib/hype/es.Range.js |
— | — | @@ -16,6 +16,19 @@ |
17 | 17 | this.normalize(); |
18 | 18 | }; |
19 | 19 | |
| 20 | + |
| 21 | +/** |
| 22 | + * Creates a new es.Range object that's a translated version of another. |
| 23 | + * |
| 24 | + * @method |
| 25 | + * @param {es.Range} range Range to base new range on |
| 26 | + * @param {Integer} distance Distance to move range by |
| 27 | + * @returns {es.Range} New translated range |
| 28 | + */ |
| 29 | +es.Range.newFromTranslatedRange = function( range, distance ) { |
| 30 | + return new es.Range( range.from + distance, range.to + distance ); |
| 31 | +}; |
| 32 | + |
20 | 33 | /* Methods */ |
21 | 34 | |
22 | 35 | /** |