• Stars
    star
    238
  • Rank 169,306 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 13 years ago
  • Updated over 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An absurdly fast CSS selector engine.

Zest

zest is a fast, lightweight, and extensible CSS selector engine.

Zest was designed to be very concise while still supporting CSS3/CSS4 selectors and remaining fast.

Usage

zest('section! > div[title="hello" i] > :local-link /href/ h1');

Benchmarks

Each selector run 1000 times on Google Chrome 13 beta (ms):

benchmarking: `header > h1` 1000 times.
zest: 13
sizzle: 24
native: 13
benchmarking: `body > header > h1` 1000 times.
zest: 16
sizzle: 26
native: 13
benchmarking: `html a` 1000 times.
zest: 45
sizzle: 55
native: 12
benchmarking: `:first-child` 1000 times.
zest: 44
sizzle: 68
native: 11
benchmarking: `:only-child` 1000 times.
zest: 49
sizzle: 66
native: 12
benchmarking: `:not(a)` 1000 times.
zest: 51
sizzle: 125
native: 12
benchmarking: `h1 + time:last-child` 1000 times.
zest: 15
sizzle: 32
native: 13
benchmarking: `h1 + time[datetime]:last-child` 1000 times.
zest: 21
sizzle: 45
native: 14
benchmarking: `header > h1, :not(a)` 1000 times.
zest: 72
sizzle: 212
native: 17
benchmarking: `a[rel~="section"]` 1000 times.
zest: 41
sizzle: 54
native: 11
benchmarking: `a, h1` 1000 times.
zest: 25
sizzle: 55
native: 11
benchmarking: `:nth-child(2n+1)` 1000 times.
zest: 82
sizzle: 97
native: 13

NOTE: If you want to run these benchmarks yourself make sure to turn off Sizzle's (and Zest's) document.querySelectorAll delegation mechanism, otherwise you will be benchmarking against document.querySelectorAll.

Zest will cache compiled selectors if it can't delegate to document.querySelectorAll, document.getElementById, or document.getElementsByClassName (depending). The benchmark tests you see above were performed with the caching mechanism disabled. If caching were enabled, Zest would be faster than the native document.querySelectorAll.

Install

$ npm install zest

Notes

Zest currently includes support for ender.js, Prototype, and jQuery.

Unsupported Selectors: :hover, :active, :link, :visited, all pseudo elements, and namespaces.

