css-sanitizer
Classes to parse and sanitize CSS
Loading...
Searching...
No Matches
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.
 
 clearParseErrors ()
 Clear parse errors.
 
 parseStylesheet ()
 Parse a stylesheet.
 
 parseRuleList ()
 Parse a list of rules.
 
 parseRule ()
 Parse a rule.
 
 parseDeclaration ()
 Parse a declaration.
 
 parseDeclarationList ()
 Parse a list of declarations.
 
 parseDeclarationOrAtRuleList ()
 Parse a list of declarations and at-rules.
 
 parseComponentValue ()
 Parse a (non-whitespace) component value.
 
 parseComponentValueList ()
 Parse a list of component values.
 
 parseCommaSeparatedComponentValueList ()
 Parse a comma-separated list of component values.
 

Static Public Member Functions

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

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.
 
 consumeRuleList ( $topLevel)
 Consume a list of rules.
 
 consumeDeclarationOrAtRuleList ( $allowAtRules=true)
 Consume a list of declarations and at-rules.
 
 consumeDeclaration ()
 Consume a declaration.
 
 consumeAtRule ()
 Consume an at-rule.
 
 consumeQualifiedRule ()
 Consume a qualified rule.
 
 consumeComponentValue ()
 Consume a component value.
 
 consumeSimpleBlock ()
 Consume a simple block.
 
 consumeFunction ()
 Consume a function.
 

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/2019/CR-css-syntax-3-20190716/

The usual entry points are:

  • Parser::parseStylesheet() to parse a stylesheet or the contents of a <style> tag.
  • Parser::parseDeclarationList() to parse an inline style attribute

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

Consume an at-rule.

See also
https://www.w3.org/TR/2019/CR-css-syntax-3-20190716/#consume-at-rule
Returns
AtRule @suppress PhanPluginNeverReturnMethod due to break 2;

◆ consumeComponentValue()

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

Consume a component value.

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

◆ consumeDeclaration()

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

Consume a declaration.

See also
https://www.w3.org/TR/2019/CR-css-syntax-3-20190716/#consume-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/2019/CR-css-syntax-3-20190716/#consume-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/2019/CR-css-syntax-3-20190716/#consume-function
Returns
CSSFunction @suppress PhanPluginNeverReturnMethod due to break 2;

◆ consumeQualifiedRule()

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

Consume a qualified rule.

See also
https://www.w3.org/TR/2019/CR-css-syntax-3-20190716/#consume-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/2019/CR-css-syntax-3-20190716/#consume-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/2019/CR-css-syntax-3-20190716/#consume-simple-block
Returns
SimpleBlock @suppress PhanPluginNeverReturnMethod due to break 2;

◆ 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

◆ parseCommaSeparatedComponentValueList()

Wikimedia\CSS\Parser\Parser::parseCommaSeparatedComponentValueList ( )

Parse a comma-separated list of component values.

See also
https://www.w3.org/TR/2019/CR-css-syntax-3-20190716/#parse-comma-separated-list-of-component-values
Returns
ComponentValueList[]

◆ parseComponentValue()

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

Parse a (non-whitespace) component value.

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

◆ parseComponentValueList()

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

Parse a list of component values.

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

◆ parseDeclaration()

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

Parse a declaration.

See also
https://www.w3.org/TR/2019/CR-css-syntax-3-20190716/#parse-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/2019/CR-css-syntax-3-20190716/#parse-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 ( )

Parse a list of rules.

See also
https://www.w3.org/TR/2019/CR-css-syntax-3-20190716/#parse-list-of-rules
Returns
RuleList

◆ parseStylesheet()

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

Parse a stylesheet.

See also
https://www.w3.org/TR/2019/CR-css-syntax-3-20190716/#parse-stylesheet
Returns
Stylesheet

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