Skip to content

Commit

Permalink
Add data-defines on well-known patterns of term/definition association
Browse files Browse the repository at this point in the history
As discussed in speced#4522
See also w3c/reffy#1444
  • Loading branch information
dontcallmedom committed Dec 21, 2023
1 parent 365bf67 commit 82a3cf3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/core/dfn.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,26 @@ function addContractDefaults() {
for (const dfn of exportableDfns) {
dfn.dataset.export = "";
}

// - Sets data-defines on well-known definition content patterns

// A dt with class dfn-desc (on in a dl with such a class)
// containing a definition
// indicates that the following dd or div element contains its prose content
for (const dt of document.querySelectorAll("dl.dfn-desc dt:has(dfn[data-dfn-type]), dt.dfn-desc:has(dfn[data-dfn-type])")) {
const dfnId = dt.querySelector("dfn[id]").id;
const dfnContent = dt.nextElementSibling;
if (dfnId && ["DIV", "DD"].includes(dfnContent.tagName)) {
dfnContent.dataset.defines = `#${dfnId}`;
}
}

// a non-dt element with class dfn-desc containing a definition
// indicates that the said element contains its prose content
for (const el of document.querySelectorAll(":not(dt):not(dl).dfn-desc:has(dfn[data-dfn-type])")) {
const dfnId = el.querySelector("dfn[id]").id;
if (dfnId) {
el.dataset.defines = `#${dfnId}`;
}
}
}
28 changes: 28 additions & 0 deletions tests/spec/core/dfn-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,5 +655,33 @@ describe("Core — Definitions", () => {
expect(errors[0].message).toContain("Declares both");
expect(errors[1].message).toContain("but also has a");
});

it("assigns data-defines on well-known pattern", async () => {
const body = `
<section>
<h2>Definition and its description</h2>
<p id="desc1" class="dfn-desc">
A <dfn>definition</dfn> can also have an associated description
</p>
<dl class="dfn-desc">
<dt><dfn>different convention</dfn></dt>
<dd id="desc2">Different conventions can be applied to associate a term with its description</dd>
</dl>
<dl>
<dt class="dfn-desc"><dfn>local convention</dfn></dt>
<dd id="desc3">The local convention can be applied to a dt individually</dd>
</dl>
</section>
`;
const ops = makeStandardOps(null, body);
const doc = await makeRSDoc(ops);
const desc1 = doc.getElementById("desc1");
const desc2 = doc.getElementById("desc2");
const desc3 = doc.getElementById("desc3");
expect(desc1.dataset.defines).toBe("#dfn-definition");
expect(desc2.dataset.defines).toBe("#dfn-different-convention");
expect(desc3.dataset.defines).toBe("#dfn-local-convention");
});

});
});

0 comments on commit 82a3cf3

Please sign in to comment.