Skip to content

Commit 8cc7751

Browse files
committed
fix: Check for node.childNodes before accessing them
See https://developer.mozilla.org/en-US/docs/Web/API/Node/childNodes#simple_usage This is typed weirdly, because apparently you should check for existence here. Technically doing `node.hasChildNodes()` would be correct, but since we tend to serialize stuff here or there I feel this is safer (and we already do that in other places too). Closes getsentry/sentry-javascript#15948
1 parent 1b1d711 commit 8cc7751

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

packages/rrweb-snapshot/src/snapshot.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,8 @@ export function serializeNodeWithId(
13461346
stylesheetLoadTimeout,
13471347
keepIframeSrcFn,
13481348
};
1349-
for (const childN of Array.from(n.childNodes)) {
1349+
const childNodes = n.childNodes ? Array.from(n.childNodes) : [];
1350+
for (const childN of childNodes) {
13501351
const serializedChildNode = serializeNodeWithId(childN, bypassOptions);
13511352
if (serializedChildNode) {
13521353
serializedNode.childNodes.push(serializedChildNode);

packages/rrweb/src/record/mutation.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,9 @@ export default class MutationBuffer {
829829
false,
830830
)
831831
) {
832-
n.childNodes.forEach((childN) => this.genAdds(childN));
832+
if (n.childNodes) {
833+
n.childNodes.forEach((childN) => this.genAdds(childN));
834+
}
833835
if (hasShadowRoot(n)) {
834836
n.shadowRoot.childNodes.forEach((childN) => {
835837
this.processedNodeManager.add(childN, this);
@@ -848,7 +850,9 @@ export default class MutationBuffer {
848850
*/
849851
function deepDelete(addsSet: Set<Node>, n: Node) {
850852
addsSet.delete(n);
851-
n.childNodes.forEach((childN) => deepDelete(addsSet, childN));
853+
if (n.childNodes) {
854+
n.childNodes.forEach((childN) => deepDelete(addsSet, childN));
855+
}
852856
}
853857

854858
function isParentRemoved(

0 commit comments

Comments
 (0)