Skip to content

Add has() selector helper - #5518

Merged
fregante merged 12 commits into
mainfrom
has-selector
Jul 5, 2022
Merged

fregante merged 12 commits into
mainfrom
has-selector

Conversation

@fregante

@fregante fregante commented Mar 17, 2022 •

Copy link
Copy Markdown
Member

4 fun

It can be used to replace:

- select('child').closest('parent');
+ selectHas('parent:has(child)')
- select('child').closest('parent').querySelector('family');
+ selectHas('parent:has(child) family')

but not:

existingElement.closest('parent')

Since this isn't native, it can't be used:

  • in selector-observer
  • in element-ready

as well as:

Side note:

  • Fixed: it's probably less efficient because instead of using select + closest it uses select.all + select multipletimes 😅 but how bad can it be? Maybe I can fix this though
  • Untested

@fregante fregante added the meta Related to Refined GitHub itself label Mar 17, 2022
@fregante fregante closed this Apr 8, 2022
@fregante
fregante deleted the has-selector branch April 8, 2022 04:38
@fregante
fregante restored the has-selector branch July 4, 2022 03:10
@fregante fregante reopened this Jul 4, 2022
@fregante
fregante marked this pull request as ready for review July 4, 2022 08:37
@fregante

fregante commented Jul 4, 2022

Copy link
Copy Markdown
Member Author

Tested.

Almost every replacement I made was incorrect: x.closest(div) is not equivalent to div:has(x)

  • If x has many div parents, the selector will pick the "farthest" instead of the "closest"
  • If x is a composite selector like .a .b, the final selector could look like .a div:has(.b) instead of div:has(.a .b)

I'll merge this PR to enable more usage in the future. Also ideally we'd add select.all support because that's the most useful/common one.

function init(): void {
const actionRepo = select('aside .octicon-repo')!
.closest('a')!
const actionRepo = selectHas('a:has(aside .octicon-repo)')!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These selectors are real, valid selectors and can be tested natively in Safari. In the future we'll be able to just replace selectHas with select

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm tempted to #enable-experimental-web-platform-features now

Or maybe install Chrome Dev which just added :has() support in 105. But it is scheduled to release on Aug 30 according to Chromium Dash, so I'll just wait for two months 😆

@fregante fregante Jul 5, 2022 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Firefox will delay this a while longer. No work in progress at all. https://bugzilla.mozilla.org/show_bug.cgi?id=418039

I think we can’t try the native implementation until we’re ready to ship or else we need to test each selector for both native and polyfilled version. The polyfill is very limited

@fregante
fregante merged commit 88e2668 into main Jul 5, 2022
@fregante
fregante deleted the has-selector branch July 5, 2022 16:26
@fregante

fregante commented Jul 5, 2022

Copy link
Copy Markdown
Member Author

Looking forward to seeing more uses in our features!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

meta Related to Refined GitHub itself

Development

Successfully merging this pull request may close these issues.

Replace select().closest() calls with :has

2 participants