Commit bc51af63 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Added support for exotic browser-specific syntax such as @-ms-viewport and filter(opacity=60)

parent ef3a6884
...@@ -106,6 +106,8 @@ rule token = parse ...@@ -106,6 +106,8 @@ rule token = parse
| '@' S U P P O R T S { SUPPORTS_SYM } | '@' S U P P O R T S { SUPPORTS_SYM }
| '@' (('-' ident '-')? as prefix) K E Y F R A M E S | '@' (('-' ident '-')? as prefix) K E Y F R A M E S
{ KEYFRAMES_SYM (String.lowercase prefix) } { KEYFRAMES_SYM (String.lowercase prefix) }
| '@' (('-' ident '-')? as prefix) V I E W P O R T
{ VIEWPORT_SYM (String.lowercase prefix) }
| (s | comment)* s comment* A N D comment* s (s | comment)* | (s | comment)* s comment* A N D comment* s (s | comment)*
{ advance_pos lexbuf; WS_AND } { advance_pos lexbuf; WS_AND }
......
...@@ -58,7 +58,8 @@ ...@@ -58,7 +58,8 @@
%token NAMESPACE_SYM SUPPORTS_SYM IMPORTANT_SYM %token NAMESPACE_SYM SUPPORTS_SYM IMPORTANT_SYM
%token <float> PERCENTAGE NUMBER %token <float> PERCENTAGE NUMBER
%token <float * string> UNIT_VALUE %token <float * string> UNIT_VALUE
%token <string> KEYFRAMES_SYM COMBINATOR RELATION STRING IDENT HASH URI FUNCTION %token <string> KEYFRAMES_SYM VIEWPORT_SYM COMBINATOR RELATION STRING IDENT HASH
%token <string> URI FUNCTION
%token LPAREN RPAREN LBRACE RBRACE LBRACK RBRACK SEMICOL COLON DOUBLE_COLON %token LPAREN RPAREN LBRACE RBRACE LBRACK RBRACK SEMICOL COLON DOUBLE_COLON
%token COMMA DOT PLUS MINUS SLASH STAR ONLY AND (*OR*) NOT FROM TO EOF %token COMMA DOT PLUS MINUS SLASH STAR ONLY AND (*OR*) NOT FROM TO EOF
%token WS_AND WS_OR %token WS_AND WS_OR
...@@ -90,7 +91,7 @@ stylesheet: ...@@ -90,7 +91,7 @@ stylesheet:
nested_statement: nested_statement:
| s=ruleset | s=media | s=page | s=font_face_rule | s=keyframes_rule | s=ruleset | s=media | s=page | s=font_face_rule | s=keyframes_rule
| s=supports_rule | s=supports_rule | s=viewport_rule
{ s } { s }
group_rule_body: group_rule_body:
...@@ -209,6 +210,10 @@ S*; ...@@ -209,6 +210,10 @@ S*;
unused : block | ATKEYWORD S* | ';' S* | CDO S* | CDC S*; unused : block | ATKEYWORD S* | ';' S* | CDO S* | CDC S*;
*) *)
viewport_rule:
| pre=VIEWPORT_SYM S* decls=decls_block
{ Viewport (pre, decls) }
%inline decls_block: %inline decls_block:
| LBRACE S* hd=declaration? tl=wspreceded(SEMICOL, declaration?)* RBRACE S* | LBRACE S* hd=declaration? tl=wspreceded(SEMICOL, declaration?)* RBRACE S*
{ filter_none (hd :: tl) } { filter_none (hd :: tl) }
...@@ -285,13 +290,30 @@ term: ...@@ -285,13 +290,30 @@ term:
| v=numval S* { v } | v=numval S* { v }
| str=STRING S* { Strlit str } | str=STRING S* { Strlit str }
| id=IDENT S* { Ident (String.lowercase id) } | id=IDENT S* { Ident (String.lowercase id) }
| ONLY S* { Ident "only" }
| NOT S* { Ident "not" }
| AND S* { Ident "and" }
| FROM S* { Ident "from" }
| TO S* { Ident "to" }
| uri=URI S* { Uri uri } | uri=URI S* { Uri uri }
| fn=FUNCTION arg=expr RPAREN S* { Function (String.lowercase fn, arg) } | fn=FUNCTION arg=expr RPAREN S* { Function (String.lowercase fn, arg) }
| key=IDENT S* COLON S* value=term
{ Key_value (key, ":", value) }
| key=IDENT S* DOT S* value=term
{ Key_value (key, ".", value) }
| key=IDENT S* rel=RELATION S* value=term
{
if rel = "="
then Key_value (key, "=", value)
else raise (Syntax_error ("unexpected '" ^ rel ^ "'"))
}
| hex=HASH S* | hex=HASH S*
{ let h = "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]" in {
let h = "[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]" in
if Str.string_match (Str.regexp ("^" ^ h ^ "\\(" ^ h ^ "\\)?$")) hex 0 if Str.string_match (Str.regexp ("^" ^ h ^ "\\(" ^ h ^ "\\)?$")) hex 0
then Hexcolor (String.lowercase hex) then Hexcolor (String.lowercase hex)
else raise (Syntax_error ("invalid color #" ^ hex)) } else raise (Syntax_error ("invalid color #" ^ hex))
}
unary_operator: unary_operator:
| MINUS { "-" } | MINUS { "-" }
| PLUS { "+" } | PLUS { "+" }
......
...@@ -34,6 +34,7 @@ let rec string_of_expr = function ...@@ -34,6 +34,7 @@ let rec string_of_expr = function
| Unary (op, opnd) -> op ^ string_of_expr opnd | Unary (op, opnd) -> op ^ string_of_expr opnd
| Nary (",", opnds) -> cat ", " string_of_expr opnds | Nary (",", opnds) -> cat ", " string_of_expr opnds
| Nary (op, opnds) -> cat op string_of_expr opnds | Nary (op, opnds) -> cat op string_of_expr opnds
| Key_value (key, op, value) -> key ^ op ^ string_of_expr value
let string_of_declaration (name, value, important) = let string_of_declaration (name, value, important) =
let imp = if important then " !important" else "" in let imp = if important then " !important" else "" in
...@@ -141,6 +142,8 @@ let rec string_of_statement = function ...@@ -141,6 +142,8 @@ let rec string_of_statement = function
| Supports (condition, statements) -> | Supports (condition, statements) ->
"@supports " ^ string_of_condition condition ^ "@supports " ^ string_of_condition condition ^
block (cat "\n\n" string_of_statement statements) block (cat "\n\n" string_of_statement statements)
| Viewport (prefix, decls) ->
"@" ^ prefix ^ "viewport" ^ block (cat "\n" string_of_declaration decls)
let string_of_stylesheet = cat "\n\n" string_of_statement let string_of_stylesheet = cat "\n\n" string_of_statement
...@@ -168,6 +171,7 @@ let rec minify_expr = function ...@@ -168,6 +171,7 @@ let rec minify_expr = function
| Nary (op, opnds) -> cat op minify_expr opnds | Nary (op, opnds) -> cat op minify_expr opnds
| Number (n, None) -> minify_num n | Number (n, None) -> minify_num n
| Number (n, Some u) -> minify_num n ^ u | Number (n, Some u) -> minify_num n ^ u
| Key_value (key, op, value) -> key ^ op ^ minify_expr value
| expr -> string_of_expr expr | expr -> string_of_expr expr
let minify_declaration (name, value, important) = let minify_declaration (name, value, important) =
...@@ -219,6 +223,8 @@ let rec minify_statement = function ...@@ -219,6 +223,8 @@ let rec minify_statement = function
| Supports (condition, statements) -> | Supports (condition, statements) ->
"@supports " ^ stringify_condition "" condition ^ "@supports " ^ stringify_condition "" condition ^
"{" ^ cat "" minify_statement statements ^ "}" "{" ^ cat "" minify_statement statements ^ "}"
| Viewport (prefix, decls) ->
"@" ^ prefix ^ "viewport{" ^ cat ";" minify_declaration decls ^ "}"
| statement -> string_of_statement statement | statement -> string_of_statement statement
let minify_stylesheet = cat "" minify_statement let minify_stylesheet = cat "" minify_statement
......
...@@ -8,6 +8,7 @@ type expr = ...@@ -8,6 +8,7 @@ type expr =
| Hexcolor of string | Hexcolor of string
| Unary of string * expr | Unary of string * expr
| Nary of string * expr list | Nary of string * expr list
| Key_value of string * string * expr
type declaration = string * expr * bool type declaration = string * expr * bool
...@@ -63,6 +64,8 @@ type statement = ...@@ -63,6 +64,8 @@ type statement =
(* @[-<prefix>-]keyframes <id> { <rulesets> } *) (* @[-<prefix>-]keyframes <id> { <rulesets> } *)
| Supports of condition * statement list | Supports of condition * statement list
(* @supports <condition> { <rulesets> } *) (* @supports <condition> { <rulesets> } *)
| Viewport of string * declaration list
(* @[-<prefix>-]viewport { <declarations> } *)
type stylesheet = statement list type stylesheet = statement list
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment