Skip to content

Commit 0a49a58

Browse files
authored
fix(core/dfn): add contractDefaults, dfnPointers before running linters (#4664)
1 parent ea0ca4a commit 0a49a58

File tree

6 files changed

+65
-62
lines changed

6 files changed

+65
-62
lines changed

profiles/aom.js

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const modules = [
4444
import("../src/core/anchor-expander.js"),
4545
import("../src/core/dfn-panel.js"),
4646
import("../src/core/custom-elements/index.js"),
47+
import("../src/core/dfn-contract.js"),
4748
/* Linter must be the last thing to run */
4849
import("../src/core/linter-rules/check-charset.js"),
4950
import("../src/core/linter-rules/check-punctuation.js"),

profiles/dini.js

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const modules = [
4444
import("../src/core/anchor-expander.js"),
4545
import("../src/core/dfn-panel.js"),
4646
import("../src/core/custom-elements/index.js"),
47+
import("../src/core/dfn-contract.js"),
4748
/* Linter must be the last thing to run */
4849
import("../src/core/linter-rules/check-charset.js"),
4950
import("../src/core/linter-rules/check-punctuation.js"),

profiles/geonovum.js

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const modules = [
4242
import("../src/core/algorithms.js"),
4343
import("../src/core/anchor-expander.js"),
4444
import("../src/core/dfn-panel.js"),
45+
import("../src/core/dfn-contract.js"),
4546
/* Linter must be the last thing to run */
4647
import("../src/core/linter-rules/check-charset.js"),
4748
import("../src/core/linter-rules/check-punctuation.js"),

profiles/w3c.js

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const modules = [
6060
import("../src/core/dfn-panel.js"),
6161
import("../src/core/custom-elements/index.js"),
6262
import("../src/core/web-monetization.js"),
63+
import("../src/core/dfn-contract.js"),
6364
import("../src/core/before-save.js"),
6465
/* Linters must be the last thing to run */
6566
import("../src/core/linter-rules/check-charset.js"),

src/core/dfn-contract.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
export const name = "core/dfn-contract";
2+
3+
export function run() {
4+
addContractDefaults();
5+
addDefinitionPointers();
6+
}
7+
8+
function addContractDefaults() {
9+
// Find all dfns that don't have a type and default them to "dfn".
10+
/** @type NodeListOf<HTMLElement> */
11+
const dfnsWithNoType = document.querySelectorAll(
12+
"dfn:is([data-dfn-type=''],:not([data-dfn-type]))"
13+
);
14+
for (const dfn of dfnsWithNoType) {
15+
dfn.dataset.dfnType = "dfn";
16+
}
17+
18+
// Per "the contract", export all definitions, except where:
19+
// - Explicitly marked with data-noexport.
20+
// - The type is "dfn" and not explicitly marked for export (i.e., just a regular definition).
21+
// - definitions was included via (legacy) data-cite="foo#bar".
22+
/** @type NodeListOf<HTMLElement> */
23+
const exportableDfns = document.querySelectorAll(
24+
"dfn:not([data-noexport], [data-export], [data-dfn-type='dfn'], [data-cite])"
25+
);
26+
for (const dfn of exportableDfns) {
27+
dfn.dataset.export = "";
28+
}
29+
}
30+
31+
// - Sets data-defines on well-known definition content patterns
32+
function addDefinitionPointers() {
33+
// A dl with class hasdefinitions associated the dfn in each dt
34+
// the definition in the following sibling element
35+
/** @type NodeListOf<HTMLElement> */
36+
const describedDTs = document.querySelectorAll(
37+
"dl.definitions dt:has(dfn[data-dfn-type])"
38+
);
39+
for (const dt of describedDTs) {
40+
const dfnId = dt.querySelector("dfn[data-dfn-type]").id;
41+
const dfnContent = /** @type {HTMLElement | null} */ (
42+
dt.nextElementSibling
43+
);
44+
if (dfnContent && !dfnContent.dataset.defines && dfnId) {
45+
dfnContent.dataset.defines = `#${dfnId}`;
46+
}
47+
}
48+
49+
// an element with class "definition" is marked as defining the term
50+
// found in the element
51+
/** @type NodeListOf<HTMLElement> */
52+
const definitionContainers = document.querySelectorAll(
53+
".definition:has(dfn[data-dfn-type])"
54+
);
55+
for (const el of definitionContainers) {
56+
const dfn = el.querySelector("dfn[data-dfn-type]");
57+
if (dfn.id && !el.dataset.defines) {
58+
el.dataset.defines = `#${dfn.id}`;
59+
}
60+
}
61+
}

src/core/dfn.js

-62
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
} from "./dfn-validators.js";
1919
import { registerDefinition } from "./dfn-map.js";
2020
import { slotRegex } from "./inline-idl-parser.js";
21-
import { sub } from "./pubsubhub.js";
2221

2322
export const name = "core/dfn";
2423

@@ -78,7 +77,6 @@ export function run() {
7877
}
7978
dfn.dataset.lt = titles.join("|");
8079
}
81-
sub("plugins-done", completeDefinitionMarkup);
8280
}
8381

8482
/**
@@ -215,63 +213,3 @@ function processAsInternalSlot(title, dfn) {
215213
}
216214
return dfnType;
217215
}
218-
219-
function completeDefinitionMarkup() {
220-
addContractDefaults();
221-
addDefinitionPointers();
222-
}
223-
224-
function addContractDefaults() {
225-
// Find all dfns that don't have a type and default them to "dfn".
226-
/** @type NodeListOf<HTMLElement> */
227-
const dfnsWithNoType = document.querySelectorAll(
228-
"dfn:is([data-dfn-type=''],:not([data-dfn-type]))"
229-
);
230-
for (const dfn of dfnsWithNoType) {
231-
dfn.dataset.dfnType = "dfn";
232-
}
233-
234-
// Per "the contract", export all definitions, except where:
235-
// - Explicitly marked with data-noexport.
236-
// - The type is "dfn" and not explicitly marked for export (i.e., just a regular definition).
237-
// - definitions was included via (legacy) data-cite="foo#bar".
238-
/** @type NodeListOf<HTMLElement> */
239-
const exportableDfns = document.querySelectorAll(
240-
"dfn:not([data-noexport], [data-export], [data-dfn-type='dfn'], [data-cite])"
241-
);
242-
for (const dfn of exportableDfns) {
243-
dfn.dataset.export = "";
244-
}
245-
}
246-
247-
// - Sets data-defines on well-known definition content patterns
248-
function addDefinitionPointers() {
249-
// A dl with class hasdefinitions associated the dfn in each dt
250-
// the definition in the following sibling element
251-
/** @type NodeListOf<HTMLElement> */
252-
const describedDTs = document.querySelectorAll(
253-
"dl.definitions dt:has(dfn[data-dfn-type])"
254-
);
255-
for (const dt of describedDTs) {
256-
const dfnId = dt.querySelector("dfn[data-dfn-type]").id;
257-
const dfnContent = /** @type {HTMLElement | null} */ (
258-
dt.nextElementSibling
259-
);
260-
if (dfnContent && !dfnContent.dataset.defines && dfnId) {
261-
dfnContent.dataset.defines = `#${dfnId}`;
262-
}
263-
}
264-
265-
// an element with class "definition" is marked as defining the term
266-
// found in the element
267-
/** @type NodeListOf<HTMLElement> */
268-
const definitionContainers = document.querySelectorAll(
269-
".definition:has(dfn[data-dfn-type])"
270-
);
271-
for (const el of definitionContainers) {
272-
const dfn = el.querySelector("dfn[data-dfn-type]");
273-
if (dfn.id && !el.dataset.defines) {
274-
el.dataset.defines = `#${dfn.id}`;
275-
}
276-
}
277-
}

0 commit comments

Comments
 (0)