Skip to content

Commit 1877c2e

Browse files
committed
[fix] XML namespace fallback
1 parent 41a6fd2 commit 1877c2e

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dom-renderer",
3-
"version": "2.4.0",
3+
"version": "2.4.1",
44
"license": "LGPL-3.0-or-later",
55
"author": "[email protected]",
66
"description": "A light-weight DOM Renderer supports Web components standard & TypeScript language",

source/dist/DOMRenderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export class DOMRenderer {
183183
return newVNode;
184184
}
185185

186-
render(vNode: VNode, node: ParentNode = this.document.body) {
186+
render(vNode: VNode, node: ParentNode = globalThis.document?.body) {
187187
this.document = node.ownerDocument;
188188

189189
var root = this.treeCache.get(node) || VNode.fromDOM(node);

source/dist/VDOM.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,12 @@ export class VNode extends VNodeMeta {
5050
while ((current = current.parent)) yield current;
5151
}
5252

53-
findNamespace() {
53+
namespaceOf(tagName: string) {
54+
if (XMLNamespace[tagName]) return XMLNamespace[tagName];
55+
5456
for (const { namespace } of this.walkUp()) if (namespace) return namespace;
57+
58+
return XMLNamespace.html;
5559
}
5660

5761
createDOM(document = globalThis.document) {
@@ -61,11 +65,9 @@ export class VNode extends VNodeMeta {
6165
? document.createTextNode(text)
6266
: !tagName
6367
? document.createDocumentFragment()
64-
: document.createElementNS(
65-
(this.namespace ||= XMLNamespace[tagName] || this.findNamespace()),
66-
tagName,
67-
{ is }
68-
));
68+
: document.createElementNS((this.namespace ||= this.namespaceOf(tagName)), tagName, {
69+
is
70+
}));
6971
}
7072

7173
toJSON(): VNodeMeta {

test/jsx-runtime.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,11 @@ describe('JSX runtime', () => {
180180
<text x="150" y="125" font-size="60" text-anchor="middle" fill="white">
181181
SVG
182182
</text>
183-
</svg>,
184-
document.body
183+
</svg>
185184
);
186185
expect(document.body.innerHTML).toBe(
187186
'<svg version="1.1" width="300" height="200" xmlns="http://www.w3.org/2000/svg"><rect width="100%" height="100%" fill="red"></rect><circle cx="150" cy="100" r="80" fill="green"></circle><text x="150" y="125" font-size="60" text-anchor="middle" fill="white">SVG</text></svg>'
188187
);
188+
expect(document.body.firstElementChild).toBeInstanceOf(SVGSVGElement);
189189
});
190190
});

0 commit comments

Comments
 (0)