Skip to content

Commit 8c3a98e

Browse files
committed
init
0 parents  commit 8c3a98e

56 files changed

Lines changed: 4261 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea/
2+
node_modules/
3+
4+
npm-debug.log

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
3+
node_js:
4+
- "node"
5+
- "iojs"

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 David Kudera
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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`)

lib/ast.d.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
export declare enum ASTNodeType {
2+
QUERY = 0,
3+
SELECTOR = 1,
4+
PARTS = 2,
5+
ELEMENT = 3,
6+
CLASS = 4,
7+
ID = 5,
8+
PSEUDO_CLASS = 6,
9+
ATTRIBUTE = 7,
10+
DESCENDANT = 8,
11+
CHILD = 9,
12+
ADJACENT_SIBLING = 10,
13+
GENERAL_SIBLING = 11,
14+
}
15+
export declare type ASTSimpleSelector = ASTElement | ASTId | ASTClass | ASTPseudoClass | ASTAttribute;
16+
export declare type ASTSelectorNode = ASTParts | ASTDescendant | ASTChild | ASTAdjacentSibling | ASTGeneralSibling;
17+
export interface ASTNode {
18+
type: ASTNodeType;
19+
}
20+
export interface ASTQuery extends ASTNode {
21+
type: ASTNodeType.QUERY;
22+
selectors: Array<ASTSelector>;
23+
}
24+
export interface ASTSelector extends ASTNode {
25+
type: ASTNodeType.SELECTOR;
26+
nodes: Array<ASTSelectorNode>;
27+
}
28+
export interface ASTParts extends ASTNode {
29+
type: ASTNodeType.PARTS;
30+
parts: Array<ASTSimpleSelector>;
31+
}
32+
export interface ASTElement extends ASTNode {
33+
type: ASTNodeType.ELEMENT;
34+
name: string;
35+
}
36+
export interface ASTClass extends ASTNode {
37+
type: ASTNodeType.CLASS;
38+
name: string;
39+
}
40+
export interface ASTId extends ASTNode {
41+
type: ASTNodeType.ID;
42+
name: string;
43+
}
44+
export interface ASTPseudoClass extends ASTNode {
45+
type: ASTNodeType.PSEUDO_CLASS;
46+
name: string;
47+
}
48+
export interface ASTAttribute extends ASTNode {
49+
type: ASTNodeType.ATTRIBUTE;
50+
caseSensitive: boolean;
51+
name: string;
52+
operator?: string;
53+
value?: string;
54+
}
55+
export interface ASTDescendant extends ASTNode {
56+
type: ASTNodeType.DESCENDANT;
57+
}
58+
export interface ASTChild extends ASTNode {
59+
type: ASTNodeType.CHILD;
60+
}
61+
export interface ASTAdjacentSibling extends ASTNode {
62+
type: ASTNodeType.ADJACENT_SIBLING;
63+
}
64+
export interface ASTGeneralSibling extends ASTNode {
65+
type: ASTNodeType.GENERAL_SIBLING;
66+
}

lib/ast.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
var ASTNodeType;
4+
(function (ASTNodeType) {
5+
ASTNodeType[ASTNodeType["QUERY"] = 0] = "QUERY";
6+
ASTNodeType[ASTNodeType["SELECTOR"] = 1] = "SELECTOR";
7+
ASTNodeType[ASTNodeType["PARTS"] = 2] = "PARTS";
8+
ASTNodeType[ASTNodeType["ELEMENT"] = 3] = "ELEMENT";
9+
ASTNodeType[ASTNodeType["CLASS"] = 4] = "CLASS";
10+
ASTNodeType[ASTNodeType["ID"] = 5] = "ID";
11+
ASTNodeType[ASTNodeType["PSEUDO_CLASS"] = 6] = "PSEUDO_CLASS";
12+
ASTNodeType[ASTNodeType["ATTRIBUTE"] = 7] = "ATTRIBUTE";
13+
ASTNodeType[ASTNodeType["DESCENDANT"] = 8] = "DESCENDANT";
14+
ASTNodeType[ASTNodeType["CHILD"] = 9] = "CHILD";
15+
ASTNodeType[ASTNodeType["ADJACENT_SIBLING"] = 10] = "ADJACENT_SIBLING";
16+
ASTNodeType[ASTNodeType["GENERAL_SIBLING"] = 11] = "GENERAL_SIBLING";
17+
})(ASTNodeType = exports.ASTNodeType || (exports.ASTNodeType = {}));

lib/data.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export declare const OPERATORS: string[];
2+
export declare const SELECTOR_SEPARATOR = ",";

lib/data.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.OPERATORS = [
4+
'~=', '|=', '^=', '$=', '*=', '=', '+', '~', '>',
5+
];
6+
exports.SELECTOR_SEPARATOR = ',';

lib/documentWalker.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export declare type DocumentNode = Object;
2+
export declare type DocumentParent = DocumentNode | Object;
3+
export interface IDocumentWalker {
4+
getNodeName(node: DocumentNode): string;
5+
isElement(node: DocumentNode): boolean;
6+
isString(node: DocumentNode): boolean;
7+
getParentNode(node: DocumentNode): DocumentParent;
8+
getChildNodes(parent: DocumentParent): Array<DocumentNode>;
9+
getAttribute(node: DocumentNode, name: string): string;
10+
}

lib/documentWalker.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });

0 commit comments

Comments
 (0)