r104427 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r104426‎ | r104427 | r104428 >
Date:14:00, 28 November 2011
Author:gwicke
Status:deferred
Tags:
Comment:
Keep going of the HTML parser fails to normalize the expected test outcome.
Minor code simplification, and recognition of tr, td and tbody as block-level
elements.
Modified paths:
  • /trunk/extensions/VisualEditor/tests/parser/parserTests.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/tests/parser/parserTests.js
@@ -131,17 +131,24 @@
132132 return $('<div>').append(node).html();
133133 }
134134
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+ */
138139 function normalizeHTML(source) {
139140 var parser = new HTML5.Parser();
140141 // TODO: Do not strip newlines in pre and nowiki blocks!
141142 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+
146153 }
147154
148155 // Specialized normalization of the parser output, mostly to ignore a few
@@ -153,14 +160,13 @@
154161
155162 function formatHTML ( source ) {
156163 // 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>');
159166 }
160167
161168
162169 function processTest(item) {
163170 var tokenizer = new FauxHTML5.Tokenizer();
164 - // ordinary HTML5 parser for DOM comparison
165171 if (!('title' in item)) {
166172 console.log(item);
167173 throw new Error('Missing title from test case.');
@@ -182,7 +188,7 @@
183189 console.log(item.input + "\n");
184190 }
185191
186 - parser.parseToTree(item.input + "\n", function(tree, err) {
 192+ parser.parseToTree(item.input + "\n", function(tokens, err) {
187193 if (err) {
188194 printTitle();
189195 console.log('PARSE FAIL', err);
@@ -193,39 +199,40 @@
194200 'references': MWReferencesTagHook
195201 }
196202 });
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);
199206 var out = tokenizer.parser.document
200207 .getElementsByTagName('body')[0]
201208 .innerHTML;
202209
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");
211217
212 - console.log('RAW RENDERED:');
213 - console.log(formatHTML(out) + "\n");
 218+ console.log('RAW RENDERED:');
 219+ console.log(formatHTML(out) + "\n");
214220
215 - var a = formatHTML(normalizeHTML( item.result ));
 221+ var a = formatHTML(normalizeHTML( item.result ));
216222
217 - console.log('NORMALIZED EXPECTED:');
218 - console.log(a + "\n");
 223+ console.log('NORMALIZED EXPECTED:');
 224+ console.log(a + "\n");
219225
220 - var b = formatHTML(normalizeOut( out ));
 226+ var b = formatHTML(normalizeOut( out ));
221227
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');
225231
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+ }
230237 }
231238 });
232239 }

Status & tagging log