Skip to content

Instantly share code, notes, and snippets.

@TakayoshiKochi
Created August 9, 2017 07:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TakayoshiKochi/a7cdfc79a3b985f5d486a0b29171c4ac to your computer and use it in GitHub Desktop.
Save TakayoshiKochi/a7cdfc79a3b985f5d486a0b29171c4ac to your computer and use it in GitHub Desktop.
Replacement for querySelector("::slotted(x)")
function querySlotted(root, selector) {
let slots = root.querySelectorAll('slot');
let matched = [];
slots.forEach((slot) => {
matched = matched.concat(slot.assignedNodes().filter((el) => {
return el.matches(selector);
}));
});
return matched;
}
@jbanety
Copy link

jbanety commented May 19, 2020

You can use slot.assignedElements to prevent testing selector on Text nodes.

@pbek
Copy link

pbek commented Jun 15, 2021

Marvelous, thank you very much!

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