W3C home > Mailing lists > Public > whatwg@whatwg.org > September 2005

[whatwg] getElementsByClassName()

From: Lachlan Hunt <lachlan.hunt@lachy.id.au>
Date: Mon, 05 Sep 2005 18:43:10 +1000
Message-ID: <431C051E.9050604@lachy.id.au>
Aankhen wrote:
> On 9/5/05, Lachlan Hunt <lachlan.hunt at lachy.id.au> wrote:
> 
>>1. Equivalent to ("foo", "bar") (or [class~=foo][class~=bar], or
>>    .foo.bar in CSS)
>>2. The way it currently works. ie. matches "foo bar", not "bar foo"
>>3. Error, return nothing.
> 
> I suggest #2, which implies consistently treating the first argument
> passed to the function as a single class name to match (this means
> "foo bar" would always return no elements,

No, as already demonstrated, #2 does return matches in some cases. 
However, after some more testing, the results are inconsistent among 
browsers, which makes that alternative inappropriate.  For example, 
compare the results of these three tests in different browsers:  (I 
tested Firefox, Opera and IE on Windows).

http://www.lachy.id.au/dev/script/examples/DOM/tests/007.html
http://www.lachy.id.au/dev/script/examples/DOM/tests/011.html
http://www.lachy.id.au/dev/script/examples/DOM/tests/012.html

007: Firefox: E and F, Opera and IE: E
011: All: (none)
012: Firefox: (none), Opera and IE: F

The only difference between the tests is the white space in the 
parameter.  The first uses a single space, the second uses multiple 
spaces and the third uses a space followed by a tab.

> Special-casing "foo bar" and other values seems to be adding
> complexity without much return.

It's not about special casing, it's about defining error recovery 
consistently between implementations.  As it's currently defined, ("foo 
bar" is, I believe, erroneous since each parameter represents a single 
class name.  However, the results from different implementations should 
be identical, which is why the behaviour needs to be defined and why #2 
is not appropriate.

That leaves the choice between #1 and #3 (or possibly #4 if someone 
thinks of another).  I have no strong opinion either way, I just need it 
defined so I can implement it.

> If multiple class names really need to be handled, my suggestion would
> be to take a single array as a parameter, e.g.
> `getElementsByClassName(["foo"])` and `getElementsByClassName(["foo",
> "bar"])`.

Multiple class names are already supported with the ability to pass any 
number of arguments.  It is defined in the spec, and I implemented it, as:

getElementsByClassName(className1 [, className2, ...] )

-- 
Lachlan Hunt
http://lachy.id.au/
Received on Monday, 5 September 2005 01:43:10 UTC

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 16:58:42 UTC