GithubHelp home page GithubHelp logo

isabella232 / query-selector Goto Github PK

View Code? Open in Web Editor NEW

This project forked from github/query-selector

0.0 0.0 0.0 188 KB

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

License: MIT License

JavaScript 75.38% TypeScript 24.62%

query-selector's Introduction

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.

query-selector's People

Contributors

dependabot[bot] avatar dgraham avatar josh avatar koddsson avatar stof avatar xt0rted avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.