http://www.mediawiki.org/wiki/Special:Code/MediaWiki/89777
Revision: 89777
Author: neilk
Date: 2011-06-09 17:42:56 +0000 (Thu, 09 Jun 2011)
Log Message:
-----------
speed improvements for flow, using direct DOM methods
Modified Paths:
--------------
trunk/parsers/wikidom/lib/jquery.flow.js
Modified: trunk/parsers/wikidom/lib/jquery.flow.js
===================================================================
--- trunk/parsers/wikidom/lib/jquery.flow.js 2011-06-09 17:29:40 UTC (rev
89776)
+++ trunk/parsers/wikidom/lib/jquery.flow.js 2011-06-09 17:42:56 UTC (rev
89777)
@@ -8,18 +8,15 @@
console.time( 'flow' );
function encodeHtml( c ) {
- return c.replace( /&/g, '&' )
- .replace( / /g, ' ' )
+ return c.replace( /\&/g, '&' )
.replace( /</g, '<' )
- .replace( />/g, '>' )
- .replace( /\'/g, ''' )
- .replace( /"/g, '"' );
+ .replace( />/g, '>' );
}
var breakableRe = /[\s\r\n\f]/;
var $this = $(this);
-
+
$this.empty();
var width = $this.innerWidth();
@@ -31,6 +28,7 @@
var breakPos = pos;
var $line = $( '<div class="editSurface-line"></div>'
).appendTo( $this );
+ var lineElem = $line.get(0);
var lineText = '';
var lineMetrics = [];
var lineWidth = 0;
@@ -38,20 +36,24 @@
while ( pos < text.length && lineWidth < width ) {
// Append text
- lineText += text.charAt( pos );
- // Apply to DOM
- $line.html( encodeHtml( lineText ) );
+ var c = text.charAt( pos );
+ lineText += c;
+ lineElem.innerHTML = encodeHtml( lineText );
+
// Get new line width from DOM
lastLineWidth = lineWidth;
- lineWidth = $line.innerWidth();
+ // $.innerWidth call is expensive. Assume padding and
border = 0 and this should be okay
+ lineWidth = lineElem.offsetWidth;
// Push difference (character width)
lineMetrics.push( lineWidth - lastLineWidth );
- if ( breakableRe( text.charAt(pos) ) ) {
+
+ if ( breakableRe( c ) ) {
breakPos = pos;
}
+
pos++;
}
-
+
if ( lineWidth >= width ) {
if ( breakPos === lineStartPos ) {
// There was no breakable position between the
start of the line and here, so we
@@ -68,18 +70,18 @@
pos = breakPos;
// Truncate characters that won't fit
lineText = text.substring( lineStartPos, breakPos );
- $line.html( encodeHtml( lineText ) );
+ lineElem.innerHTML = encodeHtml( lineText );
// Don't leave metrics from truncated characters around
lineMetrics = lineMetrics.slice( 0, pos - lineStartPos
);
}
-
+
$line
.data( 'metrics', lineMetrics )
.data( 'text', lineText )
.data( 'line', line );
if ( lineStartPos === pos ) {
- $line.html( ' ' )
+ lineElem.innerHtml = ' ';
}
line++;
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs