add visit function for snapshot

This commit is contained in:
Yanzhen Yu
2026-04-01 12:00:00 +08:00
parent 29fcf03d4f
commit 52aac74fa2
2 changed files with 23 additions and 1 deletions

View File

@@ -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,
};

View File

@@ -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;