:link, :visited, and pseudo elements are unsupported for obvious reasons (they don't work). :hover and :active aren't supported because they examine a dynamic state, you should be binding to events for this (:focus is supported, but there is no fallback for legacy browsers).

Extension

Zest doesn't support (m)any non-standard selectors, but it is possible to add your own.

Adding a simple selector

Adding simple selectors is fairly straight forward. Only the addition of pseudo classes and attribute operators is possible. (Adding your own "style" of selector would require changes to the core logic.)

Here is an example of a custom :name selector which will match for an element's name attribute: e.g. h1:name(foo). Effectively an alias for h1[name=foo].

// if there was a parameter,
// it gets closured as `param`
zest.selectors[':name'] = function(param) {
  return function(el) {
    if (el.name === param) return true;
  };
};

NOTE: if you're pseudo-class does not take a parameter, there will be no closure.

Adding an attribute operator

// `attr` is the attribute
// `val` is the value to match
zest.operators['!='] = function(attr, val) {
  return attr !== val;
};

Adding a combinator

Adding a combinator is a bit trickier. It may seem confusing at first because the logic is upside-down. Zest interprets selectors from right to left.

Here is an example how a parent combinator could be implemented:

zest.combinators['<'] = function(test) {
  return function(el) { // `el` is the current element
    el = el.firstChild;
    while (el) {
      // return the relevant element
      // if it passed the test
      if (el.nodeType === 1 && test(el)) {
        return el;
      }
      el = el.nextSibling;
    }
  };
};

The test function tests whatever simple selectors it needs to look for, but it isn't important what it does. The most important part is that you return the relevant element once it's found.

Contribution and License Agreement

If you contribute code to this project, you are implicitly allowing your code to be distributed under the MIT license. You are also implicitly verifying that all code is your original work. </legalese>

License

(c) Copyright 2011-2012, Christopher Jeffrey (MIT Licensed). See LICENSE for more info.

More Repositories

1

blessed

A high-level terminal interface library for node.js.
JavaScript
11,297
star
2

tty.js

A terminal for your browser, using node/express/socket.io
JavaScript
4,194
star
3

ttystudio

A terminal-to-gif recorder minus the headaches.
JavaScript
3,239
star
4

compton

A compositor for X11.
C
2,247
star
5

term.js

A terminal written in javascript.
JavaScript
1,550
star
6

pty.js

Bindings to forkpty(3) for node.js.
C++
857
star
7

mako

Bitcoin node written in C
C
578
star
8

termcoin

A bitcoin wallet and blockchain explorer for your terminal.
JavaScript
481
star
9

liburkel

Authenticated key-value store (i.e. an urkel tree)
C
315
star
10

slock

Fork of suckless screen locker for the extremely paranoid.
C
152
star
11

tiny

A small database for node.js.
JavaScript
111
star
12

lcdb

LevelDB implemented in C (unofficial -- not affiliated with Google in any way)
C
98
star
13

bns

Recursive DNS server and resolver for node.js
JavaScript
65
star
14

parted

Streaming body parser for node.js.
JavaScript
63
star
15

bthreads

worker threads for javascript
JavaScript
48
star
16

bpkg

Bundler and release tool for node.js
JavaScript
46
star
17

tng

A full-featured PNG renderer for the terminal.
JavaScript
41
star
18

coined

A high-level wrapper around BCoin
JavaScript
25
star
19

node-uo

A UO server for node.js
JavaScript
25
star
20

n64

Int64 object for javascript
JavaScript
24
star
21

liquor

A templating engine minus the code.
JavaScript
19
star
22

daemonic

A dead-simple module to daemonize a node. No compilation required.
JavaScript
19
star
23

node-telnet2

Telnet implementation for node.js, based on node-telnet
JavaScript
18
star
24

gitj

gitk in your terminal.
JavaScript
15
star
25

node-pingback

pingbacks for node.js
JavaScript
15
star
26

dilated

A blog for node.js.
JavaScript
14
star
27

csslike

A CSS preprocessor for node.js, designed to conform to the most recent www-style proposals.
CSS
12
star
28

cmake-node

node.js toolchain for cmake
C
11
star
29

rondo

DOM library and app framework.
JavaScript
11
star
30

st

A fork of st implementing scrollback, keyboard selection, and tabs.
C
11
star
31

highlighter.js

a quick and dirty JS highlighter
JavaScript
10
star
32

charged

High-level Chargify API binding for node.js
JavaScript
10
star
33

supersha

Fast SHA256 for node.js
C
10
star
34

dwm

My dwm fork and configuration.
C
10
star
35

tmux

A fork of tmux implementing xterm behavior.
C
8
star
36

vanilla

A framework for node.js.
JavaScript
8
star
37

Live-Stylesheets

small google chrome extension for editing a page's raw css
JavaScript
8
star
38

shim.htc

An HTML5 Shim in an HTML Component
JavaScript
8
star
39

epsilon-not

Weblog
PHP
5
star
40

unbound

Bindings to libunbound for node.js
C
5
star
41

evilpart

A Node multipart parser that is positively evil
JavaScript
5
star
42

N

pretty control for js
JavaScript
5
star
43

nmterm

A wicd-curses-like interface for NetworkManager
JavaScript
5
star
44

pulsemixer.js

An alsamixer-like interface for PulseAudio
JavaScript
4
star
45

rocksdown

RocksDB backend for LevelUP
C++
4
star
46

bsert

Minimal assertions for javascript
JavaScript
4
star
47

bitcoind.js

bitcoind.js has moved to https://github.com/bitpay/bitcoind.js
C++
4
star
48

wazm

WASM abstraction and EMCC preamble
JavaScript
3
star
49

babylonia

zero-dependency babel
JavaScript
3
star
50

bslint

eslint with less (or more) bullshit
JavaScript
3
star
51

bdoc

zero-dependency jsdoc
JavaScript
3
star
52

pkg-verify

Dependency verifier for node.js
JavaScript
3
star
53

buffer-map

Buffer-keyed map for javascript
JavaScript
2
star
54

loady

dynamic loader for node.js
JavaScript
2
star
55

leasedump

Dump dhcpcd lease files
C
1
star
56

qrsuite

jsqrcode and qr.js rolled into one package
JavaScript
1
star