Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Merge branch 'develop' into gh-pages
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoscaceres committed Jun 4, 2017
2 parents 03042c3 + 0679704 commit 08435fa
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 90 deletions.
2 changes: 1 addition & 1 deletion builds/respec-w3c-common.build.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion builds/respec-w3c-common.js

Large diffs are not rendered by default.

18 changes: 0 additions & 18 deletions js/tmpl.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "respec",
"version": "12.2.3",
"version": "13.0.0",
"license": "W3C",
"description": "A technical specification pre-processor.",
"engines": {
Expand Down
107 changes: 64 additions & 43 deletions src/core/data-cite.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,47 @@ import { pub } from "core/pubsubhub";
import { resolveRef } from "core/biblio";
export const name = "core/data-cite";

async function toLookupRequest(elem) {
const originalKey = elem.dataset.cite;
let { key, frag } = toCiteDetails(elem);
const entry = await resolveRef(key);
cleanElement(elem);
if (!entry) {
var msg = `Couldn't find a match for 'data-cite=${originalKey}'.`;
console.warn(msg, elem);
msg += " Please check developer console for offending element.";
pub("warn", msg);
return;
}
let { href } = entry;
if (frag) {
href += frag;
}
switch (elem.localName) {
case "a": {
elem.href = href;
break;
function requestLookup(conf) {
const toCiteDetails = citeDetailsConverter(conf);
return async function(elem) {
const originalKey = elem.dataset.cite;
let { key, frag } = toCiteDetails(elem);
let href = "";
// This is just referring to this document
if (key === conf.shortName) {
href = document.location.href;
} else {
// Let's go look it up in spec ref...
const entry = await resolveRef(key);
cleanElement(elem);
if (!entry) {
var msg = `Couldn't find a match for 'data-cite=${originalKey}'.`;
console.warn(msg, elem);
msg += " Please check developer console for offending element.";
pub("warn", msg);
return;
}
href = entry.href;
}
if (frag) {
href = new URL(frag, href).href;
}
case "dfn": {
const a = elem.ownerDocument.createElement("a");
a.href = href;
while (elem.firstChild) {
a.appendChild(elem.firstChild);
switch (elem.localName) {
case "a": {
elem.href = href;
break;
}
case "dfn": {
const a = elem.ownerDocument.createElement("a");
a.href = href;
while (elem.firstChild) {
a.appendChild(elem.firstChild);
}
elem.appendChild(a, elem);
break;
}
elem.appendChild(a, elem);
break;
}
}
};
}

function cleanElement(elem) {
Expand All @@ -57,24 +67,34 @@ function cleanElement(elem) {
.forEach(attrName => elem.removeAttribute(attrName));
}

function toCiteDetails({ dataset }) {
let { cite: key, citeFrag: frag } = dataset;
const isNormative = key.startsWith("!");
const fragPosition = key.search("#");
if (fragPosition !== -1) {
frag = !frag ? key.substr(fragPosition) : frag;
key = key.substring(0, fragPosition);
}
if (isNormative) {
key = key.substr(1);
}
if (frag && !frag.startsWith("#")) {
frag = "#" + frag;
}
return { key, isNormative, frag };
function citeDetailsConverter(conf) {
return function toCiteDetails(elem) {
const { dataset } = elem;
let { cite: key, citeFrag: frag } = dataset;
const isNormative = key.startsWith("!");
const fragPosition = key.search("#");
// The key is a fragment, resolve using the shortName as key
if (key.startsWith("#") && !frag && conf.shortName) {
elem.dataset.cite = conf.shortName;
elem.dataset.citeFrag = key;
return toCiteDetails(elem);
}
if (fragPosition !== -1) {
frag = !frag ? key.substr(fragPosition) : frag;
key = key.substring(0, fragPosition);
}
if (isNormative) {
key = key.substr(1);
}
if (frag && !frag.startsWith("#")) {
frag = "#" + frag;
}
return { key, isNormative, frag };
};
}

export function run(conf, doc, cb) {
const toCiteDetails = citeDetailsConverter(conf);
Array.from(doc.querySelectorAll(["dfn[data-cite], a[data-cite]"]))
.filter(el => el.dataset.cite)
.map(toCiteDetails)
Expand All @@ -90,6 +110,7 @@ export function run(conf, doc, cb) {
}

export async function linkInlineCitations(doc) {
const toLookupRequest = requestLookup(doc.defaultView.respecConfig);
const citedSpecs = doc.querySelectorAll("dfn[data-cite], a[data-cite]");
const lookupRequests = Array.from(citedSpecs).map(toLookupRequest);
return await Promise.all(lookupRequests);
Expand Down
84 changes: 58 additions & 26 deletions tests/spec/core/data-cite-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,37 @@ describe("Core — data-cite attribute", () => {
flushIframes();
done();
});
it(`treats data-cite="#foo" as self citing`, done => {
const ops = {
config: makeBasicConfig(),
body:
makeDefaultBody() +
`
<section>
<h2>test</h2>
<p>
<a id="t1" data-cite="#test">a</a>
<dfn id="t2" data-cite="#test">a</dfn>
</p>
</section>
`,
};
ops.config.shortName = "dahut";
makeRSDoc(ops).then(doc => {
const t1 = doc.getElementById("t1");
const t2 = doc.getElementById("t2").querySelector("a");
const location = new URL("#test", doc.location).href;
expect(t1.href).toEqual(location);
expect(t2.href).toEqual(location);
done();
});
});
it("links directly to externally defined references", done => {
const ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +
`
body:
makeDefaultBody() +
`
<section>
<p id="t1"><a>inline link</a></p>
<p id="t2"><dfn data-cite="!WHATWG-HTML#test">inline link</dfn></p>
Expand All @@ -22,28 +48,29 @@ describe("Core — data-cite attribute", () => {
expect(a.href).toEqual("https://html.spec.whatwg.org/multipage/#test");
expect(a.hasAttribute("data-cite")).toEqual(false);
expect(
doc.querySelector("#bib-WHATWG-HTML").closest("section").id
doc.querySelector("#bib-WHATWG-HTML").closest("section").id,
).toEqual("normative-references");
// Definition part
const dfn = doc.querySelector("#t2 > dfn");
expect(dfn).toBeTruthy();
const dfnA = doc.querySelector("#t2 > dfn > a");
expect(dfnA.textContent).toEqual("inline link");
expect(dfnA.href).toEqual(
"https://html.spec.whatwg.org/multipage/#test"
"https://html.spec.whatwg.org/multipage/#test",
);
expect(dfnA.hasAttribute("data-cite")).toEqual(false);
expect(
doc.querySelector("#bib-WHATWG-HTML").closest("section").id
doc.querySelector("#bib-WHATWG-HTML").closest("section").id,
).toEqual("normative-references");
})
.then(done);
});
it("links data-cite attributes as normative reference", done => {
const ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +
`
body:
makeDefaultBody() +
`
<section>
<p id="t1"><a data-cite="!WHATWG-HTML">inline link</a></p>
</section>
Expand All @@ -56,16 +83,17 @@ describe("Core — data-cite attribute", () => {
expect(a.href).toEqual("https://html.spec.whatwg.org/multipage/");
expect(a.hasAttribute("data-cite")).toEqual(false);
expect(
doc.querySelector("#bib-WHATWG-HTML").closest("section").id
doc.querySelector("#bib-WHATWG-HTML").closest("section").id,
).toEqual("normative-references");
})
.then(done);
});
it("links data-cite attributes as informative reference", done => {
const ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +
`
body:
makeDefaultBody() +
`
<section>
<p id="t1"><a data-cite="WHATWG-DOM">inline link</a></p>
</section>
Expand All @@ -78,16 +106,17 @@ describe("Core — data-cite attribute", () => {
expect(a.href).toEqual("https://dom.spec.whatwg.org/");
expect(a.hasAttribute("data-cite")).toEqual(false);
expect(
doc.querySelector("#bib-WHATWG-DOM").closest("section").id
doc.querySelector("#bib-WHATWG-DOM").closest("section").id,
).toEqual("informative-references");
})
.then(done);
});
it("handles bogus data-cite values", done => {
const ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +
`
body:
makeDefaultBody() +
`
<section>
<p id="t1"><a data-cite="no-exist-inf">link 1</a></p>
<p id="t2"><a data-cite="!no-exist-norm">link 2</a></p>
Expand All @@ -105,19 +134,20 @@ describe("Core — data-cite attribute", () => {
expect(a1.hasAttribute("data-cite")).toEqual(false);
expect(a2.hasAttribute("data-cite")).toEqual(false);
expect(
doc.querySelector("#bib-no-exist-inf").closest("section").id
doc.querySelector("#bib-no-exist-inf").closest("section").id,
).toEqual("informative-references");
expect(
doc.querySelector("#bib-no-exist-norm").closest("section").id
doc.querySelector("#bib-no-exist-norm").closest("section").id,
).toEqual("normative-references");
})
.then(done);
});
it("adds the fragment identifier to the link", done => {
const ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +
`
body:
makeDefaultBody() +
`
<section>
<p id="t1"><a
data-cite="!WHATWG-HTML#test">inline link</a></p>
Expand All @@ -131,7 +161,7 @@ describe("Core — data-cite attribute", () => {
expect(a.href).toEqual("https://html.spec.whatwg.org/multipage/#test");
expect(a.hasAttribute("data-cite")).toEqual(false);
expect(
doc.querySelector("#bib-WHATWG-HTML").closest("section").id
doc.querySelector("#bib-WHATWG-HTML").closest("section").id,
).toEqual("normative-references");
})
.then(done);
Expand All @@ -140,8 +170,9 @@ describe("Core — data-cite attribute", () => {
it("adds the fragment identifier to the link", done => {
const ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +
`
body:
makeDefaultBody() +
`
<section>
<p id="t1"><a
data-cite="WHATWG-HTML"
Expand All @@ -154,20 +185,21 @@ describe("Core — data-cite attribute", () => {
const a = doc.querySelector("#t1 > a");
expect(a.textContent).toEqual("inline link");
expect(a.href).toEqual(
"https://html.spec.whatwg.org/multipage/#pass"
"https://html.spec.whatwg.org/multipage/#pass",
);
expect(a.hasAttribute("data-cite")).toEqual(false);
expect(
doc.querySelector("#bib-WHATWG-HTML").closest("section").id
doc.querySelector("#bib-WHATWG-HTML").closest("section").id,
).toEqual("informative-references");
})
.then(done);
});
it("cited fragments are overridden by cite-frag", done => {
const ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +
`
body:
makeDefaultBody() +
`
<section>
<p id="t1"><a
data-cite="!WHATWG-HTML#fail"
Expand All @@ -180,11 +212,11 @@ describe("Core — data-cite attribute", () => {
const a = doc.querySelector("#t1 > a");
expect(a.textContent).toEqual("inline link");
expect(a.href).toEqual(
"https://html.spec.whatwg.org/multipage/#pass"
"https://html.spec.whatwg.org/multipage/#pass",
);
expect(a.hasAttribute("data-cite")).toEqual(false);
expect(
doc.querySelector("#bib-WHATWG-HTML").closest("section").id
doc.querySelector("#bib-WHATWG-HTML").closest("section").id,
).toEqual("normative-references");
})
.then(done);
Expand Down

0 comments on commit 08435fa

Please sign in to comment.