Index: trunk/parsers/wikidom/tests/annotations/test.js |
— | — | @@ -2,6 +2,37 @@ |
3 | 3 | |
4 | 4 | /* Functions */ |
5 | 5 | |
| 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 | + |
6 | 37 | function diff( a, b ) { |
7 | 38 | var result = []; |
8 | 39 | for ( var i = 0; i < b.length; i++ ) { |
— | — | @@ -22,11 +53,11 @@ |
23 | 54 | return ''; |
24 | 55 | } |
25 | 56 | |
26 | | -function renderText( text, renderedAnnotations ) { |
| 57 | +function renderText( text, charAnnotations ) { |
27 | 58 | var out = ''; |
28 | 59 | var left = []; |
29 | 60 | for (i in text) { |
30 | | - var right = renderedAnnotations[i] || []; |
| 61 | + var right = charAnnotations[i] || []; |
31 | 62 | out += openAnnotations( diff( left, right ) ); |
32 | 63 | out += text[i]; |
33 | 64 | out += closeAnnotations( diff( right, left ) ); |
— | — | @@ -39,7 +70,7 @@ |
40 | 71 | |
41 | 72 | var lines = [ |
42 | 73 | { |
43 | | - "text": "This is a test paragraph!", |
| 74 | + "text": "This is a test paragraph!\n", |
44 | 75 | "annotations": [ |
45 | 76 | { |
46 | 77 | "type": "italic", |
— | — | @@ -68,7 +99,7 @@ |
69 | 100 | ] |
70 | 101 | }, |
71 | 102 | { |
72 | | - "text": "Paragraphs can have more than one line.", |
| 103 | + "text": "Paragraphs can have more than one line.\n", |
73 | 104 | "annotations": [ |
74 | 105 | { |
75 | 106 | "type": "italic", |
— | — | @@ -95,19 +126,19 @@ |
96 | 127 | for ( var j in line.annotations ) { |
97 | 128 | var annotation = line.annotations[j]; |
98 | 129 | 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 |
102 | 133 | line.charAnnotations[k].push( annotation ); |
103 | | - } |
| 134 | + } |
104 | 135 | } |
105 | 136 | } |
106 | 137 | } |
107 | 138 | |
| 139 | +/* Tests */ |
| 140 | + |
108 | 141 | convertAnnotations( lines ); |
109 | 142 | |
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' ); |
114 | 145 | } ); |