You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
4.6 KiB
136 lines
4.6 KiB
#! /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 <path-to-wpt-base>");
|
|
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,
|
|
message: message,
|
|
};
|
|
prog();
|
|
window.genericTestSet.push(window.genericTest);
|
|
};
|
|
window.Sanitizer = function(conf) {
|
|
if (conf) {
|
|
if (Array.isArray(conf.dropAttributes)) {
|
|
var o = {};
|
|
conf.dropAttributes.forEach(function(v) {
|
|
|
|
o[v.name] = (v.elements === "*") ? ["*"] : v.elements;
|
|
});
|
|
conf.dropAttributes = o;
|
|
}
|
|
if (Array.isArray(conf.allowAttributes)) {
|
|
var o = {};
|
|
conf.allowAttributes.forEach(function(v) {
|
|
o[v.name] = (v.elements === "*") ? ["*"] : v.elements;
|
|
});
|
|
conf.allowAttributes = o;
|
|
}
|
|
}
|
|
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);
|
|
console.log(JSON.stringify(window.genericTestSet, null, 2));
|