Skip to content

Commit

Permalink
Feat (biblio.js): don't go to network for local refs
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoscaceres committed Apr 14, 2016
1 parent 7f84f62 commit d24c244
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 49 deletions.
99 changes: 61 additions & 38 deletions js/core/biblio.js
@@ -1,9 +1,12 @@

// Module core/biblio
// Handles bibliographic references
// Configuration:
// - localBiblio: override or supplement the official biblio with your own.

/*jshint jquery: true*/
/*globals console*/
"use strict";

define(
[],
function () {
Expand Down Expand Up @@ -80,7 +83,7 @@ define(
var $sec = $("<section><h3></h3></section>")
.appendTo($refsec)
.find("h3")
.text(type + " references")
.text(type + " references")
.end()
;
$sec.makeID(null, type + " references");
Expand Down Expand Up @@ -138,46 +141,66 @@ define(

return {
stringifyRef: stringifyRef,
run: function (conf, doc, cb, msg) {
run: function (conf, doc, cb, msg) {
function finish() {
msg.pub("end", "core/biblio");
cb();
}
msg.pub("start", "core/biblio");
var refs = getRefKeys(conf)
, localAliases = []
, finish = function () {
msg.pub("end", "core/biblio");
cb();
}
;
if (conf.localBiblio) {
for (var k in conf.localBiblio) {
if (typeof conf.localBiblio[k].aliasOf !== "undefined") {
localAliases.push(conf.localBiblio[k].aliasOf);
}
}
if(!conf.localBiblio){
conf.localBiblio = {};
}
refs = refs.normativeReferences
.concat(refs.informativeReferences)
.concat(localAliases);
if (refs.length) {
var url = "https://labs.w3.org/specrefs/bibrefs?refs=" + refs.join(",");
$.ajax({
dataType: "json"
, url: url
, success: function (data) {
conf.biblio = data || {};
// override biblio data
if (conf.localBiblio) {
for (var k in conf.localBiblio) conf.biblio[k] = conf.localBiblio[k];
}
bibref(conf, msg);
finish();
}
, error: function (xhr, status, error) {
msg.pub("error", "Error loading references from '" + url + "': " + status + " (" + error + ")");
finish();
}
if(conf.biblio){
var warn = "Overriding `.biblio` in config. Please use `.localBiblio` for custom biblio entries.";
msg.pub("warn", warn);
}
conf.biblio = {};
var localAliases = Array
.from(Object.keys(conf.localBiblio))
.filter(function(key){
return conf.localBiblio[key].hasOwnProperty("aliasOf");
})
.map(function(key){
return conf.localBiblio[key].aliasOf;
});

var allRefs = getRefKeys(conf);
var externalRefs = allRefs.normativeReferences
.concat(allRefs.informativeReferences)
// Filter, as to not go to network for local refs
.filter(function(key){
return !conf.localBiblio.hasOwnProperty(key);
})
// but include local aliases, in case they refer to external specs
.concat(localAliases)
// remove duplicates
.reduce(function(collector, item){
if(!collector.includes(item)){
collector.push(item);
}
return collector;
}, [])
.sort();
// If we don't need to go to network, just use internal biblio
if (!externalRefs.length) {
Object.assign(conf.biblio, conf.localBiblio);
bibref(conf, msg);
finish();
return;
}
else finish();
var url = "https://labs.w3.org/specrefs/bibrefs?refs=" + externalRefs.join(",");
fetch(url)
.then(function(response) {
return response.json();
})
.then(function(data) {
Object.assign(conf.biblio, data, conf.localBiblio);
bibref(conf, msg);
})
.catch(function(err) {
console.error(err);
})
.then(finish);
}
};
}
Expand Down
6 changes: 5 additions & 1 deletion karma.conf.js
Expand Up @@ -64,7 +64,11 @@ module.exports = function(config) {
included: false,
served: true,
}, {
pattern: "node_modules/webidl2/lib/*.js",
pattern: "./node_modules/webidl2/lib/*.js",
included: false,
served: true,
},{
pattern: "./node_modules/whatwg-fetch/fetch.js",
included: false,
served: true,
},
Expand Down
35 changes: 30 additions & 5 deletions tests/spec/core/biblio-spec.js
@@ -1,13 +1,33 @@
"use strict";
describe("W3C — Bibliographic References", function() {
var isSpecRefAvailable = true;

afterAll(function(done) {
flushIframes();
done();
});

// Ping biblio service to see if it's running
it("should reach biblio service", function(done) {
var fetchOps = {
method: "HEAD"
};
fetch("https://labs.w3.org/specrefs/bibrefs", fetchOps)
.then(function(res) {
isSpecRefAvailable = res.ok;
expect(res.ok).toBeTruthy();
done();
})
.catch(function(err) {
fail("Error", err);
done();
});
});

var customConfig = {
editors: [
{name: "Robin Berjon"}
],
editors: [{
name: "Robin Berjon"
}],
shortName: "Foo",
specStatus: "WD",
prevVersion: "FPWD",
Expand Down Expand Up @@ -43,6 +63,11 @@ describe("W3C — Bibliographic References", function() {
// Make sure the reference is added.
var ref = doc.querySelector("#bib-TestRef1 + dd");
expect(ref).toBeTruthy();
// This prevents Jasmine from taking down the whole test suite if SpecRef is down.
if (!isSpecRefAvailable) {
var err = new Error("SpecRef seems to be down. Can't proceed with this spec.");
return Promise.reject(err);
}
expect(ref.textContent).toMatch(/Publishers Inc\.\s/);
ref = null;
// Make sure the ". " is automatically added to publisher.
Expand All @@ -55,6 +80,6 @@ describe("W3C — Bibliographic References", function() {
ref = doc.querySelector("#bib-TestRef3 + dd");
expect(ref).toBeTruthy();
expect(ref.textContent).toMatch(/^Publisher Here\.\s/);
}).then(done);
}).then(done).catch(done);
});
});
});
12 changes: 11 additions & 1 deletion tests/spec/core/inlines-spec.js
Expand Up @@ -4,7 +4,7 @@ describe("Core - Inlines", function() {
flushIframes();
done();
});
it("should process all inline content", function(done) {
it("should process all in-line content", function(done) {
var ops = {
config: makeBasicConfig(),
body: makeDefaultBody() +
Expand All @@ -15,6 +15,16 @@ describe("Core - Inlines", function() {
" <p>[[!DAHU]] [[REX]]</p>" +
"</section>",
};
ops.config.localBiblio = {
"DAHU": {
title: "One short leg. How I learned to overcome.",
publisher: "Publishers Inc."
},
"REX": {
title: "Am I a dinosaur or a failed technology?",
publisher: "Publishers Inc."
},
};
makeRSDoc(ops, function(doc) {
var $inl = $("#inlines", doc);
var $nr = $("#normative-references", doc);
Expand Down
7 changes: 5 additions & 2 deletions tests/spec/w3c/local-aliases-spec.js
Expand Up @@ -12,13 +12,16 @@ describe("W3C — Aliased References", function() {
};
ops.config.localBiblio = {
"FOOBARGLOP": {
"aliasOf": "RFC2119"
"aliasOf": "BARBAR",
},
"BARBAR": {
title: "The BARBAR Spec",
}
};
makeRSDoc(ops, function(doc) {
var $r = $("#bib-FOOBARGLOP + dd", doc);
expect($r.length).toBeTruthy();
expect($r.text()).toMatch(/2119/);
expect($r.text()).toMatch(/BARBAR/);
}).then(done);
});
});
8 changes: 6 additions & 2 deletions tests/test-main.js
Expand Up @@ -17,7 +17,10 @@ var testFiles = Object.keys(window.__karma__.files)
return collector;
}, []);

var allDeps = ["js/core/jquery-enhanced"].concat(testFiles);
var allDeps = [
"js/core/jquery-enhanced",
"fetch",
].concat(testFiles);

require.config({
// Karma serves files under /base, which is the basePath from your config file
Expand All @@ -26,8 +29,9 @@ require.config({
"jquery": "/base/node_modules/jquery/dist/jquery",
"core/utils": "js/core/utils",
"core/jquery-enhanced": "js/core/jquery-enhanced",
"fetch": "/base/node_modules/whatwg-fetch/fetch"
},
// dynamically load all test files
// dynamically load all test files and other deps
deps: allDeps,
// we have to kickoff jasmine, as it is asynchronous
callback: window.__karma__.start,
Expand Down

0 comments on commit d24c244

Please sign in to comment.