]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor(vapor): reorg node op helpers + remove children helper
authorEvan You <evan@vuejs.org>
Wed, 12 Feb 2025 07:47:14 +0000 (15:47 +0800)
committerEvan You <evan@vuejs.org>
Wed, 12 Feb 2025 07:47:14 +0000 (15:47 +0800)
packages/runtime-vapor/__tests__/directives/vShow.spec.ts
packages/runtime-vapor/__tests__/dom/template.spec.ts
packages/runtime-vapor/__tests__/if.spec.ts
packages/runtime-vapor/src/dom/node.ts
packages/runtime-vapor/src/dom/template.ts
packages/runtime-vapor/src/index.ts

index f7d8757c826623c79db59d34b751ebd90cba62b0..da553785e187e023c81e02a97527586262d8f242 100644 (file)
@@ -1,6 +1,5 @@
 import {
   applyVShow,
-  children,
   createComponent,
   createIf,
   defineVaporComponent,
@@ -23,8 +22,8 @@ const createDemo = (defaultValue: boolean) =>
       '<div><button>toggle</button><h1>hello world</h1></div>',
     )
     const n0 = t0()
-    const n1 = children(n0, 0)
-    const n2 = children(n0, 1)
+    const n1 = n0.firstChild!
+    const n2 = n1.nextSibling
     applyVShow(n2 as VShowElement, () => visible.value)
     on(n1 as HTMLElement, 'click', handleClick)
     return n0
index 45395e42c0e15b4f84005fd3905f5fbdb0c4d3dc..f34702362f8efa3aad053105c1a49503f325a701 100644 (file)
@@ -1,4 +1,5 @@
-import { children, next, nextn, template } from '../../src/dom/template'
+import { template } from '../../src/dom/template'
+import { child, next, nextn } from '../../src/dom/node'
 
 describe('api: template', () => {
   test('create element', () => {
@@ -17,21 +18,10 @@ describe('api: template', () => {
     expect(root.$root).toBe(true)
   })
 
-  test('children', () => {
-    const t = template('<div><span><b>nested</b></span><p></p></div>')
-    const root = t()
-    const span = children(root, 0)
-    const b = children(span, 0)
-    const p = children(root, 1)
-    expect(span).toBe(root.firstChild)
-    expect(b).toBe(root.firstChild!.firstChild)
-    expect(p).toBe(root.firstChild!.nextSibling)
-  })
-
   test('next', () => {
     const t = template('<div><span></span><b></b><p></p></div>')
     const root = t()
-    const span = children(root, 0)
+    const span = child(root as ParentNode)
     const b = next(span)
 
     expect(span).toBe(root.childNodes[0])
index e319a884c7639759f3c8996936c11d9efc27e0b7..eed0a95ef61cbb791c060a4a64d37a346bcb689f 100644 (file)
@@ -1,5 +1,5 @@
 import {
-  children,
+  child,
   createIf,
   insert,
   renderEffect,
@@ -161,23 +161,21 @@ describe('createIf', () => {
       const n1 = createIf(
         spyConditionFn1,
         () => {
-          const n2 = t0()
-          withDirectives(children(n2, 0), [
-            [vDirective, () => (update.value, '1')],
-          ])
+          const n2 = t0() as ParentNode
+          withDirectives(child(n2), [[vDirective, () => (update.value, '1')]])
           return n2
         },
         () =>
           createIf(
             spyConditionFn2,
             () => {
-              const n2 = t0()
-              withDirectives(children(n2, 0), [[vDirective, () => '2']])
+              const n2 = t0() as ParentNode
+              withDirectives(child(n2), [[vDirective, () => '2']])
               return n2
             },
             () => {
-              const n2 = t0()
-              withDirectives(children(n2, 0), [[vDirective, () => '3']])
+              const n2 = t0() as ParentNode
+              withDirectives(child(n2), [[vDirective, () => '3']])
               return n2
             },
           ),
index 76ceba5f2b220bbc3fff060839103aa146734527..f620acb63e1414eee3345ac2c0ce1758a4b230e3 100644 (file)
@@ -12,3 +12,21 @@ export function createComment(data: string): Comment {
 export function querySelector(selectors: string): Element | null {
   return document.querySelector(selectors)
 }
+
+/*! #__NO_SIDE_EFFECTS__ */
+export function child(node: ParentNode): Node {
+  return node.firstChild!
+}
+
+/*! #__NO_SIDE_EFFECTS__ */
+export function next(node: Node): Node {
+  return node.nextSibling!
+}
+
+/*! #__NO_SIDE_EFFECTS__ */
+export function nextn(node: Node, offset: number = 1): Node {
+  for (let i = 0; i < offset; i++) {
+    node = node.nextSibling!
+  }
+  return node
+}
index 994a2c520f48e9b8047bf2683dbc5ee51dac42d5..1eff20ec94cdd73d27faa0c579afa9d2a6f22087 100644 (file)
@@ -12,33 +12,3 @@ export function template(html: string, root?: boolean) {
     return ret
   }
 }
-
-/*! #__NO_SIDE_EFFECTS__ */
-export function children(node: Node, ...paths: number[]): Node {
-  for (const idx of paths) {
-    // In various situations, select the quickest approach.
-    // See https://github.com/vuejs/vue-vapor/pull/263
-    node =
-      idx === 0
-        ? node.firstChild!
-        : idx === 1
-          ? node.firstChild!.nextSibling!
-          : node.childNodes[idx]
-  }
-  return node
-}
-
-export function child(node: ParentNode): Node {
-  return node.firstChild!
-}
-
-export function next(node: Node): Node {
-  return node.nextSibling!
-}
-
-export function nextn(node: Node, offset: number = 1): Node {
-  for (let i = 0; i < offset; i++) {
-    node = node.nextSibling!
-  }
-  return node
-}
index dd7ccfc6daa312129faeb7fd7df045b6e14adc80..e4b78391604ae0b716362377e30e2494c650bd0d 100644 (file)
@@ -9,8 +9,8 @@ export { insert, prepend, remove } from './block'
 export { createComponent, createComponentWithFallback } from './component'
 export { renderEffect } from './renderEffect'
 export { createSlot } from './componentSlots'
-export { template, children, child, next, nextn } from './dom/template'
-export { createTextNode } from './dom/node'
+export { template } from './dom/template'
+export { createTextNode, child, next, nextn } from './dom/node'
 export {
   setText,
   setHtml,