Last active
December 18, 2015 23:49
-
-
Save domenic/5864658 to your computer and use it in GitHub Desktop.
class Elements extends Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { absolutizeRelativeSelectorList } from "http://dev.w3.org/csswg/selectors/#absolutizing"; | |
// 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 | |
// http://lists.w3.org/Archives/Public/public-script-coord/2013JulSep/0350.html | |
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 = this.map(finder); | |
// Note that we are concating `Elements` with other `Elements` so it works fine. | |
const flattened = allMatching.reduce((a, b) => a.concat(b)); | |
return flattened; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Elements extends Array { | |
Elements queryAll(ToString selectors?) | |
Element query(ToString selectors?) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is lovely