@maizzle/framework
Version:
Maizzle is a framework that helps you quickly build HTML emails with Tailwind CSS.
46 lines (45 loc) • 1.52 kB
JavaScript
import render from "dom-serializer";
//#region src/utils/ast/serializer.ts
const RAW_TEXT_ELEMENTS = new Set(["script", "style"]);
/**
* Re-encode `<` and `>` in text nodes before serialization.
*
* The parser decodes entities like `<` into raw `<` in text nodes. With
* `encodeEntities: false` (needed so other transformers can emit entity
* strings such as ` ` verbatim), the serializer would write those raw
* `<` characters as-is, turning escaped text (e.g. a Vue `{{ html }}`
* interpolation containing `<p>...</p>`) into real DOM downstream.
*
* `&` is intentionally not re-encoded: the `entities` transformer writes
* literal entity strings (` `, `—`) directly into text-node data
* and relies on the serializer leaving them alone.
*
* Skip `<script>` / `<style>` — their children are raw-text containers, and
* CSS/JS legitimately contains `<` and `>`.
*/
function encodeTextNodes(dom, inRawText = false) {
for (const node of dom) {
if (node.type === "text") {
if (!inRawText) {
const text = node;
text.data = text.data.replace(/</g, "<").replace(/>/g, ">");
}
continue;
}
if ("children" in node && node.children?.length) {
const el = node;
const nextRaw = inRawText || RAW_TEXT_ELEMENTS.has(el.name);
encodeTextNodes(el.children, nextRaw);
}
}
}
function serialize(dom, options) {
encodeTextNodes(dom);
return render(dom, {
encodeEntities: false,
...options
});
}
//#endregion
export { serialize };
//# sourceMappingURL=serializer.js.map