Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-autocapture-non-element-nodes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'posthog-js': patch
---

Fix autocapture crash when encountering non-Element DOM nodes (text nodes, comment nodes, etc.) that don't have a tagName property
9 changes: 8 additions & 1 deletion packages/browser/src/autocapture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ export function autocapturePropertiesForElement(
curEl = (curEl.parentNode as any).host
continue
}
if (!isElementNode(curEl.parentNode)) {
break
}
targetElementList.push(curEl.parentNode as Element)
curEl = curEl.parentNode as Element
}
Expand All @@ -169,12 +172,16 @@ export function autocapturePropertiesForElement(
let explicitNoCapture = false

each(targetElementList, (el) => {
if (!isElementNode(el)) {
return
}

const shouldCaptureEl = shouldCaptureElement(el)

// if the element or a parent element is an anchor tag
// include the href as a property
if (el.tagName.toLowerCase() === 'a') {
href = el.getAttribute('href')
href = el.getAttribute('href') || false
href = shouldCaptureEl && href && shouldCaptureValue(href) && href
}

Expand Down
Loading