css-sanitizer
Classes to parse and sanitize CSS
Wikimedia\CSS\Parser\Parser Class Reference

Parse CSS into a structure for further processing. More...

Public Member Functions

 __construct (Tokenizer $tokenizer)
 
 getParseErrors ()
 Return all parse errors seen so far. More...
 
 clearParseErrors ()
 Clear parse errors.
 
 parseStylesheet ()
 Parse a stylesheet. More...
 
 parseRuleList ()
 Parse a list of rules. More...
 
 parseRule ()
 Parse a rule. More...
 
 parseDeclaration ()
 Parse a declaration. More...
 
 parseDeclarationList ()
 Parse a list of declarations. More...
 
 parseDeclarationOrAtRuleList ()
 Parse a list of declarations and at-rules. More...
 
 parseComponentValue ()
 Parse a (non-whitespace) component value. More...
 
 parseComponentValueList ()
 Parse a list of component values. More...
 

Static Public Member Functions

static newFromString ( $source, array $options=[])
 Create a Parser for a CSS string. More...
 
static newFromDataSource (DataSource $source, array $options=[])
 Create a Parser for a CSS DataSource. More...
 
static newFromTokens (array $tokens, Token $eof=null)
 Create a Parser for a list of Tokens. More...
 

Protected Member Functions

 consumeToken ()
 Consume a token.
 
 consumeTokenAndWhitespace ()
 Consume a token, also consuming any following whitespace (and comments)
 
 parseError ( $tag, Token $token, array $data=[])
 Record a parse error. More...
 
 consumeRuleList ( $topLevel)
 Consume a list of rules. More...
 
 consumeDeclarationOrAtRuleList ( $allowAtRules=true)
 Consume a list of declarations and at-rules. More...
 
 consumeDeclaration ()
 Consume a declaration. More...
 
 consumeAtRule ()
 Consume an at-rule. More...
 
 consumeQualifiedRule ()
 Consume a qualified rule. More...
 
 consumeComponentValue ()
 Consume a component value. More...
 
 consumeSimpleBlock ()
 Consume a simple block. More...
 
 consumeFunction ()
 Consume a function. More...
 

Protected Attributes

 $tokenizer
 
 $currentToken = null
 
 $parseErrors = []
 
 $cvDepth = 0
 

Detailed Description

Parse CSS into a structure for further processing.

This implements the CSS Syntax Module Level 3 candidate recommendation.

See also
https://www.w3.org/TR/2014/CR-css-syntax-3-20140220/

The usual entry points are:

Constructor & Destructor Documentation

◆ __construct()

Wikimedia\CSS\Parser\Parser::__construct ( Tokenizer  $tokenizer)
Parameters
Tokenizer$tokenizerCSS Tokenizer

Member Function Documentation

◆ consumeAtRule()

Wikimedia\CSS\Parser\Parser::consumeAtRule ( )
protected

◆ consumeComponentValue()

Wikimedia\CSS\Parser\Parser::consumeComponentValue ( )
protected

Consume a component value.

See also
https://www.w3.org/TR/2014/CR-css-syntax-3-20140220/#consume-a-component-value
Returns
ComponentValue

◆ consumeDeclaration()

Wikimedia\CSS\Parser\Parser::consumeDeclaration ( )
protected

Consume a declaration.

See also
https://www.w3.org/TR/2014/CR-css-syntax-3-20140220/#consume-a-declaration
Returns
Declaration|null

◆ consumeDeclarationOrAtRuleList()

Wikimedia\CSS\Parser\Parser::consumeDeclarationOrAtRuleList (   $allowAtRules = true)
protected

Consume a list of declarations and at-rules.

See also
https://www.w3.org/TR/2014/CR-css-syntax-3-20140220/#consume-a-list-of-declarations
Parameters
bool$allowAtRulesWhether to allow at-rules. This flag is not in the spec, and is used to implement the non-spec self::parseDeclarationList().
Returns
DeclarationOrAtRuleList|DeclarationList

◆ consumeFunction()

Wikimedia\CSS\Parser\Parser::consumeFunction ( )
protected

Consume a function.

See also
https://www.w3.org/TR/2014/CR-css-syntax-3-20140220/#consume-a-function
Returns
CSSFunction

◆ consumeQualifiedRule()

Wikimedia\CSS\Parser\Parser::consumeQualifiedRule ( )
protected

Consume a qualified rule.

See also
https://www.w3.org/TR/2014/CR-css-syntax-3-20140220/#consume-a-qualified-rule
Returns
QualifiedRule|null

