开发者

This doesn't happen often: Why is this JavaScript code only broken in Chrome?

I've decided that I've fallen in love with the Markdown editor on Stack Overflow. It's a fork from showdown.js from John Fraser. I want to use this parser on a project of mine, but after开发者_如何学Python analyzing the source, I found that it was a bit messy for my taste.

So I set about modifying the JavaScript code to meet my needs, namely:

  1. Getting rid of all the global variables,
  2. Combining the variable declarations to a single var per scope,
  3. Changing concatenation to array joins,
  4. various other tweaks intended to make the minified source smaller.

I've got everything working beautifully, except for one small problem: The autocomplete code in the command.doList function is wonky in Chrome. I've tested everything to work properly in Internet Explorer, FireFox, and Safari. I've isolated the matter down to the following lines:

// Get the item prefix - e.g. " 1. " for a numbered list, " - " for a bulleted
// list.
getItemPrefix = function () {
    var prefix;
    if (isNumberedList) {
        // the `s` variable is just a string space.
        prefix = [s, num, '. '].join('');
        num++;
    } else {
        prefix = [s, bullet, s].join('');
    }
    return prefix;
};

// Fixes the prefixes of the other list items.
getPrefixedItem = function (itemText) {
    // The numbering flag is unset when called by autoindent.
    if (isNumberedList === undefined) {
        isNumberedList = /^\s*\d/.test(itemText);
    }

    // Renumber/bullet the list element.
    // THE FOLLOWING LINES COMMENTED OUT TO FIX A BUG.
    //itemText = itemText.replace(/^[ ]{0,3}([\*\+\-]|\d+[.])\s/gm,
        // function () { return getItemPrefix(); });
    return itemText;
};

The two commented-out lines near the end stop Chrome from behaving wonky, but numbered lists no longer auto-increment. As soon as I un-comment the lines, all major browsers begin working normally, except for Chrome. (Chrome goes crazy and auto-completes the list with extra numbering and bullets, also happens when trying to use an unordered list.)

The problem obviously doesn't exist here on Stack Overflow, so I'm hoping it's just a stupid oversight on my part. (Either that or Stack Overflow has updated and fixed the bug.)


You can look at the fix for another WMD fork (line 32).


You shouldn't need to backslash the *+- in square brackets, should you? That might be what Chrome is having problems with.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