r91771 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91770‎ | r91771 | r91772 >
Date:23:50, 8 July 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Added multiline substring function
Modified paths:
  • /trunk/parsers/wikidom/tests/annotations/test.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/tests/annotations/test.js
@@ -2,6 +2,37 @@
33
44 /* Functions */
55
 6+function multiLineSubstring( lines, start, end ) {
 7+ var result = {
 8+ 'text': '',
 9+ 'charAnnotations': []
 10+ },
 11+ line,
 12+ left = 0,
 13+ right,
 14+ from,
 15+ to;
 16+ for ( var l = 0; l < lines.length; l++ ) {
 17+ line = lines[l];
 18+ right = left + line.text.length;
 19+ if ( start >= left && start < right ) {
 20+ from = start - left;
 21+ to = end < right ? end - left : line.text.length;
 22+ } else if ( from !== undefined && to !== undefined ) {
 23+ from = 0;
 24+ to = end < right ? end - left : line.text.length;
 25+ }
 26+ if ( from !== undefined && to !== undefined ) {
 27+ result.text += line.text.substring( from, to );
 28+ for ( var c = from; c < to; c++ ) {
 29+ line.charAnnotations[c] && ( result.charAnnotations[left + c] = line.charAnnotations[c] );
 30+ }
 31+ }
 32+ left = right;
 33+ }
 34+ return result;
 35+}
 36+
637 function diff( a, b ) {
738 var result = [];
839 for ( var i = 0; i < b.length; i++ ) {
@@ -22,11 +53,11 @@
2354 return '';
2455 }
2556
26 -function renderText( text, renderedAnnotations ) {
 57+function renderText( text, charAnnotations ) {
2758 var out = '';
2859 var left = [];
2960 for (i in text) {
30 - var right = renderedAnnotations[i] || [];
 61+ var right = charAnnotations[i] || [];
3162 out += openAnnotations( diff( left, right ) );
3263 out += text[i];
3364 out += closeAnnotations( diff( right, left ) );
@@ -39,7 +70,7 @@
4071
4172 var lines = [
4273 {
43 - "text": "This is a test paragraph!",
 74+ "text": "This is a test paragraph!\n",
4475 "annotations": [
4576 {
4677 "type": "italic",
@@ -68,7 +99,7 @@
69100 ]
70101 },
71102 {
72 - "text": "Paragraphs can have more than one line.",
 103+ "text": "Paragraphs can have more than one line.\n",
73104 "annotations": [
74105 {
75106 "type": "italic",
@@ -95,19 +126,19 @@
96127 for ( var j in line.annotations ) {
97128 var annotation = line.annotations[j];
98129 for ( var k = annotation.range.start; k <= annotation.range.stop; k++ ) {
99 - if ( !line.charAnnotations[k] ) {
100 - line.charAnnotations[k] = [];
101 - }
 130+ // Auto initialize
 131+ line.charAnnotations[k] || ( line.charAnnotations[k] = [] );
 132+ // Append
102133 line.charAnnotations[k].push( annotation );
103 - }
 134+ }
104135 }
105136 }
106137 }
107138
 139+/* Tests */
 140+
108141 convertAnnotations( lines );
109142
110 -/* Tests */
111 -
112 -test( 'Dummy test', function() {
113 - equals( 1, 1 );
 143+test( 'Multiline substrings produce correct plain text', function() {
 144+ equals( multiLineSubstring( lines, 3, 39 ).text, 's is a test paragraph!\nParagraphs ca' );
114145 } );

Status & tagging log