Skip to content

Commit 9bf1ced

Browse files
committed
add visit function for snapshot
1 parent e54218b commit 9bf1ced

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import snapshot, { serializeNodeWithId, transformAttribute } from './snapshot';
1+
import snapshot, {
2+
serializeNodeWithId,
3+
transformAttribute,
4+
visit,
5+
} from './snapshot';
26
import rebuild, { buildNodeWithSN, addHoverClass } from './rebuild';
37
export * from './types';
48

@@ -9,4 +13,5 @@ export {
913
buildNodeWithSN,
1014
addHoverClass,
1115
transformAttribute,
16+
visit,
1217
};

src/snapshot.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,4 +425,21 @@ function snapshot(
425425
];
426426
}
427427

428+
export function visit(
429+
node: serializedNodeWithId,
430+
onVisit: (node: serializedNodeWithId) => unknown,
431+
) {
432+
function walk(current: serializedNodeWithId) {
433+
onVisit(current);
434+
if (
435+
current.type === NodeType.Document ||
436+
current.type === NodeType.Element
437+
) {
438+
current.childNodes.forEach(walk);
439+
}
440+
}
441+
442+
walk(node);
443+
}
444+
428445
export default snapshot;

0 commit comments

Comments
 (0)