JRigby wrote:
> After upgrading to Scriptaculous 1.6.5 I'm having a problem with my
> code. For reasons I won't get into here, I need to store the class name
> of an element as <<type>>|<<id>>, for example "Technology|999". I use
> the pipe to split the class name.
>
> In the previous version of prototype Element.hasClassName worked fine
> with this, however the newest version seems to ignore everything after
> the pipe. So:
>
> <div id='Category999' class='Category|555'>Test</div>
> Element.hasClassName('Category999','Category|555')); // Returns True
> Element.hasClassName('Category999','Category|556')); // Returns True
>
> I noticed that the hasClassName function has been changed in the newest
> prototype.js (see below). I assuming the problem is in the RegExp, but
> I don't really understand how to get around this. Escaping the pipe
> does not work.

If the pipe is in the string to be matched, e.g. "Category|555" it
requires no special treatment.

>
> Any help would be appreciated, thanks!
>
> hasClassName (New)
>  function(element, className) {
>     if (!(element = $(element))) return;
>     var elementClassName = element.className;
>     if (elementClassName.length == 0) return false;
>     if (elementClassName == className ||
>         elementClassName.match(new RegExp("(^|\\s)" + className +

The pipe "|" character in a regular expression is an "OR" operator.
The above will match the pattern at the start (^) or with a leading
space.  That seems less than optimal, a better operator is \b or word
break.  It is also better to use test than match, as it returns a
boolean, whereas match returns an object or null, which must then be
type converted to boolean.

The function then becomes:

  function hasClassName (element, className) {
    var re = new RegExp('\\b' + className '\\b');
    var elClassName = element.className;
    return elClassName && re.test(elClassName);
  }

Or if you like obfuscation (wraped for posting only):

  function hasClassName(element, className){
    return (new RegExp('\\b' + className
               + '\\b').test(element.className));
  }


> "(\\s|$)")))
>       return true;
>     return false;
>   }
>
> hasClassName (Old)
> function(element, className) {
>     if (!(element = $(element))) return;
>     return Element.classNames(element).include(className);

If the intention is that you can pass either an ID or a DOM object to
the function, then why not:

  return (element = $(element)) &&
      (new RegExp('\\b' + className + '\\b').test(element.className));

But to my mind, you are much better off to work with object references
and not IDs.  Use getElementById() (and hence $()) as little as
possible, it will save some grief and speed up your code, sometimes
enormously.


>   }

-- 
Fred


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups 
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to