Skip to content

Commit 4121de4

Browse files
committed
refactor(vapor): reorg node op helpers + remove children helper
1 parent 242cc15 commit 4121de4

File tree

6 files changed

+32
-57
lines changed

6 files changed

+32
-57
lines changed

packages/runtime-vapor/__tests__/directives/vShow.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
applyVShow,
3-
children,
43
createComponent,
54
createIf,
65
defineVaporComponent,
@@ -23,8 +22,8 @@ const createDemo = (defaultValue: boolean) =>
2322
'<div><button>toggle</button><h1>hello world</h1></div>',
2423
)
2524
const n0 = t0()
26-
const n1 = children(n0, 0)
27-
const n2 = children(n0, 1)
25+
const n1 = n0.firstChild!
26+
const n2 = n1.nextSibling
2827
applyVShow(n2 as VShowElement, () => visible.value)
2928
on(n1 as HTMLElement, 'click', handleClick)
3029
return n0

packages/runtime-vapor/__tests__/dom/template.spec.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { children, next, nextn, template } from '../../src/dom/template'
1+
import { template } from '../../src/dom/template'
2+
import { child, next, nextn } from '../../src/dom/node'
23

34
describe('api: template', () => {
45
test('create element', () => {
@@ -17,21 +18,10 @@ describe('api: template', () => {
1718
expect(root.$root).toBe(true)
1819
})
1920

20-
test('children', () => {
21-
const t = template('<div><span><b>nested</b></span><p></p></div>')
22-
const root = t()
23-
const span = children(root, 0)
24-
const b = children(span, 0)
25-
const p = children(root, 1)
26-
expect(span).toBe(root.firstChild)
27-
expect(b).toBe(root.firstChild!.firstChild)
28-
expect(p).toBe(root.firstChild!.nextSibling)
29-
})
30-
3121
test('next', () => {
3222
const t = template('<div><span></span><b></b><p></p></div>')
3323
const root = t()
34-
const span = children(root, 0)
24+
const span = child(root as ParentNode)
3525
const b = next(span)
3626

3727
expect(span).toBe(root.childNodes[0])

packages/runtime-vapor/__tests__/if.spec.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
children,
2+
child,
33
createIf,
44
insert,
55
renderEffect,
@@ -161,23 +161,21 @@ describe('createIf', () => {
161161
const n1 = createIf(
162162
spyConditionFn1,
163163
() => {
164-
const n2 = t0()
165-
withDirectives(children(n2, 0), [
166-
[vDirective, () => (update.value, '1')],
167-
])
164+
const n2 = t0() as ParentNode
165+
withDirectives(child(n2), [[vDirective, () => (update.value, '1')]])
168166
return n2
169167
},
170168
() =>
171169
createIf(
172170
spyConditionFn2,
173171
() => {
174-
const n2 = t0()
175-
withDirectives(children(n2, 0), [[vDirective, () => '2']])
172+
const n2 = t0() as ParentNode
173+
withDirectives(child(n2), [[vDirective, () => '2']])
176174
return n2
177175
},
178176
() => {
179-
const n2 = t0()
180-
withDirectives(children(n2, 0), [[vDirective, () => '3']])
177+
const n2 = t0() as ParentNode
178+
withDirectives(child(n2), [[vDirective, () => '3']])
181179
return n2
182180
},
183181
),

packages/runtime-vapor/src/dom/node.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,21 @@ export function createComment(data: string): Comment {
1212
export function querySelector(selectors: string): Element | null {
1313
return document.querySelector(selectors)
1414
}
15+
16+
/*! #__NO_SIDE_EFFECTS__ */
17+
export function child(node: ParentNode): Node {
18+
return node.firstChild!
19+
}
20+
21+
/*! #__NO_SIDE_EFFECTS__ */
22+
export function next(node: Node): Node {
23+
return node.nextSibling!
24+
}
25+
26+
/*! #__NO_SIDE_EFFECTS__ */
27+
export function nextn(node: Node, offset: number = 1): Node {
28+
for (let i = 0; i < offset; i++) {
29+
node = node.nextSibling!
30+
}
31+
return node
32+
}

packages/runtime-vapor/src/dom/template.ts

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,3 @@ export function template(html: string, root?: boolean) {
1212
return ret
1313
}
1414
}
15-
16-
/*! #__NO_SIDE_EFFECTS__ */
17-
export function children(node: Node, ...paths: number[]): Node {
18-
for (const idx of paths) {
19-
// In various situations, select the quickest approach.
20-
// See https://github.com/vuejs/vue-vapor/pull/263
21-
node =
22-
idx === 0
23-
? node.firstChild!
24-
: idx === 1
25-
? node.firstChild!.nextSibling!
26-
: node.childNodes[idx]
27-
}
28-
return node
29-
}
30-
31-
export function child(node: ParentNode): Node {
32-
return node.firstChild!
33-
}
34-
35-
export function next(node: Node): Node {
36-
return node.nextSibling!
37-
}
38-
39-
export function nextn(node: Node, offset: number = 1): Node {
40-
for (let i = 0; i < offset; i++) {
41-
node = node.nextSibling!
42-
}
43-
return node
44-
}

packages/runtime-vapor/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export { insert, prepend, remove } from './block'
99
export { createComponent, createComponentWithFallback } from './component'
1010
export { renderEffect } from './renderEffect'
1111
export { createSlot } from './componentSlots'
12-
export { template, children, child, next, nextn } from './dom/template'
13-
export { createTextNode } from './dom/node'
12+
export { template } from './dom/template'
13+
export { createTextNode, child, next, nextn } from './dom/node'
1414
export {
1515
setText,
1616
setHtml,

0 commit comments

Comments
 (0)