diff --git a/src/browser/CdpCaptureDocuments.baseUrl.test.ts b/src/browser/CdpCaptureDocuments.baseUrl.test.ts index 29492706e..2bd43fc08 100644 --- a/src/browser/CdpCaptureDocuments.baseUrl.test.ts +++ b/src/browser/CdpCaptureDocuments.baseUrl.test.ts @@ -208,7 +208,7 @@ describe("empty form destinations with a document base URL", () => { if (nodes === undefined) throw new TypeError("Expected a DOM snapshot"); nodes.attributes[2] = [href, guide, href, value.strings.length - 1]; const result = capture(value); - expect(result.nodes[2]?.attribute_names).toEqual(["href", "href"]); + expect(result.nodes[2]?.attribute_names).toEqual(["href"]); expect(result.urls.map(({ url }) => url)).toContain( `${origin}/screens/guide`, ); @@ -223,3 +223,57 @@ describe("empty form destinations with a document base URL", () => { expect(capture(value).urls[3]?.url).toBe(baseUrl); }); }); + +describe("repeated DOM attribute names", () => { + it("keeps the first declaration for attribute names and destination URLs", () => { + const value = snapshot(undefined, "assets/", documentUrl, "guide"); + const hrefUpperIndex = value.strings.length; + const secondGuideIndex = hrefUpperIndex + 1; + value.strings.push("href", "second-guide"); + const docNodes = value.documents[0]?.nodes; + if (docNodes !== undefined) { + docNodes.attributes[2] = [8, 10, hrefUpperIndex, secondGuideIndex]; + } + const result = capture(value); + const linkNode = result.nodes[2]; + expect(linkNode?.attribute_names).toEqual(["href"]); + const linkUrl = result.urls.find(({ node_index }) => node_index === 2); + expect(linkUrl?.url).toBe(`${origin}/screens/guide`); + }); +}); + +it("preserves case-sensitive SVG attributes and uses the actual href", () => { + const value = snapshot(); + const first = value.strings.length; + value.strings.push( + "a", + "HREF", + "/wrong", + "href", + "/actual", + "viewBox", + "viewbox", + "0 0 10 10", + ); + value.documents[0]!.nodes.nodeName[2] = first; + value.documents[0]!.nodes.attributes[2] = [ + first + 1, + first + 2, + first + 3, + first + 4, + first + 5, + first + 7, + first + 6, + first + 7, + ]; + const result = capture(value); + expect(result.nodes[2]?.attribute_names).toEqual([ + "HREF", + "href", + "viewBox", + "viewbox", + ]); + expect(result.urls.find(({ node_index }) => node_index === 2)?.url).toBe( + `${origin}/actual`, + ); +}); diff --git a/src/browser/CdpCaptureDocuments.ts b/src/browser/CdpCaptureDocuments.ts index 38276d480..aef32f894 100644 --- a/src/browser/CdpCaptureDocuments.ts +++ b/src/browser/CdpCaptureDocuments.ts @@ -177,15 +177,24 @@ export const captureDom = ( const parent = Math.trunc(parents[index] ?? -1); const nodeIndex = nodes.length; const nodeName = indexedString(strings, nodeNames[index]); + const rawAttributeNames = attributeIndexes + .filter((_value, attributeIndex) => attributeIndex % 2 === 0) + .map((value) => indexedString(strings, value)); + const seenAttributeNames = new Set(); + const attributeNames: string[] = []; + for (const name of rawAttributeNames) { + if (!seenAttributeNames.has(name)) { + seenAttributeNames.add(name); + attributeNames.push(name); + } + } nodes.push({ index: nodeIndex, parent_index: parent < 0 ? -1 : baseIndex + parent, node_type: Math.max(0, Math.trunc(nodeTypes[index] ?? 0)), node_name: nodeName, node_value_length: indexedString(strings, nodeValues[index]).length, - attribute_names: attributeIndexes - .filter((_value, attributeIndex) => attributeIndex % 2 === 0) - .map((value) => indexedString(strings, value)), + attribute_names: attributeNames, }); const metadata = domMetadata({ strings, @@ -385,7 +394,8 @@ const domMetadata = ( } = options; const pairs = new Map(); for (let index = 0; index + 1 < attributes.length; index += 2) { - const name = indexedString(strings, attributes[index]).toLowerCase(); + // Chrome already folds HTML names; SVG/XML names remain case-sensitive. + const name = indexedString(strings, attributes[index]); const value = indexedString(strings, attributes[index + 1]); // HTML keeps the first declaration. A later repeat must not move dom_urls. if (!pairs.has(name)) pairs.set(name, value);