Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
fix(core/dfn): add contractDefaults, dfnPointers before running linte…
Browse files Browse the repository at this point in the history
…rs (speced#4664)
  • Loading branch information
sidvishnoi committed Mar 11, 2024
1 parent ea0ca4a commit 0a49a58
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 62 deletions.
1 change: 1 addition & 0 deletions profiles/aom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
1 change: 1 addition & 0 deletions profiles/dini.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
1 change: 1 addition & 0 deletions profiles/geonovum.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
1 change: 1 addition & 0 deletions profiles/w3c.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
61 changes: 61 additions & 0 deletions src/core/dfn-contract.js
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}`;
}
}
}
62 changes: 0 additions & 62 deletions src/core/dfn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -78,7 +77,6 @@ export function run() {
}
dfn.dataset.lt = titles.join("|");
}
sub("plugins-done", completeDefinitionMarkup);
}

/**
Expand Down Expand Up @@ -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<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}`;
}
}
}

0 comments on commit 0a49a58

Please sign in to comment.