Pārlūkot izejas kodu

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

Taddeus Kroes 11 gadi atpakaļ
vecāks
revīzija
bc51af6350
4 mainītis faili ar 37 papildinājumiem un 4 dzēšanām
  1. 2 0
      lexer.mll
  2. 26 4
      parser.mly
  3. 6 0
      stringify.ml
  4. 3 0
      types.ml

+ 2 - 0
lexer.mll

@@ -106,6 +106,8 @@ rule token = parse
   | '@' S U P P O R T S           { SUPPORTS_SYM }
   | '@' (('-' ident '-')? as prefix) K E Y F R A M E S
   { 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)*
   { advance_pos lexbuf; WS_AND }

+ 26 - 4
parser.mly

@@ -58,7 +58,8 @@
 %token NAMESPACE_SYM SUPPORTS_SYM IMPORTANT_SYM
 %token <float> PERCENTAGE NUMBER
 %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 COMMA DOT PLUS MINUS SLASH STAR ONLY AND (*OR*) NOT FROM TO EOF
 %token WS_AND WS_OR
@@ -90,7 +91,7 @@ stylesheet:
 
 nested_statement:
   | s=ruleset | s=media | s=page | s=font_face_rule | s=keyframes_rule
-  | s=supports_rule
+  | s=supports_rule | s=viewport_rule
   { s }
 
 group_rule_body:
@@ -209,6 +210,10 @@ 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:
   | LBRACE S* hd=declaration? tl=wspreceded(SEMICOL, declaration?)* RBRACE S*
   { filter_none (hd :: tl) }
@@ -285,13 +290,30 @@ term:
   | v=numval S*                     { v }
   | str=STRING S*                   { Strlit str }
   | 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 }
   | 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*
-  { 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
       then Hexcolor (String.lowercase hex)
-      else raise (Syntax_error ("invalid color #" ^ hex)) }
+      else raise (Syntax_error ("invalid color #" ^ hex))
+  }
 unary_operator:
   | MINUS  { "-" }
   | PLUS   { "+" }

+ 6 - 0
stringify.ml

@@ -34,6 +34,7 @@ let rec string_of_expr = function
   | Unary (op, opnd) -> op ^ string_of_expr opnd
   | Nary (",", opnds) -> cat ", " 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 imp = if important then " !important" else "" in
@@ -141,6 +142,8 @@ let rec string_of_statement = function
   | Supports (condition, statements) ->
     "@supports " ^ string_of_condition condition ^
     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
 
@@ -168,6 +171,7 @@ let rec minify_expr = function
   | Nary (op, opnds) -> cat op minify_expr opnds
   | Number (n, None) -> minify_num n
   | Number (n, Some u) -> minify_num n ^ u
+  | Key_value (key, op, value) -> key ^ op ^ minify_expr value
   | expr -> string_of_expr expr
 
 let minify_declaration (name, value, important) =
@@ -219,6 +223,8 @@ let rec minify_statement = function
   | Supports (condition, statements) ->
     "@supports " ^ stringify_condition "" condition ^
     "{" ^ cat "" minify_statement statements ^ "}"
+  | Viewport (prefix, decls) ->
+    "@" ^ prefix ^ "viewport{" ^ cat ";" minify_declaration decls ^ "}"
   | statement -> string_of_statement statement
 
 let minify_stylesheet = cat "" minify_statement

+ 3 - 0
types.ml

@@ -8,6 +8,7 @@ type expr =
   | Hexcolor of string
   | Unary of string * expr
   | Nary of string * expr list
+  | Key_value of string * string * expr
 
 type declaration = string * expr * bool
 
@@ -63,6 +64,8 @@ type statement =
   (* @[-<prefix>-]keyframes <id> { <rulesets> } *)
   | Supports of condition * statement list
   (* @supports <condition> { <rulesets> } *)
+  | Viewport of string * declaration list
+  (* @[-<prefix>-]viewport { <declarations> } *)
 
 type stylesheet = statement list