UNPKG

@tko/provider.mustache

Version:

Interpolate text/node attributes {{ }}

43 lines (42 loc) 1.15 kB
"use strict"; // @tko/provider.mustache 🥊 4.0.0-beta1.6 ESM import { Provider } from "@tko/provider"; import { parseInterpolation } from "./mustacheParser"; export default class TextMustacheProvider extends Provider { get FOR_NODE_TYPES() { return [3]; } *textToNodes(textNode) { const parent = textNode.parentNode; const isTextarea = parent && parent.nodeName === "TEXTAREA"; const hasStash = textNode.nodeValue && textNode.nodeValue.includes("{{"); if (!hasStash || isTextarea) { return; } for (const part of parseInterpolation(textNode.nodeValue)) { yield* part.textNodeReplacement(textNode); } } textInterpolation(textNode) { const newNodes = Array.from(this.textToNodes(textNode)); if (newNodes.length === 0) { return; } if (textNode.parentNode) { const parent = textNode.parentNode; const n = newNodes.length; for (let i = 0; i < n; ++i) { parent.insertBefore(newNodes[i], textNode); } parent.removeChild(textNode); } return newNodes; } preprocessNode(node) { return this.textInterpolation(node); } }