Skip to content

Commit

Permalink
Adds additional author information (closes #507)
Browse files Browse the repository at this point in the history
* Added an extra check to avoid a dangling ',' in the output

* Added a trim to the name of the extra entry, to avoid problems.

* Add initial config back to config object

* Force JSON parsing + recovery

* Add tests for author extras
  • Loading branch information
iherman authored and marcoscaceres committed Oct 11, 2015
1 parent 3df9ca1 commit f48b0cb
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 4 deletions.
1 change: 1 addition & 0 deletions js/core/include-config.js
Expand Up @@ -40,6 +40,7 @@ define(
script.innerHTML = JSON.stringify(initialUserConfig, confFilter, 2);
script.type = 'application/json';
doc.head.appendChild(script);
conf.initialUserConfig = initialUserConfig;
});
msg.pub('end', 'core/include-config');
cb();
Expand Down
8 changes: 6 additions & 2 deletions js/core/override-configuration.js
Expand Up @@ -26,9 +26,13 @@ define(
else if (v === "null") v = null;
else if (/\[\]$/.test(k)) {
k = k.replace(/\[\]/, "");
v = $.parseJSON(v);
v = JSON.parse(v);
}
try {
conf[k] = JSON.parse(v);
} catch (err) {
conf[k] = v;
}
conf[k] = v;
}
}
msg.pub("end", "core/override-configuration");
Expand Down
33 changes: 32 additions & 1 deletion js/w3c/headers.js
Expand Up @@ -123,6 +123,7 @@ define(
rm = " property='foaf:mbox'";
rwu = " property='foaf:workplaceHomepage'";
rpu = " property='foaf:homepage'";
propSeeAlso = " property='rdfs:seeAlso'";
}
var ret = "";
for (var i = 0, n = items.length; i < n; i++) {
Expand Down Expand Up @@ -158,6 +159,34 @@ define(
ret += ", <span class='ed_mailto'><a class='u-email email' " + rm + " href='mailto:" + p.mailto + "'>" + p.mailto + "</a></span>";
}
if (p.note) ret += " (" + p.note + ")";
if (p.extras) {
var resultHTML = p.extras
// Remove empty names
.filter(function (extra) {
return extra.name && extra.name.trim();
})
// Convert to HTML
.map(function (extra) {
var span = document.createElement('span');
var textContainer = span;
if (extra.class) {
span.className = extra.class;
}
if (extra.href) {
var a = document.createElement('a');
span.appendChild(a);
a.href = extra.href;
textContainer = a;
if (this.doRDFa) {
a.setAttribute('property', 'rdfs:seeAlso');
}
}
textContainer.innerHTML = extra.name;
return span.outerHTML;
}.bind(this))
.join(', ');
ret += resultHTML;
}
if (this.doRDFa) {
ret += "</span>\n";
if (name === "Editor") ret += "<span property='rdf:rest' resource='" + bn + "'></span>\n";
Expand Down Expand Up @@ -358,7 +387,9 @@ define(
var peopCheck = function (i, it) {
if (!it.name) msg.pub("error", "All authors and editors must have a name.");
};
$.each(conf.editors, peopCheck);
if(conf.editors) {
conf.editors.forEach(peopCheck);
}
$.each(conf.authors || [], peopCheck);
conf.multipleEditors = conf.editors.length > 1;
conf.multipleAuthors = conf.authors && conf.authors.length > 1;
Expand Down
91 changes: 90 additions & 1 deletion tests/spec/w3c/headers-spec.js
@@ -1,3 +1,6 @@
/*globals expect, it, $, runs, waitsFor, describe*/
(function(){ // prevent this loadWithConfig being trashed by other files
"use strict";
function loadWithConfig (conf, check) {
var config = [];
for (var k in conf) {
Expand All @@ -24,6 +27,10 @@ function loadWithConfig (conf, check) {
});
}

function isPhantom () {
return window.callPhantom || window._phantom
}

// the matrix of features here is such that we're not testing everything
// however the intent is that as bugs are found, we add tests for them
// we're definitely not testing SotD much
Expand Down Expand Up @@ -79,6 +86,87 @@ describe("W3C — Headers", function () {
});
});

it("should not add RDFa stuff to editors extras when doRDFa is false", function() {
var config = {
specStatus: "REC",
doRDFa: false,
editors: [{
name: "Mr foo",
extras: [{
"name": "0000-0003-0782-2704",
"href": "http://orcid.org/0000-0003-0782-2704",
"class": "orcid"
}]
}]
};
loadWithConfig(config, function($ifr) {
var doc = $ifr[0].contentDocument;
var oricdHref = config.editors[0].extras[0].href;
var orcidAnchor = doc.querySelector("a[href='" + oricdHref + "']");
if (!isPhantom()) {
// Check that RDFa is applied
expect(orcidAnchor.getAttribute("property")).toEqual(null);
expect(orcidAnchor.parentNode.className).toEqual("orcid");
}
});
});

it("should take editors extras into account", function() {
var config = {
specStatus: "REC",
doRDFa: true,
editors: [{
name: "Mr foo",
extras: [{
"name": "0000-0003-0782-2704",
"href": "http://orcid.org/0000-0003-0782-2704",
"class": "orcid"
}, {
"name": "@ivan_herman",
"href": "http://twitter.com/ivan_herman",
"class": "twitter"
},
//this should not exist in the doc, as it doesn't have a name
{
"href": "http://not-valid-missing-name",
"class": "invalid"
},
//this should not exist in the doc, as the name is empty
{
"name": "\n\t \n",
"href": "http://empty-name",
"class": "invalid"
}
]
}]
};
loadWithConfig(config, function($ifr) {
var doc = $ifr[0].contentDocument;
var oricdHref = config.editors[0].extras[0].href;
var twitterHref = config.editors[0].extras[1].href;
var orcidAnchor = doc.querySelector("a[href='" + oricdHref + "']");
var twitterAnchor = doc.querySelector("a[href='" + twitterHref + "']");
// general checks
var header = doc.querySelector("#respecHeader");
if (!isPhantom()) {
[orcidAnchor, twitterAnchor].forEach(function(elem) {
// Check parent is correct.
expect(elem.parentNode.localName).toEqual("span");
// Check that RDFa is applied
expect(elem.hasAttribute("property")).toEqual(true);
// Check that it's in the header of the document
expect(header.contains(elem)).toEqual(true);
});
// Check CSS is correctly applied
expect(orcidAnchor.parentNode.className).toEqual("orcid");
expect(twitterAnchor.parentNode.className).toEqual("twitter");
}
// check that extra items with no name are ignored
expect(document.querySelector("a[href='http://not-valid']")).toEqual(null);
expect(document.querySelector("a[href='http://empty-name']")).toEqual(null);
});
});

// authors
it("should take authors into account", function () {
loadWithConfig({ specStatus: "REC", doRDFa: false, "authors[]": [{ name: "NAME1" }] }, function ($ifr) {
Expand Down Expand Up @@ -137,7 +225,7 @@ describe("W3C — Headers", function () {
var licenses = document.querySelectorAll("#respecHeader a[rel=license]");
expect(licenses.length).toEqual(1);
expect(licenses.item(0).tagName).toEqual("A");
expect(licenses.item().href).toEqual("http://www.w3.org/Consortium/Legal/2015/copyright-software-and-document");
expect(licenses.item(0).href).toEqual("http://www.w3.org/Consortium/Legal/2015/copyright-software-and-document");
});
});

Expand Down Expand Up @@ -349,3 +437,4 @@ describe("W3C — Headers", function () {
});
});
});
}());

0 comments on commit f48b0cb

Please sign in to comment.