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

Add "definedIn" property to dfns extract #446

Merged
merged 1 commit into from
Nov 16, 2020
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
39 changes: 38 additions & 1 deletion builds/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2670,6 +2670,12 @@ for more information.`;
* "private" when it should be viewed as a local definition.
* - informative: true when definition appears in an informative section,
* false if it is normative
* - heading: Heading under which the term is to be found. An object with "id",
* "title", and "number" properties
* - definedIn: An indication of where the definition appears in the spec. Value
* can be one of "dt", "pre", "table", "heading", "note", "example", or
* "prose" (last one indicates that definition appears in the main body of
* the spec)
*
* @function
* @public
Expand All @@ -2681,6 +2687,32 @@ for more information.`;
return str.trim().replace(/\s+/g, ' ');
}

let definedIn = 'prose';
const enclosingEl = el.closest('dt,pre,table,h1,h2,h3,h4,h5,h6,.note,.example') || el;
switch (enclosingEl.nodeName) {
case 'DT':
case 'PRE':
case 'TABLE':
definedIn = enclosingEl.nodeName.toLowerCase();
break;
case 'H1':
case 'H2':
case 'H3':
case 'H4':
case 'H5':
case 'H6':
definedIn = 'heading';
break;
default:
if (enclosingEl.classList.contains('note')) {
definedIn = 'note';
}
else if (enclosingEl.classList.contains('example')) {
definedIn = 'example';
}
break;
}

