forked from w3c/respec
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core/dfn): add contractDefaults, dfnPointers before running linte…
…rs (speced#4664)
- Loading branch information
1 parent
ea0ca4a
commit 0a49a58
Showing
6 changed files
with
65 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<HTMLElement> */ | ||
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<HTMLElement> */ | ||
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<HTMLElement> */ | ||
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<HTMLElement> */ | ||
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}`; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters