0.9.5 • Published 7 years ago
selector-tokenizer v0.9.5
Selector Tokenizer
Module for fast css selector tokenization from string
What does this repo about
The selector tokenizer allows to break a css selector string into set of tokens.
The tokenization method is based on a set of lexer grammars rules. The full list of available token types is next:
- \ - for basic type selectors eg. "article", "h1", "p" etc.
- \ - for basic class selectors eg. ".button", ".post", etc.
- \ - for basic universal selector "*"
- \ - for basic attribute selectors eg. "attr", "attr=val", "attr^=val" etc.
- \ - for pseudo-element and pseudo-class-selectors eg. ":first-child", "::first-letter"
- \ - for selector's descendant combinator " "
- \ - for selector's adjacent sibling combinator "+"
- \ - for selector's general sibling combinator "~"
- \ - for selector's child combinator ">"
- \ - to match the start of tokenization scopes ()
- \ - to match the end of tokenization scopes ()
How to use
Basic Example
The following example illustrates the principle the SelectorTokenizer.tokenize method
tokenizer = new SelectorTokenizer();
tokens = tokenizer.tokenize(".page main");
tokens //=> [{type: "class", value: ".page"}, {type: "descendant", value: " "}, {type: "type", value: "main"}]
Methods
SelectorTokenizer has a set of different methods you can find to be useful.
tokenize(selectorText);
Create a set of tokens from target selector string
tokenizer = new SelectorTokenizer();
tokens = tokenizer.tokenize(".page main");
tokens //=> [{type: "class", value: ".page"}, {type: "descendant", value: " "}, {type: "type", value: "main"}]
tokenAt(selectorText, startIndex);
Read a grammar token from a string starting at target position.
tokenizer = new SelectorTokenizer();
token = tokenizer.tokenAt(".classname", 0);
token //=> { type: "class", value: ".classname" }
Building
npm install
Development and Testing
npm run gulp
will launch dist buildingnpm run watch
will launch a watcher for dist buildingnpm run test
will launch unit-test buildingnpm run test-watch
will launch a watcher for unit-test building
License
It's all about MIT stuff. (C) 2017 Eugene Ford