Random mode: Wikipedia link malformed if title contains apostrophe
Issue #4
new
I don't know how to reproduce because it's random and there's no stable URLs, but if during ?mode=random
result for Wikipedia appears, with title containing apostrophe '
, link to Wikipedia article appears cut after '
.
h += "<td><a class='external' target='_blank' href='//"+lang+".wikipedia.org/wiki/" + encodeURIComponent(v.title.replace(/ /g,'_')) + "'>" + v.title + "</a>" ;
"encodeURIComponent escapes all characters except the following: alphabetic, decimal digits, - _ . ! ~ * ' ( )" (MDN)
lang = "en"
// -> "en"
v = {title: "Conan O'Brien"}
// -> Object {title: "Conan O'Brien"}
"<td><a class='external' target='_blank' href='//"+lang+".wikipedia.org/wiki/" + encodeURIComponent(v.title.replace(/ /g,'_')) + "'>" + v.title + "</a>"
// -> "<td><a class='external' target='_blank' href='//en.wikipedia.org/wiki/Conan_O'Brien'>Conan O'Brien</a>"
Quick-fix is to replace it with:
h += "<td><a class='external' target='_blank' href='//"+lang+".wikipedia.org/wiki/" + encodeURIComponent(v.title.replace(/ /g,'_')).replace(/[!'()*]/g, escape) + "'>" + v.title + "</a>"
Or to replace encodeURIComponent with RFC 3986-compliant function from MDN:
function fixedEncodeURIComponent (str) {
return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16);
});
}