UNPKG

2.97 kBMarkdownView Raw
1[1]: https://secure.travis-ci.org/litejs/selector-lite.png
2[2]: https://travis-ci.org/litejs/selector-lite
3[3]: https://coveralls.io/repos/litejs/selector-lite/badge.png
4[4]: https://coveralls.io/r/litejs/selector-lite
5[npm package]: https://npmjs.org/package/selector-lite
6[GitHub repo]: https://github.com/litejs/selector-lite
7
8
9 @version 0.1.1
10 @date 2015-05-10
11 @stability 2 - Unstable
12
13
14Selector lite – [![Build][1]][2] [![Coverage][3]][4]
15=============
16
17A small pure-JavaScript CSS selector engine.
18
19 - no library dependencies
20 - CSS 3 Selector support
21 - only 2KB minified and 1KB gzipped
22
23Examples
24--------
25
26```javascript
27var selector = require("selector-lite")
28
29// Can be used to implement browser built-in functions.
30
31function getElementById(id) {
32 return selector.find(this, "#" + id, true)
33}
34function getElementsByTagName(tag) {
35 return selector.find(this, tag)
36}
37function getElementsByClassName(sel) {
38 return selector.find(this, "." + sel.replace(/\s+/g, "."))
39}
40function querySelector(sel) {
41 return selector.find(this, sel, true)
42}
43function querySelectorAll(sel) {
44 return selector.find(this, sel)
45}
46```
47
48Methods
49-------
50
51 - selector.`find(node, selector, returnFirstMatch)` - Find matching elements like querySelector.
52 - selector.`matches(node, selector)` - Returns a Boolean indicating whether or not
53 the element would be selected by the specified selector string.
54 - selector.`closest(selector)` - Returns the Element, descendant of this element
55 (or this element itself), that is the closest ancestor of the elements
56 selected by the selectors given in parameter.
57 - selector.`next(selector)` - Retrieves the next sibling that matches selector.
58 - selector.`prev(selector)` - Retrieves the preceding sibling that matches selector.
59
60
61Custom selectors
62----------------
63
64Custom selector can be added to selector.selectorMap,
65where method shortcuts are available (m->matches, c->closest, n->next, p->prev).
66
67 - `_` - node.
68 - `v` - part between `()` in `:nth-child(2n+1)`.
69 - `a` and `b` can be used as temp variables.
70
71```javascript
72// Add `:input` selector
73selector.selectorMap.input = "_.tagName=='INPUT'"
74
75// Add `:val()` selector
76selector.selectorMap.val = "_.value==v"
77```
78
79Coding Style Guidelines
80-----------------------
81
82 - Use tabs for indentation, align with spaces
83 - Use lowerCamelCase for method and variable names
84 - Use UpperCamelCase for constructor names
85 - Commit files with Unix-style line endings
86 - Do not use spaces in file and directory names
87 Consider substituting a dash (-) where you would normally use spaces.
88 - Rebase before pushing
89 - Fix tests before push or pull request
90
91
92External links
93--------------
94
95 - [GitHub repo][]
96 - [npm package][]
97 - [DOM spec](https://dom.spec.whatwg.org/)
98 - [Selectors Level 3](http://www.w3.org/TR/selectors/)
99
100
101
102### Licence
103
104Copyright (c) 2015 Lauri Rooden <lauri@rooden.ee>
105[The MIT License](http://lauri.rooden.ee/mit-license.txt)
106
107