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

Added support for CSS3 pseudo-elements

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