prototype: Make document.getElementsByClassName match a subset of 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.

This commit is contained in:
Sam Stephenson 2007-06-19 20:59:14 +00:00
parent 0894f235b8
commit d6bf5e40ce
3 changed files with 17 additions and 15 deletions

View File

@ -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]

View File

@ -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) ||

View File

@ -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');