r99127 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99126‎ | r99127 | r99128 >
Date:18:30, 6 October 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Fixed use of the range parameter for getContent
Modified paths:
  • /trunk/parsers/wikidom/lib/hype/es.Range.js (modified) (history)
  • /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
@@ -205,5 +205,21 @@
206206
207207 deepEqual( documentModel.getData(), data, 'Flattening plain objects results in correct data' );
208208 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' );
210226 } );
Index: trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js
@@ -514,15 +514,14 @@
515515 es.DocumentModel.prototype.getContent = function( node, range ) {
516516 if ( range ) {
517517 range.normalize();
518 - } else {
519 - range = {
520 - 'start': 0,
521 - 'end': this.contentLength
522 - };
523518 }
524519 var offset = this.getOffsetFromNode( node );
525520 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 );
527526 }
528527 return null;
529528 };
Index: trunk/parsers/wikidom/lib/hype/es.Range.js
@@ -16,6 +16,19 @@
1717 this.normalize();
1818 };
1919
 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+
2033 /* Methods */
2134
2235 /**

Status & tagging log