Source file rule_order.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
(** Canonical cascade-safe rule ordering: project each run of reorderable
statements through {!Rule_graph} to its canonical linear extension. *)
open Stylesheet
let rec element_rules (stmt : statement) : rule list option =
match stmt with
| Rule r -> if r.nested = [] then Some [ r ] else None
| Media (_, b) | Supports (_, b) | Container (_, _, b) -> block_rules b
| _ -> None
and block_rules (stmts : statement list) : rule list option =
List.fold_left
(fun acc stmt ->
match (acc, element_rules stmt) with
| Some acc, Some rules -> Some (List.rev_append rules acc)
| _ -> None)
(Some []) stmts
let element_node (stmt : statement) (rules : rule list) : rule =
match (stmt, rules) with
| Rule r, _ -> r
| _, [] ->
{
selector = Selector.universal;
declarations = [];
nested = [];
merge_key = None;
}
| _, rules ->
let branches =
List.concat_map (fun (r : rule) -> Edge.selectors r.selector) rules
in
let selector =
match branches with [ sel ] -> sel | sels -> Selector.List sels
in
{
selector;
declarations = List.concat_map (fun (r : rule) -> r.declarations) rules;
nested = [];
merge_key = None;
}
let is_identity order =
let id = ref true in
Array.iteri
(fun i v -> if i <> Rule_graph.Node_id.to_int v then id := false)
order;
!id
let overlap_indegrees (arr : Declaration.declaration array) : int array =
let n = Array.length arr in
let indeg = Array.make n 0 in
for j = 0 to n - 1 do
for i = 0 to j - 1 do
if Shorthand.declarations_overlap arr.(i) arr.(j) then
indeg.(j) <- indeg.(j) + 1
done
done;
indeg
let ready_min ~emitted ~indeg ~keys =
let best = ref (-1) in
for i = 0 to Array.length keys - 1 do
if (not emitted.(i)) && indeg.(i) = 0 then
if
!best < 0
||
let c = String.compare keys.(i) keys.(!best) in
c < 0 || (c = 0 && i < !best)
then best := i
done;
!best
let canonical_declarations (decls : Declaration.declaration list) :
Declaration.declaration list =
match decls with
| [] | [ _ ] -> decls
| _ ->
let arr = Array.of_list decls in
let n = Array.length arr in
let keys =
Array.map
(fun d -> Pp.to_string ~minify:true Declaration.pp_declaration d)
arr
in
let indeg = overlap_indegrees arr in
let emitted = Array.make n false in
let order = Array.make n 0 in
let changed = ref false in
for pos = 0 to n - 1 do
let b = ready_min ~emitted ~indeg ~keys in
if b <> pos then changed := true;
emitted.(b) <- true;
order.(pos) <- b;
for j = b + 1 to n - 1 do
if (not emitted.(j)) && Shorthand.declarations_overlap arr.(b) arr.(j)
then indeg.(j) <- indeg.(j) - 1
done
done;
if not !changed then decls
else Array.to_list (Array.map (fun i -> arr.(i)) order)
let sort_run ?parent changed (run : (statement * rule list) list) :
statement list =
let arr = Array.of_list run in
let n = Array.length arr in
if n < 2 then List.map fst run
else
let g =
Rule_graph.of_rules ?parent
(List.map (fun (stmt, rules) -> element_node stmt rules) run)
in
let keys =
Array.map
(fun (stmt, _) -> Pp.to_string ~minify:true pp_stylesheet [ stmt ])
arr
in
let ranked = Array.init n (fun i -> i) in
Array.sort
(fun a b ->
match String.compare keys.(a) keys.(b) with
| 0 -> Int.compare a b
| c -> c)
ranked;
let rank = Array.make n 0 in
Array.iteri (fun pos i -> rank.(i) <- pos) ranked;
let order =
Rule_graph.canonical_order_by g (fun node ->
rank.(Rule_graph.Node_id.to_int node))
in
if is_identity order then List.map fst run
else begin
changed := true;
Array.to_list
(Array.map (fun i -> fst arr.(Rule_graph.Node_id.to_int i)) order)
end
let branch_key sel = Pp.to_string ~minify:true Selector.pp sel
let shared_branches (stmts : statement list) : (string, unit) Hashtbl.t =
let counts = Hashtbl.create 64 in
List.iter
(function
| Rule r when r.nested = [] && r.merge_key = None ->
List.iter
(fun sel ->
let k = branch_key sel in
Hashtbl.replace counts k
(1 + Option.value ~default:0 (Hashtbl.find_opt counts k)))
(Edge.selectors r.selector)
| _ -> ())
stmts;
let shared = Hashtbl.create 16 in
Hashtbl.iter (fun k n -> if n > 1 then Hashtbl.replace shared k ()) counts;
shared
let expand_lists shared (stmt : statement) : statement list =
match stmt with
| Rule r when r.nested = [] && r.merge_key = None -> (
match Edge.selectors r.selector with
| [] | [ _ ] -> [ stmt ]
| branches
when List.exists
(fun sel -> Hashtbl.mem shared (branch_key sel))
branches ->
List.map (fun selector -> Rule { r with selector }) branches
| _ -> [ stmt ])
| _ -> [ stmt ]
let coalesced_declarations decls = Shorthand.deduplicate_declarations decls
type scan = {
graph : Rule_graph.t;
arr : (statement * rule list) array;
members : int list array;
alive : bool array;
mutable merged_any : bool;
}
let interval_clear scan ~lo ~hi mems =
let node i = Rule_graph.Node_id.of_int_exn i in
let ok = ref true in
for m = lo + 1 to hi - 1 do
if
List.exists
(fun a -> Rule_graph.conflict scan.graph (node m) (node a))
mems
then ok := false
done;
!ok
let merge scan changed ~from ~into =
(match (scan.arr.(from), scan.arr.(into)) with
| (Rule rf, _), (Rule ri, _) ->
let earlier, later = if from < into then (rf, ri) else (ri, rf) in
let merged =
{
earlier with
declarations =
canonical_declarations
(coalesced_declarations
(earlier.declarations @ later.declarations));
}
in
scan.arr.(into) <- (Rule merged, [ merged ])
| _ -> assert false);
scan.members.(into) <- scan.members.(from) @ scan.members.(into);
scan.alive.(from) <- false;
scan.merged_any <- true;
changed := true
let try_merge scan changed ~last ~key i j =
if interval_clear scan ~lo:i ~hi:j scan.members.(i) then begin
merge scan changed ~from:i ~into:j;
Hashtbl.replace last key j
end
else if interval_clear scan ~lo:i ~hi:j scan.members.(j) then
merge scan changed ~from:j ~into:i
else Hashtbl.replace last key j
let coalesce ?parent changed (run : (statement * rule list) list) :
(statement * rule list) list =
match run with
| [] | [ _ ] -> run
| _ ->
let arr = Array.of_list run in
let n = Array.length arr in
let graph =
Rule_graph.of_rules ?parent
(List.map (fun (stmt, rules) -> element_node stmt rules) run)
in
let scan =
{
graph;
arr;
members = Array.init n (fun i -> [ i ]);
alive = Array.make n true;
merged_any = false;
}
in
let selector_key i =
match arr.(i) with
| Rule r, _ when r.merge_key = None ->
Some (Pp.to_string ~minify:true Selector.pp r.selector)
| _ -> None
in
let last = Hashtbl.create 16 in
for j = 0 to n - 1 do
match selector_key j with
| None -> ()
| Some key -> (
match Hashtbl.find_opt last key with
| Some i when scan.alive.(i) ->
try_merge scan changed ~last ~key i j
| _ -> Hashtbl.replace last key j)
done;
if not scan.merged_any then run
else Array.to_list arr |> List.filteri (fun i _ -> scan.alive.(i))
let rec settle ?parent changed (run : (statement * rule list) list) :
statement list =
let stmts = sort_run ?parent changed run in
let sorted =
List.filter_map
(fun s ->
match element_rules s with
| Some rules -> Some (s, rules)
| None -> None)
stmts
in
let coalesced = coalesce ?parent changed sorted in
if coalesced == sorted then stmts else settle ?parent changed coalesced
let rec recurse ~(parent : Selector.t option) changed (stmt : statement) :
statement =
let here b = canonicalize_block ~parent changed b in
match stmt with
| Rule r ->
let child_parent =
match parent with
| None -> r.selector
| Some p -> Nest.combine p r.selector
in
let nested =
canonicalize_block ~parent:(Some child_parent) changed r.nested
in
let declarations = canonical_declarations r.declarations in
if declarations != r.declarations then changed := true;
if nested == r.nested && declarations == r.declarations then stmt
else Rule { r with declarations; nested }
| Layer (n, b) -> Layer (n, here b)
| Media (m, b) -> Media (m, here b)
| Container (n, c, b) -> Container (n, c, here b)
| Supports (s, b) -> Supports (s, here b)
| Moz_document (c, b) -> Moz_document (c, here b)
| When (c, b) -> When (c, here b)
| Else (c, b) -> Else (c, here b)
| Starting_style b -> Starting_style (here b)
| Origin (o, b) -> Origin (o, here b)
| Scope (s, e, b) -> Scope (s, e, here b)
| other -> other
and canonicalize_block ~parent changed (stmts : statement list) : statement list
=
let stmts = List.map (recurse ~parent changed) stmts in
let shared = shared_branches stmts in
let expanded = List.concat_map (expand_lists shared) stmts in
if List.compare_lengths expanded stmts <> 0 then changed := true;
let rec go = function
| [] -> []
| stmt :: rest -> (
match element_rules stmt with
| Some rules ->
let rec take acc = function
| s :: r as l -> (
match element_rules s with
| Some rs -> take ((s, rs) :: acc) r
| None -> (List.rev acc, l))
| [] -> (List.rev acc, [])
in
let run, rest = take [ (stmt, rules) ] rest in
settle ?parent changed run @ go rest
| None -> stmt :: go rest)
in
go expanded
let canonicalize (stmts : statement list) : statement list =
let changed = ref false in
let result =
canonicalize_block ~parent:(None : Selector.t option) changed stmts
in
if !changed then result else stmts