@popeindustries/lit-html-server
Version:
Efficiently render streaming lit-html templates on the server (or in a ServiceWorker!)
97 lines (95 loc) • 2.47 kB
JavaScript
// src/internal/escape.js
var HTML_ESCAPES = {
'"': """,
"'": "'",
"&": "&",
"<": "<",
">": ">"
};
var RE_HTML = /["'&<>]/g;
var RE_SCRIPT_STYLE_TAG = /<\/(script|style)/gi;
function escape(string, context = "text") {
switch (context) {
case "script":
case "style":
return string.replace(RE_SCRIPT_STYLE_TAG, "<\\/$1").replace(/<!--/g, "\\x3C!--");
case "attribute":
case "text":
default:
return string.replace(RE_HTML, (match) => HTML_ESCAPES[match]);
}
}
// src/internal/element-renderer.js
var ElementRenderer = class {
/**
* @param { CustomElement } ceClass
* @param { string } tagName
*/
static matchesClass(ceClass, tagName) {
return false;
}
/**
* @param { string } tagName
*/
constructor(tagName) {
this.tagName = tagName;
this.element;
this.observedAttributes = [];
}
connectedCallback() {
var _a, _b;
this.observedAttributes = /** @type { Array<string> } */
this.element.constructor.observedAttributes ?? [];
(_b = (_a = this.element).connectedCallback) == null ? void 0 : _b.call(_a);
}
/**
* @param { string } name
* @param { string | null } oldValue
* @param { string | null } newValue
*/
attributeChangedCallback(name, oldValue, newValue) {
}
/**
* @param { string } name
* @param { unknown } value
*/
setProperty(name, value) {
this.element[name] = value;
}
/**
* @param { string } name
* @param { string } value
*/
setAttribute(name, value) {
const oldValue = this.element.getAttribute(name);
this.element.setAttribute(name, value);
if (this.observedAttributes.includes(name)) {
this.attributeChangedCallback(name, oldValue, value);
}
}
renderAttributes() {
let attributes = "";
for (const { name, value } of this.element.attributes) {
if (value === "" || value === void 0 || value === null) {
attributes += ` ${name}`;
} else {
attributes += ` ${name}="${escape(value, "attribute")}"`;
}
}
return attributes;
}
renderStyles() {
return "";
}
/**
* @returns { TemplateResult | string | null }
*/
render() {
var _a, _b, _c;
const innerHTML = ((_a = this.element.shadowRoot) == null ? void 0 : _a.innerHTML) || this.element.innerHTML;
return innerHTML || (((_c = (_b = this.element).render) == null ? void 0 : _c.call(_b)) ?? null);
}
};
export {
ElementRenderer
};