r91935 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91934‎ | r91935 | r91936 >
Date:01:07, 12 July 2011
Author:inez
Status:deferred
Tags:
Comment:
Move escape from TextFlow to Content and add support for basic formatting: italic and bold.
Modified paths:
  • /trunk/parsers/wikidom/lib/es/es.Content.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.TextFlow.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/es/es.Content.js
@@ -80,4 +80,85 @@
8181
8282 Content.prototype.getLength = function() {
8383 return this.data.length;
84 -};
\ No newline at end of file
 84+};
 85+
 86+Content.prototype.render = function( start, end ) {
 87+ if ( start || end ) {
 88+ return this.slice( start, end ).render();
 89+ }
 90+
 91+ // TODO: Find a better place for this function
 92+ function diff( a, b ) {
 93+ var result = [];
 94+ for ( var i = 1; i < b.length; i++ ) {
 95+ if ( a.indexOf( b[i] ) === -1 ) {
 96+ result.push( b[i] );
 97+ }
 98+ }
 99+ return result;
 100+ }
 101+
 102+ function openAnnotations( annotations ) {
 103+ var out = '';
 104+ for ( var i = 0; i < annotations.length; i++ ) {
 105+ switch (annotations[i].type) {
 106+ case 'bold':
 107+ out += '<b>';
 108+ break;
 109+ case 'italic':
 110+ out += '<i>';
 111+ break;
 112+ }
 113+ }
 114+ return out;
 115+ }
 116+
 117+ function closeAnnotations ( annotations ) {
 118+ var out = '';
 119+ for ( var i = 0; i < annotations.length; i++ ) {
 120+ switch (annotations[i].type) {
 121+ case 'bold':
 122+ out += '</b>';
 123+ break;
 124+ case 'italic':
 125+ out += '</i>';
 126+ break;
 127+ }
 128+ }
 129+ return out;
 130+ }
 131+
 132+ var left = [],
 133+ right,
 134+ out = '';
 135+
 136+ for ( var i = 0; i < this.data.length; i++ ) {
 137+ right = this.data[i] || [];
 138+
 139+ if ( typeof right == 'string' ) {
 140+ right = [right];
 141+ }
 142+
 143+ var diffout = diff( left, right );
 144+ //debugger;
 145+ out += openAnnotations( diffout );
 146+
 147+ out += right[0]
 148+ // Tags
 149+ .replace( /&/g, '&amp;' )
 150+ .replace( /</g, '&lt;' )
 151+ .replace( />/g, '&gt;' )
 152+ // Quotes - probably not needed
 153+ //.replace( /'/g, '&#039;' )
 154+ //.replace( /"/g, '&quot;' )
 155+ // Whitespace
 156+ .replace( / /g, '&nbsp;' )
 157+ .replace( /\n/g, '<span class="editSurface-whitespace">\\n</span>' )
 158+ .replace( /\t/g, '<span class="editSurface-whitespace">\\t</span>' );
 159+
 160+ out += closeAnnotations( diff( right, left ) );
 161+ left = right;
 162+ }
 163+
 164+ return out;
 165+}
Index: trunk/parsers/wikidom/lib/es/es.TextFlow.js
@@ -15,30 +15,9 @@
1616 }
1717
1818 /**
19 - * Encodes text as an HTML string.
20 - *
21 - * @param text {String} Text to escape
22 - * @return {String} HTML escaped text
23 - */
24 -TextFlow.prototype.escape = function( start, end ) {
25 - return this.content.substring( start, end )
26 - // Tags
27 - .replace( /&/g, '&amp;' )
28 - .replace( /</g, '&lt;' )
29 - .replace( />/g, '&gt;' )
30 - // Quotes - probably not needed
31 - //.replace( /'/g, '&#039;' )
32 - //.replace( /"/g, '&quot;' )
33 - // Whitespace
34 - .replace( / /g, '&nbsp;' )
35 - .replace( /\n/g, '<span class="editSurface-whitespace">\\n</span>' )
36 - .replace( /\t/g, '<span class="editSurface-whitespace">\\t</span>' );
37 -};
38 -
39 -/**
4019 * Gets offset within content closest to of a given position.
4120 *
42 - * @param x {Integer} Horizontal position in pixels
 21+ * @pthis.content.render {Integer} Horizontal position in pixels
4322 * @param y {Integer} Vertical position in pixels
4423 * @return {Integer} Offset within content nearest the given coordinates
4524 */
@@ -81,9 +60,9 @@
8261 fit = this.fitCharacters(
8362 this.lines[line].start, this.lines[line].end, ruler, position.left
8463 );
85 - ruler.innerHTML = this.escape( this.lines[line].start, fit.end );
 64+ ruler.innerHTML = this.content.render( this.lines[line].start, fit.end );
8665 var left = ruler.clientWidth;
87 - ruler.innerHTML = this.escape( this.lines[line].start, fit.end + 1 );
 66+ ruler.innerHTML = this.content.render( this.lines[line].start, fit.end + 1 );
8867 var right = ruler.clientWidth;
8968 var center = Math.round( left + ( ( right - left ) / 2 ) );
9069 $ruler.remove();
@@ -159,7 +138,7 @@
160139 if ( this.lines[line].start < offset ) {
161140 var $ruler = $( '<div class="editSurface-line"></div>' ).appendTo( this.$ ),
162141 ruler = $ruler[0];
163 - ruler.innerHTML = this.escape( this.lines[line].start, offset );
 142+ ruler.innerHTML = this.content.render( this.lines[line].start, offset );
164143 position.left = ruler.clientWidth;
165144 $ruler.remove();
166145 }
@@ -297,7 +276,7 @@
298277 */
299278 TextFlow.prototype.appendLine = function( start, end ) {
300279 $line = $( '<div class="editSurface-line" line-index="'
301 - + this.lines.length + '">' + this.escape( start, end ) + '</div>' )
 280+ + this.lines.length + '">' + this.content.render( start, end ) + '</div>' )
302281 .appendTo( this.$ );
303282 // Collect line information
304283 this.lines.push({
@@ -343,7 +322,7 @@
344323 cacheKey = this.boundaries[offset] + ':' + this.boundaries[middle];
345324
346325 // Prepare the line for measurement using pre-escaped HTML
347 - ruler.innerHTML = this.escape( this.boundaries[offset], this.boundaries[middle] );
 326+ ruler.innerHTML = this.content.render( this.boundaries[offset], this.boundaries[middle] );
348327 // Test for over/under using width of the rendered line
349328 this.widthCache[cacheKey] = lineWidth = ruler.clientWidth;
350329
@@ -392,7 +371,7 @@
393372 lineWidth = this.widthCache[cacheKey];
394373 } else {
395374 // Fill the line with a portion of the text, escaped as HTML
396 - ruler.innerHTML = this.escape( offset, middle );
 375+ ruler.innerHTML = this.content.render( offset, middle );
397376 // Test for over/under using width of the rendered line
398377 this.widthCache[cacheKey] = lineWidth = ruler.clientWidth;
399378 }

Status & tagging log