diff --git a/profiles/aom.js b/profiles/aom.js index 3b71513814..175c33e934 100644 --- a/profiles/aom.js +++ b/profiles/aom.js @@ -44,6 +44,7 @@ const modules = [ import("../src/core/anchor-expander.js"), import("../src/core/dfn-panel.js"), import("../src/core/custom-elements/index.js"), + import("../src/core/dfn-contract.js"), /* Linter must be the last thing to run */ import("../src/core/linter-rules/check-charset.js"), import("../src/core/linter-rules/check-punctuation.js"), diff --git a/profiles/dini.js b/profiles/dini.js index 2d5b9a08f7..794b8654b9 100644 --- a/profiles/dini.js +++ b/profiles/dini.js @@ -44,6 +44,7 @@ const modules = [ import("../src/core/anchor-expander.js"), import("../src/core/dfn-panel.js"), import("../src/core/custom-elements/index.js"), + import("../src/core/dfn-contract.js"), /* Linter must be the last thing to run */ import("../src/core/linter-rules/check-charset.js"), import("../src/core/linter-rules/check-punctuation.js"), diff --git a/profiles/geonovum.js b/profiles/geonovum.js index 6562eb10d2..34f17fc740 100644 --- a/profiles/geonovum.js +++ b/profiles/geonovum.js @@ -42,6 +42,7 @@ const modules = [ import("../src/core/algorithms.js"), import("../src/core/anchor-expander.js"), import("../src/core/dfn-panel.js"), + import("../src/core/dfn-contract.js"), /* Linter must be the last thing to run */ import("../src/core/linter-rules/check-charset.js"), import("../src/core/linter-rules/check-punctuation.js"), diff --git a/profiles/w3c.js b/profiles/w3c.js index fbb00036d1..50537315c9 100644 --- a/profiles/w3c.js +++ b/profiles/w3c.js @@ -60,6 +60,7 @@ const modules = [ import("../src/core/dfn-panel.js"), import("../src/core/custom-elements/index.js"), import("../src/core/web-monetization.js"), + import("../src/core/dfn-contract.js"), import("../src/core/before-save.js"), /* Linters must be the last thing to run */ import("../src/core/linter-rules/check-charset.js"), diff --git a/src/core/dfn-contract.js b/src/core/dfn-contract.js new file mode 100644 index 0000000000..83bebdaa43 --- /dev/null +++ b/src/core/dfn-contract.js @@ -0,0 +1,61 @@ +export const name = "core/dfn-contract"; + +export function run() { + addContractDefaults(); + addDefinitionPointers(); +} + +function addContractDefaults() { + // Find all dfns that don't have a type and default them to "dfn". + /** @type NodeListOf */ + const dfnsWithNoType = document.querySelectorAll( + "dfn:is([data-dfn-type=''],:not([data-dfn-type]))" + ); + for (const dfn of dfnsWithNoType) { + dfn.dataset.dfnType = "dfn"; + } + + // Per "the contract", export all definitions, except where: + // - Explicitly marked with data-noexport. + // - The type is "dfn" and not explicitly marked for export (i.e., just a regular definition). + // - definitions was included via (legacy) data-cite="foo#bar". + /** @type NodeListOf */ + const exportableDfns = document.querySelectorAll( + "dfn:not([data-noexport], [data-export], [data-dfn-type='dfn'], [data-cite])" + ); + for (const dfn of exportableDfns) { + dfn.dataset.export = ""; + } +} + +// - Sets data-defines on well-known definition content patterns +function addDefinitionPointers() { + // A dl with class hasdefinitions associated the dfn in each dt + // the definition in the following sibling element + /** @type NodeListOf */ + const describedDTs = document.querySelectorAll( + "dl.definitions dt:has(dfn[data-dfn-type])" + ); + for (const dt of describedDTs) { + const dfnId = dt.querySelector("dfn[data-dfn-type]").id; + const dfnContent = /** @type {HTMLElement | null} */ ( + dt.nextElementSibling + ); + if (dfnContent && !dfnContent.dataset.defines && dfnId) { + dfnContent.dataset.defines = `#${dfnId}`; + } + } + + // an element with class "definition" is marked as defining the term + // found in the element + /** @type NodeListOf */ + const definitionContainers = document.querySelectorAll( + ".definition:has(dfn[data-dfn-type])" + ); + for (const el of definitionContainers) { + const dfn = el.querySelector("dfn[data-dfn-type]"); + if (dfn.id && !el.dataset.defines) { + el.dataset.defines = `#${dfn.id}`; + } + } +} diff --git a/src/core/dfn.js b/src/core/dfn.js index aac0e65ca6..59bb1dcc08 100644 --- a/src/core/dfn.js +++ b/src/core/dfn.js @@ -18,7 +18,6 @@ import { } from "./dfn-validators.js"; import { registerDefinition } from "./dfn-map.js"; import { slotRegex } from "./inline-idl-parser.js"; -import { sub } from "./pubsubhub.js"; export const name = "core/dfn"; @@ -78,7 +77,6 @@ export function run() { } dfn.dataset.lt = titles.join("|"); } - sub("plugins-done", completeDefinitionMarkup); } /** @@ -215,63 +213,3 @@ function processAsInternalSlot(title, dfn) { } return dfnType; } - -function completeDefinitionMarkup() { - addContractDefaults(); - addDefinitionPointers(); -} - -function addContractDefaults() { - // Find all dfns that don't have a type and default them to "dfn". - /** @type NodeListOf */ - const dfnsWithNoType = document.querySelectorAll( - "dfn:is([data-dfn-type=''],:not([data-dfn-type]))" - ); - for (const dfn of dfnsWithNoType) { - dfn.dataset.dfnType = "dfn"; - } - - // Per "the contract", export all definitions, except where: - // - Explicitly marked with data-noexport. - // - The type is "dfn" and not explicitly marked for export (i.e., just a regular definition). - // - definitions was included via (legacy) data-cite="foo#bar". - /** @type NodeListOf */ - const exportableDfns = document.querySelectorAll( - "dfn:not([data-noexport], [data-export], [data-dfn-type='dfn'], [data-cite])" - ); - for (const dfn of exportableDfns) { - dfn.dataset.export = ""; - } -} - -// - Sets data-defines on well-known definition content patterns -function addDefinitionPointers() { - // A dl with class hasdefinitions associated the dfn in each dt - // the definition in the following sibling element - /** @type NodeListOf */ - const describedDTs = document.querySelectorAll( - "dl.definitions dt:has(dfn[data-dfn-type])" - ); - for (const dt of describedDTs) { - const dfnId = dt.querySelector("dfn[data-dfn-type]").id; - const dfnContent = /** @type {HTMLElement | null} */ ( - dt.nextElementSibling - ); - if (dfnContent && !dfnContent.dataset.defines && dfnId) { - dfnContent.dataset.defines = `#${dfnId}`; - } - } - - // an element with class "definition" is marked as defining the term - // found in the element - /** @type NodeListOf */ - const definitionContainers = document.querySelectorAll( - ".definition:has(dfn[data-dfn-type])" - ); - for (const el of definitionContainers) { - const dfn = el.querySelector("dfn[data-dfn-type]"); - if (dfn.id && !el.dataset.defines) { - el.dataset.defines = `#${dfn.id}`; - } - } -}