shady-css-parser
- Version 0.1.0
- Published
- No dependencies
- BSD-3-Clause license
Install
npm i shady-css-parser
yarn add shady-css-parser
pnpm add shady-css-parser
Overview
A fast, small and flexible CSS parser.
Index
Functions
Classes
Interfaces
Enums
Type Aliases
Functions
function iterateOverAst
iterateOverAst: (node: Node) => Iterable<Node>;
Classes
class NodeFactory
class NodeFactory {}
Class used for generating nodes in a CSS AST. Extend this class to implement visitors to different nodes while the tree is being generated, and / or custom node generation.
method atRule
atRule: ( name: string, parameters: string, rulelist: Rulelist | undefined, nameRange: Range, parametersRange: Range | undefined, range: Range) => AtRule;
Creates an At Rule node.
Parameter name
The "name" of the At Rule (e.g.,
charset
)Parameter parameters
The "parameters" of the At Rule (e.g.,
utf8
)Parameter rulelist
The Rulelist node (if any) of the At Rule.
method comment
comment: (value: string, range: Range) => Comment;
Creates a Comment node.
Parameter value
The full text content of the comment, including opening and closing comment signature.
method declaration
declaration: ( name: string, value: Expression | Rulelist | undefined, nameRange: Range, range: Range) => Declaration;
Creates a Declaration node.
Parameter name
The property name of the Declaration (e.g.,
color
).Parameter value
Either an Expression node, or a Rulelist node, that corresponds to the value of the Declaration.
method discarded
discarded: (text: string, range: Range) => Discarded;
Creates a Discarded node. Discarded nodes contain content that was not parseable (usually due to typos, or otherwise unrecognized syntax).
Parameter text
The text content that is discarded.
method expression
expression: (text: string, range: Range) => Expression;
Creates an Expression node.
Parameter text
The full text content of the expression (e.g.,
url(img.jpg)
)
method rulelist
rulelist: (rules: Rule[], range: Range) => Rulelist;
Creates a Rulelist node.
Parameter rules
An array of the Rule nodes found within the Ruleset.
method ruleset
ruleset: ( selector: string, rulelist: Rulelist, selectorRange: Range, range: Range) => Ruleset;
Creates a Ruleset node.
Parameter selector
The selector that corresponds to the Selector (e.g.,
#foo > .bar
).Parameter rulelist
The Rulelist node that corresponds to the Selector.
method stylesheet
stylesheet: (rules: Rule[], range: Range) => Stylesheet;
Creates a Stylesheet node.
Parameter rules
The list of rules that appear at the top level of the stylesheet.
class NodeVisitor
class NodeVisitor< Node extends { type: any; }, ReturnValue> {}
Class that implements a visitor pattern for ASTs produced by the Parser. Extend the NodeVisitor class to implement useful tree traversal operations such as stringification.
constructor
constructor();
Create a NodeVisitor instance.
property path
readonly path: Node[];
A list of nodes that corresponds to the current path through an AST being visited, leading to where the currently visited node will be found.
method visit
visit: (node: Node) => ReturnValue | undefined;
Visit a node. The visited node will be added to the
path
before it is visited, and will be removed after it is visited. Nodes are "visited" by calling a method on the NodeVisitor instance that matches the node's type, if one is available on the NodeVisitor instance.Parameter node
The node to be visited. The return value of the method visiting the node, if any.
class Parser
class Parser {}
Class that implements a shady CSS parser.
constructor
constructor(nodeFactory?: NodeFactory);
Create a Parser instance. When creating a Parser instance, a specialized NodeFactory can be supplied to implement streaming analysis and manipulation of the CSS AST.
property nodeFactory
nodeFactory: NodeFactory;
method parse
parse: (cssText: string) => Stylesheet;
Parse CSS and generate an AST.
Parameter cssText
The CSS to parse. A CSS AST containing nodes that correspond to those generated by the Parser's NodeFactory.
method parseAtRule
parseAtRule: (tokenizer: Tokenizer) => AtRule | null;
Consumes tokens from a Tokenizer to parse an At Rule node.
Parameter tokenizer
A Tokenizer instance.
method parseComment
parseComment: (tokenizer: Tokenizer) => Comment | null;
Consumes tokens from a Tokenizer to parse a Comment node.
Parameter tokenizer
A Tokenizer instance.
method parseDeclarationOrRuleset
parseDeclarationOrRuleset: ( tokenizer: Tokenizer) => Declaration | Ruleset | null;
Consumes tokens from a Tokenizer instance to produce a Declaration node or a Ruleset node, as appropriate.
Parameter tokenizer
A Tokenizer node.
method parseRule
parseRule: (tokenizer: Tokenizer) => Rule | null;
Consumes tokens from a Tokenizer to parse a single rule.
Parameter tokenizer
A Tokenizer instance. If the current token in the Tokenizer is whitespace, returns null. Otherwise, returns the next parseable node.
method parseRulelist
parseRulelist: (tokenizer: Tokenizer) => Rulelist;
Consumes tokens from a Tokenizer to produce a Rulelist node.
Parameter tokenizer
A Tokenizer instance.
method parseRules
parseRules: (tokenizer: Tokenizer) => Rule[];
Consumes tokens from a Tokenizer to parse a sequence of rules.
Parameter tokenizer
A Tokenizer instance. A list of nodes corresponding to rules. For a parser configured with a basic NodeFactory, any of Comment, AtRule, Ruleset, Declaration and Discarded nodes may be present in the list.
method parseStylesheet
parseStylesheet: (tokenizer: Tokenizer) => Stylesheet;
Consumes tokens from a Tokenizer to parse a Stylesheet node.
Parameter tokenizer
A Tokenizer instance.
method parseUnknown
parseUnknown: (tokenizer: Tokenizer) => Discarded | null;
Consumes tokens from a Tokenizer through the next boundary token to produce a Discarded node. This supports graceful recovery from many malformed CSS conditions.
Parameter tokenizer
A Tokenizer instance.
class Stringifier
class Stringifier extends NodeVisitor<Node, string> {}
Class that implements basic stringification of an AST produced by the Parser.
method stringify
stringify: (ast: Node) => string;
Stringify an AST such as one produced by a Parser.
Parameter ast
A node object representing the root of an AST. The stringified CSS corresponding to the AST.
class Token
class Token {}
Class that describes individual tokens as produced by the Tokenizer.
constructor
constructor(type: number, start: number, end: number);
Create a Token instance.
Parameter type
The lexical type of the Token.
Parameter start
The start index of the text corresponding to the Token in the CSS text.
Parameter end
The end index of the text corresponding to the Token in the CSS text.
property end
readonly end: number;
property next
next: Token;
property previous
previous: Token;
property start
readonly start: number;
property type
static type: typeof TokenType;
property type
readonly type: number;
method is
is: (type: TokenType) => boolean;
Test if the Token matches a given numeric type. Types match if the bitwise AND of the Token's type and the argument type are equivalent to the argument type.
Parameter type
The numeric type to test for equivalency with the Token.
class Tokenizer
class Tokenizer {}
Class that implements tokenization of significant lexical features of the CSS syntax.
constructor
constructor(cssText: string);
Create a Tokenizer instance.
Parameter cssText
The raw CSS string to be tokenized.
property cssText
cssText: string;
property currentToken
readonly currentToken: Token;
The current token that will be returned by a call to
advance
. This reference is useful for "peeking" at the next token ahead in the sequence. If the entire CSS text has been tokenized, thecurrentToken
will be null.
property offset
readonly offset: number;
method advance
advance: () => Token | null;
Advance the Tokenizer to the next token in the sequence. The current token prior to the call to
advance
, or null if the entire CSS text has been tokenized.
method flush
flush: () => (Token | null)[];
Flush all tokens from the Tokenizer. An array of all tokens corresponding to the CSS text.
method getRange
getRange: ( startToken: Token, endToken?: Token | undefined | null) => { start: number; end: number };
Like
slice
, but returns the offsets into the source, rather than the substring itself.
method slice
slice: (startToken: Token, endToken?: Token | undefined | null) => string;
Extract a slice from the CSS text, using two tokens to represent the range of text to be extracted. The extracted text will include all text between the start index of the first token and the offset index of the second token (or the offset index of the first token if the second is not provided).
Parameter startToken
The token that represents the beginning of the text range to be extracted.
Parameter endToken
The token that represents the end of the text range to be extracted. Defaults to the startToken if no endToken is provided. The substring of the CSS text corresponding to the startToken and endToken.
method tokenizeBoundary
tokenizeBoundary: (offset: number) => Token;
Tokenize a boundary at a given offset in the CSS text. A boundary is any single structurally significant character. These characters include braces, semicolons, the "at" symbol and others.
Parameter number
An offset in the CSS text. A boundary Token instance.
method tokenizeComment
tokenizeComment: (offset: number) => Token;
Tokenize a comment starting at a given offset in the CSS text. A comment is any span of text beginning with the two characters / and *, and ending with a matching counterpart pair of consecurtive characters (* and /).
Parameter number
An offset in the CSS text. A comment Token instance.
method tokenizeString
tokenizeString: (offset: number) => Token;
Tokenize a string starting at a given offset in the CSS text. A string is any span of text that is wrapped by eclusively paired, non-escaped matching quotation marks.
Parameter offset
An offset in the CSS text. A string Token instance.
method tokenizeWhitespace
tokenizeWhitespace: (offset: number) => Token;
Tokenize whitespace starting at a given offset in the CSS text. Whitespace is any span of text made up of consecutive spaces, tabs, newlines and other single whitespace characters.
Parameter number
An offset in the CSS text. A whitespace Token instance.
method tokenizeWord
tokenizeWord: (offset: number) => Token;
Tokenize a word starting at a given offset in the CSS text. A word is any span of text that is not whitespace, is not a string, is not a comment and is not a structural delimiter (such as braces and semicolon).
Parameter number
An offset in the CSS text. A word Token instance.
method trimRange
trimRange: ({ start, end }: Range) => Range;
Interfaces
interface AtRule
interface AtRule {}
property name
name: string;
The "name" of the At Rule (e.g.,
charset
)
property nameRange
nameRange: Range;
property parameters
parameters: string;
The "parameters" of the At Rule (e.g.,
utf8
)
property parametersRange
parametersRange: Range | undefined;
property range
range: Range;
property rulelist
rulelist: Rulelist | undefined;
The Rulelist node (if any) of the At Rule.
property type
type: nodeType.atRule;
interface Comment
interface Comment {}
A Comment node.
interface Declaration
interface Declaration {}
A Declaration node.
property name
name: string;
The property name of the Declaration (e.g.,
color
).
property nameRange
nameRange: Range;
The range in the original sourcetext where the name can be found.
property range
range: Range;
property type
type: nodeType.declaration;
property value
value: Expression | Rulelist | undefined;
Either an Expression node, or a Rulelist node, that corresponds to the value of the Declaration.
interface Discarded
interface Discarded {}
A Discarded node. Discarded nodes contain content that was not parseable (usually due to typos, or otherwise unrecognized syntax).
interface Expression
interface Expression {}
An Expression node.
interface Rulelist
interface Rulelist {}
A Rulelist node.
interface Ruleset
interface Ruleset {}
A Ruleset node.
property range
range: Range;
property rulelist
rulelist: Rulelist;
The Rulelist node that corresponds to the Selector.
property selector
selector: string;
The selector that corresponds to the Ruleset (e.g.,
#foo > .bar
).
property selectorRange
selectorRange: Range;
property type
type: nodeType.ruleset;
interface Stylesheet
interface Stylesheet {}
A Stylesheet node.
Enums
enum nodeType
enum nodeType { stylesheet = 'stylesheet', comment = 'comment', atRule = 'atRule', ruleset = 'ruleset', expression = 'expression', declaration = 'declaration', rulelist = 'rulelist', discarded = 'discarded',}
An enumeration of Node types.
member atRule
atRule = 'atRule'
member comment
comment = 'comment'
member declaration
declaration = 'declaration'
member discarded
discarded = 'discarded'
member expression
expression = 'expression'
member rulelist
rulelist = 'rulelist'
member ruleset
ruleset = 'ruleset'
member stylesheet
stylesheet = 'stylesheet'
Type Aliases
Package Files (9)
Dependencies (0)
No dependencies.
Dev Dependencies (12)
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/shady-css-parser
.
- Markdown[](https://www.jsdocs.io/package/shady-css-parser)
- HTML<a href="https://www.jsdocs.io/package/shady-css-parser"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 2347 ms. - Missing or incorrect documentation? Open an issue for this package.