fix(core): look up SVG selector aliases by namespace before matching their values (#8354)

* test(core): a predicate reads only aliases in its own namespace

* fix(core): look up SVG selector aliases by namespace before matching their values

* refactor(core): index each SVG alias by attribute in its own function

* chore(core): regenerate the inlined alias module and the catalog font mirror index

* test(core): re-record the page goldens for the new inlined alias module

* refactor(core): pick a predicate's alias index in one place

* chore(core): regenerate the inlined alias module and page goldens
This commit is contained in:
Miguel Ángel
2026-11-21 07:51:06 +00:00
committed by GitHub
parent de63e97311
commit c8c5214a41
6 changed files with 1646 additions and 1546 deletions
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -912,3 +912,29 @@ it("gives each attribute operator the same alternatives as matching every alias
).toBe(`${expected} { fill: red; }`);
}
});
// fails without the fix: each predicate visited every alias of its name in any namespace, then dropped them unpaid.
it("reads only aliases in the predicate's namespace, so other-namespace matches cost nothing per predicate", () => {
let namespaceReads = 0;
const references = Array.from({ length: 2000 }, (_, i) => ({
name: "xlink:href",
localName: "href",
get namespaceURI() {
namespaceReads++;
return "http://www.w3.org/1999/xlink";
},
before: `#a${i}`,
after: `#m${i}`,
}));
const predicates = ['="#a1"', '^="#a"', '$="1"', '|="#a1"', '~="#a1"'].flatMap((match) => [
`[xlink\\:href${match}]`,
`[xlink\\:href${match} i]`,
]);
const css = Array.from({ length: 100 }, () => predicates)
.flat()
.map((selector) => `${selector} { fill: red; }`)
.join("\n");
expect(rewriteSvgIdReferencesInCss(css, new Map(), references)).toBe(css);
expect(namespaceReads).toBeLessThan(10 * references.length);
});
@@ -29,28 +29,41 @@ interface SortedKeys {
interface AliasIndex {
everyRecordedId: Set<string>;
renamedIdAlternatives: Map<string, string>;
byName: Map<string, AttributeAliases>;
byLocalName: Map<string, AttributeAliases>;
unprefixed: Map<string, AttributeAliases>;
anyNamespace: Map<string, AttributeAliases>;
inNamespace: Map<string, AttributeAliases>;
}
const namespacedKey = (localName: string, uri: string | null) =>
JSON.stringify([lower(localName), uri]);
// A scope's alias list is read once and then only passed around, so its index is built once per list.
const aliasIndexes = new WeakMap<readonly SvgReferenceAlias[], AliasIndex>();
function addTo(map: Map<string, AttributeAliases>, key: string, alias: SvgReferenceAlias): void {
let entry = map.get(key);
if (!entry) map.set(key, (entry = { all: [], lookups: new Map() }));
entry.all.push(alias);
}
function indexByAttribute(index: AliasIndex, alias: SvgReferenceAlias): void {
const { namespaceURI } = alias;
if (namespaceURI === null) addTo(index.unprefixed, lower(alias.name), alias);
addTo(index.anyNamespace, lower(alias.localName), alias);
addTo(index.inNamespace, namespacedKey(alias.localName, namespaceURI), alias);
}
function aliasIndex(references: readonly SvgReferenceAlias[]): AliasIndex {
let index = aliasIndexes.get(references);
if (index) return index;
index = {
everyRecordedId: new Set(),
renamedIdAlternatives: new Map(),
byName: new Map(),
byLocalName: new Map(),
unprefixed: new Map(),
anyNamespace: new Map(),
inNamespace: new Map(),
};
const ids = new Map<string, Set<string>>();
const add = (map: Map<string, AttributeAliases>, name: string, alias: SvgReferenceAlias) => {
let entry = map.get(lower(name));
if (!entry) map.set(lower(name), (entry = { all: [], lookups: new Map() }));
entry.all.push(alias);
};
// Every element that references one symbol records the same alias; one copy is enough.
const seen = new Set<string>();
for (const alias of references) {
@@ -65,8 +78,7 @@ function aliasIndex(references: readonly SvgReferenceAlias[]): AliasIndex {
]);
if (seen.has(key)) continue;
seen.add(key);
add(index.byName, alias.name, alias);
add(index.byLocalName, alias.localName, alias);
indexByAttribute(index, alias);
if (alias.name !== "id") continue;
let targets = ids.get(alias.before);
if (!targets)
@@ -176,20 +188,28 @@ function positionsFor(
return lookups[operator]({ entry, value: fold(value), fold, id, chargeScan });
}
function entryFor(
index: AliasIndex,
{ name, namespace }: ParsedAttribute,
namespaces?: Namespaces,
): AttributeAliases | undefined {
if (namespace.kind === "none") return index.unprefixed.get(lower(name));
if (namespace.kind === "any") return index.anyNamespace.get(lower(name));
const uri = namespaces!.get(namespace.prefix);
return uri === undefined ? undefined : index.inNamespace.get(namespacedKey(name, uri));
}
function aliasesFor(
index: AliasIndex,
parsed: ParsedAttribute,
namespaces?: Namespaces,
chargeScan?: (chars: number) => void,
) {
const { name, namespace } = parsed;
const forName = (namespace.kind === "none" ? index.byName : index.byLocalName).get(lower(name));
if (!forName) return [];
const uri = namespace.kind === "named" ? namespaces!.get(namespace.prefix) : null;
return positionsFor(forName, parsed, chargeScan)
const entry = entryFor(index, parsed, namespaces);
if (!entry) return [];
return positionsFor(entry, parsed, chargeScan)
.sort((a, b) => a - b)
.map((at) => forName.all[at]!)
.filter((alias) => namespace.kind === "any" || alias.namespaceURI === uri);
.map((at) => entry.all[at]!);
}
/**
File diff suppressed because one or more lines are too long
+15 -15
View File
@@ -4,47 +4,47 @@
"contentType": "text/css; charset=utf-8",
"file": "13ea284e2ea4ac4ab37c98e5.bin"
},
"https://fonts.googleapis.com/css2?family=Inter:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,400;1,700&text=%3C!DdOoCcTtYyPpEe%20hHmMlL%3E%0AaAnNgG%3D%22sS-rRfFxXiIuU%2CbB*%7B%3A%7D%2F8vVwW1920.jJ%4034%3BkK%23%5B%5DzZ(5)6%257%3F_%7C%5C%09%60%5E%26qQ%2B%24'~%E2%80%94": {
"https://fonts.googleapis.com/css2?family=Inter:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,400;1,700&text=%3C!DdOoCcTtYyPpEe%20hHmMlL%3E%0AaAnNgG%3D%22sS-rRfFxXiIuU%2CbB*%7B%3A%7D%2F8vVwW1920.jJ%4034%3BkK%23%5B%5DzZ(5)6%257%3F_%7CqQ%5C%09%60%5E%26%2B%24'~%E2%80%94": {
"status": 200,
"contentType": "text/css; charset=utf-8",
"file": "13ea284e2ea4ac4ab37c98e5.bin"
},
"https://fonts.googleapis.com/css2?family=Inter:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,400;1,700&text=%3C!DdOoCcTtYyPpEe%20hHmMlL%3E%0AaAnNgG%3D%22sS-rRfFxXiIuU%2CbB*%7B%3A%7D%2F8vVwW1920.jJ%4034%3BkK%23zZ%5B%5D()765%25%60_%E2%8C%84%E2%97%87%E2%80%BA%7C%3F%5C%09%5E%26qQ%2B%24'~%E2%80%94": {
"https://fonts.googleapis.com/css2?family=Inter:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,400;1,700&text=%3C!DdOoCcTtYyPpEe%20hHmMlL%3E%0AaAnNgG%3D%22sS-rRfFxXiIuU%2CbB*%7B%3A%7D%2F8vVwW1920.jJ%4034%3BkK%23zZ%5B%5D()765%25%60_%E2%8C%84%E2%97%87%E2%80%BA%7CqQ%3F%5C%09%5E%26%2B%24'~%E2%80%94": {
"status": 200,
"contentType": "text/css; charset=utf-8",
"file": "0529bdbdcf740f78d5b9e331.bin"
},
"https://fonts.googleapis.com/css2?family=Inter:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,400;1,700&text=%3C!DdOoCcTtYyPpEe%20hHmMlL%3E%0AaAnNgG%3D%22sS-rRfFxXiIuU%2CbB*%7B%3A%7D%2F8vVwW1920.jJ%4034%3BkK%23zZ%5B6%5D()75%25%60_%E2%8C%84%E2%97%87%E2%80%BA%7C%3F%5C%09%5E%26qQ%2B%24'~%E2%80%94": {
"https://fonts.googleapis.com/css2?family=Inter:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,400;1,700&text=%3C!DdOoCcTtYyPpEe%20hHmMlL%3E%0AaAnNgG%3D%22sS-rRfFxXiIuU%2CbB*%7B%3A%7D%2F8vVwW1920.jJ%4034%3BkK%23zZ%5B6%5D()75%25%60_%E2%8C%84%E2%97%87%E2%80%BA%7CqQ%3F%5C%09%5E%26%2B%24'~%E2%80%94": {
"status": 200,
"contentType": "text/css; charset=utf-8",
"file": "0529bdbdcf740f78d5b9e331.bin"
},
"https://fonts.googleapis.com/css2?family=Inter:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,400;1,700&text=%3C!DdOoCcTtYyPpEe%20hHmMlL%3E%0AaAnNgG%3D%22sS-rRfFxXiIuU%2CbB*%7B%3A%7D%2F8vVwW1920.jJ%4034zZ%3B%23kK5%25%5B%5D()76%C2%B7_%7C%3F%5C%09%60%5E%26qQ%2B%24'~%E2%80%94": {
"https://fonts.googleapis.com/css2?family=Inter:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,400;1,700&text=%3C!DdOoCcTtYyPpEe%20hHmMlL%3E%0AaAnNgG%3D%22sS-rRfFxXiIuU%2CbB*%7B%3A%7D%2F8vVwW1920.jJ%4034zZ%3B%23kK5%25%5B%5D()76%C2%B7_%7CqQ%3F%5C%09%60%5E%26%2B%24'~%E2%80%94": {
"status": 200,
"contentType": "text/css; charset=utf-8",
"file": "bed471e981d73b3db0c86a41.bin"
},
"https://fonts.googleapis.com/css2?family=JetBrains%20Mono:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,400;1,700&text=%3C!DdOoCcTtYyPpEe%20hHmMlL%3E%0AaAnNgG%3D%22sS-rRfFxXiIuU%2CbB*%7B%3A%7D%2F8vVwW1920.jJ%4034%3BkK%23zZ%5B%5D()765%25%60_%E2%8C%84%E2%97%87%E2%80%BA%7C%3F%5C%09%5E%26qQ%2B%24'~%E2%80%94": {
"https://fonts.googleapis.com/css2?family=JetBrains%20Mono:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,400;1,700&text=%3C!DdOoCcTtYyPpEe%20hHmMlL%3E%0AaAnNgG%3D%22sS-rRfFxXiIuU%2CbB*%7B%3A%7D%2F8vVwW1920.jJ%4034%3BkK%23zZ%5B%5D()765%25%60_%E2%8C%84%E2%97%87%E2%80%BA%7CqQ%3F%5C%09%5E%26%2B%24'~%E2%80%94": {
"status": 200,
"contentType": "text/css; charset=utf-8",
"file": "89a83af9f5863c5898d0ba50.bin"
},
"https://fonts.googleapis.com/css2?family=JetBrains%20Mono:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,400;1,700&text=%3C!DdOoCcTtYyPpEe%20hHmMlL%3E%0AaAnNgG%3D%22sS-rRfFxXiIuU%2CbB*%7B%3A%7D%2F8vVwW1920.jJ%4034%3BkK%23zZ%5B6%5D()75%25%60_%E2%8C%84%E2%97%87%E2%80%BA%7C%3F%5C%09%5E%26qQ%2B%24'~%E2%80%94": {
"https://fonts.googleapis.com/css2?family=JetBrains%20Mono:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,400;1,700&text=%3C!DdOoCcTtYyPpEe%20hHmMlL%3E%0AaAnNgG%3D%22sS-rRfFxXiIuU%2CbB*%7B%3A%7D%2F8vVwW1920.jJ%4034%3BkK%23zZ%5B6%5D()75%25%60_%E2%8C%84%E2%97%87%E2%80%BA%7CqQ%3F%5C%09%5E%26%2B%24'~%E2%80%94": {
"status": 200,
"contentType": "text/css; charset=utf-8",
"file": "89a83af9f5863c5898d0ba50.bin"
},
"https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,400;1,700&text=%3C!DdOoCcTtYyPpEe%20hHmMlL%3E%0AaAnNgG%3D%22sS-rRfFxXiIuU%2CbB*%7B%3A%7D%2F8vVwW1920.jJ%4034%3BkK%23%5B%5DzZ(5)6%257%3F_%7C%5C%09%60%5E%26qQ%2B%24'~%E2%80%94": {
"https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,400;1,700&text=%3C!DdOoCcTtYyPpEe%20hHmMlL%3E%0AaAnNgG%3D%22sS-rRfFxXiIuU%2CbB*%7B%3A%7D%2F8vVwW1920.jJ%4034%3BkK%23%5B%5DzZ(5)6%257%3F_%7CqQ%5C%09%60%5E%26%2B%24'~%E2%80%94": {
"status": 200,
"contentType": "text/css; charset=utf-8",
"file": "852b31c7d0aecc0f1843bc09.bin"
},
"https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,400;1,700&text=%3C!DdOoCcTtYyPpEe%20hHmMlL%3E%0AaAnNgG%3D%22sS-rRfFxXiIuU%2CbB*%7B%3A%7D%2F8vVwW1920.jJ%4034%3BkK%23zZ%5B%5D()765%25%60_%E2%8C%84%E2%97%87%E2%80%BA%7C%3F%5C%09%5E%26qQ%2B%24'~%E2%80%94": {
"https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,400;1,700&text=%3C!DdOoCcTtYyPpEe%20hHmMlL%3E%0AaAnNgG%3D%22sS-rRfFxXiIuU%2CbB*%7B%3A%7D%2F8vVwW1920.jJ%4034%3BkK%23zZ%5B%5D()765%25%60_%E2%8C%84%E2%97%87%E2%80%BA%7CqQ%3F%5C%09%5E%26%2B%24'~%E2%80%94": {
"status": 200,
"contentType": "text/css; charset=utf-8",
"file": "ae13656ef251eb2b9f869407.bin"
},
"https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,400;1,700&text=%3C!DdOoCcTtYyPpEe%20hHmMlL%3E%0AaAnNgG%3D%22sS-rRfFxXiIuU%2CbB*%7B%3A%7D%2F8vVwW1920.jJ%4034%3BkK%23zZ%5B6%5D()75%25%60_%E2%8C%84%E2%97%87%E2%80%BA%7C%3F%5C%09%5E%26qQ%2B%24'~%E2%80%94": {
"https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,400;1,700&text=%3C!DdOoCcTtYyPpEe%20hHmMlL%3E%0AaAnNgG%3D%22sS-rRfFxXiIuU%2CbB*%7B%3A%7D%2F8vVwW1920.jJ%4034%3BkK%23zZ%5B6%5D()75%25%60_%E2%8C%84%E2%97%87%E2%80%BA%7CqQ%3F%5C%09%5E%26%2B%24'~%E2%80%94": {
"status": 200,
"contentType": "text/css; charset=utf-8",
"file": "ae13656ef251eb2b9f869407.bin"
@@ -56,12 +56,12 @@
},
"https://fonts.gstatic.com/l/font?kit=KFO5CnqEu92Fr1Mu53ZEC9_Vu3r1gIhOszmUBHMdazTqPu8GDmixiyv7sHsO9kUd_1ZZwvfxd8PedhYMCPK-7KsgrD_GTTHR6jtWpE1QSC-n0tGj5Y0Iea5lPneAWMuTqRLj9_emvoF8BoqCq3MRviSSf0DASiOS6kszpAyy8pOPJOwPd1c&skey=c608c610063635f9&v=v51": {
"status": 200,
"contentType": "text/html; charset=utf-8",
"contentType": "font/woff2",
"file": "fa10510792f83e658fd729ad.bin"
},
"https://fonts.gstatic.com/l/font?kit=KFO7CnqEu92Fr1ME7kSn66aGLdTylUAMW36eA1EfUVDRUMYtDGq3jSn5vnUM9EMb_VRH3PXzccXcdBgCCvC46qki0kHETzfX6DlYqk9STiml0M-9548Of6xnMHmCWs2VqxDdyfWkuId-BISMqXEXuCaQYV7CSCWU6Ek9qg6w9JWNuxJ5&skey=a0a0114a1dcab3ac&v=v51": {
"status": 200,
"contentType": "text/html; charset=utf-8",
"contentType": "font/woff2",
"file": "b7ee14896537ab8f9ffaa90b.bin"
},
"https://fonts.gstatic.com/l/font?kit=KFO7CnqEu92Fr1ME7kSn66aGLdTylUAMW36eA1EfUVnRUMYtDGq3jSn5vnUM9EMb_VRH3PXzccXcdBgCCvC46qki0kHETzfX6DlYqk9STiml0M-9548Of6xnMHmCWs2VqxDdyfWkuId-BISMqXEXuCaQYV7CSCWU6Ek9qg6w9JWNuxJ5FUMVsrXuswTH&skey=a0a0114a1dcab3ac&v=v51": {
@@ -76,22 +76,22 @@
},
"https://fonts.gstatic.com/l/font?kit=UcC53FwrK3iLTcvneQg7Ca725JhhKnNqk5L7W0Q5n-ZxsX78MLpiqZCmvMf9UxqhNKllH1cNgx0bTsIBOBs7SNX-j4s4ylNm2UCwCZNIPBLj_BHVI3u89wkVczLFGLd6G0t36lHOtbPXenGd4StCfEyMjMdBkkEqHiUdbEm-sNOJof6Thg&skey=e5dfd0c3910c7ec9&v=v20": {
"status": 200,
"contentType": "text/html; charset=utf-8",
"contentType": "font/woff2",
"file": "d3b6c159f462183ba7796270.bin"
},
"https://fonts.gstatic.com/l/font?kit=UcC53FwrK3iLTcvneQg7Ca725JhhKnNqk5L7W0Q5n-ZzsX78MLpiqZCmvMf9UxqhNKllH1cNgx0bTsIBOBs7SNX-j4s4ylNm2UCwCZNIPBLj_BHVI3u89wkVczLFGLd6G0t36lHOtbPXenGd4StCfEyMjMdBkkEqHiUdbEm-sNOJod6k8C7f&skey=e5dfd0c3910c7ec9&v=v20": {
"status": 200,
"contentType": "text/html; charset=utf-8",
"contentType": "font/woff2",
"file": "9fca1e9095ed294968aa18f7.bin"
},
"https://fonts.gstatic.com/l/font?kit=UcC73FwrK3iLTeHuS_nVMrMxCp50SjIq15j8eUYD-90fmFX-Mrxkq5KossX_VRyjNrd7HVULhR8ZQMwDOh09SteA8Yk6zFVk206-C5FOOhDh4g_XIX269QsbfTDHHrF4GXVJ6FPIs7HVdH-f4y1Efk6SksVDlEcoHCsTbku4ttEWX4g&skey=c491285d6722e4fa&v=v20": {
"status": 200,
"contentType": "text/html; charset=utf-8",
"contentType": "font/woff2",
"file": "8bfba0d6e85d9f059d9b5982.bin"
},
"https://fonts.gstatic.com/l/font?kit=UcC73FwrK3iLTeHuS_nVMrMxCp50SjIq15j8eUYD-d0fmFX-Mrxkq5KossX_VRyjNrd7HVULhR8ZQMwDOh09SteA8Yk6zFVk206-C5FOOhDh4g_XIX269QsbfTDHHrF4GXVJ6FPIs7HVdH-f4y1Efk6SksVDlEcoHCsTbku4ttE2aP6Thg&skey=c491285d6722e4fa&v=v20": {
"status": 200,
"contentType": "text/html; charset=utf-8",
"contentType": "font/woff2",
"file": "57dacd2ef96ca543d3e0e6a3.bin"
},
"https://fonts.gstatic.com/l/font?kit=UcC73FwrK3iLTeHuS_nVMrMxCp50SjIq15j8eUYD8t0fmFX-Mrxkq5KossX_VRyjNrd7HVULhR8ZQMwDOh09SteA8Yk6zFVk206-C5FOOhDh4g_XIX269QsbfTDHHrF4GXVJ6FPIs7HVdH-f4y1Efk6SksVDlEcoHCsTbku4ttEWX4jxkhSpeRcu3GQ&skey=c491285d6722e4fa&v=v20": {