2

I'm testing in internet explorer 8 on windows XP and hitting into a tedious bug. I am trying to test strings with a regular expression that works fine in firefox and fine tested independently in the ie8 console.

But when it through my closure function the string acts strangely

[Edit] More detailed code: Not as nice or clean as the earlier snippet.

var m_TableSorter = (function() {

    // static reg expression string and array for ordering dates
    var dateRegEx = new RegExp(
     "(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\\s\\d{4}");
     ...
     ...
    function greaterThan(left, right) {
        window["globalLeft"] = left;
        window["globalRight"] = right;
        var a = $.trim(left.toLowerCase());
        var b = $.trim(right.toLowerCase());
        window["globalA"] = a.toString();
        window["globalReg"] = dateRegEx;
        if (dateRegEx.test(a) && dateRegEx.test(b)) {
            var yearA = parseInt(a.substring(4,8), 10);
            var yearB = parseInt(b.substring(4,8), 10);
            if (yearA > yearB) {
                return true;
            } else if (yearB > yearA) {
                return false;
            } else {
                /* ... */
                var monthA =
                    $.inArray(a.substring(0,3).toUpperCase(), monthArray);
                var monthB = 
                    $.inArray(b.substring(0,3).toUpperCase(), monthArray);
                m_Debug.tryAssert(monthA >= 0, "Date string malformed");
                m_Debug.tryAssert(monthB >= 0, "Date string malformed");
                if (monthA > monthB) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        //alert("NONDATE");
        if ( a.toUpperCase() >= b.toUpperCase() ) {
            return true;
        }
        return false;
    }

    function mergeArrays(pr_left, pr_right, pr_compareFunction, pr_rowIndex) 
    {
        m_Debug.debugUtilityFunction(arguments);
        var results = new Array();
        var obj;
        /* Merges in order so that if right > left then the results is
         * [left right] otherwise the result is [right left] (Dependant on
         * ascending or descending order)
         */
        while (pr_left.length > 0 || pr_right.length > 0) {
            if (pr_left.length > 0 && pr_right.length > 0) {
                window["globalLeft1"] = $(pr_left[0].children[pr_rowIndex]).text().toString();
                window["globalRight1"] = $(pr_right[0].children[pr_rowIndex]).text().toString();
                var bool = pr_compareFunction(
                    $.trim($(pr_left[0].children[pr_rowIndex]).text()),
                    $.trim($(pr_right[0].children[pr_rowIndex]).text())
                );
                if (bool) {
                    results.push(pr_left.shift());
                } else {
                    results.push(pr_right.shift());
                }
            } else  if (pr_left.length > 0) {
                for (obj in pr_left) {
                    results.push(pr_left[obj]);
                }
                break;
            } else if (pr_right.length > 0) {
                for (obj in pr_right) {
                    results.push(pr_right[obj]);
                }
                break;
            }
        }
        return results;

    }

For the mergeArrays function pr_left & pr_right are jQuery list of TR objects. and im comparing the text in the pr_rowIndex-th cell of the row for two rows.

pr_compareFunction is greaterThan.

dateRegEx.test(a) returns false because the string a is bugged.

Testing in the ie8 console it tells me that

globalA.toLowerCase() == "sep 2010"

returns false. In this case alert(globalA) has shown "sep 2010" and alert(globalLeft) has shown "Sep 2010". I've checked .length on both to be 8 as expected.

globalLeft1 == globalLeft seems to be true but neither are equal to normal strings.

I can't see at all why JScript cannot recognise these two strings. reproducing these exact steps in the console works as expected.

[Further Edit]

Turn's out different implementation of javascript treat non breaking space characters as either a space character class or non-space character class. Whether this is a bug or working as intended is open to discussion.

slava
  • 1,901
  • 6
  • 28
  • 32
Raynos
  • 166,823
  • 56
  • 351
  • 396
  • 1
    You should show more of your code. IE8 does not actually have such bugs as you describe. Also, there's absolutely no need to call "toString()" on the result of "toLowerCase". And why are you using all those global variables? Yuk. – Pointy Oct 06 '10 at 11:50
  • Also: there's no need to put "\\d" in square brackets; "[\\d]" is exactly the same as just "\\d" by itself. Same with "\\s". And do you really want that quantifier "{4}" *outside* the parenthesis? – Pointy Oct 06 '10 at 11:54
  • The global variables are there to force my local variables into global scope so I can debug and mess with them. The toString() is used because I'm paranoid and think JScript is doing something stupid. The regExp became more and more messy because I dont trust it. – Raynos Oct 06 '10 at 12:14
  • Well, whenever you start resorting to superstitious changes while debugging, it's a good indication that something may be wrong in a way that you don't expect. Internet Explorer has its problems, but string comparison is not one of them. You have not posted enough code for anyone to help you, unfortunately. – Pointy Oct 06 '10 at 12:24
  • Non breaking space characters are not treated as whitespace in /s by ie whereas they are treated as whitespace by FF. This causes the regular expression to fail – Raynos Oct 06 '10 at 12:37
  • Ah!! Good job finding that! I think that sometimes non-breaking space is actually implemented as a different Unicode character than plain space characters. – Pointy Oct 06 '10 at 12:43
  • If every browser treated nbsp the same it wouldnt be so hard to find. How do I round up or finish obscure questions like these without actaul answers? – Raynos Oct 06 '10 at 12:49

1 Answers1

3

As mentioned. The issue here was that "\s" in javascript does not include a non breaking space in IE but includes a non breaking space in FF.

Raynos
  • 166,823
  • 56
  • 351
  • 396