UNPKG

2.21 kBJavaScriptView Raw
1'use strict';
2var domino = require('../');
3var html5lib_tests = require('./html5lib-tests.json');
4
5// These test cases are taken from the `html5lib/html5lib-tests` package
6// on github, in the directory `tree-construction`. The filename in that
7// directory is the name of each suite of tests.
8
9function cases(filename, tc) {
10 return tc.filter(function(test) {
11 // We don't support some of these test cases...
12 if (test.fragment && test.fragment.ns) { return false; }
13 // Scripting is always enabled in domino.
14 if (test.script === 'off') { return false; }
15 return true;
16 }).reduce(function(r, test) {
17 var input = test.data, expected = test.document.html,
18 fragment = test.fragment && test.fragment.name;
19 // Come up with a helpful name for the testcase.
20 var trimmed = input, n, candidate;
21 if (trimmed==='') { trimmed = '{no input}'; }
22 if (fragment) { trimmed = fragment + ':' + trimmed; }
23 if (r[trimmed]) {
24 //console.warn("Duplicate test in "+filename+": "+trimmed);
25 }
26 for (n = 40; n < trimmed.length; n += 5) {
27 candidate = trimmed.slice(0, n) + '...';
28 if (!r[candidate]) { trimmed = candidate; break; }
29 }
30 if (/\n/.test(trimmed)) {
31 candidate = trimmed.split(/\n/)[0] + '...';
32 if (!r[candidate]) { trimmed = candidate; }
33 }
34 r[trimmed] = makeOneTest(fragment, input, expected);
35 return r;
36 }, {});
37}
38
39function makeOneTest(fragment, input, expected) {
40 return function() {
41 var doc, context;
42 if (fragment) {
43 doc = domino.createDocument();
44 context = (fragment==='body') ? doc.body : doc.createElement(fragment);
45 context.innerHTML = input;
46 context.innerHTML.should.equal(expected);
47 } else {
48 doc = domino.createDocument(input, true);
49 doc.outerHTML.should.equal(expected);
50 }
51 };
52}
53
54exports.parseAlgorithm = Object.keys(html5lib_tests).reduce(function(r, file) {
55 r[file] = cases(file, html5lib_tests[file]);
56 return r;
57}, {});
58
59// Some extra tests.
60
61// https://github.com/html5lib/html5lib-tests/issues/20
62exports.parseAlgorithm['github issue #20'] = {
63 'test1': makeOneTest(
64 'body', '<table><li><li></table>', '<li></li><li></li><table></table>'
65 )
66};