]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip(hydration): hydrate multi-element static nodes
authorEvan You <yyx990803@gmail.com>
Fri, 15 May 2020 20:30:20 +0000 (16:30 -0400)
committerEvan You <yyx990803@gmail.com>
Fri, 15 May 2020 20:30:20 +0000 (16:30 -0400)
packages/runtime-core/__tests__/hydration.spec.ts
packages/runtime-core/src/hydration.ts

index e39ecfdb88ebddfe7d1c92d1f6c014ec043718ee..947d1f2963feba2354486ac51c3affd4433f6f0d 100644 (file)
@@ -56,10 +56,31 @@ describe('SSR hydration', () => {
   test('static', () => {
     const html = '<div><span>hello</span></div>'
     const { vnode, container } = mountWithHydration(html, () =>
-      createStaticVNode(html)
+      createStaticVNode('', 1)
     )
     expect(vnode.el).toBe(container.firstChild)
     expect(vnode.el.outerHTML).toBe(html)
+    expect(vnode.anchor).toBe(container.firstChild)
+    expect(vnode.children).toBe(html)
+  })
+
+  test('static (multiple elements)', () => {
+    const staticContent = '<div></div><span>hello</span>'
+    const html = `<div><div>hi</div>` + staticContent + `<div>ho</div></div>`
+
+    const n1 = h('div', 'hi')
+    const s = createStaticVNode('', 2)
+    const n2 = h('div', 'ho')
+
+    const { container } = mountWithHydration(html, () => h('div', [n1, s, n2]))
+
+    const div = container.firstChild!
+
+    expect(n1.el).toBe(div.firstChild)
+    expect(n2.el).toBe(div.lastChild)
+    expect(s.el).toBe(div.childNodes[1])
+    expect(s.anchor).toBe(div.childNodes[2])
+    expect(s.children).toBe(staticContent)
   })
 
   test('element with text children', async () => {
index 4b7cd5a3207caa3a34863dfa3942ec50bc11b1fc..5816b183c3dd96214c9814b3c837b308938c0d19 100644 (file)
@@ -117,7 +117,18 @@ export function createHydrationFunctions(
         if (domType !== DOMNodeTypes.ELEMENT) {
           return onMismatch()
         }
-        return nextSibling(node)
+        // determine anchor, adopt content
+        let content = ''
+        let cur = node
+        for (let i = 0; i < vnode.staticCount; i++) {
+          content += (cur as Element).outerHTML
+          if (i === vnode.staticCount - 1) {
+            vnode.anchor = cur
+          }
+          cur = nextSibling(cur)!
+        }
+        vnode.children = content
+        return cur
       case Fragment:
         if (!isFragmentStart) {
           return onMismatch()