views:

56

answers:

1

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 indepedantly in the ie8 console.

But when it through my closure function the string acts strangly

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