I just happened to notice that entering a URL that contains emoji or other non-BMP Unicode characters into the "Insert Hyperlink" dialog in the SE Markdown editor will fail with an uncaught URIError. This happens both with the old-style hyperlink dialog (as currently used here on meta.SE) and with the new-style one (as currently used on SO).
Steps to reproduce:
Open a post editor, e.g. by clicking the Ask Question link. The "Your Answer" editor at the bottom of this page will also work.
Open the "Insert Hyperlink" dialog, either by clicking the icon showing a pair of chain links above the edit box, or by pressing Ctrl+L while the editor has focus.
Enter a URL that contains non-BMP Unicode characters, like
https://en.wikipedia.org/wiki/😃
, into the dialog.Submit the dialog by pressing Enter or clicking the "OK" button.
Observe that no link was actually inserted into the post (and that an uncaught
URIError: malformed URI sequence
was logged to the browser's JS console instead).
Looking at the Markdown editor code, the problem appears to occur in the properlyEncoded()
function, on the following line:
link = link.replace(/%(?:[\da-fA-F]{2})|\?|\+|[^\w\d-./[\]]/g, function (match) {
The regexp on that line is supposed to match (valid %
escape sequences and) any single characters in the URL outside the character class \w\d-./[\]
, and feed them one by one to the following callback function which URL-encodes them. However, it instead ends up matching individual UTF-16 code units, including halves of a surrogate pair. Since these individual surrogates do not represent valid Unicode characters, and so cannot be correctly URL-encoded without knowing the other half of the pair, passing them to encodeURI()
causes it to throw an error.
https://en.wikipedia.org/wiki/😃
works and (currently) redirects to en.wikipedia.org/wiki/Emoticon, even if SE's parser doesn't currently handle it properly. Try pasting it into your browser's URL bar if you don't believe me.https://en.wikipedia.org/wiki/%F0%9F%98%83
before actually going to this address, so it's not relevant.