Thanks to visit codestin.com
Credit goes to phabricator.services.mozilla.com

Page MenuHomePhabricator

Bug 1861466 - Add CustomStateSet and :state() pseudo. r=emilio,smaug,canadahonk
ClosedPublic

Authored by keithamus on Oct 26 2023, 5:17 PM.
Referenced Files
Unknown Object (File)
Wed, Feb 25, 11:35 PM
Unknown Object (File)
Nov 13 2025, 12:27 PM
Unknown Object (File)
Nov 13 2025, 12:27 PM
Unknown Object (File)
Nov 13 2025, 12:27 PM
Unknown Object (File)
Nov 13 2025, 12:27 PM
Unknown Object (File)
Nov 13 2025, 12:27 PM
Unknown Object (File)
Nov 13 2025, 12:27 PM
Unknown Object (File)
Nov 13 2025, 12:27 PM

Event Timeline

There are a very large number of changes, so older changes are hidden. Show Older Changes
keithamus updated this revision to Diff 780867.
keithamus retitled this revision from WIP: Bug 1861466 - A very early WIP of CustomStateSet to Bug 1861466 - Add CustomStateSet and :state() psuedo. r=emilio,smaug,canadahonk.
keithamus added reviewers: emilio, smaug, canadahonk.
dom/html/CustomStateSet.cpp
92

Figured it out for now by getting the presshell to invalid styles.

4 defects closed compared to the previous diff 780867.


If you see a problem in this automated review, please report it here.

emilio requested changes to this revision.Oct 28 2023, 11:51 PM
emilio added inline comments.
dom/base/rust/lib.rs
135 ↗(On Diff #780869)

I don't understand why you need this flag, care explaining?

dom/html/CustomStateSet.cpp
88

This is not only too inefficient. It's also incorrect if there are combinators (:state(foo) + div won't get invalidated). Need tests for that, but also we should hook this up into the style invalidation machinery just like ElementStatesChanged does.

92

Fwiw, please use needinfo for questions next time since phab email is super easy to miss

125

This assertion seems a bit silly

dom/html/CustomStateSet.h
62

This seems leftover

dom/html/ElementInternals.cpp
31

(here)

55

No need to initialise a refptr explicitly fwiw

dom/html/ElementInternals.h
209

This needs to be traversed / unlinked to prevent leaks.

servo/components/style/gecko/wrapper.rs
1217

Leftover?

servo/components/style/invalidation/element/invalidation_map.rs
570 ↗(On Diff #780869)

Hm?

This revision now requires changes to proceed.Oct 28 2023, 11:51 PM
keithamus added inline comments.
dom/base/rust/lib.rs
135 ↗(On Diff #780869)

I figured that this flag would allow the matcher to skip a bunch of work if there are no custom states set. Premature/needless optimization?

dom/html/CustomStateSet.cpp
88

I've added some tests. I'm struggling to know what to do here though.

92

Thanks for the tip! Still new to all this, so this kind of information is very much appreciated.

125

Oh yeah 😅 this was when I was failing to understand why my methods were being called because I missed that setlike<> IDL needs the partial interface to override the methods for this code to actually kick in.

servo/components/style/gecko/wrapper.rs
1217

Yes 😆 can you tell I struggled with this?

keithamus updated this revision to Diff 780892.
keithamus marked 3 inline comments as done.

psuedo in the commit message ;)

smaug requested changes to this revision.Oct 31 2023, 10:26 PM

r- because I couldn't figure out how CheckStateToken should work.

dom/base/Element.cpp
4407

How can we know that we have slots around? Is there some hidden expectation that GetCustomStates is called only after slots has been created? If so, that should be somehow reflected in the method name.
Or at least some comment is needed to explain when it is ok to call the method.

dom/html/CustomStateSet.cpp
26

One could simplify this by using NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE. I know I know, there are tons of macros.

73

This doesn't seem to quite check whether aState is valid.
It should be <dashed-ident>, which is -- and <custom-ident>, and <custom-ident> is https://drafts.csswg.org/css-values-4/#identifier-value
Looks like --default as an example wouldn't be valid.
.... If I'm reading https://wicg.github.io/custom-state-pseudo-class/#dom-customstateset-add

On the other hand if I'm looking at https://github.com/whatwg/html/pull/8467, I'm not sure what the behavior should be.

This revision now requires changes to proceed.Oct 31 2023, 10:26 PM
keithamus updated this revision to Diff 781970.
keithamus marked 2 inline comments as done.
dom/base/Element.cpp
4407

CustomStateSet can only be retrieved from ElementInternals which only exists if CustomElementData does - and that only exists in the extended DOM slots. However I have no strong opinions on it remaining that way and I'm happy to use ExtendedDOMSlots() instead.

dom/html/CustomStateSet.cpp
73

Based on further discussions in the whatwg channels, we've settled to not add validation - so I've removed it.

emilio requested changes to this revision.Nov 1 2023, 6:49 PM

Looks good modulo the comments below. Thanks!

dom/base/rust/lib.rs
135 ↗(On Diff #780869)

Is it really all that much work? If there are no states you don't do much work at all, the array is empty etc. Let's remove this.

dom/html/CustomStateSet.cpp
48

OwnerDoc can't return null.

59

Okay, we can go for this for now, but let's file a follow-up to make invalidation correct and reference it from here, since this doesn't deal with :has, sibling combinators, :nth-child(.. of :state()), etc. All those things need tests (they can be marked as failing for now).

63

There's no need to check Failed() at the beginning of the method right?

dom/html/ElementInternals.cpp
347

Can this just be a regular pointer? It's kept alive by the ElementInternals anyways.

servo/components/style/gecko/wrapper.rs
1370

I'd remove these rustfmt changes.

2024

This can just be self.has_custom_state(..). It's not substantially more expensive if there are no states to check html-ness.

This revision now requires changes to proceed.Nov 1 2023, 6:49 PM
keithamus updated this revision to Diff 782194.
keithamus marked 6 inline comments as done.
dom/html/CustomStateSet.cpp
59

Definitely happy to follow up with more robust tests and a proper implementation here.

keithamus retitled this revision from Bug 1861466 - Add CustomStateSet and :state() psuedo. r=emilio,smaug,canadahonk to Bug 1861466 - Add CustomStateSet and :state() pseudo. r=emilio,smaug,canadahonk.
emilio added a project: testing-approved.

Style stuff looks good, one suggestion about the DOM

dom/base/Element.h
623

Let's call this either EnsureCustomStates(), or GetOrCreateCustomStates(), since it's otherwise very subtle that it allocates.

smaug added inline comments.
dom/html/CustomStateSet.cpp
59

Delete returns true if something was deleted, so wouldn't it make sense to check also the return value before removing entry from native side?

dom/html/CustomStateSet.h
56

It makes very much sense that mTarget is a strong pointer. I wonder if there are tests where JS has strong reference only to CustomStateSet and weak ref to the relevant element, and then GC/CC is triggered and element should stay around.
I'm pretty sure we're missing similar tests elsewhere, so I'm not really requesting one, but something to think about anyhow.

This revision is now accepted and ready to land.Nov 3 2023, 5:32 PM
dom/html/CustomStateSet.h
56

Should I use a WeakPtr instead?

keithamus added inline comments.
dom/html/CustomStateSet.h
56

Actually I wrote a test instead, I know you didn't request one but I felt better with it. Hopefully it is sufficient.