fix(browser): keep the first DOM attribute when the name is repeated (#1516)
* fix(browser): keep the first DOM attribute when the name is repeated 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 #1496 * fix(browser): preserve case-sensitive DOM attribute names * test(browser): align repeated attribute coverage with deduplication --------- Co-authored-by: taljeri <318237806+taljeri@users.noreply.github.com> Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
co-authored by
taljeri
morluto
parent
4d8d76b253
commit
f6009d0077
@@ -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`,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -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<string>();
|
||||
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<string, string>();
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user