Skip to content
This repository has been archived by the owner on Oct 21, 2021. It is now read-only.

Releases: github/query-selector

1.0.4

26 Mar 23:14
v1.0.4
Compare
Choose a tag to compare
  • Remove syntax transformations in module output #10

v1.0.3

06 Nov 11:56
62c191e
Compare
Choose a tag to compare

Update eslint-utils from 1.3.1 to 1.4.2 #8

v1.0.2...v1.0.3

v1.0.2

06 Aug 14:39
ed91a5b
Compare
Choose a tag to compare
  • Merge pull request #7 from github/publish-to-gpr-as-well ba14d58
  • publish to GPR as a postpublish step 1f55216

v1.0.1...v1.0.2

1.0.2

06 Aug 14:39
ba14d58
Compare
Choose a tag to compare

A typed querySelector function

Query the document tree by selector, filtering by element type.

Installation

$ npm install @github/query-selector

Usage

This library provides a set of functions to query the document tree with a
standard selector paired with an additional type filter applied to the result.

An element must match the selector as well as the type for it to be returned.

  • query(context, selector, klass)
  • querySelectorAll(context, selector, klass)
  • closest(element, selector, klass)
  • namedItem(element, name, klass)
  • getAttribute(element, name)
import {closest, getAttribute, namedItem, query, querySelectorAll} from '@github/query-selector'

// Find an element by selector and type, or throw if not found.
const image: HTMLImageElement = query(document, '.avatar', HTMLImageElement)
image.src = '/hubot.png'

// Find the parent by selector and type, or throw if not found.
const parent: HTMLDetailsElement = closest(image, '.container', HTMLDetailsElement)
parent.open = true

// Filter all children by selector and type.
const inputs: Array<HTMLInputElement> = querySelectorAll(document, 'input', HTMLInputElement)
for (const input of inputs) {
  input.value = ''
}

// Retrieve the attribute's value or throw.
const url: string = getAttribute(image, 'data-url')

// Find the form's `input[name=login]` field or throw if not found.
const form: HTMLFormElement = query(document, 'form', HTMLFormElement)
const input: HTMLInputElement = namedItem(form, 'login')

Motivation

Finding an individual element in the document tree and operating on it can
lead to null pointer exceptions.

const el = document.querySelector('.expected-element')
// el may be null!
el.classList.add('selected')
el.setAttribute('title', 'hello')

A safer alternative is to guard against null values with a conditional statement.

const el = document.querySelector('.expected-element')
if (el) {
  el.classList.add('selected')
  el.setAttribute('title', 'hello')
}

Even if found, the element may be of the wrong type.

const el = document.querySelector('.expected-element')
if (el) {
  // Element might not have a value property!
  el.value = 'hello'
}

Adding an instanceof test would verify the element has the properties and
methods we expect.

const el = document.querySelector('.expected-element')
if (el instanceof HTMLInputElement) {
  el.value = 'hello'
}

Because document.querySelector is so frequently used in web applications,
and it's tedious to guard every element query with null checks, these tests
are most often omitted. When using Flow, however, these tests become
required to pass the type checker.

The combination of null tests and subclass type refinements feels like we're
working against the type system, rather than with it. So, typed query functions
consider a missing element, or an element of the wrong type, to be failed
assertions and throw an exception to fail as early as possible.

Development

npm install
npm test

License

Distributed under the MIT license. See LICENSE for details.

v1.0.1

31 Jul 16:25
297aa2c
Compare
Choose a tag to compare
  • Merge pull request #6 from github/dependabot/npm_and_yarn/lodash-4.17.15 f17e5f1
  • Bump lodash from 4.17.11 to 4.17.15 eb79440
  • use github-lint as a test script ef3485f

v1.0.0...v1.0.1