diff --git a/src/core/webidl.js b/src/core/webidl.js index 21afbaa173..e248dd1a64 100644 --- a/src/core/webidl.js +++ b/src/core/webidl.js @@ -333,6 +333,7 @@ function renderWebIDL(idlElement, index) { // Skip this
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 }); @@ -364,6 +365,29 @@ 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} parse + */ +function addDataDfnFor(idlElement, parse) { + const closestSection = idlElement.closest("section"); + if (!closestSection || closestSection.hasAttribute("data-dfn-for")) return; + + const dfnFors = []; + for (const { tokens } of parse) { + if (["interface", "dictionary", "enum"].includes(tokens.base.type)) { + const dfnFor = tokens.name.value; + dfnFors.push(dfnFor); + } + } + if (dfnFors.length === 1) { + closestSection.dataset.dfnFor = dfnFors[0]; + } + // TODO: make linker support multiple dfnFors +} + /** * Adds a "WebIDL" decorative header/permalink to a block of WebIDL. * @param {HTMLPreElement} pre