solid-register
Version:
integrated package to run solid browser code in node
63 lines (62 loc) • 2.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const read_config_1 = require("./read-config");
const empty_page_1 = require("./empty-page");
const url = (typeof read_config_1.config.dom === "object" && read_config_1.config.dom.url) ||
"https://localhost:3000";
try {
const parseHTML = require("linkedom").parseHTML;
const { window, document, Node, HTMLElement, requestAnimationFrame, cancelAnimationFrame, navigator, } = parseHTML(empty_page_1.emptyPage);
window.location = new window.URL(url);
window.location.reload = () => { };
window.location.assign = (url) => {
window.location = new window.URL(url);
};
window.location.replace = window.location.assign;
window.getComputedStyle = (node, pseudoElt) => {
const collectedStyles = {
display: "block",
getPropertyValue: function (name) {
return name === "getPropertyValue" ? "" : this[name] ?? "";
},
};
const styleRules = {};
// parse styles
document.querySelectorAll("style").forEach((node) => {
node.innerHTML.replace(/(?:^|\}[\s\r\n]*)([^\{]+?)\s*\{[\r\n\s]*([^}]+)/g, (_, sel, attr) => {
if (Array.from(document.querySelectorAll(sel)).includes(node)) {
let specificy = 0;
sel.replace(/#\s+|\.\w+|\[[^\]]+\]|\w+/g, (part) => {
const firstChar = part.charAt(0);
specificy +=
firstChar === "#" ? 100000000 : firstChar === "." ? 10000 : 1;
return "";
});
attr.replace(/(?:^|;\s*)([^:]):\s*([^;}]+)/g, (_, name, value) => {
styleRules[name] = { ...styleRules[name], [specificy]: value };
return "";
});
}
return "";
});
});
Object.entries(styleRules).forEach(([name, values]) => {
collectedStyles[name] = Object.entries(values).reduce(([prevSpecificy, prevValue], [specificy, value]) => prevSpecificy > specificy
? [prevSpecificy, prevValue]
: [specificy, value], [0, collectedStyles[name]])[1];
});
return collectedStyles;
};
Object.assign(globalThis, {
window,
document,
Node,
HTMLElement,
requestAnimationFrame,
cancelAnimationFrame,
navigator,
});
}
catch (e) {
console.warn("\x1b[33m⚠️ package missing to run the configured dom.\n Please run:\x1b[0m\n\nnpm i --save-dev linkedom\n");
}