Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent duplicates in dfns extracts #1110

Merged
merged 1 commit into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/browserlib/extract-dfns.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ import {parse} from "../../node_modules/webidl2/index.js";
* "prose" (last one indicates that definition appears in the main body of
* the spec)
*
* The extraction ignores definitions with an unknown type. A warning is issued
* to the console when that happens.
*
* The extraction uses the first definition it finds when it bumps into a term
* that is defined more than once (same "linkingText", same "type", same "for").
* A warning is issued to the console when that happens.
*
* @function
* @public
* @return {Array(Object)} An Array of definitions
Expand Down Expand Up @@ -99,6 +106,20 @@ function hasValidType(el) {
return isValid;
}

// Return true when definition is not already defined in the list,
// Return false and issue a warning when it is already defined.
function isNotAlreadyDefined(dfn, idx, list) {
const first = list.find(d => d === dfn ||
(d.type === dfn.type &&
d.linkingText.length === dfn.linkingText.length &&
d.linkingText.every(lt => dfn.linkingText.find(t => t == lt)) &&
d.for.length === dfn.for.length &&
d.for.every(lt => dfn.for.find(t => t === lt))));
if (first !== dfn) {
console.warn('[reffy]', `Duplicate dfn found for "${dfn.linkingText[0]}", type="${dfn.type}", for="${dfn.for[0]}"`);
}
return first === dfn;
}

function definitionMapper(el, idToHeading) {
let definedIn = 'prose';
Expand Down Expand Up @@ -237,7 +258,8 @@ export default function (spec, idToHeading = {}) {
const link = node.querySelector('a[href^="http"]');
return !link || (node.textContent.trim() !== link.textContent.trim());
})
.map(node => definitionMapper(node, idToHeading));
.map(node => definitionMapper(node, idToHeading))
.filter(isNotAlreadyDefined);
}

function preProcessEcmascript() {
Expand Down
4 changes: 4 additions & 0 deletions tests/extract-dfns.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ const tests = [
html: "<dfn id=foo data-dfn-type=invalidtype>Foo</dfn>",
changesToBaseDfn: []
},
{title: "ignores dfns already defined",
html: "<dfn id='foo'>Foo</dfn>. <dfn id='foo2'>Foo</dfn> is already defined.",
changesToBaseDfn: [{}]
},
{title: "automatically fixes internal slots dfns with an invalid 'idl' data-dfn-type",
html: "<dfn id=foo data-dfn-type=idl>Foo</dfn>",
changesToBaseDfn: [{type: "attribute", access: "public"}]
Expand Down