From f6e324ceee88b687890c8f6256a4157603919461 Mon Sep 17 00:00:00 2001 From: Sid Vishnoi Date: Thu, 21 May 2020 16:02:02 +0530 Subject: [PATCH 1/9] feat(core/webidl): add data-dfn-for to IDL sections --- src/core/webidl.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/core/webidl.js b/src/core/webidl.js index 5f455f888b..86faa98a3f 100644 --- a/src/core/webidl.js +++ b/src/core/webidl.js @@ -328,6 +328,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 });
@@ -358,6 +359,38 @@ 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.hasAttribute("data-dfn-for")) return;
+
+  const topLevelEntities = ["dictionary", "interface", "callback interface"];
+
+  const dfnFors = [];
+  for (const { tokens } of parse) {
+    if (topLevelEntities.includes(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(", ");
+    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

From dd1ad56f165f1e27166686c6fbe385b9c66eccff Mon Sep 17 00:00:00 2001
From: Sid Vishnoi 
Date: Thu, 21 May 2020 16:19:20 +0530
Subject: [PATCH 2/9] maybe not set dfnFor when ambiguous

---
 src/core/webidl.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/core/webidl.js b/src/core/webidl.js
index 86faa98a3f..d6afdb3311 100644
--- a/src/core/webidl.js
+++ b/src/core/webidl.js
@@ -383,7 +383,7 @@ function addDataDfnFor(idlElement, parse) {
   } else if (!dfnFors.length) {
     return;
   } else {
-    closestSection.dataset.dfnFor = "";
+    // closestSection.dataset.dfnFor = "";
     const options = dfnFors.map(dfnFor => `"${dfnFor}"`).join(", ");
     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}`;

From 1cf76e2775baf0504a4bba49e803302898da6c72 Mon Sep 17 00:00:00 2001
From: Sid Vishnoi 
Date: Thu, 21 May 2020 16:26:43 +0530
Subject: [PATCH 3/9] export topLevelEntities from dfn-finder.js

---
 src/core/dfn-finder.js | 2 +-
 src/core/webidl.js     | 6 ++----
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/src/core/dfn-finder.js b/src/core/dfn-finder.js
index fb8e5b049f..cde499bdd8 100644
--- a/src/core/dfn-finder.js
+++ b/src/core/dfn-finder.js
@@ -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",
diff --git a/src/core/webidl.js b/src/core/webidl.js
index d6afdb3311..67ac0e9946 100644
--- a/src/core/webidl.js
+++ b/src/core/webidl.js
@@ -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";
@@ -369,11 +369,9 @@ function addDataDfnFor(idlElement, parse) {
   const closestSection = idlElement.closest("section");
   if (closestSection.hasAttribute("data-dfn-for")) return;
 
-  const topLevelEntities = ["dictionary", "interface", "callback interface"];
-
   const dfnFors = [];
   for (const { tokens } of parse) {
-    if (topLevelEntities.includes(tokens.base.type)) {
+    if (topLevelEntities.has(tokens.base.type)) {
       const dfnFor = tokens.name.value;
       dfnFors.push(dfnFor);
     }

From e28e2f34845f329eacda23d7e6c6d86a5ccb43d3 Mon Sep 17 00:00:00 2001
From: Sid Vishnoi 
Date: Thu, 21 May 2020 16:32:26 +0530
Subject: [PATCH 4/9] improve error message

---
 src/core/webidl.js | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/core/webidl.js b/src/core/webidl.js
index 67ac0e9946..05045f3459 100644
--- a/src/core/webidl.js
+++ b/src/core/webidl.js
@@ -381,11 +381,11 @@ function addDataDfnFor(idlElement, parse) {
   } else if (!dfnFors.length) {
     return;
   } else {
-    // closestSection.dataset.dfnFor = "";
-    const options = dfnFors.map(dfnFor => `"${dfnFor}"`).join(", ");
-    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);
+    const title = "data-dfn-for attribute not found";
+    const message = `${title}. Please add a \`data-dfn-for\` attribute`;
+    const topLevelStr = [...topLevelEntities].join(", ");
+    const details = `Sections describing top-level IDL entities (${topLevelStr}) require a \`data-dfn-attribute\``;
+    showInlineError(closestSection, message, title, { details });
   }
 }
 

From 79833a6b9fa20764d9564ca3c89066fcb15db19d Mon Sep 17 00:00:00 2001
From: Sid Vishnoi 
Date: Thu, 21 May 2020 16:37:53 +0530
Subject: [PATCH 5/9] Error not needed

---
 src/core/webidl.js | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)

diff --git a/src/core/webidl.js b/src/core/webidl.js
index 05045f3459..324c875888 100644
--- a/src/core/webidl.js
+++ b/src/core/webidl.js
@@ -376,17 +376,7 @@ function addDataDfnFor(idlElement, parse) {
       dfnFors.push(dfnFor);
     }
   }
