Skip to content

fix(browser): keep the first DOM attribute when the name is repeated - #1516

Merged
morluto merged 4 commits into
morluto:mainfrom
taljeri:fix/browser-dom-first-attribute
Oct 10, 2026
Merged

morluto merged 4 commits into
morluto:mainfrom
taljeri:fix/browser-dom-first-attribute

Conversation

@taljeri

@taljeri taljeri commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #1496

Page capture lists DOM attributes and computes URLs/hints from them. When an element carried duplicate attribute declarations (e.g. repeated href, action, or src), domMetadata folded them into a Map with pairs.set(), causing later duplicate values to overwrite earlier ones.

In HTML, the first declaration is kept when an attribute name is repeated. This aligns domMetadata and attribute_names to preserve the first occurrence case-insensitively, ensuring that attribute_names and dom_urls describe the same attributes according to HTML rules.

Changes

  • In src/browser/CdpCaptureDocuments.ts, keep the first attribute occurrence in domMetadata using if (!pairs.has(name)) pairs.set(...).
  • Deduplicate attribute_names on each node by case-insensitive first appearance.
  • Add regression test in src/browser/CdpCaptureDocuments.baseUrl.test.ts.

Verification

  • npm run test:focused -- src/browser/CdpCaptureDocuments.baseUrl.test.ts src/browser/CdpCaptureDocuments.test.ts (all passed)
  • npm run check:fast (typecheck & lint clean)
  • npm run docs:check (clean metadata contracts)

When an element in a captured DOM tree carries duplicate attribute
declarations (e.g. repeated href, action, or src), domMetadata folded
the attributes into a Map using pairs.set(), overwriting earlier
declarations with the last one.

HTML specifies that the first declaration is kept when an attribute
name is repeated. This aligns domMetadata and attribute_names to retain
the first occurrence case-insensitively, matching HTML and ensuring that
attribute_names and dom_urls reflect the same attributes.

Fixes morluto#1496
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@CassieFox23

