stylo: Implement the class/id case-insensitive matching quirk

NEW
Unassigned

Status

()

Core
CSS Parsing and Computation
P2
normal
18 days ago
4 days ago

People

(Reporter: bz, Unassigned)

Tracking

(Blocks: 2 bugs)

53 Branch
Points:
---
Dependency tree / graph

Firefox Tracking Flags

(Not tracked)

Details

Spec: https://drafts.csswg.org/selectors/#id-selectors and https://drafts.csswg.org/selectors/#class-html both say that matching is ASCII-case-insensitive in quirks mode.

Testcase:

  <style>#foo { color: green; }</style>                                                
  <div id="FOO">Should be green</div>
Blocks: 1324348
Just to scope out the work, there are two main parts here:

1)  Fixing selector matching to do case-insensitive matching for ids and classes in quirks mode.
2)  Fixing the rulehashes in stylist to do case-insensitive lookups.
Depends on: 1364746
Simon, you seem to be working on parts of this. How much overlap is there?
Flags: needinfo?(simon.sapin)
Priority: -- → P2
Oh nevermind, looks like Simon was working on regular case-insensitive matching.
Flags: needinfo?(simon.sapin)
In current code there’s an asymmetry between handling of IDs and classes. In the selectors::Element trait, the impl return the optional ID of an element and the selectors crate does the string (or atom) comparison. For classes, selectors provides the string/atom form the class selector and the impl returns a boolean. I think these should be "unified", picking one of:

* The impl does the string comparisons, replacing Element::get_id with Element::has_id. However Servo + Stylo has a number of these impls (I think 5 in total?), so it’s as many places that need to not forget to check for quirks mode.

* The selectors crate does the string comparisons, deciding which kind based on a new Element::in_quirks_mode_document method. The selectors crate needs some way to iterate on an element’s classes. An external iterator with the std::iter::Iterator trait is verbose to implement, especially in a generic context. So we could have an internal iterator: an FnMut(&ClassName) callback called for each class. But we need to stop iteration when a match is found so the callback needs to return at least a boolean, but I’d prefer a new `enum Next { Continue, Stop }`. [1] has a trait for internal iterators, but it seems to be unmaintained [2].

[1] https://docs.rs/traverse/
[2] https://github.com/reem/rust-traverse/pull/26 is months old and didn’t get a response.
One note on the above: We want to minimize the number of FFI calls as we go, and we do NOT want to be doing UTF16-to-UTF8-and-back conversions and allocating string copies.

That means we'll probably want a Rust-side no-copy no-conversion ASCII-case-insensitive comparison on atoms.
We already have Atom::eq_ignore_ascii_case: https://github.com/servo/servo/blob/7f80d9cb45/components/style/gecko_string_cache/mod.rs#L241

We probably want to add a fast path that tries `self == other` first.
(In reply to Simon Sapin (:SimonSapin) from comment #6)
> We already have Atom::eq_ignore_ascii_case:
> https://github.com/servo/servo/blob/7f80d9cb45/components/style/
> gecko_string_cache/mod.rs#L241

Nice!

> We probably want to add a fast path that tries `self == other` first.

Pushed https://github.com/servo/servo/pull/17022 for this.
You need to log in before you can comment on or make changes to this bug.