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

Added support for CSS3 pseudo-elements

parent cccf4c43
RESULT := mincss RESULT := mincss
DIST := dist/mincss DIST := dist/mincss
PRE_TGTS := types PRE_TGTS := types
MODULES := color_names util stringify parser lexer parse selector simple \ MODULES := color_names util stringify parser lexer parse simple shorthand \
shorthand duplicates main duplicates main
ALL_NAMES := $(PRE_TGTS) $(MODULES) ALL_NAMES := $(PRE_TGTS) $(MODULES)
OCAMLCFLAGS := -g OCAMLCFLAGS := -g
......
...@@ -146,6 +146,7 @@ rule token = parse ...@@ -146,6 +146,7 @@ rule token = parse
| ']' { RBRACK } | ']' { RBRACK }
| ';' { SEMICOL } | ';' { SEMICOL }
| ':' { COLON } | ':' { COLON }
| "::" { DOUBLE_COLON }
| ',' { COMMA } | ',' { COMMA }
| '.' { DOT } | '.' { DOT }
......
...@@ -47,8 +47,10 @@ ...@@ -47,8 +47,10 @@
append_addons (Class (base, cls)) tl append_addons (Class (base, cls)) tl
| `Attribute (attr, value) :: tl -> | `Attribute (attr, value) :: tl ->
append_addons (Attribute (base, attr, value)) tl append_addons (Attribute (base, attr, value)) tl
| `Pseudo (f, args) :: tl -> | `Pseudo_class (f, args) :: tl ->
append_addons (Pseudo (base, f, args)) tl append_addons (Pseudo_class (base, f, args)) tl
| `Pseudo_element elem :: tl ->
append_addons (Pseudo_element (base, elem)) tl
%} %}
(* Tokens *) (* Tokens *)
...@@ -57,8 +59,8 @@ ...@@ -57,8 +59,8 @@
%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 COMBINATOR RELATION STRING IDENT HASH URI FUNCTION
%token LPAREN RPAREN LBRACE RBRACE LBRACK RBRACK SEMICOL COLON COMMA DOT PLUS %token LPAREN RPAREN LBRACE RBRACE LBRACK RBRACK SEMICOL COLON DOUBLE_COLON
%token 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
(* Start symbol *) (* Start symbol *)
...@@ -254,9 +256,11 @@ attrib: ...@@ -254,9 +256,11 @@ attrib:
| S* s=STRING S* { Strlit s } | S* s=STRING S* { Strlit s }
pseudo: pseudo:
| COLON id=IDENT | COLON id=IDENT
{ `Pseudo (String.lowercase id, None) } { `Pseudo_class (String.lowercase id, None) }
| COLON f=FUNCTION args=wslist(COMMA, simple_selector) RPAREN | COLON f=FUNCTION args=wslist(COMMA, simple_selector) RPAREN
{ `Pseudo (String.lowercase f, Some args) } { `Pseudo_class (String.lowercase f, Some args) }
| DOUBLE_COLON id=IDENT
{ `Pseudo_element (String.lowercase id) }
declaration: declaration:
| name=property S* COLON S* value=expr important=boption(ig2(IMPORTANT_SYM, S*)) | name=property S* COLON S* value=expr important=boption(ig2(IMPORTANT_SYM, S*))
......
...@@ -6,7 +6,7 @@ open Types ...@@ -6,7 +6,7 @@ open Types
*) *)
let is_pseudo_class = function let is_pseudo_class = function
| Pseudo (_, ("link" | "hover" | "visited" | "active"), None) -> true | Pseudo_class (_, ("link" | "hover" | "visited" | "active"), None) -> true
| _ -> false | _ -> false
(* Specificity (a, b, c, d): (* Specificity (a, b, c, d):
...@@ -26,10 +26,11 @@ let rec specificity = ...@@ -26,10 +26,11 @@ let rec specificity =
add (0, 1, 0, 0) (specificity base) add (0, 1, 0, 0) (specificity base)
| Class (base, _) | Attribute (base, _, _) -> | Class (base, _) | Attribute (base, _, _) ->
add (0, 0, 1, 0) (specificity base) add (0, 0, 1, 0) (specificity base)
| Pseudo (base, _, _) as addon when is_pseudo_class addon -> | Pseudo_class (base, _, _) as addon when is_pseudo_class addon ->
add (0, 0, 1, 0) (specificity base) add (0, 0, 1, 0) (specificity base)
| Pseudo (base, _, _) -> | Pseudo_class (base, _, _) ->
add (0, 0, 0, 1) (specificity base) add (0, 0, 0, 1) (specificity base)
(* XXX: Pseudo_element *)
| Combinator (left, _, right) -> | Combinator (left, _, right) ->
add (specificity left) (specificity right) add (specificity left) (specificity right)
......
...@@ -53,10 +53,12 @@ let rec stringify_selector w selector = ...@@ -53,10 +53,12 @@ let rec stringify_selector w selector =
str base ^ "[" ^ attr ^ "]" str base ^ "[" ^ attr ^ "]"
| Attribute (base, attr, Some (op, value)) -> | Attribute (base, attr, Some (op, value)) ->
str base ^ "[" ^ attr ^ w ^ op ^ w ^ string_of_expr value ^ "]" str base ^ "[" ^ attr ^ w ^ op ^ w ^ string_of_expr value ^ "]"
| Pseudo (base, sel, None) -> | Pseudo_class (base, cls, None) ->
str base ^ ":" ^ sel str base ^ ":" ^ cls
| Pseudo (base, fn, Some args) -> | Pseudo_class (base, fn, Some args) ->
str base ^ ":" ^ fn ^ "(" ^ cat ("," ^ w) str args ^ ")" str base ^ ":" ^ fn ^ "(" ^ cat ("," ^ w) str args ^ ")"
| Pseudo_element (base, elem) ->
str base ^ "::" ^ elem
| Combinator (left, " ", right) -> | Combinator (left, " ", right) ->
str left ^ " " ^ str right str left ^ " " ^ str right
| Combinator (left, com, right) -> | Combinator (left, com, right) ->
......
...@@ -17,7 +17,8 @@ type selector = ...@@ -17,7 +17,8 @@ type selector =
| Element of string | Element of string
| Id of selector * string | Id of selector * string
| Class of selector * string | Class of selector * string
| Pseudo of selector * string * selector list option | Pseudo_class of selector * string * selector list option
| Pseudo_element of selector * string
| Attribute of selector * string * (string * expr) option | Attribute of selector * string * (string * expr) option
| Combinator of selector * string * selector | Combinator of selector * string * selector
......
...@@ -139,11 +139,13 @@ let transform_stylesheet f stylesheet = ...@@ -139,11 +139,13 @@ let transform_stylesheet f stylesheet =
f (Selector (Class (expect_selector base, cls))) f (Selector (Class (expect_selector base, cls)))
| Attribute (base, attr, value) -> | Attribute (base, attr, value) ->
f (Selector (Attribute (expect_selector base, attr, value))) f (Selector (Attribute (expect_selector base, attr, value)))
| Pseudo (base, sel, None) -> | Pseudo_class (base, cls, None) ->
f (Selector (Pseudo (expect_selector base, sel, None))) f (Selector (Pseudo_class (expect_selector base, cls, None)))
| Pseudo (base, fn, Some args) -> | Pseudo_class (base, fn, Some args) ->
let args = trav_all_selector args in let args = trav_all_selector args in
f (Selector (Pseudo (expect_selector base, fn, Some args))) f (Selector (Pseudo_class (expect_selector base, fn, Some args)))
| Pseudo_element (base, elem) ->
f (Selector (Pseudo_element (expect_selector base, elem)))
| Combinator (left, com, right) -> | Combinator (left, com, right) ->
let left = expect_selector left in let left = expect_selector left in
let right = expect_selector right in let right = expect_selector right in
......
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