shorthand.ml 10 KB

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