From 1fbc6a864184429c1ee115402161b716f26413e1 Mon Sep 17 00:00:00 2001 From: Jan Andrle Date: Thu, 24 Aug 2023 15:31:22 +0200 Subject: [PATCH] :sparkles: add namespace function --- src/dom.js | 29 ++++++++++++++--------------- test/index.js | 4 ++-- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/dom.js b/src/dom.js index 4ba060a..df2ad30 100644 --- a/src/dom.js +++ b/src/dom.js @@ -1,24 +1,23 @@ +let namespace_curr= "html"; +export function namespace(namespace){ + namespace_curr= namespace==="svg" ? "http://www.w3.org/2000/svg" : namespace; + return { + append(el){ namespace_curr= "html"; return el; } + }; +} export function createElement(tag, attributes, ...connect){ let el; switch(true){ - case typeof tag==="function": el= tag(attributes || undefined); break; - case tag==="<>": el= document.createDocumentFragment(); break; - case tag==="#text": el= assign(document.createTextNode(""), attributes); break; - default: el= assign(document.createElement(tag), attributes); + case typeof tag==="function": el= tag(attributes || undefined); break; + case tag==="<>": el= document.createDocumentFragment(); break; + case tag==="#text": el= assign(document.createTextNode(""), attributes); break; + case namespace_curr!=="html": el= assign(document.createElementNS(namespace_curr, tag), attributes); break; + default: el= assign(document.createElement(tag), attributes); } connect.forEach(c=> c(el)); return el; } export { createElement as el }; -export function createElementNS(tag, attributes, attributes_todo){ - let namespace= "svg"; - if(typeof attributes_todo !== "undefined"){ - namespace= tag; tag= attributes; attributes= attributes_todo; } - if(tag==="<>") return document.createDocumentFragment(); - if(tag==="") return document.createTextNode(attributes.textContent ?? attributes.innerText ?? attributes.innerHTML); - return assign(document.createElementNS(namespace==="svg" ? "http://www.w3.org/2000/svg" : namespace, tag), attributes); -} -export { createElementNS as elNS }; import { watch, isSignal } from './signals.js'; export function assign(element, ...attributes){ @@ -27,10 +26,10 @@ export function assign(element, ...attributes){ const setRemoveAttr= (is_svg ? setRemoveNS : setRemove).bind(null, element, "Attribute"); Object.entries(Object.assign({}, ...attributes)).forEach(function assignNth([ key, attr ]){ - if(key[0]==="=") return setRemoveAttr(key.slice(1), attr); - if(key[0]===".") return setDelete(element, key.slice(1), attr); if(isSignal(attr)) //TODO: unmounted return watch(()=> assignNth([ key, attr() ])); + if(key[0]==="=") return setRemoveAttr(key.slice(1), attr); + if(key[0]===".") return setDelete(element, key.slice(1), attr); if(typeof attr === "object"){ switch(key){ case "style": return forEachEntries(attr, setRemove.bind(null, element.style, "Property")) diff --git a/test/index.js b/test/index.js index 0e92d03..6d2978f 100644 --- a/test/index.js +++ b/test/index.js @@ -1,5 +1,5 @@ -import { S, watch, el, elNS, assign, listen, dispatch } from "../index.js"; -Object.assign(globalThis, { S, watch, el, elNS, assign, listen, dispatch }); +import { S, watch, el, namespace, assign, listen, dispatch } from "../index.js"; +Object.assign(globalThis, { S, watch, el, namespace, assign, listen, dispatch }); const { style, css }= createStyle(); globalThis.test= console.log;