r91933 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91932‎ | r91933 | r91934 >
Date:00:30, 12 July 2011
Author:inez
Status:deferred
Tags:
Comment:
Added caching for line width based on offset and length as a key. Also fixed escape method.
Modified paths:
  • /trunk/parsers/wikidom/lib/es/es.TextFlow.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/es/es.TextFlow.js
@@ -11,6 +11,7 @@
1212 this.lines = [];
1313 this.width = null;
1414 this.boundaryTest = /([ \-\t\r\n\f])/g;
 15+ this.widthCache = {};
1516 }
1617
1718 /**
@@ -20,7 +21,7 @@
2122 * @return {String} HTML escaped text
2223 */
2324 TextFlow.prototype.escape = function( start, end ) {
24 - return text
 25+ return this.content.substring( start, end )
2526 // Tags
2627 .replace( /&/g, '&' )
2728 .replace( /</g, '&lt;' )
@@ -214,6 +215,8 @@
215216 * @param callback {Function} Function to execute when flowing is complete
216217 */
217218 TextFlow.prototype.render = function( offset, callback ) {
 219+ this.widthCache = {};
 220+
218221 // Reset lines in the DOM and the "lines" array
219222 this.$.empty();
220223
@@ -331,14 +334,21 @@
332335 TextFlow.prototype.fitWords = function( start, end, ruler, width ) {
333336 var offset = start,
334337 middle,
335 - lineWidth;
 338+ lineWidth,
 339+ cacheKey;
336340 do {
337341 // Place "middle" directly in the center of "start" and "end"
338342 middle = Math.ceil( ( start + end ) / 2 );
 343+
 344+ cacheKey = this.boundaries[offset] + ':' + this.boundaries[middle];
 345+
339346 // Prepare the line for measurement using pre-escaped HTML
340347 ruler.innerHTML = this.escape( this.boundaries[offset], this.boundaries[middle] );
341348 // Test for over/under using width of the rendered line
342 - if ( ( lineWidth = ruler.clientWidth ) > width ) {
 349+ this.widthCache[cacheKey] = lineWidth = ruler.clientWidth;
 350+
 351+ // Test for over/under using width of the rendered line
 352+ if ( lineWidth > width ) {
343353 // Detect impossible fit (the first word won't fit by itself)
344354 if (middle - offset === 1) {
345355 start = middle;
@@ -370,14 +380,24 @@
371381 TextFlow.prototype.fitCharacters = function( start, end, ruler, width ) {
372382 var offset = start,
373383 middle,
374 - lineWidth;
 384+ lineWidth,
 385+ cacheKey;
375386 do {
376387 // Place "middle" directly in the center of "start" and "end"
377388 middle = Math.ceil( ( start + end ) / 2 );
378 - // Fill the line with a portion of the text, escaped as HTML
379 - ruler.innerHTML = this.escape( offset, middle );
380 - // Test for over/under using width of the rendered line
381 - if ( ( lineWidth = ruler.clientWidth ) > width ) {
 389+
 390+ cacheKey = offset + ':' + middle;
 391+
 392+ if ( cacheKey in this.widthCache ) {
 393+ lineWidth = this.widthCache[cacheKey];
 394+ } else {
 395+ // Fill the line with a portion of the text, escaped as HTML
 396+ ruler.innerHTML = this.escape( offset, middle );
 397+ // Test for over/under using width of the rendered line
 398+ this.widthCache[cacheKey] = lineWidth = ruler.clientWidth;
 399+ }
 400+
 401+ if ( lineWidth > width ) {
382402 // Detect impossible fit (the first character won't fit by itself)
383403 if (middle - offset === 1) {
384404 start = middle - 1;

Status & tagging log