Skip to content

Commit

Permalink
fix(core/link-to-dfn-spec): prevent nesting code elements (#3332)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoscaceres authored Mar 2, 2021
1 parent 8422057 commit f54dde9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/core/link-to-dfn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand All @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions tests/spec/core/link-to-dfn-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
<section>
<h2 id="header">
Some {{MediaDevices}}
</h2>
<pre class="idl">
[Exposed=Window]
interface MediaDevices {};
</pre>
</section>
`;
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();
});
});

0 comments on commit f54dde9

Please sign in to comment.