CassieFox23 commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR: Main already has the URL half of this change since b4db35f09 (#1513), so the PR now has a merge conflict. The other half lists each name once, and that fails a new test on main that wants both names kept. Both still pick the wrong link when HREF comes first, since Chromium uses href.

Live pages

HTML markup never gets this far, because the parser drops a repeated name. Two hrefs with different case do get through, in XHTML pages or with setAttributeNS. I loaded each page in Chromium 154 (BSD-3-Clause) and ran inspect-web-page. The cells show the path of dom_urls[].url for the link.

Page and order Chromium follows 7a56fe626 46c9cba03 #1516
HTML, href twice /good-html /good-html /good-html /good-html
XHTML, href then HREF /good-x1 /bad-x1 /good-x1 /good-x1
XHTML, HREF then href /good-x2 /good-x2 /bad-x2 /bad-x2
Script, href then HREF /good-n1 /bad-n1 /good-n1 /good-n1
Script, HREF then href /good-n2 /good-n2 /bad-n2 /bad-n2
XHTML, HREF only no link /only-upper /only-upper /only-upper

7a56fe626 is main before b4db35f09 and 46c9cba03 is main after it. #1516 is head cc145a95d. "Chromium follows" is the a.href value Chromium gave for that link.

Chromium picks the attribute whose name is href in lowercase. Main and this PR both keep the first name after lowercasing, so the "HREF then href" rows went from right to wrong. On those rows this PR also lists ["id", "HREF"], so the real href drops out of attribute_names.

The last row is wrong on every build. Chromium makes no link from HREF alone, but REA still reports one. Matching the exact name href might fix all six rows. I have not tried that change.

Tests

Check Node 22.23.1 Node 24.18.0 Node 26.11.1
The two test files the PR lists, on #1516 23 of 23 pass 23 of 23 pass 23 of 23 pass
PR's new test on 7a56fe626 not run fails at attribute_names not run
PR's new test on 46c9cba03 not run fails at attribute_names not run
PR applied on 46c9cba03, same two files not run 23 of 24 pass not run

The PR's domMetadata change no longer applies to 46c9cba03, because main already has the same line. I applied the rest with patch in a scratch copy. Main's new test "keeps the first value when an attribute name repeats" then fails. It expects ["href", "href"], and the PR gives ["href"].

Not run

Edit: I tested most of this list after the merge. The results are in #1516 (comment). The scenario item was wrong: that artifact is page.content() text, not captureDom. Still not run: a rebase (not needed now, since #1516 merged as f6009d007), and the full suite, check:fast and docs:check on the merged commit.

  • A real rebase of the PR onto 46c9cba03. (Not needed now: the PR merged.)
  • Other URL names: src, action, formaction and poster.
  • <link rel> agent hints, which read the same href value.
  • SVG and MathML elements. Two oddities remain, and they are not about repeated names. See fix(browser): keep the first DOM attribute when the name is repeated #1516 (comment).
  • The DOM artifact from capture-browser-scenario, which uses the same captureDom. It is page.content() text from Playwright, not captureDom. My note here was wrong.
  • The full test suite, check:fast and docs:check. (Only CdpCaptureDocuments.baseUrl.test.ts ran, on f6009d007.)

Thanks, taljeri, for the fix and the regression test.

Test pages
<!-- dup.xhtml, served as application/xhtml+xml -->
<a id="x1" href="/good-x1" HREF="/bad-x1">lower first</a>
<a id="x2" HREF="/bad-x2" href="/good-x2">upper first</a>

<!-- ns.html -->
<a id="n1" href="/good-n1">lower first</a>
<a id="n2">upper first</a>
<script>
document.getElementById("n1").setAttributeNS(null, "HREF", "/bad-n1");
const n2 = document.getElementById("n2");
n2.setAttributeNS(null, "HREF", "/bad-n2");
n2.setAttribute("href", "/good-n2");
</script>
Commands
npm ci && npm run build
rea inspect-web-page http://127.0.0.1:9335 <target> --format json
node scripts/run-development-tests.mjs focused \
  src/browser/CdpCaptureDocuments.baseUrl.test.ts \
  src/browser/CdpCaptureDocuments.test.ts
patch -p1 < 1516.diff   # on a scratch copy of 46c9cba03
Test setup
  • Box: Nobara 44 (Fedora 44 base), Linux 7.2.9
  • CPU: AMD Ryzen 9 5900X, 12 cores / 24 threads
  • Node: 22.23.1, 24.18.0, 26.11.1
  • Head / base: cc145a95d / 983b070d4
  • Main: 7a56fe626 and 46c9cba03
  • Other: Chromium 154 (BSD-3-Clause), the one Fedora 44 ships
  • Build: 154.0.8037.57
  • Pages: headless, served from 127.0.0.1

🤖 Generated with Claude Code (Opus 5.5) 🦊

@morluto
morluto merged commit f6009d0 into morluto:main Oct 10, 2026
@morluto

morluto commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Thanks @taljeri for catching repeated DOM attributes and preserving the first declaration! Squash merged with the review fixes for case-sensitive attributes. Welcome to REA, and thank you for your first PR.

@CassieFox23

CassieFox23 commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR: The merge fixed what I found about repeated names. REA now follows the same value as Chromium when HREF comes before href, for all five URL names and the <link rel> hints. Two other things are still off: SVG xlink:href alone gives no URL, and MathML href is listed though Chromium ignores it.

Results after the merge

My earlier comment said the scenario DOM artifact uses captureDom, and that was wrong: it is the page text from page.content().

I reran every case from my earlier comment in Chromium 154 (BSD-3-Clause). I built cc145a95d, the PR head I tested before, and f6009d007, the merge. Main has no change under src/browser after the merge, so the merge build stands for main (cca7bf002). "Chromium uses" is the element's own property, such as img.src, or relList for rel.

Case Chromium uses REA before, cc145a95d REA after, f6009d007
Uppercase name first good value bad value good value
Lowercase name first good value good value good value
<link REL="mcp" rel="stylesheet"> a stylesheet an mcp hint no hint
HREF alone on an XHTML a no link a link no link
attribute_names, HREF then href both names ["id", "HREF"] ["id", "HREF", "href"]
HTML page, mixed case in the source good value good value good value
SVG xlink:href alone a link no URL no URL
MathML href not a link listed listed

In my test pages, the lowercase name holds the good value and the uppercase name holds the bad one. "Uppercase name first" covers link, img, form, button, video and SVG a, so href, src, action, formaction and poster. On the HTML page, the output after the merge is the same as before it. The same pairs set from a script on an HTML page gave the same results as the XHTML pages. The scenario dom artifact still keeps both names in order. The last two rows are still off. I filed the SVG one as #1586.

Check on f6009d007 Node 22.23.1 Node 24.18.0 Node 26.11.1
CdpCaptureDocuments.baseUrl.test.ts 22 of 22 pass 22 of 22 pass 22 of 22 pass

This is one file, not the two from my earlier comment. The test I said failed, "keeps the first value when an attribute name repeats", now expects ["href"], so that failure is gone.

Still not run

  • The full suite, check:fast and docs:check on f6009d007.
  • Other browsers.
  • Other elements that take URLs, such as input.

Thanks, morluto, for the review fix that stops lowercasing names. It made the uppercase rows right.

Per-page counts

Elements whose dom_urls differ from Chromium's own property, per page:

Page Before After Left over after
urls.xhtml (uppercase and lowercase first) 8 2 SVG xlink:href only, MathML mi
urls.html (set from a script, uppercase first) 8 3 SVG xlink:href only, MathML math and mi
urls-lf.html (set from a script, lowercase first) 0 0 none
mixed.html (mixed case in the source) 1 1 MathML mi
dup.html (plain HTML repeat) 0 0 none
dup.xhtml 1 0 none
ns.html 1 0 none
upper.xhtml (HREF alone) 1 0 none

The <link rel> hints matched Chromium on every page after the merge. Before, they were wrong on urls.xhtml and urls.html. For mixed.html and dup.html, the names, URLs and hints are the same before and after.

Also checked on f6009d007: <div href src> is listed in dom_urls, and SVG image and use with only xlink:href get no URL, like SVG a.

Commands
rea inspect-web-page http://127.0.0.1:<port> <target> --format json
rea capture-browser-scenario scenario.json --format json
node scripts/run-development-tests.mjs focused \
  src/browser/CdpCaptureDocuments.baseUrl.test.ts
Test pages added since my last comment
<!-- mixed.html: HTML with uppercase names in the source -->
<LINK ID="l3" REL="stylesheet" Rel="mcp" HREF="/good-l3.css">
<A ID="a1" HREF="/good-a1" Href="/bad-a1">upper then mixed</A>
<svg width=10 height=10><A ID="s1" HREF="/good-s1" href="/bad-s1"><text>s1</text></A></svg>
<math><mi id="m1" HREF="/good-m1">x</mi></math>

<!-- extra.html -->
<svg xmlns:xlink="http://www.w3.org/1999/xlink">
  <image id="im1" xlink:href="/xl-image.png" width="1" height="1"/>
  <use id="u1" xlink:href="#g0"/>
</svg>
Test setup
  • Box: Nobara 44 (Fedora 44 base), Linux 7.2.9
  • CPU: AMD Ryzen 9 5900X, 12 cores / 24 threads
  • Node: 22.23.1, 24.18.0, 26.11.1
  • Before / after: cc145a95d / f6009d007
  • Main: cca7bf002
  • Other: Chromium 154 (BSD-3-Clause), the one Fedora 44 ships
  • Build: 154.0.8037.57
  • Pages: headless, served from 127.0.0.1

🤖 Generated with Claude Code (Sonnet 5.5, verified by Opus 5.5) 🦊

@morluto morluto added bug Something isn't working area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior labels Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(browser): keep the first DOM attribute when the name is repeated

3 participants