css-selector-parser / Exports
- AstAttribute
- AstClassName
- AstFactory
- AstFormula
- AstFormulaOfSelector
- AstId
- AstNamespaceName
- AstNoNamespace
- AstPseudoClass
- AstPseudoElement
- AstRule
- AstSelector
- AstString
- AstSubstitution
- AstTagName
- AstWildcardNamespace
- AstWildcardTag
- ParserError
- SyntaxDefinition
Ƭ AstEntity: AstSelector
| AstRule
| AstTagName
| AstWildcardTag
| AstId
| AstClassName
| AstNamespaceName
| AstWildcardNamespace
| AstNoNamespace
| AstSubstitution
| AstString
| AstFormula
| AstFormulaOfSelector
| AstPseudoClass
| AstAttribute
| AstPseudoElement
One of CSS AST entity types.
Ƭ CssLevel: "css1"
| "css2"
| "css3"
| "selectors-3"
| "selectors-4"
| "latest"
| "progressive"
Ƭ Parser: (input
: string
) => AstSelector
▸ (input
): AstSelector
Parses CSS selector string and returns CSS selector AST.
Throws
Name | Type |
---|---|
input |
string |
• Const
ast: AstFactory
AST structure generators and matchers.
For instance, ast.selector({rules: [...]})
creates AstSelector and ast.isSelector(...)
checks if
AstSelector was specified.
Example
// Represents CSS selector: ns|div#user-34.user.user-active[role="button"]:lang(en)::before > *
const selector = ast.selector({
rules: [
ast.rule({
items: [
ast.tagName({name: 'div', namespace: ast.namespaceName({name: 'ns'})}),
ast.id({name: 'user-34'}),
ast.className({name: 'user'}),
ast.className({name: 'user-active'}),
ast.attribute({
name: 'role',
operator: '=',
value: ast.string({value: 'button'})
}),
ast.pseudoClass({
name: 'lang',
argument: ast.string({value: 'en'})
}),
ast.pseudoElement({name: 'before'})
],
nestedRule: ast.rule({combinator: '>', items: [ast.wildcardTag()]})
})
]
});
console.log(ast.isSelector(selector)); // prints true
console.log(ast.isRule(selector)); // prints false
▸ createParser(options?
): Parser
Creates a parse function to be used later to parse CSS selectors.
Name | Type | Description |
---|---|---|
options |
Object |
- |
options.strict? |
boolean |
CSS selector parser in modern browsers is very forgiving. For instance, it works fine with unclosed attribute selectors: "[attr=value" . Set to false in order to mimic browser behaviour. Default: true |
options.substitutes? |
boolean |
Flag to enable substitutes. This is not part of CSS syntax, but rather a useful feature to pass variables into CSS selectors. Default: false Example ts "[attr=$variable]" |
options.syntax? |
CssLevel | SyntaxDefinition |
CSS Syntax options to be used for parsing. Can either be one of the predefined CSS levels (CssLevel) or a more detailed syntax definition (SyntaxDefinition). Default: "latest" |
▸ render(entity
): string
Renders CSS Selector AST back to a string.
Example
import {ast, render} from 'css-selector-parser';
const selector = ast.selector({
rules: [
ast.rule({
items: [
ast.tagName({name: 'a'}),
ast.id({name: 'user-23'}),
ast.className({name: 'user'}),
ast.pseudoClass({name: 'visited'}),
ast.pseudoElement({name: 'before'})
]
})
]
});
console.log(render(selector)); // a#user-23.user:visited::before
Name | Type |
---|---|
entity |
AstEntity |
string