Skip to content

Commit 2a6772c

Browse files
committed
fix(xref/headings): trim inputs, add version/series fallback in getHeading
Per Copilot review: - Trim spec and id before lookup (validation checked trim but passed raw) - Add version-stripped and series-shortname fallback in getHeading so both "cssom-view-1" and "cssom-view" resolve headings correctly - Also resolves specTitle via stripped form as fallback
1 parent d1cbc2d commit 2a6772c

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

routes/xref/headings.post.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default function route(req: IRequest, res: Response) {
4747
}
4848
}
4949
const result = queries.map(({ spec, id }) => {
50-
const heading = store.getHeading(spec, id);
50+
const heading = store.getHeading(spec.trim(), id.trim());
5151
if (!heading) {
5252
return { spec, id, error: "not found" };
5353
}

routes/xref/lib/store.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,46 @@ export class Store {
4141
id: string,
4242
): (HeadingEntry & { specTitle: string }) | null {
4343
const normalizedSpec = spec.toLowerCase();
44-
const specHeadings = this.headings[normalizedSpec];
44+
let specHeadings = this.headings[normalizedSpec];
45+
// Fallback: try stripping version suffix (e.g., cssom-view → cssom-view-1)
46+
// or adding it via specmap lookup
47+
if (!specHeadings) {
48+
const stripped = normalizedSpec.replace(/-\d+$/, "");
49+
if (stripped !== normalizedSpec) {
50+
specHeadings = this.headings[stripped];
51+
}
52+
if (!specHeadings) {
53+
// Try resolving series shortname to versioned via specmap
54+
for (const [specId, entry] of this.specmapEntries()) {
55+
if (entry.shortname === normalizedSpec || entry.shortname === stripped) {
56+
specHeadings = this.headings[specId];
57+
if (specHeadings) break;
58+
}
59+
}
60+
}
61+
}
4562
if (!specHeadings) return null;
4663

4764
const heading = specHeadings[id];
4865
if (!heading) return null;
4966

5067
return {
5168
...heading,
52-
specTitle: this.specTitleByShortname.get(normalizedSpec) || spec,
69+
specTitle: this.specTitleByShortname.get(normalizedSpec)
70+
|| this.specTitleByShortname.get(normalizedSpec.replace(/-\d+$/, ""))
71+
|| spec,
5372
};
5473
}
74+
75+
private *specmapEntries() {
76+
for (const group of Object.values(this.specmap)) {
77+
for (const [specId, entry] of Object.entries(
78+
group as Record<string, { shortname: string; title: string }>
79+
)) {
80+
yield [specId, entry] as const;
81+
}
82+
}
83+
}
5584
}
5685

5786
/** Read a required JSON data file. Throws if missing. */

0 commit comments

Comments
 (0)