diff --git a/CHANGELOG b/CHANGELOG index 6505636..b906e43 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Add Element#identify, which returns the element's ID if it exists, or sets and returns a unique, auto-generated ID (of the form "anonymous_element_" + auto-incremented digit) otherwise. Use this when you need to ensure an element has an ID. Closes #9012. [Jeff Watkins, sam, Tobie Langel] + * Make Element#readAttribute work for cloned elements in IE. Closes #8481. [chem, Tobie Langel] * Template enhancements. Closes #8166. [Christophe Porteneuve] diff --git a/src/dom.js b/src/dom.js index df6e9a9..f2ab680 100644 --- a/src/dom.js +++ b/src/dom.js @@ -233,6 +233,15 @@ Element.Methods = { return Selector.findChildElements(element.parentNode, args).without(element); }, + identify: function(element) { + element = $(element); + var id = element.readAttribute('id'), self = arguments.callee; + if (id) return id; + do { id = 'anonymous_element_' + self.counter++ } while ($(id)); + element.writeAttribute('id', id); + return id; + }, + readAttribute: function(element, name) { element = $(element); if (Prototype.Browser.IE) { @@ -597,7 +606,7 @@ Element.Methods = { } }; -Element.Methods.getElementsBySelector = Element.Methods.select; +Element.Methods.identify.counter = 1; if (!document.getElementsByClassName) document.getElementsByClassName = function(instanceMethods){ function isArray(className) { @@ -636,6 +645,7 @@ if (!document.getElementsByClassName) document.getElementsByClassName = function }(Element.Methods); Object.extend(Element.Methods, { + getElementsBySelector: Element.Methods.select, childElements: Element.Methods.immediateDescendants }); diff --git a/test/unit/dom.html b/test/unit/dom.html index 124fac7..b7dc397 100644 --- a/test/unit/dom.html +++ b/test/unit/dom.html @@ -342,6 +342,13 @@
+