Skip to content

Commit 8135d4c

Browse files
committed
fix(runtime): implement prev/nextSibling getters
1 parent fbed77d commit 8135d4c

File tree

2 files changed

+74
-7
lines changed

2 files changed

+74
-7
lines changed

packages/runtime/__tests__/nodes.spec.ts

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ describe('NSVElement', () => {
1515

1616
it('caches meta', () => {
1717
// mock getViewMeta
18-
const getViewMeta = jest.fn(
19-
(): NSVViewMeta => {
20-
return { viewFlags: NSVViewFlags.SKIP_ADD_TO_DOM }
21-
}
22-
)
18+
const getViewMeta = jest.fn((): NSVViewMeta => {
19+
return { viewFlags: NSVViewFlags.SKIP_ADD_TO_DOM }
20+
})
2321
const orig = require('../src/registry').getViewMeta
2422
require('../src/registry').getViewMeta = getViewMeta
2523

@@ -57,6 +55,46 @@ describe('NSVElement', () => {
5755
expect(elem.lastChild).toBe(child2)
5856
})
5957

58+
it('returns prevSibling/nextSibling', () => {
59+
const elem = new NSVElement('StackLayout')
60+
const child1 = new NSVElement('Label')
61+
const child2 = new NSVElement('Label')
62+
const child3 = new NSVElement('Label')
63+
64+
expect(child1.prevSibling).toBe(null)
65+
expect(child1.nextSibling).toBe(null)
66+
67+
expect(child2.prevSibling).toBe(null)
68+
expect(child2.nextSibling).toBe(null)
69+
70+
expect(child3.prevSibling).toBe(null)
71+
expect(child3.nextSibling).toBe(null)
72+
73+
elem.appendChild(child1)
74+
elem.appendChild(child2)
75+
elem.appendChild(child3)
76+
77+
expect(child1.prevSibling).toBe(null)
78+
expect(child1.nextSibling).toBe(child2)
79+
80+
expect(child2.prevSibling).toBe(child1)
81+
expect(child2.nextSibling).toBe(child3)
82+
83+
expect(child3.prevSibling).toBe(child2)
84+
expect(child3.nextSibling).toBe(null)
85+
86+
elem.removeChild(child2)
87+
88+
expect(child1.prevSibling).toBe(null)
89+
expect(child1.nextSibling).toBe(child3)
90+
91+
expect(child2.prevSibling).toBe(null)
92+
expect(child2.nextSibling).toBe(null)
93+
94+
expect(child3.prevSibling).toBe(child1)
95+
expect(child3.nextSibling).toBe(null)
96+
})
97+
6098
it('insertBefore falls back to appendChild if anchor not found', () => {
6199
const elem = new NSVElement('StackLayout')
62100
const child = new NSVElement('Label')

packages/runtime/src/nodes.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,37 @@ export abstract class NSVNode implements INSVNode {
9898
parentNode: INSVElement | null = null
9999
childNodes: INSVNode[] = []
100100

101-
nextSibling: INSVNode | null = null
102-
prevSibling: INSVNode | null = null
101+
get nextSibling(): INSVNode | null {
102+
if (!this.parentNode) {
103+
return null
104+
}
105+
106+
const selfIndex = this.parentNode.childNodes.findIndex(
107+
(n) => n.nodeId === this.nodeId
108+
)
109+
110+
if (selfIndex > -1 && selfIndex < this.parentNode.childNodes.length - 1) {
111+
return this.parentNode.childNodes[selfIndex + 1]
112+
}
113+
114+
return null
115+
}
116+
117+
get prevSibling(): INSVNode | null {
118+
if (!this.parentNode) {
119+
return null
120+
}
121+
122+
const selfIndex = this.parentNode.childNodes.findIndex(
123+
(n) => n.nodeId === this.nodeId
124+
)
125+
126+
if (selfIndex > 0) {
127+
return this.parentNode.childNodes[selfIndex - 1]
128+
}
129+
130+
return null
131+
}
103132

104133
get firstChild() {
105134
return this.childNodes.length ? this.childNodes[0] : null

0 commit comments

Comments
 (0)