All Projects → github → Query Selector

github / Query Selector

Licence: mit
Query the document tree by selector, filtering by element type.

Programming Languages

javascript
184084 projects - #8 most used programming language
flow
126 projects

Labels

Projects that are alternatives of or similar to Query Selector

Hyperx
🏷 - tagged template string virtual dom builder
Stars: ✭ 991 (+1315.71%)
Mutual labels:  dom
Browser Monkey
Reliable DOM testing
Stars: ✭ 53 (-24.29%)
Mutual labels:  dom
Elementx
⚡️ Functionally create DOM elements and compose them to a tree quickly
Stars: ✭ 62 (-11.43%)
Mutual labels:  dom
React Propers
Select react doms , and update props.
Stars: ✭ 40 (-42.86%)
Mutual labels:  dom
Ng Focus On
A directive to make angular elements focusable
Stars: ✭ 51 (-27.14%)
Mutual labels:  dom
Nito
A jQuery library for building user interfaces
Stars: ✭ 56 (-20%)
Mutual labels:  dom
Dom99
Extend html with directives
Stars: ✭ 37 (-47.14%)
Mutual labels:  dom
Dom To Svg
Library to convert a given HTML DOM node into an accessible SVG "screenshot".
Stars: ✭ 67 (-4.29%)
Mutual labels:  dom
Monoapp
choo architecture without a renderer
Stars: ✭ 52 (-25.71%)
Mutual labels:  dom
Dom
DOM Standard
Stars: ✭ 1,114 (+1491.43%)
Mutual labels:  dom
Curtainsjs
curtains.js is a lightweight vanilla WebGL javascript library that turns HTML DOM elements into interactive textured planes.
Stars: ✭ 1,039 (+1384.29%)
Mutual labels:  dom
Gomponents
Declarative view components in Go, that can render to HTML5.
Stars: ✭ 49 (-30%)
Mutual labels:  dom
Tokamak
SwiftUI-compatible framework for building browser apps with WebAssembly and native apps for other platforms
Stars: ✭ 1,083 (+1447.14%)
Mutual labels:  dom
Layui dropdown
基于layui框架的下拉控件,支持菜单下拉,自定义下拉内容,兼容表格。
Stars: ✭ 40 (-42.86%)
Mutual labels:  dom
Autosize Input
🎈 Effortless, dynamic-width text boxes in vanilla JavaScript
Stars: ✭ 64 (-8.57%)
Mutual labels:  dom
Onthefly
🔗 Generate TinySVG, HTML and CSS on the fly
Stars: ✭ 37 (-47.14%)
Mutual labels:  dom
Scalajs Bootstrap
Scala.js bootstrap components
Stars: ✭ 55 (-21.43%)
Mutual labels:  dom
Anglesharp.js
👼 Extends AngleSharp with a .NET-based JavaScript engine.
Stars: ✭ 68 (-2.86%)
Mutual labels:  dom
Web Template
web-template.js 是一款基于 ES6 模板字符串解析的模板引擎。
Stars: ✭ 67 (-4.29%)
Mutual labels:  dom
Sentineljs
Detect new DOM nodes using CSS selectors (650 bytes)
Stars: ✭ 1,100 (+1471.43%)
Mutual labels:  dom

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].