shorthand.ml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. in
  101. let id_repeat = function
  102. | repeat :: (Uri _ as image) :: tl ->
  103. make "repeat" repeat :: make "image" image :: id_color tl
  104. | Uri _ as image :: tl ->
  105. make "image" image :: id_color tl
  106. | tl -> id_color tl
  107. in
  108. let id_attachment = function
  109. | Ident _ as attachment :: (Ident _ as repeat) :: tl ->
  110. make "attachment" attachment :: make "repeat" repeat :: id_repeat tl
  111. | Ident ("scroll" | "fixed") as attachment :: (Uri url :: _ as tl) ->
  112. make "attachment" attachment :: id_repeat tl
  113. | (_ :: Uri _ :: _) as tl
  114. | tl -> id_repeat tl
  115. in
  116. let id_pos = function
  117. | Number _ as posy :: (Number _ as posx) :: tl
  118. | (Ident ("top" | "center" | "bottom") as posy) ::
  119. (Ident ("left" | "center" | "right") as posx) :: tl ->
  120. make "position-y" posy :: make "position-x" posx :: id_attachment tl
  121. | tl -> id_attachment tl
  122. in
  123. List.rev (id_pos (List.rev values)) @ unfold tl
  124. | ("background", (Uri _ as image), imp) :: tl ->
  125. ("background-image", image, imp) :: unfold tl
  126. | ("background", color, imp) :: tl ->
  127. ("background-color", color, imp) :: unfold tl
  128. (* border: [width] style [color] *)
  129. | ("border", Concat [Ident _ as style], imp) :: tl ->
  130. ("border-style", style, imp) :: unfold tl
  131. | ("border", Concat [width; Ident _ as style; color], imp) :: tl ->
  132. ("border-width", width, imp) ::
  133. ("border-style", style, imp) ::
  134. ("border-color", color, imp) :: unfold tl
  135. | ("border", Concat [Number _ as width; Ident _ as style], imp) :: tl ->
  136. ("border-width", width, imp) ::
  137. ("border-style", style, imp) :: unfold tl
  138. | ("border", Concat [Ident _ as style; color], imp) :: tl ->
  139. ("border-style", style, imp) ::
  140. ("border-color", color, imp) :: unfold tl
  141. (* font: [style] [variant] [weight] size[/line-height] family *)
  142. | ("font", Concat values, imp) as orig :: tl ->
  143. let replacement =
  144. let make sub value = ("font-" ^ sub, value, imp) in
  145. let identify options =
  146. let return sub = assert (List.mem sub options); sub in
  147. function
  148. | Ident "normal" -> List.hd options
  149. | Ident ("italic" | "oblique") -> return "style"
  150. | Ident "small-caps" -> return "variant"
  151. | _ -> return "weight"
  152. in
  153. match values with
  154. | [size; family] ->
  155. [make "size" size; make "family" family]
  156. | [first; size; family] ->
  157. [make (identify ["weight"; "variant"; "style"] first) first;
  158. make "size" size; make "family" family]
  159. | [first; second; size; family] ->
  160. [make (identify ["variant"; "style"] first) first;
  161. make (identify ["weight"; "variant"] second) second;
  162. make "size" size; make "family" family]
  163. | [style; variant; weight; size; family] ->
  164. [make "style" style; make "variant" variant; make "weight" weight;
  165. make "size" size; make "family" family]
  166. | _ -> [orig]
  167. in
  168. let rec split_size = function
  169. | [] -> []
  170. | ("font-size", Nary ("/", [size; line_height]), _) :: tl ->
  171. ("font-size", size, imp) ::
  172. ("line-height", line_height, imp) :: tl
  173. | hd :: tl -> hd :: split_size tl
  174. in
  175. split_size replacement @ unfold tl
  176. (* list-style: [type] [position] [image] *)
  177. | ("list-style", Concat [ltype; pos; image], imp) :: tl ->
  178. ("list-style-type", ltype, imp) ::
  179. ("list-style-position", pos, imp) ::
  180. ("list-style-image", image, imp) :: unfold tl
  181. | ("list-style", Concat [Ident _ as pos; Uri _ as image], imp) :: tl ->
  182. ("list-style-position", pos, imp) ::
  183. ("list-style-image", image, imp) :: unfold tl
  184. | ("list-style", Concat [ltype; Ident _ as pos], imp) :: tl ->
  185. ("list-style-type", ltype, imp) ::
  186. ("list-style-position", pos, imp) :: unfold tl
  187. (* margin: top right bottom left
  188. * | top right-left bottom
  189. * | top-bottom right-left
  190. * | top right bottom left
  191. * | all
  192. *)
  193. | (("margin" | "padding") as base, value, imp) :: tl ->
  194. let (top, right, bottom, left) =
  195. match value with
  196. | Concat [top; right; bottom; left] ->
  197. (top, right, bottom, left)
  198. | Concat [top; right; bottom] ->
  199. (top, right, bottom, right)
  200. | Concat [top; right] ->
  201. (top, right, top, right)
  202. | _ ->
  203. (value, value, value, value)
  204. in
  205. let make dir value = (base ^ "-" ^ dir, value, imp) in
  206. make "top" top :: make "right" right :: make "bottom" bottom ::
  207. make "left" left :: unfold tl
  208. | hd :: tl ->
  209. hd :: unfold tl
  210. let pattern = Str.regexp ("^\\(background\\|border\\|font\\|list-style" ^
  211. "\\|outline\\|padding\\|margin\\)-\\(.*\\)$")
  212. let rec make_shorthands decls =
  213. (* unfold currently existing shorthands into separate properties for merging
  214. * with override properties that are defined later on *)
  215. (*let decls = unfold decls in
  216. XXX: done by main function for correct pruning of duplicate declarations*)
  217. let rec extract_groups decl_skipped groups rest =
  218. let rec find_in_group name = function
  219. | [] -> false
  220. | (nm, _, _) :: _ when nm = name -> true
  221. | _ :: tl -> find_in_group name tl
  222. in
  223. let should_skip base name imp =
  224. try find_in_group name (KM.find (base, imp) groups)
  225. with Not_found -> false
  226. in
  227. let add base imp value =
  228. let key = base, imp in
  229. let group = try KM.find key groups with Not_found -> [] in
  230. KM.add key (value :: group) groups
  231. in
  232. function
  233. | [] -> decl_skipped, groups, rest
  234. | (("line-height", _, imp) as hd) :: tl
  235. when should_skip "font" "line_height" imp ->
  236. extract_groups true groups (hd :: rest) tl
  237. | (("line-height", _, imp) as hd) :: tl ->
  238. extract_groups decl_skipped (add "font" imp hd) rest tl
  239. | ((name, _, imp) as hd) :: tl when Str.string_match pattern name 0 ->
  240. let base = Str.matched_group 1 name in
  241. let sub = Str.matched_group 2 name in
  242. let skip_this = should_skip base name imp in
  243. if not skip_this && List.mem sub (order base)
  244. then extract_groups decl_skipped (add base imp hd) rest tl
  245. else extract_groups (decl_skipped || skip_this) groups (hd :: rest) tl
  246. | hd :: tl -> extract_groups decl_skipped groups (hd :: rest) tl
  247. in
  248. let decl_skipped, groups, rest = extract_groups false KM.empty [] decls in
  249. let replace (base, important) group tl =
  250. match fold (List.rev group) base with
  251. | Some short_value -> (base, short_value, important) :: tl
  252. | None -> List.rev_append group tl
  253. in
  254. let shorthands = KM.fold replace groups [] in
  255. let decls = List.rev_append rest shorthands in
  256. if decl_skipped then make_shorthands decls else decls
  257. let compress =
  258. Util.transform_stylesheet begin function
  259. | Statement (Ruleset (selectors, decls)) ->
  260. Statement (Ruleset (selectors, make_shorthands decls))
  261. | v -> v
  262. end
  263. let unfold_stylesheet =
  264. Util.transform_stylesheet begin function
  265. | Statement (Ruleset (selectors, decls)) ->
  266. Statement (Ruleset (selectors, unfold decls))
  267. | v -> v
  268. end