Skip to content

Commit fa9b5ea

Browse files
feat(core/dfn-index): add prefer-full-spec-title class and localize text (#5073)
1 parent 3485208 commit fa9b5ea

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

src/core/dfn-index.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import { addId, getIntlData, norm, xmlEscape } from "./utils.js";
9+
import { biblio } from "./biblio.js";
910
import css from "../styles/dfn-index.css.js";
1011
import { getTermFromElement } from "./xref.js";
1112
import { html } from "./import-maps.js";
@@ -21,48 +22,56 @@ const localizationStrings = {
2122
headingExternal: "Terms defined by reference",
2223
headingLocal: "Terms defined by this specification",
2324
dfnOf: "definition of",
25+
definesFollowing: "defines the following:",
2426
},
2527
cs: {
2628
heading: "Glosář",
2729
headingExternal: "Termíny definované odkazem",
2830
headingLocal: "Termíny definované touto specifikací",
2931
dfnOf: "definice",
32+
definesFollowing: "definuje následující:",
3033
},
3134
de: {
3235
heading: "Index",
3336
headingExternal: "Begriffe, die durch Verweis definiert sind",
3437
headingLocal: "Begriffe, die in dieser Spezifikation definiert sind",
3538
dfnOf: "Definition von",
39+
definesFollowing: "definiert Folgendes:",
3640
},
3741
es: {
3842
heading: "Índice",
3943
headingExternal: "Términos definidos por referencia",
4044
headingLocal: "Términos definidos por esta especificación",
4145
dfnOf: "definición de",
46+
definesFollowing: "define lo siguiente:",
4247
},
4348
ja: {
4449
heading: "索引",
4550
headingExternal: "参照によって定義された用語",
4651
headingLocal: "この仕様で定義された用語",
4752
dfnOf: "の定義",
53+
definesFollowing: "以下を定義します:",
4854
},
4955
ko: {
5056
heading: "색인",
5157
headingExternal: "참조로 정의된 용어",
5258
headingLocal: "이 명세서에서 정의된 용어",
5359
dfnOf: "정의",
60+
definesFollowing: "다음을 정의합니다:",
5461
},
5562
nl: {
5663
heading: "Index",
5764
headingExternal: "Termen gedefinieerd door verwijzing",
5865
headingLocal: "Termen gedefinieerd door deze specificatie",
5966
dfnOf: "definitie van",
67+
definesFollowing: "definieert het volgende:",
6068
},
6169
zh: {
6270
heading: "索引",
6371
headingExternal: "通过引用定义的术语",
6472
headingLocal: "由本规范定义的术语",
6573
dfnOf: "的定义",
74+
definesFollowing: "定义以下内容:",
6675
},
6776
};
6877
const l10n = getIntlData(localizationStrings);
@@ -283,10 +292,21 @@ function createExternalTermIndex() {
283292
const dataSortedBySpec = [...data.entries()].sort(([specA], [specB]) =>
284293
specA.localeCompare(specB)
285294
);
295+
const indexSection = document.querySelector("section#index");
296+
const useFullTitle = !!indexSection?.classList.contains(
297+
"prefer-full-spec-title"
298+
);
286299
return html`<ul class="index">
287300
${dataSortedBySpec.map(([spec, entries]) => {
301+
let citationElement;
302+
if (useFullTitle && biblio[spec]?.title) {
303+
citationElement = renderInlineCitation(spec, biblio[spec].title);
304+
} else {
305+
citationElement = renderInlineCitation(spec);
306+
}
307+
288308
return html`<li data-spec="${spec}">
289-
${renderInlineCitation(spec)} defines the following:
309+
${citationElement} ${l10n.definesFollowing}
290310
<ul>
291311
${entries
292312
.sort((a, b) => a.term.localeCompare(b.term))

tests/spec/core/dfn-index-spec.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,28 @@ describe("Core — dfn-index", () => {
154154
expect(secNum.textContent).toBe("§1.");
155155
expect(item.textContent.endsWith("§1.")).toBeTrue();
156156
});
157+
158+
it("prefer-full-spec-title class doesn't affect local terms", async () => {
159+
const body = `<section id="content">
160+
<h2>Whatever</h2>
161+
<p class="test"><dfn>hello</dfn> <dfn>bar</dfn></p>
162+
</section>
163+
<section id="index" class="prefer-full-spec-title"></section>`;
164+
const ops = makeStandardOps(null, body);
165+
const doc = await makeRSDoc(ops);
166+
const localIndex = doc.getElementById("index-defined-here");
167+
168+
// Local terms should not be affected by prefer-full-spec-title class
169+
// They should still show just the term names without any spec citations
170+
const terms = [...localIndex.querySelectorAll("ul.index > li")].map(
171+
term => term.textContent.trim().split(/\s/)[0]
172+
);
173+
expect(terms).toEqual(["bar", "hello"]);
174+
175+
// Verify no citation elements are present in local terms
176+
expect(localIndex.querySelectorAll("cite")).toHaveSize(0);
177+
expect(localIndex.querySelectorAll(".bibref")).toHaveSize(0);
178+
});
157179
});
158180

159181
describe("External Terms Index", () => {
@@ -281,6 +303,51 @@ describe("Core — dfn-index", () => {
281303
expect(termsInDom).toHaveSize(4);
282304
});
283305

306+
it("uses localized text for 'defines the following'", async () => {
307+
// Test with default language (English)
308+
const bySpecs = index.querySelectorAll("ul.index > li");
309+
expect(bySpecs[0].textContent.trim()).toMatch(
310+
/\[DOM\] defines the following:/
311+
);
312+
313+
// Test with a different language
314+
const body = ` <section id="content">
315+
<p>{{ Event }}</p>
316+
</section>
317+
<section id="index"></section>`;
318+
const ops = makeStandardOps({ xref: "web-platform" }, body);
319+
ops.htmlAttrs = { lang: "es" };
320+
const doc = await makeRSDoc(ops);
321+
const spanishIndex = doc.getElementById("index-defined-elsewhere");
322+
const spanishBySpecs = spanishIndex.querySelectorAll("ul.index > li");
323+
if (spanishBySpecs.length > 0) {
324+
expect(spanishBySpecs[0].textContent.trim()).toMatch(
325+
/\[DOM\] define lo siguiente:/
326+
);
327+
}
328+
});
329+
330+
it("uses full spec title when prefer-full-spec-title class is present", async () => {
331+
const body = ` <section id="content">
332+
<p>{{ Event }}</p>
333+
</section>
334+
<section id="index" class="prefer-full-spec-title"></section>`;
335+
const ops = makeStandardOps({ xref: "web-platform" }, body);
336+
const doc = await makeRSDoc(ops);
337+
const fullTitleIndex = doc.getElementById("index-defined-elsewhere");
338+
const bySpecs = fullTitleIndex.querySelectorAll("ul.index > li");
339+
340+
if (bySpecs.length > 0) {
341+
// Should use full title instead of just [DOM]
342+
expect(bySpecs[0].textContent.trim()).toMatch(
343+
/DOM Standard defines the following:/
344+
);
345+
expect(bySpecs[0].textContent.trim()).not.toMatch(
346+
/^\[DOM\] defines the following:/
347+
);
348+
}
349+
});
350+
284351
it("lists terms in a spec in sorted order", () => {
285352
const termsInDom = index.querySelectorAll("[data-spec='DOM'] li");
286353
expect(termsInDom).toHaveSize(4);

0 commit comments

Comments
 (0)