Skip to content

Commit 1184118

Browse files
committed
wip(runtime): test for static vnode handling
1 parent baa6973 commit 1184118

File tree

2 files changed

+58
-20
lines changed

2 files changed

+58
-20
lines changed

packages/runtime-core/src/renderer.ts

+8-20
Original file line numberDiff line numberDiff line change
@@ -485,26 +485,14 @@ function baseCreateRenderer(
485485
anchor: RendererNode | null,
486486
isSVG: boolean
487487
) => {
488-
if (n2.el && hostCloneNode !== undefined) {
489-
// static node was already mounted (and reused), or adopted
490-
// server-rendered node during hydration (in this case its children can be
491-
// stripped by SSR optimizations). Clone the dom nodes instead.
492-
let cur: RendererElement | null = n2.el
493-
while (cur && cur !== n2.anchor) {
494-
hostInsert(hostCloneNode(cur), container, anchor)
495-
cur = hostNextSibling(cur)
496-
}
497-
hostInsert(hostCloneNode(n2.anchor!), container, anchor)
498-
} else {
499-
// static nodes are only present when used with compiler-dom/runtime-dom
500-
// which guarantees presence of hostInsertStaticContent.
501-
;[n2.el, n2.anchor] = hostInsertStaticContent!(
502-
n2.children as string,
503-
container,
504-
anchor,
505-
isSVG
506-
)
507-
}
488+
// static nodes are only present when used with compiler-dom/runtime-dom
489+
// which guarantees presence of hostInsertStaticContent.
490+
;[n2.el, n2.anchor] = hostInsertStaticContent!(
491+
n2.children as string,
492+
container,
493+
anchor,
494+
isSVG
495+
)
508496
}
509497

510498
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { createStaticVNode, h, render } from '../src'
2+
3+
describe('static vnode handling', () => {
4+
const content = `<div>hello</div><p>world</p>`
5+
const content2 = `<p>foo</p><div>bar</div><span>baz</span>`
6+
7+
const s = createStaticVNode(content, 2)
8+
const s2 = createStaticVNode(content2, 3)
9+
10+
test('should mount from string', () => {
11+
const root = document.createElement('div')
12+
render(h('div', [s]), root)
13+
expect(root.innerHTML).toBe(`<div>${content}</div>`)
14+
})
15+
16+
test('should support reusing the same hoisted node', () => {
17+
const root = document.createElement('div')
18+
render(h('div', [s, s]), root)
19+
expect(root.innerHTML).toBe(`<div>${content}${content}</div>`)
20+
})
21+
22+
// the rest only happens during HMR but needs to be correctly supported
23+
test('should update', () => {
24+
const root = document.createElement('div')
25+
render(h('div', [s]), root)
26+
expect(root.innerHTML).toBe(`<div>${content}</div>`)
27+
render(h('div', [s2]), root)
28+
expect(root.innerHTML).toBe(`<div>${content2}</div>`)
29+
})
30+
31+
test('should move', () => {
32+
const root = document.createElement('div')
33+
render(h('div', [h('b'), s, h('b')]), root)
34+
expect(root.innerHTML).toBe(`<div><b></b>${content}<b></b></div>`)
35+
render(h('div', [s, h('b'), h('b')]), root)
36+
expect(root.innerHTML).toBe(`<div>${content}<b></b><b></b></div>`)
37+
render(h('div', [h('b'), h('b'), s]), root)
38+
expect(root.innerHTML).toBe(`<div><b></b><b></b>${content}</div>`)
39+
})
40+
41+
test('should remove', () => {
42+
const root = document.createElement('div')
43+
render(h('div', [h('b'), s, h('b')]), root)
44+
expect(root.innerHTML).toBe(`<div><b></b>${content}<b></b></div>`)
45+
render(h('div', [h('b'), h('b')]), root)
46+
expect(root.innerHTML).toBe(`<div><b></b><b></b></div>`)
47+
render(h('div', [h('b'), h('b'), s]), root)
48+
expect(root.innerHTML).toBe(`<div><b></b><b></b>${content}</div>`)
49+
})
50+
})

0 commit comments

Comments
 (0)