shorthand.ml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. (* CSS shorthand creation based on:
  2. * - http://www.cssshorthand.com/
  3. *)
  4. open Types
  5. open Util
  6. module KM = Map.Make(struct
  7. type t = string * bool
  8. let compare a b =
  9. match a, b with
  10. | (_, false), (_, true) -> -1
  11. | (_, true), (_, false) -> 1
  12. | (base_a, _), (base_b, _) -> String.compare base_a base_b
  13. end)
  14. let order = function
  15. | "background" -> ["color"; "image"; "repeat"; "attachment"; "position-x";
  16. "position-y"]
  17. | "border" -> ["width"; "style"; "color"]
  18. | "font" -> ["style"; "variant"; "weight"; "size"; "family"]
  19. | "list-style" -> ["type"; "position"; "image"]
  20. | "outline" -> ["color"; "style"; "width"]
  21. | "margin"
  22. | "padding" -> ["top"; "right"; "bottom"; "left"]
  23. | _ -> failwith "not a shorthand property"
  24. let fold_box_dims = function
  25. | [top; right; bottom; left]
  26. when top = bottom && right = left && top = right -> [top]
  27. | [top; right; bottom; left] when top = bottom && right = left -> [top; right]
  28. | [top; right; bottom; left] when right = left -> [top; right; bottom]
  29. | dims -> dims
  30. let fold group base =
  31. let group_mem name =
  32. let rec mem = function
  33. | [] -> false
  34. | (nm, _, _) :: _ when nm = name -> true
  35. | _ :: tl -> mem tl
  36. in
  37. mem group
  38. in
  39. let group_find name =
  40. let rec wrap known = function
  41. | [] ->
  42. (match known with Some value -> value | None -> raise Not_found)
  43. | (nm, value, _) :: tl when nm = name ->
  44. wrap (Some value) tl
  45. | _ :: tl ->
  46. wrap known tl
  47. in
  48. wrap None group
  49. in
  50. let exists sub = group_mem (base ^ "-" ^ sub) in
  51. let find sub = group_find (base ^ "-" ^ sub) in
  52. let rec lookup = function
  53. | [] -> []
  54. (* `font-size` and `line-height` are slash-separated in `font` *)
  55. | "size" :: tl when base = "font" && group_mem "line-height" ->
  56. Nary ("/", [find "size"; group_find "line-height"]) :: lookup tl
  57. | name :: tl when exists name -> find name :: lookup tl
  58. | _ :: tl -> lookup tl
  59. in
  60. match base with
  61. (* `font-size` and `font-family` are required for `font` *)
  62. | "font" when exists "size" && exists "family" ->
  63. Some (Concat (lookup (order "font")))
  64. (* `border-style` is required for `border` *)
  65. | "border" when exists "style" ->
  66. Some (Concat (lookup (order "border")))
  67. (* others require at least one property, which is already the case when this
  68. * function is called *)
  69. | ("background" | "list-style" | "outline") as base ->
  70. Some (Concat (lookup (order base)))
  71. (* margin and padding can only be shorthanded when all directions are known,
  72. * merging into even shorter shorthands is done by `fold_box_dims` *)
  73. | "margin" | "padding"
  74. when exists "top" && exists "right" && exists "bottom" && exists "left" ->
  75. let dirs = [find "top"; find "right"; find "bottom"; find "left"] in
  76. Some (Concat (fold_box_dims dirs))
  77. | _ -> None
  78. let rec list_from i = function
  79. | [] when i > 0 -> raise (Invalid_argument "l")
  80. | [] -> [] (* make the compiler happy *)
  81. | l when i = 0 -> l
  82. | _ :: tl -> list_from (i - 1) tl
  83. let is_width = function
  84. | Ident ("thin" | "thick" | "medium")
  85. | Number _ -> true
  86. | _ -> false
  87. let rec unfold = function
  88. | [] -> []
  89. (* do not unfold "<shorthand>: inherit;" *)
  90. | (("background" | "border" | "font" | "list-style" | "outline" | "margin" |
  91. "padding"), Ident "inherit", _) as orig :: tl ->
  92. orig :: unfold tl
  93. (* background: [color] [image] [repeat] [attachment] [position] *)
  94. | ("background", Concat values, imp) :: tl ->
  95. let make sub value = ("background-" ^ sub, value, imp) in
  96. let id_color = function
  97. | [] -> []
  98. | [color] when Color_names.is_color color -> [make "color" color]
  99. | tl -> raise (Box_error (Expr (Concat tl), "invalid background shortcut"))
  100. (*| _ -> failwith "invalid background shortcut"*)
  101. in
  102. let id_repeat = function
  103. | repeat :: (Uri _ as image) :: tl ->
  104. make "repeat" repeat :: make "image" image :: id_color tl
  105. | Uri _ as image :: tl ->
  106. make "image" image :: id_color tl
  107. | tl -> id_color tl
  108. in
  109. let id_attachment = function
  110. | Ident _ as attachment :: (Ident _ as repeat) :: tl ->
  111. make "attachment" attachment :: make "repeat" repeat :: id_repeat tl
  112. | Ident ("scroll" | "fixed") as attachment :: (Uri url :: _ as tl) ->
  113. make "attachment" attachment :: id_repeat tl
  114. | (_ :: Uri _ :: _) as tl
  115. | tl -> id_repeat tl
  116. in
  117. let id_pos = function
  118. | Number _ as posy :: (Number _ as posx) :: tl
  119. | (Ident ("top" | "center" | "bottom") as posy) ::
  120. (Ident ("left" | "center" | "right") as posx) :: tl ->
  121. make "position-y" posy :: make "position-x" posx :: id_attachment tl
  122. | tl -> id_attachment tl
  123. in
  124. List.rev (id_pos (List.rev values)) @ unfold tl
  125. | ("background", (Uri _ as image), imp) :: tl ->
  126. ("background-image", image, imp) :: unfold tl
  127. | ("background", color, imp) :: tl ->
  128. ("background-color", color, imp) :: unfold tl
  129. (* border: [width] style [color] *)
  130. | ("border", Concat [Ident _ as style], imp) :: tl ->
  131. ("border-style", style, imp) :: unfold tl
  132. | ("border", Concat [width; Ident _ as style; color], imp) :: tl ->
  133. ("border-width", width, imp) ::
  134. ("border-style", style, imp) ::
  135. ("border-color", color, imp) :: unfold tl
  136. | ("border", Concat [Number _ as width; Ident _ as style], imp) :: tl ->
  137. ("border-width", width, imp) ::
  138. ("border-style", style, imp) :: unfold tl
  139. | ("border", Concat [Ident _ as style; color], imp) :: tl ->
  140. ("border-style", style, imp) ::
  141. ("border-color", color, imp) :: unfold tl
  142. (* font: [style] [variant] [weight] size[/line-height] family *)
  143. | ("font", Concat values, imp) as orig :: tl ->
  144. let replacement =
  145. let make sub value = ("font-" ^ sub, value, imp) in
  146. let identify options =
  147. let return sub = assert (List.mem sub options); sub in
  148. function
  149. | Ident "normal" -> List.hd options
  150. | Ident ("italic" | "oblique") -> return "style"
  151. | Ident "small-caps" -> return "variant"
  152. | _ -> return "weight"
  153. in
  154. match values with
  155. | [size; family] ->
  156. [make "size" size; make "family" family]
  157. | [first; size; family] ->
  158. [make (identify ["weight"; "variant"; "style"] first) first;
  159. make "size" size; make "family" family]
  160. | [first; second; size; family] ->
  161. [make (identify ["variant"; "style"] first) first;
  162. make (identify ["weight"; "variant"] second) second;
  163. make "size" size; make "family" family]
  164. | [style; variant; weight; size; family] ->
  165. [make "style" style; make "variant" variant; make "weight" weight;
  166. make "size" size; make "family" family]
  167. | _ -> [orig]
  168. in
  169. let rec split_size = function
  170. | [] -> []
  171. | ("font-size", Nary ("/", [size; line_height]), _) :: tl ->
  172. ("font-size", size, imp) ::
  173. ("line-height", line_height, imp) :: tl
  174. | hd :: tl -> hd :: split_size tl
  175. in
  176. split_size replacement @ unfold tl
  177. (* list-style: [type] [position] [image] *)
  178. | ("list-style", Concat [ltype; pos; image], imp) :: tl ->
  179. ("list-style-type", ltype, imp) ::
  180. ("list-style-position", pos, imp) ::
  181. ("list-style-image", image, imp) :: unfold tl
  182. | ("list-style", Concat [Ident _ as pos; Uri _ as image], imp) :: tl ->
  183. ("list-style-position", pos, imp) ::
  184. ("list-style-image", image, imp) :: unfold tl
  185. | ("list-style", Concat [ltype; Ident _ as pos], imp) :: tl ->
  186. ("list-style-type", ltype, imp) ::
  187. ("list-style-position", pos, imp) :: unfold tl
  188. (* margin: top right bottom left
  189. * | top right-left bottom
  190. * | top-bottom right-left
  191. * | top right bottom left
  192. * | all
  193. *)
  194. | (("margin" | "padding") as base, value, imp) :: tl ->
  195. let (top, right, bottom, left) =
  196. match value with
  197. | Concat [top; right; bottom; left] ->
  198. (top, right, bottom, left)
  199. | Concat [top; right; bottom] ->
  200. (top, right, bottom, right)
  201. | Concat [top; right] ->
  202. (top, right, top, right)
  203. | _ ->
  204. (value, value, value, value)
  205. in
  206. let make dir value = (base ^ "-" ^ dir, value, imp) in
  207. make "top" top :: make "right" right :: make "bottom" bottom ::
  208. make "left" left :: unfold tl
  209. | hd :: tl ->
  210. hd :: unfold tl
  211. let pattern = Str.regexp ("^\\(background\\|border\\|font\\|list-style" ^
  212. "\\|outline\\|padding\\|margin\\)-\\(.*\\)$")
  213. let rec make_shorthands decls =
  214. (* unfold currently existing shorthands into separate properties for merging
  215. * with override properties that are defined later on *)
  216. (*let decls = unfold decls in
  217. XXX: done by main function for correct pruning of duplicate declarations*)
  218. let rec extract_groups decl_skipped groups rest =
  219. let rec find_in_group name = function
  220. | [] -> false
  221. | (nm, _, _) :: _ when nm = name -> true
  222. | _ :: tl -> find_in_group name tl
  223. in
  224. let should_skip base name imp =
  225. try find_in_group name (KM.find (base, imp) groups)
  226. with Not_found -> false
  227. in
  228. let add base imp value =
  229. let key = base, imp in
  230. let group = try KM.find key groups with Not_found -> [] in
  231. KM.add key (value :: group) groups
  232. in
  233. function
  234. | [] -> decl_skipped, groups, rest
  235. | (("line-height", _, imp) as hd) :: tl
  236. when should_skip "font" "line_height" imp ->
  237. extract_groups true groups (hd :: rest) tl
  238. | (("line-height", _, imp) as hd) :: tl ->
  239. extract_groups decl_skipped (add "font" imp hd) rest tl
  240. | ((name, _, imp) as hd) :: tl when Str.string_match pattern name 0 ->
  241. let base = Str.matched_group 1 name in
  242. let sub = Str.matched_group 2 name in
  243. let skip_this = should_skip base name imp in
  244. if not skip_this && List.mem sub (order base)
  245. then extract_groups decl_skipped (add base imp hd) rest tl
  246. else extract_groups (decl_skipped || skip_this) groups (hd :: rest) tl
  247. | hd :: tl -> extract_groups decl_skipped groups (hd :: rest) tl
  248. in
  249. let decl_skipped, groups, rest = extract_groups false KM.empty [] decls in
  250. let replace (base, important) group tl =
  251. match fold (List.rev group) base with
  252. | Some short_value -> (base, short_value, important) :: tl
  253. | None -> group @ tl
  254. in
  255. let shorthands = KM.fold replace groups [] in
  256. let decls = List.rev_append rest shorthands in
  257. if decl_skipped then make_shorthands decls else decls
  258. let compress =
  259. Util.transform_stylesheet begin function
  260. | Statement (Ruleset (selectors, decls)) ->
  261. Statement (Ruleset (selectors, make_shorthands decls))
  262. | v -> v
  263. end
  264. let unfold_stylesheet =
  265. Util.transform_stylesheet begin function
  266. | Statement (Ruleset (selectors, decls)) ->
  267. Statement (Ruleset (selectors, unfold decls))
  268. | v -> v
  269. end