-  if (dfnFors.length === 1) {
-    closestSection.dataset.dfnFor = dfnFors[0];
-  } else if (!dfnFors.length) {
-    return;
-  } else {
-    const title = "data-dfn-for attribute not found";
-    const message = `${title}. Please add a \`data-dfn-for\` attribute`;
-    const topLevelStr = [...topLevelEntities].join(", ");
-    const details = `Sections describing top-level IDL entities (${topLevelStr}) require a \`data-dfn-attribute\``;
-    showInlineError(closestSection, message, title, { details });
-  }
+  closestSection.dataset.dfnFor = dfnFors.join(" ");
 }
 
 /**

From 9184efb242b066247907970f876f96db431b20f6 Mon Sep 17 00:00:00 2001
From: Sid Vishnoi 
Date: Thu, 21 May 2020 16:48:53 +0530
Subject: [PATCH 6/9] support only single dfnFor

---
 src/core/webidl.js | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/core/webidl.js b/src/core/webidl.js
index 324c875888..e2d616fd69 100644
--- a/src/core/webidl.js
+++ b/src/core/webidl.js
@@ -376,7 +376,9 @@ function addDataDfnFor(idlElement, parse) {
       dfnFors.push(dfnFor);
     }
   }
-  closestSection.dataset.dfnFor = dfnFors.join(" ");
+  if (dfnFors.length === 1) {
+    closestSection.dataset.dfnFor = dfnFors[0];
+  }
 }
 
 /**

From fe08287d0da2999ad7f58d259333205160fd5a1a Mon Sep 17 00:00:00 2001
From: Sid Vishnoi 
Date: Thu, 21 May 2020 16:51:46 +0530
Subject: [PATCH 7/9] add TODO

---
 src/core/webidl.js | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/core/webidl.js b/src/core/webidl.js
index e2d616fd69..e63a3c5414 100644
--- a/src/core/webidl.js
+++ b/src/core/webidl.js
@@ -379,6 +379,7 @@ function addDataDfnFor(idlElement, parse) {
   if (dfnFors.length === 1) {
     closestSection.dataset.dfnFor = dfnFors[0];
   }
+  // TODO: make linker support multiple dfnFors
 }
 
 /**

From f0ff768185d605fb3d5b1e79d0da704b92acbcad Mon Sep 17 00:00:00 2001
From: Sid Vishnoi 
Date: Thu, 21 May 2020 16:52:22 +0530
Subject: [PATCH 8/9] don't use all those topLevelEntities

---
 src/core/dfn-finder.js | 2 +-
 src/core/webidl.js     | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/core/dfn-finder.js b/src/core/dfn-finder.js
index cde499bdd8..fb8e5b049f 100644
--- a/src/core/dfn-finder.js
+++ b/src/core/dfn-finder.js
@@ -2,7 +2,7 @@
 import { definitionMap, registerDefinition } from "./dfn-map.js";
 import { showInlineError, wrapInner } from "./utils.js";
 
-export const topLevelEntities = new Set([
+const topLevelEntities = new Set([
   "callback interface",
   "callback",
   "dictionary",
diff --git a/src/core/webidl.js b/src/core/webidl.js
index e63a3c5414..aad6c6491c 100644
--- a/src/core/webidl.js
+++ b/src/core/webidl.js
@@ -10,7 +10,7 @@ import {
   showInlineWarning,
   xmlEscape,
 } from "./utils.js";
-import { decorateDfn, findDfn, topLevelEntities } from "./dfn-finder.js";
+import { decorateDfn, findDfn } from "./dfn-finder.js";
 import { html, webidl2 } from "./import-maps.js";
 import { addCopyIDLButton } from "./webidl-clipboard.js";
 import { fetchAsset } from "./text-loader.js";
@@ -371,7 +371,7 @@ function addDataDfnFor(idlElement, parse) {
 
   const dfnFors = [];
   for (const { tokens } of parse) {
-    if (topLevelEntities.has(tokens.base.type)) {
+    if (["interface", "dictionary", "enum"].includes(tokens.base.type)) {
       const dfnFor = tokens.name.value;
       dfnFors.push(dfnFor);
     }

From 95e333b625ff581cab33a1ab456026ab3a2c7c8e Mon Sep 17 00:00:00 2001
From: Sid Vishnoi 
Date: Thu, 21 May 2020 17:10:22 +0530
Subject: [PATCH 9/9] HOTFIX: test fail as they don't have 
--- src/core/webidl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/webidl.js b/src/core/webidl.js index aad6c6491c..19ce18f758 100644 --- a/src/core/webidl.js +++ b/src/core/webidl.js @@ -367,7 +367,7 @@ function renderWebIDL(idlElement, index) { */ function addDataDfnFor(idlElement, parse) { const closestSection = idlElement.closest("section"); - if (closestSection.hasAttribute("data-dfn-for")) return; + if (!closestSection || closestSection.hasAttribute("data-dfn-for")) return; const dfnFors = []; for (const { tokens } of parse) {