Index: trunk/parsers/wikidom/lib/es/es.Content.js |
— | — | @@ -80,4 +80,85 @@ |
81 | 81 |
|
82 | 82 | Content.prototype.getLength = function() {
|
83 | 83 | 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, '&' )
|
| 150 | + .replace( /</g, '<' )
|
| 151 | + .replace( />/g, '>' )
|
| 152 | + // Quotes - probably not needed
|
| 153 | + //.replace( /'/g, ''' )
|
| 154 | + //.replace( /"/g, '"' )
|
| 155 | + // Whitespace
|
| 156 | + .replace( / /g, ' ' )
|
| 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 @@ |
16 | 16 | } |
17 | 17 | |
18 | 18 | /** |
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, '&' ) |
28 | | - .replace( /</g, '<' ) |
29 | | - .replace( />/g, '>' ) |
30 | | - // Quotes - probably not needed |
31 | | - //.replace( /'/g, ''' ) |
32 | | - //.replace( /"/g, '"' ) |
33 | | - // Whitespace |
34 | | - .replace( / /g, ' ' ) |
35 | | - .replace( /\n/g, '<span class="editSurface-whitespace">\\n</span>' ) |
36 | | - .replace( /\t/g, '<span class="editSurface-whitespace">\\t</span>' ); |
37 | | -}; |
38 | | - |
39 | | -/** |
40 | 19 | * Gets offset within content closest to of a given position. |
41 | 20 | * |
42 | | - * @param x {Integer} Horizontal position in pixels |
| 21 | + * @pthis.content.render {Integer} Horizontal position in pixels |
43 | 22 | * @param y {Integer} Vertical position in pixels |
44 | 23 | * @return {Integer} Offset within content nearest the given coordinates |
45 | 24 | */ |
— | — | @@ -81,9 +60,9 @@ |
82 | 61 | fit = this.fitCharacters( |
83 | 62 | this.lines[line].start, this.lines[line].end, ruler, position.left |
84 | 63 | ); |
85 | | - ruler.innerHTML = this.escape( this.lines[line].start, fit.end ); |
| 64 | + ruler.innerHTML = this.content.render( this.lines[line].start, fit.end ); |
86 | 65 | 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 ); |
88 | 67 | var right = ruler.clientWidth; |
89 | 68 | var center = Math.round( left + ( ( right - left ) / 2 ) ); |
90 | 69 | $ruler.remove(); |
— | — | @@ -159,7 +138,7 @@ |
160 | 139 | if ( this.lines[line].start < offset ) { |
161 | 140 | var $ruler = $( '<div class="editSurface-line"></div>' ).appendTo( this.$ ), |
162 | 141 | ruler = $ruler[0]; |
163 | | - ruler.innerHTML = this.escape( this.lines[line].start, offset ); |
| 142 | + ruler.innerHTML = this.content.render( this.lines[line].start, offset ); |
164 | 143 | position.left = ruler.clientWidth; |
165 | 144 | $ruler.remove(); |
166 | 145 | } |
— | — | @@ -297,7 +276,7 @@ |
298 | 277 | */ |
299 | 278 | TextFlow.prototype.appendLine = function( start, end ) { |
300 | 279 | $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>' ) |
302 | 281 | .appendTo( this.$ ); |
303 | 282 | // Collect line information |
304 | 283 | this.lines.push({ |
— | — | @@ -343,7 +322,7 @@ |
344 | 323 | cacheKey = this.boundaries[offset] + ':' + this.boundaries[middle]; |
345 | 324 | |
346 | 325 | // 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] ); |
348 | 327 | // Test for over/under using width of the rendered line |
349 | 328 | this.widthCache[cacheKey] = lineWidth = ruler.clientWidth; |
350 | 329 | |
— | — | @@ -392,7 +371,7 @@ |
393 | 372 | lineWidth = this.widthCache[cacheKey]; |
394 | 373 | } else { |
395 | 374 | // 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 ); |
397 | 376 | // Test for over/under using width of the rendered line |
398 | 377 | this.widthCache[cacheKey] = lineWidth = ruler.clientWidth; |
399 | 378 | } |