Martin Bialasinski
2005-Aug-21 12:18 UTC
[Rails-spinoffs] Patch for Element.Class - wrong regexp
Hello, I noticed (due to a recent post by Martin Honnen to clp) that Element.Class uses wrong regexps for its operations. E.g., Element.Class.has() will find "classA" in "classA-foobar" I also changed the interface of .has() to the more general one used in .has_any() --- util.js.old 2005-08-21 19:18:40.000000000 +0200 +++ util.js 2005-08-21 19:31:42.000000000 +0200 @@ -219,7 +219,7 @@ element = $(element); var regEx; for(var i = 1; i < arguments.length; i++) { - regEx = new RegExp("^" + arguments[i] + "\\b\\s*|\\s*\\b" + arguments[i] + "\\b", ''g''); + regEx = new RegExp("(^|\\s)" + arguments[i] + "(\\s|$)", ''g''); element.className = element.className.replace(regEx, '''') } }, @@ -232,14 +232,23 @@ } }, - // returns true if all given classes exist in said element + // expects arrays of strings and/or strings as optional paramters + // Element.Class.has_any(element, [''classA'',''classB'',''classC''], ''classD'') has: function(element) { element = $(element); if(!element || !element.className) return false; var regEx; for(var i = 1; i < arguments.length; i++) { - regEx = new RegExp("\\b" + arguments[i] + "\\b"); - if(!regEx.test(element.className)) return false; + if((typeof arguments[i] == ''object'') && + (arguments[i].constructor == Array)) { + for(var j = 0; j < arguments[i].length; j++) { + regEx = new RegExp("(^|\\s)" + arguments[i][j] + "(\\s|$)"); + if(!regEx.test(element.className)) return false; + } + } else { + regEx = new RegExp("(^|\\s)" + arguments[i] + "(\\s|$)"); + if(!regEx.test(element.className)) return false; + } } return true; }, @@ -254,11 +263,11 @@ if((typeof arguments[i] == ''object'') && (arguments[i].constructor == Array)) { for(var j = 0; j < arguments[i].length; j++) { - regEx = new RegExp("\\b" + arguments[i][j] + "\\b"); + regEx = new RegExp("(^|\\s)" + arguments[i][j] + "(\\s|$)"); if(regEx.test(element.className)) return true; } } else { - regEx = new RegExp("\\b" + arguments[i] + "\\b"); + regEx = new RegExp("(^|\\s)" + arguments[i] + "(\\s|$)"); if(regEx.test(element.className)) return true; } } Bye, Martin