#! /usr/bin/node /* Parses the sanitize() tests for the Sanitizer API into JSON for easier consumption by non-JavaScript environments. */ if (!process.argv[2]) { console.log("Usage: parsetests "); return 2; } const fs = require("fs"); const JSDOM = require("jsdom").JSDOM; // resolve the path to the tests var base = fs.realpathSync(process.argv[2]); // first read in the static test data var testprog = fs.readFileSync(base + "/sanitizer-api/support/testcases.sub.js").toString() + "\n\n"; // next extract the tests proper from the containing HTML file var testfile = fs.readFileSync(base + "/sanitizer-api/sanitizer-sanitize.https.tentative.html").toString(); testprog += (new JSDOM(testfile)).window.document.body.getElementsByTagName("script")[0].textContent; // now define a minimal test harness which will produce plain JSON test data var window = (new JSDOM("", {runScripts: "outside-only"})).window; window.genericTestSet = []; window.FakeTemplate = class { innerHTML = ""; get content() { return this; } }; window.origDOMParser = window.DOMParser; window.DOMParser = function() { return { parseFromString: function(data, type) { if (window.genericTest.input === undefined) { window.genericTest.input = [data]; } return (new window.origDOMParser).parseFromString(data, type); } }; }; window.document.origCreateElement = window.document.createElement; window.document.createElement = function(type) { if (type === "template") { return new window.FakeTemplate(); } return window.document.origCreateElement(type); }; window.test = function(prog, message) { // override the getFragment() and getDoc() functions to extract the input, if they haven't been overidden already if (!window.origGetFragment) { window.origGetFragment = window.getFragment; window.getFragment = function(i) { if (window.genericTest.input === undefined) { window.genericTest.input = [i]; } else if (window.genericTest.input && window.genericTest.output === undefined) { window.genericTest.output = i; } return window.origGetFragment(i); } } if (!window.origGetDoc) { window.origGetDoc = window.getDoc; window.getDoc = function(i) { if (window.genericTest.input === undefined) { window.genericTest.input = [i]; } return window.origGetDoc(i); } } window.genericTest = { config: undefined, input: undefined, inputType: undefined, output: undefined, exceptionType: undefined, negativeTest: false, message: message, }; prog(); window.genericTestSet.push(window.genericTest); }; window.Sanitizer = function(conf) { window.genericTest.config = conf || null; return { sanitize: function(dom) { if (dom instanceof window.DocumentFragment) { window.genericTest.inputType = "document-fragment"; } else if (dom instanceof window.Document) { window.genericTest.inputType = "document"; } else if (dom instanceof window.FakeTemplate) { window.genericTest.inputType = "template-content"; window.genericTest.input = dom.innerHTML; } else { window.genericTest.inputType = "literal"; if (window.genericTest.input === undefined) { window.genericTest.input = Array.from(arguments); } } return window.document.createDocumentFragment(); } }; }; window.assert_throws_js = function(t, f) { window.genericTest.exceptionType = t.prototype.toString(); window.genericTest.output = null; f(); }; window.assert_true = function() {}; window.assert_equals = function(result, exp) { window.genericTest.output = exp; }; // Finally execute the tests to produce our output window.eval(testprog); var out = window.genericTestSet; // Now do the same for namespace tests var window = (new JSDOM("", {runScripts: "outside-only"})).window; var testfile = fs.readFileSync(base + "/sanitizer-api/sanitizer-names.https.html").toString(); testprog = (new JSDOM(testfile)).window.document.body.getElementsByTagName("script")[0].textContent; window.genericTestSet = []; window.configTestSet = []; window.test = function(prog, message) { window.genericTest = { config: undefined, input: undefined, inputType: undefined, output: undefined, exceptionType: undefined, negativeTest: false, message: message, }; window.wrapOutput = false; prog(); }; window.Sanitizer = function(conf) { window.genericTest.config = conf || null; return { getConfiguration: function() { return window.genericTest.config || {}; } } }; window.assert_array_equals = function(act, exp) {}; window.document.origCreateElement = window.document.createElement; window.document.createElement = function(type) { if (type === "template") { return new window.FakeTemplate(); } return window.document.origCreateElement(type); }; window.FakeTemplate = class { innerHTML = ""; setHTML = function(str) { this.innerHTML = str; window.genericTest.input = str; } get content() { return this; } get firstElementChild() { window.wrapOutput = true; return this; } }; window.assert_equals = function(result, exp) { if (!window.wrapOutput) { window.genericTest.output = exp; } else { window.genericTest.output = "" + exp + ""; } window.genericTestSet.push(window.genericTest); }; window.assert_not_equals = function(result, exp) { if (!window.wrapOutput) { window.genericTest.output = exp; } else { window.genericTest.output = "" + exp + ""; } window.genericTest.negativeTest = true; window.genericTestSet.push(window.genericTest); }; window.eval(testprog); out = out.concat(window.genericTestSet); console.log(JSON.stringify(out, null, 2));