From f54dde986738832d16e9d6a3b55326ebdcf5e296 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20C=C3=A1ceres?= Date: Tue, 2 Mar 2021 13:11:08 +1100 Subject: [PATCH] fix(core/link-to-dfn-spec): prevent nesting code elements (#3332) --- src/core/link-to-dfn.js | 4 ++-- tests/spec/core/link-to-dfn-spec.js | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/core/link-to-dfn.js b/src/core/link-to-dfn.js index 9752462661..3d05ddb718 100644 --- a/src/core/link-to-dfn.js +++ b/src/core/link-to-dfn.js @@ -233,7 +233,7 @@ function wrapAsCode(anchor, dfn) { // only add code to IDL when the definition matches const term = anchor.textContent.trim(); const isIDL = dfn.dataset.hasOwnProperty("idl"); - const needsCode = shouldWrapByCode(anchor) || shouldWrapByCode(dfn, term); + const needsCode = shouldWrapByCode(anchor) && shouldWrapByCode(dfn, term); if (!isIDL || needsCode) { wrapInner(anchor, document.createElement("code")); } @@ -246,7 +246,7 @@ function wrapAsCode(anchor, dfn) { function shouldWrapByCode(elem, term = "") { switch (elem.localName) { case "a": - if (elem.querySelector("code")) { + if (!elem.querySelector("code")) { return true; } break; diff --git a/tests/spec/core/link-to-dfn-spec.js b/tests/spec/core/link-to-dfn-spec.js index 2a58fa1dc4..7ce67a77a4 100644 --- a/tests/spec/core/link-to-dfn-spec.js +++ b/tests/spec/core/link-to-dfn-spec.js @@ -292,4 +292,25 @@ describe("Core — Link to definitions", () => { expect(testLink.classList).not.toContain("respec-offending-element"); expect(testLink.href).toBe("https://example.com/#is-red"); }); + + it("doesn't nest code elements when linking to a IDL definition", async () => { + const body = ` +
+ +
+        [Exposed=Window]
+        interface MediaDevices {};
+      
+
+ `; + const ops = makeStandardOps({}, body); + const doc = await makeRSDoc(ops); + + const codeElem = doc.querySelector("#header a > code"); + + expect(codeElem.textContent).toEqual("MediaDevices"); + expect(codeElem.querySelector("code")).toBeNull(); + }); });