Skip to content

Instantly share code, notes, and snippets.

@domenic domenic/Elements.js

Last active Dec 18, 2015
What would you like to do?
class Elements extends Array
import { absolutizeRelativeSelectorList } from "";
// Assume JSIDL conversions have been applied already,
// i.e. we don't do `selectors = String(selectors)` manually.
class Elements extends Array {
query(selectors) {
return this.queryAll(selectors)[0]; // highly inefficient obviously, but clear semantics
queryAll(selectors) {
const absolutized = absolutizeRelativeSelectorList(selectors);
// TODO: instead of `querySelectorAll` possibly use `[[QuerySelectorAll]]` internal
// implementation, see
const finder = element => this.constructor.from(element.querySelectorAll(absolutized));
// `allFound` is now an `Elements` but it contains `Elements` instances; we need to flatten this out.
// My kingdom for an `Array.prototype.flatMap`!
const allFound =;
// Note that we are concating `Elements` with other `Elements` so it works fine.
const flattened = allMatching.reduce((a, b) => a.concat(b));
return flattened;
class Elements extends Array {
Elements queryAll(ToString selectors?)
Element query(ToString selectors?)

This comment has been minimized.

Copy link

rwaldron commented Apr 8, 2014

This is lovely

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.