diff --git a/CHANGELOG b/CHANGELOG index e1c3eb6..2e835bc 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,9 +1,8 @@ *SVN* -* Make document.getElementsByClassName match the WHATWG Web Applications 1.0 specification which was adopted in Firefox 3 (http://www.whatwg.org/specs/web-apps/current-work/#getelementsbyclassname). It now supports multiple class names given as an array or a space-separated list in a string. The method will only return the nodes that match all the class names. In browsers that implement the method natively it will not be overwritten. Closes #8401. [Mislav Marohnić] - Examples: +* Make document.getElementsByClassName match the WHATWG Web Applications 1.0 specification which was adopted in Firefox 3 (http://www.whatwg.org/specs/web-apps/current-work/#getelementsbyclassname). It now supports multiple class names given as a whitespace-separated list in a string. Array argument is not supported. The method will only return the nodes that match all the class names. In browsers that implement the method natively it will not be overwritten. Closes #8401. [Mislav Marohnić] + Example: document.getElementsByClassName('foo bar') - document.getElementsByClassName(['foo', 'bar']) * Fix a Safari rendering issue when floating elements could temporarily disappear when opacity was set to 1. Closes #7063. References #3044, #3813, #6706. [Thomas Fuchs, davidjrice] diff --git a/src/dom.js b/src/dom.js index f14fb80..ed96502 100644 --- a/src/dom.js +++ b/src/dom.js @@ -597,24 +597,24 @@ Element.Methods = { if (!document.getElementsByClassName) document.getElementsByClassName = function(instanceMethods){ function isArray(className) { - return className.constructor == Array || (/\s/.test(className) && !className.toString().blank()); - } - function classNamesArray(classNames) { - return classNames.constructor == Array ? classNames : $w(classNames.toString()); + return ; } function iter(name) { - return name.toString().blank() ? null : "[contains(concat(' ', @class, ' '), ' " + name + " ')]"; + return name.blank() ? null : "[contains(concat(' ', @class, ' '), ' " + name + " ')]"; } instanceMethods.getElementsByClassName = Prototype.BrowserFeatures.XPath ? function(element, className) { - var cond = isArray(className) ? classNamesArray(className).map(iter).join('') : iter(className); + className = className.toString().strip(); + var cond = /\s/.test(className) ? $w(className).map(iter).join('') : iter(className); return cond ? document._getElementsByXPath('.//*' + cond, element) : []; } : function(element, className) { - var elements = [], classNames = (isArray(className) ? classNamesArray(className) : null); - if (classNames ? !classNames.length : className.toString().blank()) return elements; + className = className.toString().strip(); + var elements = [], classNames = (/\s/.test(className) ? $w(className) : null); + if (!classNames && !className) return elements; + var nodes = $(element).getElementsByTagName('*'); - className = ' ' + (classNames ? classNames.join(' ') : className) + ' '; + className = ' ' + className + ' '; for (var i = 0, child, cn; child = nodes[i]; i++) { if (child.className && (cn = ' ' + child.className + ' ') && (cn.include(className) || diff --git a/test/unit/dom.html b/test/unit/dom.html index 3ad4e3b..24db2df 100644 --- a/test/unit/dom.html +++ b/test/unit/dom.html @@ -392,11 +392,14 @@ if (Prototype.Browser.IE) assertUndefined(document.getElementById('unextended').show); - assertElementsMatch(list.getElementsByClassName('A'), 'li.A.C'); - assertElementsMatch(list.getElementsByClassName('C A'), 'li.A.C'); assertElementsMatch(div.getElementsByClassName('B'), 'ul#class_names_ul.A.B', 'div.B.C.D'); assertElementsMatch(div.getElementsByClassName('D C B'), 'div.B.C.D'); - assertElementsMatch(div.getElementsByClassName($w('D C B')), 'div.B.C.D'); + assertElementsMatch(div.getElementsByClassName(' D\nC\tB '), 'div.B.C.D'); + assertElementsMatch(div.getElementsByClassName($w('D C B'))); + assertElementsMatch(list.getElementsByClassName('A'), 'li.A.C'); + assertElementsMatch(list.getElementsByClassName(' A '), 'li.A.C'); + assertElementsMatch(list.getElementsByClassName('C A'), 'li.A.C'); + assertElementsMatch(list.getElementsByClassName("C\nA "), 'li.A.C'); assertElementsMatch(list.getElementsByClassName('B')); assertElementsMatch(list.getElementsByClassName('1'), 'li.1'); assertElementsMatch(list.getElementsByClassName([1]), 'li.1');