Random mode: Wikipedia link malformed if title contains apostrophe

Issue #4 new
Konstantin Mochalov created an issue

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 '.

Caused by: https://bitbucket.org/magnusmanske/mixnmatch/src/952f22d9fda4ac7d9bb0e69e40babe95f501fc76/public_html/main.js?at=master#cl-1144

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);
  });
}

Comments (0)

  1. Log in to comment