Based largely on behavior.js by Ben Nolan, this patch adds a prototype object called Behavior which applies behaviors to page elements using the new Selector prototype object.
This will clean up the generated markup and hopefully provide an easy way to maintain consistent selector-based behavior of page elements, even when RJS and other DOM manipulation adds and removes elements.
Basic documentation of the helper methods is included and contains examples of use, such as this one, which modifies all PDF links to open in a new window:
register_behaviors 'a' =>
'if (/\.pdf$/i.test(element.href)) element.target = "_blank";'
Thanks to kastner for the idea and for design help, and to Ben Nolan for the original idea.