26 lines
1.2 KiB
JavaScript
26 lines
1.2 KiB
JavaScript
function createDebugDom() {
|
|
const descendants = node => [node, ...node.children.flatMap(descendants)];
|
|
const document = { activeElement: null };
|
|
function element(tag) {
|
|
return {
|
|
tag, children: [], attributes: {}, listeners: {}, scrollTop: 0, hidden: false, text: "",
|
|
set textContent(value) { this.text = value; this.children = []; },
|
|
get textContent() { return this.text; },
|
|
append(...nodes) { this.children.push(...nodes); },
|
|
setAttribute(key, value) { this.attributes[key] = value; },
|
|
getAttribute(key) { return this.attributes[key]; },
|
|
addEventListener(name, listener) { this.listeners[name] = listener; },
|
|
focus() { document.activeElement = this; },
|
|
querySelectorAll(selector) {
|
|
return descendants(this).filter(node => node.tag === "details"
|
|
&& node.getAttribute("data-request-id") && (!selector.includes("[open]") || node.open));
|
|
}
|
|
};
|
|
}
|
|
const panel = element("aside");
|
|
Object.assign(document, { getElementById: () => panel, createElement: element, createElementNS: (_, tag) => element(tag) });
|
|
return { document, panel, descendants };
|
|
}
|
|
|
|
module.exports = { createDebugDom };
|