From 52aac74fa26660091dbb89cd23eea66f7fcb3380 Mon Sep 17 00:00:00 2001 From: Yanzhen Yu Date: Wed, 1 Apr 2026 12:00:00 +0800 Subject: [PATCH] add visit function for snapshot --- src/index.ts | 7 ++++++- src/snapshot.ts | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 7c059f37..1d5c89ea 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,8 @@ -import snapshot, { serializeNodeWithId, transformAttribute } from './snapshot'; +import snapshot, { + serializeNodeWithId, + transformAttribute, + visit, +} from './snapshot'; import rebuild, { buildNodeWithSN, addHoverClass } from './rebuild'; export * from './types'; @@ -9,4 +13,5 @@ export { buildNodeWithSN, addHoverClass, transformAttribute, + visit, }; diff --git a/src/snapshot.ts b/src/snapshot.ts index f2300cf3..10a6d857 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -425,4 +425,21 @@ function snapshot( ]; } +export function visit( + node: serializedNodeWithId, + onVisit: (node: serializedNodeWithId) => unknown, +) { + function walk(current: serializedNodeWithId) { + onVisit(current); + if ( + current.type === NodeType.Document || + current.type === NodeType.Element + ) { + current.childNodes.forEach(walk); + } + } + + walk(node); +} + export default snapshot;