Index: trunk/extensions/VisualEditor/tests/parser/parserTests.js |
— | — | @@ -131,17 +131,24 @@ |
132 | 132 | return $('<div>').append(node).html(); |
133 | 133 | } |
134 | 134 | |
135 | | -// Normalize the expected parser output by parsing it using a HTML5 parser and |
136 | | -// re-serializing it to HTML. Ideally, the parser would normalize inter-tag |
137 | | -// whitespace for us. For now, we fake that by simply stripping all newlines. |
| 135 | +/* Normalize the expected parser output by parsing it using a HTML5 parser and |
| 136 | + * re-serializing it to HTML. Ideally, the parser would normalize inter-tag |
| 137 | + * whitespace for us. For now, we fake that by simply stripping all newlines. |
| 138 | + */ |
138 | 139 | function normalizeHTML(source) { |
139 | 140 | var parser = new HTML5.Parser(); |
140 | 141 | // TODO: Do not strip newlines in pre and nowiki blocks! |
141 | 142 | source = source.replace(/\n/g, ''); |
142 | | - parser.parse('<body>' + source + '</body>'); |
143 | | - return parser.document |
144 | | - .getElementsByTagName('body')[0] |
145 | | - .innerHTML; |
| 143 | + try { |
| 144 | + parser.parse('<body>' + source + '</body>'); |
| 145 | + return parser.document |
| 146 | + .getElementsByTagName('body')[0] |
| 147 | + .innerHTML; |
| 148 | + } catch(e) { |
| 149 | + console.log("normalizeHTML failed:" + e); |
| 150 | + return source; |
| 151 | + } |
| 152 | + |
146 | 153 | } |
147 | 154 | |
148 | 155 | // Specialized normalization of the parser output, mostly to ignore a few |
— | — | @@ -153,14 +160,13 @@ |
154 | 161 | |
155 | 162 | function formatHTML ( source ) { |
156 | 163 | // Quick hack to insert newlines before block level start tags! |
157 | | - return source.replace(/(.)<((dd|dt|li|p|table|dl|ol|ul)[^>]*)>/g, |
158 | | - '$1\n<$2>'); |
| 164 | + return source.replace(/(?!^)<((dd|dt|li|p|table|tr|td|tbody|dl|ol|ul)[^>]*)>/g, |
| 165 | + '\n<$1>'); |
159 | 166 | } |
160 | 167 | |
161 | 168 | |
162 | 169 | function processTest(item) { |
163 | 170 | var tokenizer = new FauxHTML5.Tokenizer(); |
164 | | - // ordinary HTML5 parser for DOM comparison |
165 | 171 | if (!('title' in item)) { |
166 | 172 | console.log(item); |
167 | 173 | throw new Error('Missing title from test case.'); |
— | — | @@ -182,7 +188,7 @@ |
183 | 189 | console.log(item.input + "\n"); |
184 | 190 | } |
185 | 191 | |
186 | | - parser.parseToTree(item.input + "\n", function(tree, err) { |
| 192 | + parser.parseToTree(item.input + "\n", function(tokens, err) { |
187 | 193 | if (err) { |
188 | 194 | printTitle(); |
189 | 195 | console.log('PARSE FAIL', err); |
— | — | @@ -193,39 +199,40 @@ |
194 | 200 | 'references': MWReferencesTagHook |
195 | 201 | } |
196 | 202 | }); |
197 | | - //var res = es.HtmlSerializer.stringify(tree,environment); |
198 | | - processTokens(tree, tokenizer); |
| 203 | + //var res = es.HtmlSerializer.stringify(tokens,environment); |
| 204 | + //console.log(JSON.stringify(tokens)); |
| 205 | + processTokens(tokens, tokenizer); |
199 | 206 | var out = tokenizer.parser.document |
200 | 207 | .getElementsByTagName('body')[0] |
201 | 208 | .innerHTML; |
202 | 209 | |
203 | | - if (normalizeOut(out) !== normalizeHTML( item.result ) || |
204 | | - err ) { |
205 | | - printTitle(); |
206 | | - if (err) { |
207 | | - console.log('RENDER FAIL', err); |
208 | | - } else { |
209 | | - console.log('RAW EXPECTED:'); |
210 | | - console.log(item.result + "\n"); |
| 210 | + if ( err ) { |
| 211 | + printTitle(); |
| 212 | + console.log('RENDER FAIL', err); |
| 213 | + } else if ( normalizeOut(out) !== normalizeHTML(item.result) ) { |
| 214 | + printTitle(); |
| 215 | + console.log('RAW EXPECTED:'); |
| 216 | + console.log(item.result + "\n"); |
211 | 217 | |
212 | | - console.log('RAW RENDERED:'); |
213 | | - console.log(formatHTML(out) + "\n"); |
| 218 | + console.log('RAW RENDERED:'); |
| 219 | + console.log(formatHTML(out) + "\n"); |
214 | 220 | |
215 | | - var a = formatHTML(normalizeHTML( item.result )); |
| 221 | + var a = formatHTML(normalizeHTML( item.result )); |
216 | 222 | |
217 | | - console.log('NORMALIZED EXPECTED:'); |
218 | | - console.log(a + "\n"); |
| 223 | + console.log('NORMALIZED EXPECTED:'); |
| 224 | + console.log(a + "\n"); |
219 | 225 | |
220 | | - var b = formatHTML(normalizeOut( out )); |
| 226 | + var b = formatHTML(normalizeOut( out )); |
221 | 227 | |
222 | | - console.log('NORMALIZED RENDERED:') |
223 | | - console.log(formatHTML(normalizeOut(out)) + "\n"); |
224 | | - var patch = jsDiff.createPatch('wikitext.txt', a, b, 'before', 'after'); |
| 228 | + console.log('NORMALIZED RENDERED:') |
| 229 | + console.log(formatHTML(normalizeOut(out)) + "\n"); |
| 230 | + var patch = jsDiff.createPatch('wikitext.txt', a, b, 'before', 'after'); |
225 | 231 | |
226 | | - console.log('DIFF:'); |
227 | | - console.log(patch.replace(/^[^\n]*\n[^\n]*\n[^\n]*\n[^\n]*\n/, '')); |
228 | | - } |
229 | | - } |
| 232 | + console.log('DIFF:'); |
| 233 | + console.log(patch.replace(/^[^\n]*\n[^\n]*\n[^\n]*\n[^\n]*\n/, '')); |
| 234 | + } else { |
| 235 | + console.log( 'PASS: ' + item.title ); |
| 236 | + } |
230 | 237 | } |
231 | 238 | }); |
232 | 239 | } |