r99129 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99128‎ | r99129 | r99130 >
Date:18:54, 6 October 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Made asking for content within invalid ranges throw exceptions
Modified paths:
  • /trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.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
@@ -201,7 +201,7 @@
202202 new es.ParagraphModel( data[25], 1 )
203203 ];
204204
205 -test( 'es.DocumentModel', function() {
 205+test( 'es.DocumentModel', 8, function() {
206206 var documentModel = es.DocumentModel.newFromPlainObject( obj );
207207
208208 deepEqual( documentModel.getData(), data, 'Flattening plain objects results in correct data' );
@@ -212,7 +212,7 @@
213213 ['b', { 'type': 'bold', 'hash': '#bold' }],
214214 ['c', { 'type': 'italic', 'hash': '#italic' }]
215215 ],
216 - 'Content can be extracted from nodes using relative ranges'
 216+ 'When getting content for a node, ranges can trim left'
217217 );
218218 deepEqual(
219219 documentModel[0].getContent( new es.Range( 0, 2 ) ),
@@ -220,7 +220,30 @@
221221 'a',
222222 ['b', { 'type': 'bold', 'hash': '#bold' }],
223223 ],
224 - 'Content can be extracted from nodes using relative ranges'
 224+ 'When getting content for a node, ranges can trim right'
225225 );
 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+ }
226249 deepEqual( documentModel[2].getContent(), ['a'], 'Content can be extracted from nodes' );
227250 } );
Index: trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js
@@ -512,16 +512,25 @@
513513 * @returns {Array|null} List of content and elements inside node or null if node is not found
514514 */
515515 es.DocumentModel.prototype.getContentFromNode = function( node, range ) {
 516+ var length = node.getContentLength();
516517 if ( range ) {
517518 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+ }
518530 }
519531 var offset = this.getOffsetFromNode( node );
520532 if ( offset !== -1 ) {
521533 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 );
526535 }
527536 return null;
528537 };

Status & tagging log