Skip to content

fix(plugin-autocapture-browser): fix nearest label CSS coping - #1964

Open
daniel-graham-amplitude wants to merge 1 commit into
mainfrom
SDK-229-fix-nearest-label
Open

fix(plugin-autocapture-browser): fix nearest label CSS coping#1964
daniel-graham-amplitude wants to merge 1 commit into
mainfrom
SDK-229-fix-nearest-label

Conversation

@daniel-graham-amplitude

@daniel-graham-amplitude daniel-graham-amplitude commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator

Summary

Checklist

  • Does your PR title have the correct title format?
  • Does your PR have a breaking change?:

Note

Low Risk
Narrow DOM-query fix in autocapture label enrichment with a targeted regression test; no auth, persistence, or API surface changes.

Overview
Fixes incorrect [Amplitude] Element Parent Label values when a page has headings that are not direct siblings of the interacted element (e.g. a page title h1 above a nested form input).

getNearestLabel now applies :scope> to every selector in the querySelector list (span and h1h6), so only direct child label elements are considered at each ancestor step. Previously, comma-separated h1h6 without :scope> could match any descendant heading under the current parent, causing unrelated titles to be picked up while walking up the DOM.

A regression test asserts that an input inside a nested form does not inherit a container-level h1 as its nearest label.

Reviewed by Cursor Bugbot for commit 346f9ef. Bugbot is set up for automated code reviews on this repo. Configure here.

@daniel-graham-amplitude
daniel-graham-amplitude requested a review from a team as a code owner August 31, 2026 17:03
@linear-code

linear-code Bot commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

SDK-229

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Regression test expects wrong label
    • Nested the page-title h1 under a header so the fixture exercises unscoped descendant matching instead of a sibling :scope>h1 hit.

Create PR

Or push these changes by commenting:

@cursor push a728934667
Preview (a728934667)
diff --git a/packages/plugin-autocapture-browser/test/data-extractor.test.ts b/packages/plugin-autocapture-browser/test/data-extractor.test.ts
--- a/packages/plugin-autocapture-browser/test/data-extractor.test.ts
+++ b/packages/plugin-autocapture-browser/test/data-extractor.test.ts
@@ -472,11 +472,13 @@
       // the parent. Walking up then picks the first ancestor that contains any
       // heading — e.g. a page title — instead of a sibling label.
       const container = document.createElement('div');
+      const header = document.createElement('header');
       const heading = document.createElement('h1');
       heading.innerText = 'My App';
       const form = document.createElement('form');
       const input = document.createElement('input');
-      container.appendChild(heading);
+      header.appendChild(heading);
+      container.appendChild(header);
       form.appendChild(input);
       container.appendChild(form);

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit 7a19102. Configure here.


const result = dataExtractor.getNearestLabel(input);
expect(result).toEqual('');
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regression test expects wrong label

Medium Severity

The new regression fixture puts the h1 as a direct child of container, so walking up from input still matches :scope>h1 and returns My App instead of ''. That tree does not reproduce the unscoped descendant-heading case this change fixes.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 7a19102. Configure here.

@github-actions

Copy link
Copy Markdown

size-limit report 📦

Path Size
packages/analytics-browser/lib/scripts/amplitude-min.js.gz 64.33 KB (+0.01% 🔺)
packages/session-replay-browser/lib/scripts/session-replay-browser-min.js.gz 135.32 KB (0%)
packages/unified/lib/scripts/amplitude-min.umd.js.gz 218.75 KB (+0.01% 🔺)
@amplitude/element-selector (gzipped esm) 3.4 KB (0%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant