Skip to main content
Fix a missing backtick, and do some other stuff to get us past 6 chars... maybe the language tag will even work by the time someone gets around to approving this?
Source Link

The link definition names are stored in an Array. Arrays have reduce as a method, which seems to be the source of the problem. You can reproduce it with concat or filter or any other ArrayArray method, too. I'm no Javascript expert, and I haven't dug far enough into the problem to figure out just why it happens -- especially because it begins working again if you include the optional title for the link -- but you can see for yourself that it affects the base WMD implementation, too, not just SO's version.

    if (g_urls[link_id] != undefined) {
        url = g_urls[link_id];
        if (g_titles[link_id] != undefined) {
            title = g_titles[link_id];
        }
    }
    if (g_urls[link_id] != undefined) {
        url = g_urls[link_id];
        if (g_titles[link_id] != undefined) {
            title = g_titles[link_id];
        }
    }

The above code uses link_id (which would be "reduce") in order to index into g_urls and g_titles. As I mentioned before, these will *not* be g_titlesundefined. As I mentioned before, these will not be , even when they "should" be. Instead, they'll be the undefinedreduce`, even when they "should" be. Instead, they'll be the reduce functions themselves. Note that, because of the structure here, unless both the URL and title are defined, one or both will be set to the function.

url = escapeCharacters(url,"*_");
var result = "<a href=\"" + url + "\"";

if (title != "") {
    title = title.replace(/"/g,"&quot;");
    title = escapeCharacters(title,"*_");
    result +=  " title=\"" + title + "\"";
}
url = escapeCharacters(url,"*_");
var result = "<a href=\"" + url + "\"";

if (title != "") {
    title = title.replace(/"/g,"&quot;");
    title = escapeCharacters(title,"*_");
    result +=  " title=\"" + title + "\"";
}

The code attempts to escape characters on url and title (which, again, will not be """" even when it should be). This kills the code, because you obviously can't escape characters on a function (Wouldn't that be cool if you could, though? Like, it actually stripped the characters from the function code? Pointless? Sure. But cool.). So until both url and title have actual string values, the conversion process craps out, which is why it seems like it freezes.

    if (g_urls[link_id] != undefined && typeof g_urls[link_id] != 'function') {
        url = g_urls[link_id];
        if (g_titles[link_id] != undefined && typeof g_titles[link_id] != 'function') {
            title = g_titles[link_id];
        }
    }
    if (g_urls[link_id] != undefined && typeof g_urls[link_id] != 'function') {
        url = g_urls[link_id];
        if (g_titles[link_id] != undefined && typeof g_titles[link_id] != 'function') {
            title = g_titles[link_id];
        }
    }
g_urls = new Array();
g_titles = new Array();
g_urls = new Array();
g_titles = new Array();
g_urls = {};
g_titles = {};
g_urls = {};
g_titles = {};

The link definition names are stored in an Array. Arrays have reduce as a method, which seems to be the source of the problem. You can reproduce it with concat or filter or any other Array method, too. I'm no Javascript expert, and I haven't dug far enough into the problem to figure out just why it happens -- especially because it begins working again if you include the optional title for the link -- but you can see for yourself that it affects the base WMD implementation, too, not just SO's version.

    if (g_urls[link_id] != undefined) {
        url = g_urls[link_id];
        if (g_titles[link_id] != undefined) {
            title = g_titles[link_id];
        }
    }

The above code uses link_id (which would be "reduce") in order to index into g_urls and g_titles. As I mentioned before, these will *not* be undefined, even when they "should" be. Instead, they'll be the reduce` functions themselves. Note that, because of the structure here, unless both the URL and title are defined, one or both will be set to the function.

url = escapeCharacters(url,"*_");
var result = "<a href=\"" + url + "\"";

if (title != "") {
    title = title.replace(/"/g,"&quot;");
    title = escapeCharacters(title,"*_");
    result +=  " title=\"" + title + "\"";
}

The code attempts to escape characters on url and title (which, again, will not be "" even when it should be). This kills the code, because you obviously can't escape characters on a function (Wouldn't that be cool if you could, though? Like, it actually stripped the characters from the function code? Pointless? Sure. But cool.). So until both url and title have actual string values, the conversion process craps out, which is why it seems like it freezes.

    if (g_urls[link_id] != undefined && typeof g_urls[link_id] != 'function') {
        url = g_urls[link_id];
        if (g_titles[link_id] != undefined && typeof g_titles[link_id] != 'function') {
            title = g_titles[link_id];
        }
    }
g_urls = new Array();
g_titles = new Array();
g_urls = {};
g_titles = {};

The link definition names are stored in an Array. Arrays have reduce as a method, which seems to be the source of the problem. You can reproduce it with concat or filter or any other Array method, too. I'm no Javascript expert, and I haven't dug far enough into the problem to figure out just why it happens -- especially because it begins working again if you include the optional title for the link -- but you can see for yourself that it affects the base WMD implementation, too, not just SO's version.

    if (g_urls[link_id] != undefined) {
        url = g_urls[link_id];
        if (g_titles[link_id] != undefined) {
            title = g_titles[link_id];
        }
    }

The above code uses link_id (which would be "reduce") in order to index into g_urls and g_titles. As I mentioned before, these will not be undefined, even when they "should" be. Instead, they'll be the reduce functions themselves. Note that, because of the structure here, unless both the URL and title are defined, one or both will be set to the function.

url = escapeCharacters(url,"*_");
var result = "<a href=\"" + url + "\"";

if (title != "") {
    title = title.replace(/"/g,"&quot;");
    title = escapeCharacters(title,"*_");
    result +=  " title=\"" + title + "\"";
}

The code attempts to escape characters on url and title (which, again, will not be "" even when it should be). This kills the code, because you obviously can't escape characters on a function (Wouldn't that be cool if you could, though? Like, it actually stripped the characters from the function code? Pointless? Sure. But cool.). So until both url and title have actual string values, the conversion process craps out, which is why it seems like it freezes.

    if (g_urls[link_id] != undefined && typeof g_urls[link_id] != 'function') {
        url = g_urls[link_id];
        if (g_titles[link_id] != undefined && typeof g_titles[link_id] != 'function') {
            title = g_titles[link_id];
        }
    }
g_urls = new Array();
g_titles = new Array();
g_urls = {};
g_titles = {};
added 427 characters in body
Source Link

EDIT 3:

Blixt has suggested this alternate solution, which appears to work just as well, is less of a workaround, and is probably better semantically. In lieu of the above change, instead change these lines (lines 105-106):

g_urls = new Array();
g_titles = new Array();

to this:

g_urls = {};
g_titles = {};

I haven't done especially rigorous testing, but it appears to be working like a charm.

EDIT 3:

Blixt has suggested this alternate solution, which appears to work just as well, is less of a workaround, and is probably better semantically. In lieu of the above change, instead change these lines (lines 105-106):

g_urls = new Array();
g_titles = new Array();

to this:

g_urls = {};
g_titles = {};

I haven't done especially rigorous testing, but it appears to be working like a charm.

added 1935 characters in body
Source Link

EDIT 2:

Here is some of the code in question from the SO branch of WMD:

    if (g_urls[link_id] != undefined) {
        url = g_urls[link_id];
        if (g_titles[link_id] != undefined) {
            title = g_titles[link_id];
        }
    }

The above code uses link_id (which would be "reduce") in order to index into g_urls and g_titles. As I mentioned before, these will *not* be undefined, even when they "should" be. Instead, they'll be the reduce` functions themselves. Note that, because of the structure here, unless both the URL and title are defined, one or both will be set to the function.

Next is the code that actually creates the problem:

url = escapeCharacters(url,"*_");
var result = "<a href=\"" + url + "\"";

if (title != "") {
    title = title.replace(/"/g,"&quot;");
    title = escapeCharacters(title,"*_");
    result +=  " title=\"" + title + "\"";
}

The code attempts to escape characters on url and title (which, again, will not be "" even when it should be). This kills the code, because you obviously can't escape characters on a function (Wouldn't that be cool if you could, though? Like, it actually stripped the characters from the function code? Pointless? Sure. But cool.). So until both url and title have actual string values, the conversion process craps out, which is why it seems like it freezes.

So how to solve it? The simplest way would be to change the first block of code to something like this:

    if (g_urls[link_id] != undefined && typeof g_urls[link_id] != 'function') {
        url = g_urls[link_id];
        if (g_titles[link_id] != undefined && typeof g_titles[link_id] != 'function') {
            title = g_titles[link_id];
        }
    }

Maybe a Javascript ninja will know of an even better way, but that should work. I'd submit it as a patch, but my git skill is even more limited than my Javascript.

EDIT 2:

Here is some of the code in question from the SO branch of WMD:

    if (g_urls[link_id] != undefined) {
        url = g_urls[link_id];
        if (g_titles[link_id] != undefined) {
            title = g_titles[link_id];
        }
    }

The above code uses link_id (which would be "reduce") in order to index into g_urls and g_titles. As I mentioned before, these will *not* be undefined, even when they "should" be. Instead, they'll be the reduce` functions themselves. Note that, because of the structure here, unless both the URL and title are defined, one or both will be set to the function.

Next is the code that actually creates the problem:

url = escapeCharacters(url,"*_");
var result = "<a href=\"" + url + "\"";

if (title != "") {
    title = title.replace(/"/g,"&quot;");
    title = escapeCharacters(title,"*_");
    result +=  " title=\"" + title + "\"";
}

The code attempts to escape characters on url and title (which, again, will not be "" even when it should be). This kills the code, because you obviously can't escape characters on a function (Wouldn't that be cool if you could, though? Like, it actually stripped the characters from the function code? Pointless? Sure. But cool.). So until both url and title have actual string values, the conversion process craps out, which is why it seems like it freezes.

So how to solve it? The simplest way would be to change the first block of code to something like this:

    if (g_urls[link_id] != undefined && typeof g_urls[link_id] != 'function') {
        url = g_urls[link_id];
        if (g_titles[link_id] != undefined && typeof g_titles[link_id] != 'function') {
            title = g_titles[link_id];
        }
    }

Maybe a Javascript ninja will know of an even better way, but that should work. I'd submit it as a patch, but my git skill is even more limited than my Javascript.

added 709 characters in body
Source Link
Loading
Source Link
Loading