mirror of
https://github.com/NanmiCoder/cc-haha
synced 2026-08-01 16:43:37 +08:00
feat(desktop): add DOM tree navigation helpers
This commit is contained in:
parent
19116cb4d9
commit
5083adc9f9
17
desktop/src/preview-agent/treeNav.test.ts
Normal file
17
desktop/src/preview-agent/treeNav.test.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import { describe, expect, it, beforeEach } from 'vitest'
|
||||||
|
import { climb, descend } from './treeNav'
|
||||||
|
|
||||||
|
beforeEach(() => { document.body.innerHTML = `<main><section><h1>A</h1></section></main>` })
|
||||||
|
|
||||||
|
describe('tree navigation', () => {
|
||||||
|
it('climb returns parent element, not past body', () => {
|
||||||
|
const h1 = document.querySelector('h1')!
|
||||||
|
expect(climb(h1)?.tagName.toLowerCase()).toBe('section')
|
||||||
|
expect(climb(document.querySelector('main')!)).toBeNull() // body 为界
|
||||||
|
})
|
||||||
|
it('descend returns first element child', () => {
|
||||||
|
const section = document.querySelector('section')!
|
||||||
|
expect(descend(section)?.tagName.toLowerCase()).toBe('h1')
|
||||||
|
expect(descend(document.querySelector('h1')!)).toBeNull()
|
||||||
|
})
|
||||||
|
})
|
||||||
8
desktop/src/preview-agent/treeNav.ts
Normal file
8
desktop/src/preview-agent/treeNav.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
export function climb(el: Element): Element | null {
|
||||||
|
const p = el.parentElement
|
||||||
|
if (!p || p.tagName.toLowerCase() === 'body' || p.tagName.toLowerCase() === 'html') return null
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
export function descend(el: Element): Element | null {
|
||||||
|
return el.firstElementChild
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user