return {
// ID is the id attribute
id: el.getAttribute('id'),
Expand Down Expand Up @@ -2736,7 +2768,12 @@ for more information.`;
].join(',')),

// Heading under which the term is to be found
heading: idToHeading[el.getAttribute('id')]
heading: idToHeading[el.getAttribute('id')],

// Enclosing element under which the definition appears. Value can be one of
// "dt", "pre", "table", "heading", "note", "example", or "prose" (last one
// indicates that definition appears in the main body of the specification)
definedIn
};
}

Expand Down
39 changes: 38 additions & 1 deletion src/browserlib/extract-dfns.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import {parse} from "../../node_modules/webidl2/index.js";
* "private" when it should be viewed as a local definition.
* - informative: true when definition appears in an informative section,
* false if it is normative
* - heading: Heading under which the term is to be found. An object with "id",
* "title", and "number" properties
* - definedIn: An indication of where the definition appears in the spec. Value
* can be one of "dt", "pre", "table", "heading", "note", "example", or
* "prose" (last one indicates that definition appears in the main body of
* the spec)
*
* @function
* @public
Expand All @@ -27,6 +33,32 @@ function definitionMapper(el, idToHeading) {
return str.trim().replace(/\s+/g, ' ');
}

let definedIn = 'prose';
const enclosingEl = el.closest('dt,pre,table,h1,h2,h3,h4,h5,h6,.note,.example') || el;
switch (enclosingEl.nodeName) {
case 'DT':
case 'PRE':
case 'TABLE':
definedIn = enclosingEl.nodeName.toLowerCase();
break;
case 'H1':
case 'H2':
case 'H3':
case 'H4':
case 'H5':
case 'H6':
definedIn = 'heading';
break;
default:
if (enclosingEl.classList.contains('note')) {
definedIn = 'note';
}
else if (enclosingEl.classList.contains('example')) {
definedIn = 'example';
}
break;
}

return {
// ID is the id attribute
id: el.getAttribute('id'),
Expand Down Expand Up @@ -82,7 +114,12 @@ function definitionMapper(el, idToHeading) {
].join(',')),

// Heading under which the term is to be found
heading: idToHeading[el.getAttribute('id')]
heading: idToHeading[el.getAttribute('id')],

// Enclosing element under which the definition appears. Value can be one of
// "dt", "pre", "table", "heading", "note", "example", or "prose" (last one
// indicates that definition appears in the main body of the specification)
definedIn
};
}

Expand Down
9 changes: 6 additions & 3 deletions tests/crawl-test.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"type": "dfn",
"for": [],
"access": "private",
"informative": false
"informative": false,
"definedIn": "prose"
}
],
"headings": [
Expand Down Expand Up @@ -131,7 +132,8 @@
"heading": {
"id": "title",
"title": "No Title"
}
},
"definedIn": "pre"
},
{
"id": "dom-foo-bar",
Expand All @@ -151,7 +153,8 @@
"heading": {
"id": "title",
"title": "No Title"
}
},
"definedIn": "pre"
}
],
"headings": [
Expand Down
3 changes: 2 additions & 1 deletion tests/crawl.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ nock("https://respec.org")

nock("https://specref.herokuapp.com")
.persist()
.get("/bibrefs?refs=webidl,html").reply(200, {webidl:{href:"https://heycam.github.io/webidl/"}}, {"Access-Control-Allow-Origin": "*"});
.get("/bibrefs?refs=webidl,html").reply(200, {webidl:{href:"https://heycam.github.io/webidl/"}}, {"Access-Control-Allow-Origin": "*"})
.get("/bibrefs?refs=HTML").reply(200, {HTML:{href:"https://html.spec.whatwg.org/multipage/"}}, {"Access-Control-Allow-Origin": "*"});

nock("https://www.w3.org")
.persist()
Expand Down
44 changes: 29 additions & 15 deletions tests/extract-dfns.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ const baseDfn = {
type: 'dfn',
for: [],
access: 'private',
informative: false
informative: false,
definedIn: 'prose'
};
const tests = [
{title: "parses a simple <dfn>",
Expand Down Expand Up @@ -123,7 +124,7 @@ const tests = [
},
{title: "considers definitions in headings",
html: "<h2 data-dfn-type=dfn id=foo>Foo</h2>",
changesToBaseDfn: [{heading: { id: "foo", title: "Foo"}}]
changesToBaseDfn: [{heading: { id: "foo", title: "Foo"}, definedIn: "heading"}]
},
{title: "ignores elements that aren't <dfn> and headings",
html: "<span data-dfn-type=dfn id=foo>Foo</span>",
Expand All @@ -147,7 +148,8 @@ const tests = [
access: "public",
type: "element",
linkingText: ["html"],
heading: { id: "the-html-element", title: "The html element", number: "4.1.1"}}],
heading: { id: "the-html-element", title: "The html element", number: "4.1.1"},
definedIn: "heading"}],
spec: "html"
},
{title: "handles exceptions in the HTML spec convention for defining elements",
Expand All @@ -156,22 +158,25 @@ const tests = [
access: "public",
type: "element",
linkingText: ["video"],
heading: { id: "the-video-element", title: "The video element", number: "4.8.9"}}],
heading: { id: "the-video-element", title: "The video element", number: "4.8.9"},
definedIn: "heading"}],
spec: "html"
},
{title: "handles HTML spec conventions of definitions in headings",
html: '<h6 id="parsing-main-inselect"><span class="secno">12.2.6.4.16</span> The "<dfn>in select</dfn>" insertion mode<a href="#parsing-main-inselect" class="self-link"></a></h6>',
changesToBaseDfn: [{id: "parsing-main-inselect",
linkingText: ["in select"],
heading: { id: "parsing-main-inselect", title: "The \"in select\" insertion mode", number: "12.2.6.4.16"}}],
heading: { id: "parsing-main-inselect", title: "The \"in select\" insertion mode", number: "12.2.6.4.16"},
definedIn: "heading"}],
spec: "html"
},
{title: "handles HTML spec convention for defining element interfaces",
html: '<pre><code class="idl">interface <dfn id="htmlhrelement"><c- g="">HTMLHRElement</c-></dfn> {};</code></pre>',
changesToBaseDfn: [{id: "htmlhrelement",
access: "public",
type: "interface",
linkingText: ["HTMLHRElement"]}],
linkingText: ["HTMLHRElement"],
definedIn: "pre"}],
spec: "html"
},
{title: "handles finding IDL type across mixins and partial",
Expand All @@ -187,7 +192,8 @@ const tests = [
html: '<dt><dfn id="selector-visited" data-noexport=""><code>:visited</code></dfn></dt>',
changesToBaseDfn: [{id: "selector-visited",
type: "selector",
linkingText: [":visited"]}],
linkingText: [":visited"],
definedIn: "dt"}],
spec: "html"
},
{
Expand Down Expand Up @@ -274,7 +280,8 @@ const tests = [
access: "public",
type: "dict-member",
linkingText: ["withCredentials"],
for: ['EventSourceInit']}],
for: ['EventSourceInit'],
definedIn: "pre"}],
spec: "html"
},
{
Expand Down Expand Up @@ -354,7 +361,8 @@ const tests = [
changesToBaseDfn: [{
id: "text/xml",
linkingText: ["text/xml"],
href: "https://example.org/indices.html#text/xml"
href: "https://example.org/indices.html#text/xml",
definedIn: "dt"
}],
spec: "html"
},
Expand Down Expand Up @@ -400,7 +408,8 @@ const tests = [
"use",
"video"
],
"access": "public"
"access": "public",
definedIn: "table"
}],
spec: "SVG2"
},
Expand All @@ -412,7 +421,8 @@ const tests = [
linkingText: ["link"],
type: "element",
access: "public",
heading: { id: "LinkElement", title: "External style sheets: the effect of the HTML ‘link’ element", number: "6.3"}
heading: { id: "LinkElement", title: "External style sheets: the effect of the HTML ‘link’ element", number: "6.3"},
definedIn: "heading"
}],
spec: "SVG2"
},
Expand All @@ -424,7 +434,8 @@ const tests = [
linkingText: ["patternUnits"],
type: "element-attr",
for: ["pattern"],
access: "public"
access: "public",
definedIn: "dt"
}],
spec: "SVG2"
},
Expand All @@ -436,7 +447,8 @@ const tests = [
linkingText: ["stop-opacity"],
type: "property",
for: ["stop"],
access: "public"
access: "public",
definedIn: "dt"
}],
spec: "SVG2"
},
Expand Down Expand Up @@ -472,7 +484,8 @@ When initialize(<var>newItem</var>) is called, the following steps are run:</p>`
linkingText: ["SVGAnimatedLengthList"],
type: "interface",
access: "public",
heading: { id: "InterfaceSVGAnimatedLengthList", title: "Interface SVGAnimatedLengthList", number: "4.6.10"}
heading: { id: "InterfaceSVGAnimatedLengthList", title: "Interface SVGAnimatedLengthList", number: "4.6.10"},
definedIn: "heading"
}],
spec: "SVG2"
},
Expand All @@ -490,7 +503,8 @@ When initialize(<var>newItem</var>) is called, the following steps are run:</p>`
id: "SVGBoundingBoxOptions",
linkingText: ["SVGBoundingBoxOptions"],
type: "dictionary",
access: "public"
access: "public",
definedIn: "pre"
}],
spec :"SVG2",
}
Expand Down