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. 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 + "(\\s|$)"))) return true; return false; } hasClassName (Old) function(element, className) { if (!(element = $(element))) return; return Element.classNames(element).include(className); } --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 14, 7:49 pm, "JRigby" <jeff.ri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: ...> 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....> 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 + > "(\\s|$)"))) > return true; > return false; > }How about replacing the above RegExp with this: RegExp("(^[|\\s])" + className + "([\\s|]$)") Does that help? --- A. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Christophe Porteneuve
2006-Nov-15 07:51 UTC
Re: Element.hasClassName problem after Scriptaculous 1.6.5
JRigby a écrit :> 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.Aside from valid replies by my peers, I should mention that class names are not, syntactically, allowed the ''|'' character. Class names follow Lex {ident} syntax, which mostly boils down to regular identifier syntax. If you just need a splitter, use anything else allowed, for instance ''_'', which is just as well. This way, you shouldn''t run into any trouble to start with. Replacing the code of hasClassName sounds like a very short-term solution, as it will get crushed by any code update from the repository. -- Christophe Porteneuve a.k.a. TDD "[They] did not know it was impossible, so they did it." --Mark Twain Email: tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Thomas Fuchs
2006-Nov-15 08:36 UTC
Re: Element.hasClassName problem after Scriptaculous 1.6.5
Prototype and script.aculo.us have the following convention for id attributes and CSS classnames: ''id'' attributes: all lowercase, split with ''_'' (example: list_item_1) CSS classes: all lowercase, split with ''-'' (example: error-message) Of course you can use other stuff, but any default settings, where applicable, are adjusted to best work with this scheme. This also plays part with lots of stuff in the upcoming Rails 1.2 release. Best, Thomas Am 15.11.2006 um 08:51 schrieb Christophe Porteneuve:> > JRigby a écrit : >> 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. > > Aside from valid replies by my peers, I should mention that class > names > are not, syntactically, allowed the ''|'' character. Class names follow > Lex {ident} syntax, which mostly boils down to regular identifier > syntax. If you just need a splitter, use anything else allowed, for > instance ''_'', which is just as well. > > This way, you shouldn''t run into any trouble to start with. Replacing > the code of hasClassName sounds like a very short-term solution, as it > will get crushed by any code update from the repository. > > -- > Christophe Porteneuve a.k.a. TDD > "[They] did not know it was impossible, so they did it." --Mark Twain > Email: tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org > > >-- Thomas Fuchs wollzelle http://www.wollzelle.com questentier on AIM madrobby on irc.freenode.net http://www.fluxiom.com :: online digital asset management http://script.aculo.us :: Web 2.0 JavaScript http://mir.aculo.us :: Where no web developer has gone before --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks everyone for the quick replies. I replaced everything with an underscore and it works fine. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---