Index: trunk/parsers/wikidom/tests/hype/es.DocumentModel.test.js |
— | — | @@ -201,7 +201,7 @@ |
202 | 202 | new es.ParagraphModel( data[25], 1 ) |
203 | 203 | ]; |
204 | 204 | |
205 | | -test( 'es.DocumentModel', function() { |
| 205 | +test( 'es.DocumentModel', 8, function() { |
206 | 206 | var documentModel = es.DocumentModel.newFromPlainObject( obj ); |
207 | 207 | |
208 | 208 | deepEqual( documentModel.getData(), data, 'Flattening plain objects results in correct data' ); |
— | — | @@ -212,7 +212,7 @@ |
213 | 213 | ['b', { 'type': 'bold', 'hash': '#bold' }], |
214 | 214 | ['c', { 'type': 'italic', 'hash': '#italic' }] |
215 | 215 | ], |
216 | | - 'Content can be extracted from nodes using relative ranges' |
| 216 | + 'When getting content for a node, ranges can trim left' |
217 | 217 | ); |
218 | 218 | deepEqual( |
219 | 219 | documentModel[0].getContent( new es.Range( 0, 2 ) ), |
— | — | @@ -220,7 +220,30 @@ |
221 | 221 | 'a', |
222 | 222 | ['b', { 'type': 'bold', 'hash': '#bold' }], |
223 | 223 | ], |
224 | | - 'Content can be extracted from nodes using relative ranges' |
| 224 | + 'When getting content for a node, ranges can trim right' |
225 | 225 | ); |
| 226 | + deepEqual( |
| 227 | + documentModel[0].getContent( new es.Range( 1, 2 ) ), |
| 228 | + [ |
| 229 | + ['b', { 'type': 'bold', 'hash': '#bold' }], |
| 230 | + ], |
| 231 | + 'When getting content for a node, ranges can trim left and right' |
| 232 | + ); |
| 233 | + try { |
| 234 | + documentModel[0].getContent( new es.Range( -1, 3 ) ); |
| 235 | + } catch ( err ) { |
| 236 | + ok( |
| 237 | + true, |
| 238 | + 'Exceptions are thrown when getting node content within a range starting before 0' |
| 239 | + ); |
| 240 | + } |
| 241 | + try { |
| 242 | + documentModel[0].getContent( new es.Range( 0, 4 ) ); |
| 243 | + } catch ( err ) { |
| 244 | + ok( |
| 245 | + true, |
| 246 | + 'Exceptions are thrown when getting node content within a range ending after length' |
| 247 | + ); |
| 248 | + } |
226 | 249 | deepEqual( documentModel[2].getContent(), ['a'], 'Content can be extracted from nodes' ); |
227 | 250 | } ); |
Index: trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js |
— | — | @@ -512,16 +512,25 @@ |
513 | 513 | * @returns {Array|null} List of content and elements inside node or null if node is not found |
514 | 514 | */ |
515 | 515 | es.DocumentModel.prototype.getContentFromNode = function( node, range ) { |
| 516 | + var length = node.getContentLength(); |
516 | 517 | if ( range ) { |
517 | 518 | range.normalize(); |
| 519 | + if ( range.start < 0 ) { |
| 520 | + throw 'Invalid range error. Range can not start before node start: ' + range.start; |
| 521 | + } |
| 522 | + if ( range.end > length ) { |
| 523 | + throw 'Invalid range error. Range can not end after node end: ' + range.end; |
| 524 | + } |
| 525 | + } else { |
| 526 | + range = { |
| 527 | + 'start': 0, |
| 528 | + 'end': length |
| 529 | + } |
518 | 530 | } |
519 | 531 | var offset = this.getOffsetFromNode( node ); |
520 | 532 | if ( offset !== -1 ) { |
521 | 533 | 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 ); |
| 534 | + return this.data.slice( offset + range.start, offset + range.end ); |
526 | 535 | } |
527 | 536 | return null; |
528 | 537 | }; |