duplicates.ml 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. open Types
  2. module SM = Map.Make(String)
  3. let prune_duplicates decls =
  4. let keep = Array.make (List.length decls) true in
  5. let rec tag db index = function
  6. | (name, _, imp) :: tl when SM.mem name db ->
  7. (* previous value exists, one needs to be removed *)
  8. let prev_index, prev_imp = SM.find name db in
  9. if not imp && prev_imp then begin
  10. keep.(index) <- false;
  11. tag db (index + 1) tl
  12. end else begin
  13. keep.(prev_index) <- false;
  14. tag (SM.add name (index, imp) db) (index + 1) tl
  15. end
  16. | (name, _, imp) :: tl ->
  17. tag (SM.add name (index, imp) db) (index + 1) tl
  18. | [] -> ()
  19. in
  20. tag SM.empty 0 decls;
  21. let rec prune i = function
  22. | [] -> []
  23. | hd :: tl when keep.(i) -> hd :: prune (i + 1) tl
  24. | _ :: tl -> prune (i + 1) tl
  25. in
  26. prune 0 decls
  27. let compress =
  28. Util.transform_stylesheet begin function
  29. | Statement (Ruleset (selectors, decls)) ->
  30. Statement (Ruleset (selectors, prune_duplicates decls))
  31. | v -> v
  32. end