MCPcopy Create free account
hub / github.com/bigskysoftware/_hyperscript / componentPlugin

Function componentPlugin

www/js/ext/component.esm.js:622–866  ·  view source on GitHub ↗
(_hyperscript)

Source from the content-addressed store, hash-verified

620
621// src/ext/component.js
622function componentPlugin(_hyperscript) {
623 const { runtime, createParser, reactivity } = _hyperscript.internals;
624 const tokenizer = new Tokenizer();
625 function ensureFouceGuard() {
626 if (typeof document === "undefined" || !document.head) return;
627 if (document.head.querySelector('style[data-hyperscript-component="fouce-guard"]')) return;
628 var styleEl = document.createElement("style");
629 styleEl.setAttribute("data-hyperscript-component", "fouce-guard");
630 styleEl.textContent = ":not(:defined) { visibility: hidden; }";
631 document.head.appendChild(styleEl);
632 }
633 function substituteSlots(templateSource, slotContent, scopeSel) {
634 if (!slotContent) return templateSource;
635 var tmp = document.createElement("div");
636 tmp.innerHTML = slotContent;
637 var named = {};
638 var defaultParts = [];
639 for (var child of Array.from(tmp.childNodes)) {
640 if (child.nodeType === 1 && scopeSel && !child.hasAttribute("dom-scope")) {
641 child.setAttribute("dom-scope", "parent of " + scopeSel);
642 }
643 var slotName = child.nodeType === 1 && child.getAttribute("slot");
644 if (slotName) {
645 child.removeAttribute("slot");
646 if (!named[slotName]) named[slotName] = "";
647 named[slotName] += child.outerHTML;
648 } else {
649 defaultParts.push(child.nodeType === 1 ? child.outerHTML : child.nodeType === 3 ? child.textContent : "");
650 }
651 }
652 var defaultContent = defaultParts.join("");
653 var source = templateSource.replace(
654 /<slot\s+name\s*=\s*["']([^"']+)["']\s*\/?\s*>(\s*<\/slot>)?/g,
655 function(_, name) {
656 return named[name] || "";
657 }
658 );
659 source = source.replace(/<slot\s*\/?\s*>(\s*<\/slot>)?/g, defaultContent);
660 return source;
661 }
662 function parseArg(componentEl, prop) {
663 if (typeof prop !== "string") return null;
664 var cache = componentEl._attrsCache || (componentEl._attrsCache = {});
665 if (!cache[prop]) {
666 var attrValue = componentEl.getAttribute(prop);
667 if (attrValue == null) return null;
668 try {
669 cache[prop] = createParser(tokenizer.tokenize(attrValue)).requireElement("expression");
670 } catch (e) {
671 console.error("component: failed to parse attrs." + prop + ":", e.message);
672 return null;
673 }
674 }
675 return cache[prop];
676 }
677 function parentContext(componentEl) {
678 var parent = componentEl.parentElement;
679 return parent ? runtime.makeContext(parent, null, parent, null) : null;

Callers

nothing calls this directly

Calls 6

ensureFouceGuardFunction · 0.70
registerTemplateFunction · 0.70
loadComponentBundleFunction · 0.70
addBeforeProcessHookMethod · 0.45
forEachMethod · 0.45
addAfterProcessHookMethod · 0.45

Tested by

no test coverage detected