Skip to content
2 changes: 1 addition & 1 deletion src/core/dfn-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { definitionMap, registerDefinition } from "./dfn-map.js";
import { showInlineError, wrapInner } from "./utils.js";

const topLevelEntities = new Set([
export const topLevelEntities = new Set([
"callback interface",
"callback",
"dictionary",
Expand Down
33 changes: 32 additions & 1 deletion src/core/webidl.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
showInlineWarning,
xmlEscape,
} from "./utils.js";
import { decorateDfn, findDfn } from "./dfn-finder.js";
import { decorateDfn, findDfn, topLevelEntities } from "./dfn-finder.js";
import { html, webidl2 } from "./import-maps.js";
import { addCopyIDLButton } from "./webidl-clipboard.js";
import { fetchAsset } from "./text-loader.js";
Expand Down Expand Up @@ -328,6 +328,7 @@ function renderWebIDL(idlElement, index) {
// Skip this <pre> and move on to the next one.
return [];
}
addDataDfnFor(idlElement, parse);
// we add "idl" as the canonical match, so both "webidl" and "idl" work
idlElement.classList.add("def", "idl");
const highlights = webidl2.write(parse, { templates });
Expand Down Expand Up @@ -358,6 +359,36 @@ function renderWebIDL(idlElement, index) {
addIDLHeader(idlElement);
return parse;
}

/**
* Add data-dfn-for to the closest section if not present already.
* @param {HTMLPreElement} idlElement
* @param {ReturnType<typeof webidl2.parse>} parse
*/
function addDataDfnFor(idlElement, parse) {
const closestSection = idlElement.closest("section");
if (closestSection.hasAttribute("data-dfn-for")) return;

const dfnFors = [];
for (const { tokens } of parse) {
if (topLevelEntities.has(tokens.base.type)) {
const dfnFor = tokens.name.value;
dfnFors.push(dfnFor);
}
}
if (dfnFors.length === 1) {
closestSection.dataset.dfnFor = dfnFors[0];
} else if (!dfnFors.length) {
return;
} else {
// closestSection.dataset.dfnFor = "";
const options = dfnFors.map(dfnFor => `"${dfnFor}"`).join(", ");
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should not suggest these options? User might want to add something else?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, don't suggest these yet... remember that this currently works today:

<section data-dfn-for="Whatever">
   
    <section>
    <pre class="idl">
        interface Whatever{};
        interface SomethingElse{};
    <pre>
    </section>
</section>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, the above is probably bad practice and doesn't matter...

const title = "Ambiguous data-dfn-for attribute";
const message = `${title}. Sections describing top-level IDL entities (${topLevelEntities}) require a \`data-dfn-attribute\`. Please add a \`data-dfn-for\` attribute with one of following values: ${options}`;
showInlineError(closestSection, message, title);
}
}

/**
* Adds a "WebIDL" decorative header/permalink to a block of WebIDL.
* @param {HTMLPreElement} pre
Expand Down