Skip to content

Commit

Permalink
[fix] Same VNode shouldn't share DOM nodes (fix #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
TechQuery committed Nov 9, 2024
1 parent 067b11e commit 11044cb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dom-renderer",
"version": "2.4.3",
"version": "2.4.4",
"license": "LGPL-3.0-or-later",
"author": "shiy2008@gmail.com",
"description": "A light-weight DOM Renderer supports Web components standard & TypeScript language",
Expand Down
4 changes: 2 additions & 2 deletions source/dist/DOMRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class DOMRenderer {
for (const { ref, node } of newList) ref?.(node);
}

handleCustomEvent(node: EventTarget, event: string) {
protected handleCustomEvent(node: EventTarget, event: string) {
var handler: EventListener;

Object.defineProperty(node, `on${event}`, {
Expand Down Expand Up @@ -176,7 +176,7 @@ export class DOMRenderer {
this.updateChildren(
oldVNode.node as ParentNode,
oldVNode.children || [],
newVNode.children || []
(newVNode.children = newVNode.children?.map(vNode => new VNode(vNode)) || [])
);
newVNode.node = oldVNode.node;

Expand Down
18 changes: 14 additions & 4 deletions test/jsx-runtime.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,25 @@ describe('JSX runtime', () => {
});

it('should not share a real DOM with the same VDOM', () => {
const sameVDOM = <a />;
const oldVDOM = <a>old</a>;

renderer.render(
<>
<nav>{sameVDOM}</nav>
<nav>{sameVDOM}</nav>
<nav>{oldVDOM}</nav>
<nav>{oldVDOM}</nav>
</>
);
expect(document.body.innerHTML).toBe('<nav><a></a></nav><nav><a></a></nav>');
expect(document.body.innerHTML).toBe('<nav><a>old</a></nav><nav><a>old</a></nav>');

const newVDOM = <a>new</a>;

renderer.render(
<>
<nav>{newVDOM}</nav>
<nav>{newVDOM}</nav>
</>
);
expect(document.body.innerHTML).toBe('<nav><a>new</a></nav><nav><a>new</a></nav>');
});

it('should handle Nested children arrays', () => {
Expand Down

0 comments on commit 11044cb

Please sign in to comment.