java.lang.Object
org.jhotdraw8.css.parser.CssParser
The
CssParser processes a stream of characters into a
Stylesheet object.
The CSS Syntax Module Level 3 defines a grammar which is equivalent to the following EBNF ISO/IEC 14977 productions:
stylesheet_core = { S | CDO | CDC | qualified_rule | at_rule } ;
rule_list = { S | qualified_rule | at_rule} ;
at_rule = AT_KEYWORD , { component_value } , ( curly_block | ';' ) ;
qualified_rule
= { component_value } , curly_block ;
declaration_list_core
= { S } , ( [ declaration_core ] , [ ';' , declaration_list_core ]
| at_rule , declaration_list_core ,
) ;
declaration_core = IDENT , { S } , ":", { component_value } , [ !important ] ;
!important = '!' , { S } , "important" , { S } ;
component_value
= ( preserved_token | curly_block | round_block | square_block
| function_block ) ;
curly_block = '{' , { component_value } , '}' ;
round_block = '(' , { component_value } , ')' ;
square_block = '[' , { component_value } , ']' ;
function_block
= ROUND_BLOCK , { component_value } , ')' ;
This parser parses the following syntax:
stylesheet = { S | CDO | CDC | qualified_rule | style_rule } ;
operator = ( '/' | ',' ) , { S } ;
combinator = ( '+' | '>' | '~' ) , { S } ;
unary_operator
= ( '-' | '+' ) ;
property = IDENT , { S } ;
style_rule = [ selector_group ] , "{" , declaration_list , "}" ;
selector_group
= selector , { "," , { S }, selector } ;
selector = simple_selector ,
{ ( combinator , selector
| { S }, [ [ combinator ] , selector ]
)
} ;
simple_selector
= universal_selector | type_selector | id_selector
| class_selector | pseudoclass_selector | attribute_selector ;
universal_selector = '*' ;
type_selector = ns_aware_ident ;
id_selector = HASH ;
class_selector = "." , IDENT ;
pseudoclass_selector = ":" , IDENT ;
attribute_selector = "[" , ns_aware_ident
, [ ( "=" | "~=" | "|=" ) , ( IDENT | STRING ) ],
"]" ;
ns_aware_ident = IDENT
| '*' , '|', IDENT
| IDENT , '|', IDENT
;
declaration_list
= { S } , [ declaration ] , [ ';' , declaration_list ] ;
declaration = IDENT , { S } , ":", { terms } ;
terms = { { S } , ( term | bracketedTerms ) } ;
term = any token - ( "]" | "}" | ";" | S ) ;
bracketedTerms = "{", { { S } , term } , { S } , "}"
| "[", { { S } , term } , { S } , "]";
function = ROUND_BLOCK , { S } , expr , ')' , { S } ;
expr = term , { [ operator ] , term } ;
The parser interprets the following at rules:
namespace_rule = "@namespace" , [ namespace_prefix ] , ( STRING | URI ) ; namespace_prefix = IDENT ;
References:
- CSS Syntax Module Level 3, Paragraph 5. Parsing
- w3.org
- CSS Namespaces Module Level 3, Paragraph 2 Declaring namespaces: the @namespace rulex
- w3.org
- W3C CSS2.2, Appendix G.1 Grammar of CSS 2.2
- w3.org
FIXME The parser does not support the !important declaration.
- Author:
- Werner Randelshofer
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanisStrict()Return strict parsing mode.Parses a declaration list.Parses a declaration list.parseSelector(@NonNull String css) Parses a given selector from the specified String and document home.Parses a given stylesheet from the specified String and document home.Parses a given stylesheet from the specified String and document home.parseStylesheet(@NonNull URI stylesheetUri, @Nullable URI stylesheetHome) Parses a given stylesheet from the specified URI.parseStylesheet(@NonNull CssTokenizer tt, @Nullable URI stylesheetUri, @Nullable URI stylesheetHome) Parses a given stylesheet from the specified String and document home.voidsetStrict(boolean strict) Sets strict parsing mode.voidsetStylesheetHome(@Nullable URI stylesheetHome) voidsetStylesheetUri(@Nullable URI stylesheetUri) voidsetUriResolver(@NonNull UriResolver uriResolver)
-
Field Details
-
ANY_NAMESPACE_PREFIX
- See Also:
-
DEFAULT_NAMESPACE
- See Also:
-
NAMESPACE_AT_RULE
- See Also:
-
-
Constructor Details
-
CssParser
public CssParser()
-
-
Method Details
-
getParseExceptions
-
parseDeclarationList
Parses a declaration list.- Parameters:
css- A stylesheet- Returns:
- the declaration list
- Throws:
IOException- if parsing fails
-
parseDeclarationList
Parses a declaration list.- Parameters:
css- A stylesheet- Returns:
- the declaration list
- Throws:
IOException- if parsing fails
-
parseSelectorGroup
public @NonNull SelectorGroup parseSelectorGroup(@NonNull CssTokenizer tt) throws IOException, ParseException - Throws:
IOExceptionParseException
-
parseStylesheet
public @NonNull Stylesheet parseStylesheet(@NonNull URI stylesheetUri, @Nullable URI stylesheetHome) throws IOException Parses a given stylesheet from the specified URI.- Parameters:
stylesheetUri- the URI of the stylesheet (must be known)stylesheetHome- base URI (if it exists)- Returns:
- the parsed stylesheet
- Throws:
IOException- on failure
-
parseStylesheet
public @NonNull Stylesheet parseStylesheet(@NonNull String css, @Nullable URI stylesheetUri, @Nullable URI stylesheetHome) throws IOException Parses a given stylesheet from the specified String and document home.- Parameters:
css- the uri of the stylesheet filestylesheetUri- the URI of the stylesheet (if known)stylesheetHome- base URI (if it exists)- Returns:
- the parsed stylesheet
- Throws:
IOException- on failure
-
parseSelector
Parses a given selector from the specified String and document home.- Parameters:
css- a literal selector String- Returns:
- the parsed selector
- Throws:
ParseException- on failure
-
parseStylesheet
public @NonNull Stylesheet parseStylesheet(Reader css, @Nullable URI stylesheetUri, @Nullable URI stylesheetHome) throws IOException Parses a given stylesheet from the specified String and document home.- Parameters:
css- the uri of the stylesheet filestylesheetUri- the URI of the stylesheet (if known)stylesheetHome- base URI (if it exists)- Returns:
- the parsed stylesheet
- Throws:
IOException- on failure
-
parseStylesheet
public @NonNull Stylesheet parseStylesheet(@NonNull CssTokenizer tt, @Nullable URI stylesheetUri, @Nullable URI stylesheetHome) throws IOException Parses a given stylesheet from the specified String and document home.- Parameters:
tt- the tokenierstylesheetUri- the URI of the stylesheet (if known)stylesheetHome- base URI (if it exists)- Returns:
- the parsed stylesheet
- Throws:
IOException- on failure
-
getStylesheetUri
-
setStylesheetUri
-
getStylesheetHome
-
setStylesheetHome
-
getUriResolver
-
setUriResolver
-
isStrict
public boolean isStrict()Return strict parsing mode.- Returns:
- true if the parser is in strict mode.
-
setStrict
public void setStrict(boolean strict) Sets strict parsing mode.- Parameters:
strict- true to parse in strict mode, the default value is false.
-