◆ consumeRuleList()

Wikimedia\CSS\Parser\Parser::consumeRuleList (   $topLevel)
protected

Consume a list of rules.

See also
https://www.w3.org/TR/2014/CR-css-syntax-3-20140220/#consume-a-list-of-rules
Parameters
bool$topLevelDetermines the behavior when CDO and CDC tokens are encountered
Returns
RuleList

◆ consumeSimpleBlock()

Wikimedia\CSS\Parser\Parser::consumeSimpleBlock ( )
protected

Consume a simple block.

See also
https://www.w3.org/TR/2014/CR-css-syntax-3-20140220/#consume-a-simple-block
Returns
SimpleBlock

◆ getParseErrors()

Wikimedia\CSS\Parser\Parser::getParseErrors ( )

Return all parse errors seen so far.

Returns
array Array of [ string $tag, int $line, int $pos, ... ]

◆ newFromDataSource()

static Wikimedia\CSS\Parser\Parser::newFromDataSource ( DataSource  $source,
array  $options = [] 
)
static

Create a Parser for a CSS DataSource.

Parameters
DataSource$sourceCSS to parse.
array$optionsConfiguration options, see DataSourceTokenizer::__construct().
Returns
static

◆ newFromString()

static Wikimedia\CSS\Parser\Parser::newFromString (   $source,
array  $options = [] 
)
static

Create a Parser for a CSS string.

Parameters
string$sourceCSS to parse.
array$optionsConfiguration options, see DataSourceTokenizer::__construct(). Also,
  • convert: (array) If specified, detect the encoding as defined in the CSS spec. The value is passed as the $encodings argument to Encoder::convert().
Returns
static

◆ newFromTokens()

static Wikimedia\CSS\Parser\Parser::newFromTokens ( array  $tokens,
Token  $eof = null 
)
static

Create a Parser for a list of Tokens.

Parameters
Token[]$tokensToken-stream to parse
Token | null$eofEOF-token
Returns
static

◆ parseComponentValue()

Wikimedia\CSS\Parser\Parser::parseComponentValue ( )

Parse a (non-whitespace) component value.

See also
https://www.w3.org/TR/2014/CR-css-syntax-3-20140220/#parse-a-component-value
Returns
ComponentValue|null

◆ parseComponentValueList()

Wikimedia\CSS\Parser\Parser::parseComponentValueList ( )

Parse a list of component values.

See also
https://www.w3.org/TR/2014/CR-css-syntax-3-20140220/#parse-a-list-of-component-values
Returns
ComponentValueList

◆ parseDeclaration()

Wikimedia\CSS\Parser\Parser::parseDeclaration ( )

Parse a declaration.

See also
https://www.w3.org/TR/2014/CR-css-syntax-3-20140220/#parse-a-declaration
Returns
Declaration|null

◆ parseDeclarationList()

Wikimedia\CSS\Parser\Parser::parseDeclarationList ( )

Parse a list of declarations.

Note
This is not the entry point the standard calls "parse a list of declarations", see self::parseDeclarationOrAtRuleList()
Returns
DeclarationList

◆ parseDeclarationOrAtRuleList()

Wikimedia\CSS\Parser\Parser::parseDeclarationOrAtRuleList ( )

Parse a list of declarations and at-rules.

Note
This is the entry point the standard calls "parse a list of declarations"
See also
https://www.w3.org/TR/2014/CR-css-syntax-3-20140220/#parse-a-list-of-declarations
Returns
DeclarationOrAtRuleList

◆ parseError()

Wikimedia\CSS\Parser\Parser::parseError (   $tag,
Token  $token,
array  $data = [] 
)
protected

Record a parse error.

Parameters
string$tagError tag
Token$tokenReport the error as starting at this token.
array$dataExtra data about the error.

◆ parseRule()

Wikimedia\CSS\Parser\Parser::parseRule ( )

◆ parseRuleList()

Wikimedia\CSS\Parser\Parser::parseRuleList ( )

◆ parseStylesheet()

Wikimedia\CSS\Parser\Parser::parseStylesheet ( )

Parse a stylesheet.

See also
https://www.w3.org/TR/2014/CR-css-syntax-3-20140220/#parse-a-stylesheet
Note
Per the Editor's Draft, if the first rule is an at-rule named "charset" it will be silently dropped. If you're not using the provided Sanitizer classes to further sanitize the CSS, you'll want to manually filter out any other such rules before stringifying the stylesheet and/or prepend @charset "utf-8"; after stringifying it.
Returns
Stylesheet

The documentation for this class was generated from the following file: