|
| 1 | +# Slicky/QuerySelector |
| 2 | + |
| 3 | +CSS selectors implemented in javascript |
| 4 | + |
| 5 | +This library does not depend on any javascript DOM parser and can be used in either browser or node environment. |
| 6 | + |
| 7 | +It can work with native DOM in browser, with parse5 or any other library. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +``` |
| 12 | +$ npm install @slicky/query-selector |
| 13 | +``` |
| 14 | + |
| 15 | +## Custom DocumentWalker |
| 16 | + |
| 17 | +First you will need to implement few methods, which will be used by this library to read the DOM. |
| 18 | + |
| 19 | +**Example of the simplest document walker:** |
| 20 | + |
| 21 | +```typescript |
| 22 | +import {IDocumentWalker, DocumentNode, DocumentParent} from '@slicky/query-selector'; |
| 23 | + |
| 24 | + |
| 25 | +class CustomDocumentWalker implements IDocumentWalker |
| 26 | +{ |
| 27 | + |
| 28 | + |
| 29 | + public getNodeName(node: DocumentNode): string |
| 30 | + { |
| 31 | + return node.nodeName; |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + public isElement(node: DocumentNode): boolean |
| 36 | + { |
| 37 | + return node.type === 'string'; |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + public isString(node: DocumentNode): boolean |
| 42 | + { |
| 43 | + return node.type === 'string'; |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + public getParentNode(node: DocumentNode): DocumentParent |
| 48 | + { |
| 49 | + return node.parentNode; |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + public getChildNodes(parent: DocumentParent): Array<DocumentNode> |
| 54 | + { |
| 55 | + return parent.childNodes; |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + public getAttribute(node: DocumentNode, name: string): string |
| 60 | + { |
| 61 | + return node.attributes[name]; |
| 62 | + } |
| 63 | + |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +## Usage |
| 68 | + |
| 69 | +```typescript |
| 70 | +import {Matcher} from '@slicky/query-selector'; |
| 71 | + |
| 72 | + |
| 73 | +let matcher = new Matcher(new CustomDocumentWalker); |
| 74 | +let dom = loadCustomDOM(); |
| 75 | + |
| 76 | + |
| 77 | +let element = matcher.querySelector(dom, 'div a.btn:first-child'); // one element |
| 78 | +let elements = matcher.querySelectorAll(dom, 'a.btn'); // array of elements |
| 79 | +let matches = matcher.matches(element, 'a.btn'); // true |
| 80 | +``` |
| 81 | + |
| 82 | +## API |
| 83 | + |
| 84 | +* `querySelector`: similar to [Document.querySelector()](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector) |
| 85 | +* `querySelectorAll`: similar to [Document.querySelectorAll()](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll) |
| 86 | +* `matches`: similar to [Element.matches()](https://developer.mozilla.org/en-US/docs/Web/API/Element/matches) |
| 87 | + |
| 88 | +## Supported selectors |
| 89 | + |
| 90 | +* [Type selector](https://developer.mozilla.org/en-US/docs/Web/CSS/Type_selectors) (`div`) |
| 91 | +* [Class selector](https://developer.mozilla.org/en-US/docs/Web/CSS/Class_selectors) (`.btn`) |
| 92 | +* [ID selector](https://developer.mozilla.org/en-US/docs/Web/CSS/ID_selectors) (`#header`) |
| 93 | +* [Attribute selectors](https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors) (`[attr]`, ...) |
| 94 | +* [Adjacent sibling selector](https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_selectors) (`span + i`) |
| 95 | +* [General sibling selector](https://developer.mozilla.org/en-US/docs/Web/CSS/General_sibling_selectors) (`span ~ i`) |
| 96 | +* [Child selector](https://developer.mozilla.org/en-US/docs/Web/CSS/Child_selectors) (`span > i`) |
| 97 | +* [Descendant selector](https://developer.mozilla.org/en-US/docs/Web/CSS/Descendant_selectors) (`span i`) |
| 98 | +* [Empty](https://developer.mozilla.org/en-US/docs/Web/CSS/:empty) (`:empty`) |
| 99 | +* [First child](https://developer.mozilla.org/en-US/docs/Web/CSS/:first-child) (`:first-child`) |
| 100 | +* [First of type](https://developer.mozilla.org/en-US/docs/Web/CSS/:first-of-type) (`:first-of-type`) |
| 101 | +* [Last child](https://developer.mozilla.org/en-US/docs/Web/CSS/:last-child) (`:last-child`) |
| 102 | +* [Last of type](https://developer.mozilla.org/en-US/docs/Web/CSS/:last-of-type) (`:last-of-type`) |
0 commit comments