WHAT I DO:
- Make sure Firebug is working.
- Put a break point in the isWrappedInParens() function.
- Navigate to my webapp.
- Trigger a call to isWrappedInParens().
- Step through isWrappedInParens(). Everything goes fine, but it does not proceed past the line of code indicated as the “CRASH POINT”.
- I also tried it without Firebug running or having a break point, but it still freezes.
WHAT I NOTICE:
- In most cases, isWrappedInParens() works fine.
- When it does not work, Firefox freezes. I can still minimize/expand/close the window, though.
- I also noticed that when the test string is a bit shorter (less parentheses), firefox hangs but eventually finishes (~30 seconds) correctly.
EXAMPLE STRING THAT CRASHES FIREFOX
// Note that this is not wrapped in parentheses,
// since it is two separate sets of nested parentheses
var test = "(the OR (and) OR (and) OR (and)) AND ((to) OR (to) OR (to))";
BACKGROUND
- Browser: Firefox 3.6.18
- The webapp is a jetty application.
CODE
isWrappedInParens = function(str){
if(_.isNull(str)) {
return false;
}
str = str.trim();
var pattern = /^[(](([(][^()]+[)]|[^()]+)|[(]([(][^()]+[)]|[^()]+)+[)])+[)]$/;
var matchesPattern;
try{
matchesPattern = str.match(pattern) || null; //CRASH POINT!!!!!!!!!!!
}catch(err){
return false; //Note that no error is ever caught from freezing
}
var isWrapped = !_.isUndefined(matchesPattern) && !_.isNull(matchesPattern);
return isWrapped;
}
WHERE THE REGEX CAME FROM:
// Atoms, building blocks for the expressions
var parenAtom = "[(][^()]+[)]";
var nonParenAtom = "[^()]+";
// Expressions, building blocks for the final regular expression
var baseCase = "(" + parenAtom + "|" + nonParenAtom + ")";
var nestedCase = "[(]_base_[)]"
.replace("_base_", baseCase);
// Regular Expression
var wholeCase = "^[(](_base_|_nested_)+[)]$"
.replace("_base_", baseCase)
.replace("_nested_", nestedCase);
var pattern = new RegExp(wholeCase, "");
From my comment:
In this bug there are a couple of comments from Brendan Eich about the issue, and he lists several other bugs (some pretty old). Another comment there alludes to an “overhaul in regular expression” in Firefox 4, suggesting that many changes occurred as far back as that.