From 009ed2a2d25bb979ab9997c26e83977f0543d5f4 Mon Sep 17 00:00:00 2001 From: macarc Date: Sun, 9 Jan 2022 16:15:04 +0000 Subject: [PATCH] fix: ensure SVG namespaces are correct when copying a thunk ISSUES CLOSED: #867 --- src/h.ts | 10 ++++++---- src/thunk.ts | 4 +++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/h.ts b/src/h.ts index c4eb0b2..351126c 100644 --- a/src/h.ts +++ b/src/h.ts @@ -13,17 +13,19 @@ export type VNodeChildElement = export type ArrayOrElement = T | T[]; export type VNodeChildren = ArrayOrElement; -function addNS( +export function addNS( data: any, - children: VNodes | undefined, + children: Array | undefined, sel: string | undefined ): void { data.ns = "http://www.w3.org/2000/svg"; if (sel !== "foreignObject" && children !== undefined) { for (let i = 0; i < children.length; ++i) { - const childData = children[i].data; + const child = children[i]; + if (typeof child === "string") continue; + const childData = child.data; if (childData !== undefined) { - addNS(childData, children[i].children as VNodes, children[i].sel); + addNS(childData, child.children as VNodes, child.sel); } } } diff --git a/src/thunk.ts b/src/thunk.ts index a057405..3770043 100644 --- a/src/thunk.ts +++ b/src/thunk.ts @@ -1,5 +1,5 @@ import { VNode, VNodeData } from "./vnode"; -import { h } from "./h"; +import { h, addNS } from "./h"; export interface ThunkData extends VNodeData { fn: () => VNode; @@ -16,12 +16,14 @@ export interface ThunkFn { } function copyToThunk(vnode: VNode, thunk: VNode): void { + const ns = thunk.data ? thunk.data.ns : null; (vnode.data as VNodeData).fn = (thunk.data as VNodeData).fn; (vnode.data as VNodeData).args = (thunk.data as VNodeData).args; thunk.data = vnode.data; thunk.children = vnode.children; thunk.text = vnode.text; thunk.elm = vnode.elm; + if (ns) addNS(thunk.data, thunk.children, thunk.sel); } function init(thunk: VNode): void {