-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhtest.js
73 lines (59 loc) · 1.54 KB
/
htest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
{ // Careful: this is NOT run in module context!
let currentPage;
if (/\/$/.test(location.pathname)) {
currentPage = "index";
}
else {
currentPage = (location.pathname.match(/\/([a-z-]+)(?:\.html|\/?$)/) || [, "index"])[1];
}
document.documentElement.style.setProperty("--page", `"${currentPage}"`);
let isIndex = document.documentElement.classList.contains("index");
let loaded = import(`./src/html/${isIndex? "harness" : "testpage"}.js`);
let util;
async function ready (doc = document) {
await new Promise(resolve => {
if (doc.readyState !== "loading") {
resolve();
}
else {
doc.addEventListener("DOMContentLoaded", resolve, {once: true});
}
});
await Promise.all([
loaded,
import("./src/html/util.js").then(m => util = m)
]);
}
/**
* Global functions to be available to tests
*/
async function $out (...texts) {
var script = this instanceof HTMLElement && this.matches("script")? this : document.currentScript;
for (let text of texts) {
if (typeof text === "function") {
await ready();
try {
text = text();
}
catch (err) {
text = `<div onclick="console.log(\`${err.stack}\`)">${err}</div>`;
}
}
text = util.output(text);
if (document.readyState == "loading") {
document.write(text);
}
else if (script && script.parentNode) {
script.insertAdjacentHTML("afterend", text);
}
else {
console.log(script, script.parentNode)
console.log("Test print", text);
}
}
}
function $outln (...text) {
$out(...text, " ", document.createElement("br"));
}
Object.assign(globalThis, {$out, $outln});
}