Ticket #195 (closed enhancement: fixed)

Opened 2 years ago

Last modified 2 years ago

Support for getting elements by CSS selectors

Reported by: arnarbi at gmail Assigned to:
Priority: highest Milestone:
Component: component1 Version:
Severity: normal Keywords:
Cc: therve

Description

Common functionality in js libraries are convenience functions to get elements by CSS selector syntax. Attached is an impementation of this, initially ported from Prototype.

It supports the following features from the CSS3 draft:

  • Select by tag name (A)
  • Select by class (.theclass)
  • Select by id (#someid)
  • Combinators
    • Descendant: E F
    • Child: E > F
    • Immediate following sibling: E + F
    • Following sibling: E ~ F
  • Attribute selectors
    • simple "has attribute" (without operator)
    • = equal
    • != not equal (not in CSS std.)
    • ~= word containment
    • ^= starts-with
    • $= ends-with
    • *= substring containment
    • |= first part of hyphen deleimited (eg. lang|="en" matches lang="en-US")
  • Pseudo-classes
    • :root, :nth-child, :nth-last-child, :nth-of-type, :nth-last-of-type, :first-child, :last-child, :first-of-type, :last-of-type, :only-child, :only-of-type, :empty, :enabled, :disabled, :checked, :not(..)

For the :nth-* pseudoclasses, the "an+b" syntax is partially supported, specifically a and b must be non-negative and only a is optional (this differs from the CSS std.) Also, "odd" and "even" are supported, e.g. "table tr:nth-child(odd)" gives you every other row of table starting with the first one.

I'm not too sure if :enabled, :disabled or :checked work correctly.

This needs testing and writing of unit tests.

At first this is a seperate module, but I would like to see this be included ind MochiKit.DOM if people like it and don't mind the cost (13Kb).

The W3C CSS Selectors working draft: http://www.w3.org/TR/2005/WD-css3-selectors-20051215/

Attachments

Selector.js (12.1 kB) - added by arnarb at gmail on 10/06/06 10:54:39.

Change History

10/06/06 10:54:39 changed by arnarb at gmail

  • attachment Selector.js added.

10/07/06 18:00:44 changed by bob@redivi.com

  • type changed from defect to enhancement.

When this gets unit tests, it'll go in.

10/08/06 03:41:16 changed by therve@gmail.com

I've created a branch in source:/mochikit/branches/selector. I thought it'll be easier to add tests against this branch.

10/08/06 09:07:43 changed by anonymous

  • cc set to therve.

OK copied tests for the groups message, they seem to pass under firefox/konqueror.

10/08/06 12:43:13 changed by arnarbi@gmail.com

Thanks, I'll check out the branch tomorrow and clean this up and add some tests.

Also, I'll probably remove the UI pseudo classes (:enabled, :disabled and :checked) to begin with since they didn't work in IE at all.

Should I keep this as a seperate module - or integrate id into MochiKit.DOM as someone on the mailinglist suggested?

10/09/06 06:38:16 changed by arnarbi@gmail.com

I have a patch against the branch (r1155) that cleans upp stuff and fixes a few bugs to make things work in IE.

The tests all pass in Firefox, Opera and Safari. Most pass in IE. The ones that don't pass in IE are a[title] - because IE claims all A elements have a title attribute, by default an empty string - and the :enabled pseudoclass - because IE DOM defines a "disabled" property on all elements, not just UI elements.

I can't attach the patch, it gets rejected as spam - probably because of all the hyperlinks in the test.

(follow-up: ↓ 7 ) 10/09/06 07:36:59 changed by therve@gmail.com

I applied your patch, the tests pass under Firefox and Konqueror. I also did some cleanup in the namespaces calls.

I'm more for keeping it separated: the more modular we are, the better it is. But we could make it into the packed version.

(in reply to: ↑ 6 ) 10/09/06 17:12:23 changed by arnarbi@gmail.com

Replying to therve@gmail.com:

I'm more for keeping it separated: the more modular we are, the better it is. But we could make it into the packed version.

Cool, I'm happy with that.

11/10/06 02:05:04 changed by therve@gmail.com

  • owner deleted.
  • priority changed from normal to highest.

11/14/06 05:19:13 changed by therve@gmail.com

OK it's in the trunk. I'm leaving this opened for discussion on inclusion in the packed version.

11/17/06 07:36:16 changed by therve@gmail.com

  • status changed from new to closed.
  • resolution set to fixed.