From: Evan You Date: Wed, 12 Feb 2025 07:47:14 +0000 (+0800) Subject: refactor(vapor): reorg node op helpers + remove children helper X-Git-Tag: v3.6.0-alpha.1~16^2~63 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4121de49602356d18dd8f1fa4546288e81f2817d;p=thirdparty%2Fvuejs%2Fcore.git refactor(vapor): reorg node op helpers + remove children helper --- diff --git a/packages/runtime-vapor/__tests__/directives/vShow.spec.ts b/packages/runtime-vapor/__tests__/directives/vShow.spec.ts index f7d8757c82..da553785e1 100644 --- a/packages/runtime-vapor/__tests__/directives/vShow.spec.ts +++ b/packages/runtime-vapor/__tests__/directives/vShow.spec.ts @@ -1,6 +1,5 @@ import { applyVShow, - children, createComponent, createIf, defineVaporComponent, @@ -23,8 +22,8 @@ const createDemo = (defaultValue: boolean) => '

hello world

', ) 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 diff --git a/packages/runtime-vapor/__tests__/dom/template.spec.ts b/packages/runtime-vapor/__tests__/dom/template.spec.ts index 45395e42c0..f34702362f 100644 --- a/packages/runtime-vapor/__tests__/dom/template.spec.ts +++ b/packages/runtime-vapor/__tests__/dom/template.spec.ts @@ -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('
nested

') - 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('

') const root = t() - const span = children(root, 0) + const span = child(root as ParentNode) const b = next(span) expect(span).toBe(root.childNodes[0]) diff --git a/packages/runtime-vapor/__tests__/if.spec.ts b/packages/runtime-vapor/__tests__/if.spec.ts index e319a884c7..eed0a95ef6 100644 --- a/packages/runtime-vapor/__tests__/if.spec.ts +++ b/packages/runtime-vapor/__tests__/if.spec.ts @@ -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 }, ), diff --git a/packages/runtime-vapor/src/dom/node.ts b/packages/runtime-vapor/src/dom/node.ts index 76ceba5f2b..f620acb63e 100644 --- a/packages/runtime-vapor/src/dom/node.ts +++ b/packages/runtime-vapor/src/dom/node.ts @@ -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 +} diff --git a/packages/runtime-vapor/src/dom/template.ts b/packages/runtime-vapor/src/dom/template.ts index 994a2c520f..1eff20ec94 100644 --- a/packages/runtime-vapor/src/dom/template.ts +++ b/packages/runtime-vapor/src/dom/template.ts @@ -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 -} diff --git a/packages/runtime-vapor/src/index.ts b/packages/runtime-vapor/src/index.ts index dd7ccfc6da..e4b7839160 100644 --- a/packages/runtime-vapor/src/index.ts +++ b/packages/runtime-vapor/src/index.ts @@ -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,