-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathCss_to_runtime.re
More file actions
580 lines (546 loc) · 17.1 KB
/
Css_to_runtime.re
File metadata and controls
580 lines (546 loc) · 17.1 KB
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
open Styled_ppx_css_parser.Ast;
module Helper = Ppxlib.Ast_helper;
module Builder = Ppxlib.Ast_builder.Default;
exception Empty_buffer(string);
module CSS = {
/* This is the public API of the CSS module */
let ident = (~loc, name) =>
{
txt: Ldot(Lident("CSS"), name),
loc,
}
|> Builder.pexp_ident(~loc);
let selectorMany = (~loc) => ident(~loc, "selectorMany");
let media = (~loc) => ident(~loc, "media");
let global = (~loc) => ident(~loc, "global");
let label = (~loc) => ident(~loc, "label");
let style = (~loc) => ident(~loc, "style");
let keyframes = (~loc) => ident(~loc, "keyframes");
};
let string_to_const = (~loc, s) => {
switch (File.get()) {
| Some(ReScript) =>
Helper.Exp.constant(
~loc,
~attrs=Platform_attributes.template(~loc),
Helper.Const.string(~quotation_delimiter="*j", s),
)
| Some(Reason)
| _ =>
Helper.Exp.constant(
~loc,
Helper.Const.string(~quotation_delimiter="js", s),
)
};
};
let render_variable = (~loc, v) => {
Helper.Exp.ident({
loc,
txt: v |> String.concat(".") |> Ppxlib.Longident.parse,
});
};
let source_code_of_loc = (loc: Ppxlib.Location.t) => {
let {loc_start, loc_end, _} = loc;
switch (Styled_ppx_css_parser.Driver.last_buffer^) {
| Some(buffer: Sedlexing.lexbuf) =>
/* TODO: pos_offset is hardcoded to 0, unsure about the effects */
let pos_offset = 0;
let loc_start = loc_start.pos_cnum - pos_offset;
let loc_end = loc_end.pos_cnum - pos_offset;
Sedlexing.Latin1.sub_lexeme(buffer, loc_start, loc_end - loc_start);
| None => raise(Empty_buffer("last buffer not set"))
};
};
let concat = (~loc, expr, acc) => {
let concat_fn =
{
txt: Lident("^"),
loc,
}
|> Helper.Exp.ident(~loc);
Helper.Exp.apply(~loc, concat_fn, [(Nolabel, expr), (Nolabel, acc)]);
};
let rec render_at_rule = (~loc, at_rule: at_rule) => {
let (at_rule_name, at_rule_name_loc) = at_rule.name;
let at_rule_name_loc =
Styled_ppx_css_parser.Parser_location.update_pos_lnum(
{
...at_rule_name_loc,
loc_start: {
...at_rule_name_loc.loc_start,
pos_bol: at_rule_name_loc.loc_end.pos_bol,
pos_cnum:
at_rule_name_loc.loc_end.pos_cnum - String.length(at_rule_name),
pos_lnum: at_rule_name_loc.loc_end.pos_lnum,
},
},
loc,
);
switch (at_rule_name) {
| "media" => render_media_query(~loc, at_rule)
| "container" => render_container_query(~loc, at_rule)
| "keyframes" =>
Error.expr(
~loc=at_rule_name_loc,
"@keyframes should be defined with %%keyframe(...)",
)
| "charset" as n
| "import" as n
| "namespace" as n
| "supports" as n
| "page" as n
| "font-face" as n
| "counter-style" as n
| "font-feature-values" as n
| "swash" as n
| "ornaments" as n
| "annotation" as n
| "stylistic" as n
| "styleset" as n
| "character-variant" as n
| "property" as n
| "font-palette-values" as n
| "layer" as n
| "scope" as n
| "starting-style" as n
| /* Experimental */ "color-profile" as n
| /* Experimental */ "viewport" as n
| /* Deprecated */ "document" as n =>
Error.expr(
~loc=at_rule_name_loc,
Printf.sprintf("At-rule @%s is not supported in styled-ppx", n),
)
| n => Error.expr(~loc=at_rule_name_loc, Printf.sprintf("Unknown @%s ", n))
};
}
and render_media_query = (~loc, at_rule: at_rule) => {
let (_, at_rule_prelude_loc) = at_rule.prelude;
let parse_condition = {
let prelude = source_code_of_loc(at_rule_prelude_loc) |> String.trim;
Css_property_parser.Parser.parse(
Css_property_parser.Parser.media_query_list,
prelude,
)
|> Result.map(_ => prelude);
};
let (delimiter, attrs) =
Platform_attributes.string_delimiter(~loc=at_rule.loc);
switch (parse_condition) {
| Error(error_msg) =>
Error.expr(
~loc=
Styled_ppx_css_parser.Parser_location.update_pos_lnum(
at_rule_prelude_loc,
loc,
),
error_msg,
)
| Ok(conditions) =>
let query =
conditions
|> String_interpolation.transform(~attrs, ~delimiter, ~loc=at_rule.loc);
let rules =
switch (at_rule.block) {
| Empty => Builder.pexp_array(~loc=at_rule.loc, [])
| Rule_list(declaration) =>
render_declarations(~loc, declaration)
|> Builder.pexp_array(~loc=at_rule.loc)
| Stylesheet(stylesheet) =>
render_declarations(~loc, stylesheet)
|> Builder.pexp_array(~loc=at_rule.loc)
};
Helper.Exp.apply(
~loc=at_rule.loc,
CSS.media(~loc=at_rule.loc),
[(Nolabel, query), (Nolabel, rules)],
);
};
}
and render_container_query = (~loc, at_rule: at_rule) => {
let (_, at_rule_prelude_loc) = at_rule.prelude;
let parse_condition = {
let prelude = source_code_of_loc(at_rule_prelude_loc) |> String.trim;
Css_property_parser.Parser.parse(
Css_property_parser.Parser.container_condition_list,
prelude,
)
|> Result.map(_ => prelude);
};
let (delimiter, attrs) =
Platform_attributes.string_delimiter(~loc=at_rule.loc);
switch (parse_condition) {
| Error(error_msg) =>
Error.expr(
~loc=
Styled_ppx_css_parser.Parser_location.update_pos_lnum(
at_rule_prelude_loc,
loc,
),
error_msg,
)
| Ok(conditions) =>
let query =
[
String_interpolation.transform(
~attrs,
~delimiter,
~loc=at_rule.loc,
"@container " ++ conditions,
),
]
|> Builder.pexp_array(~loc=at_rule.loc);
let rules =
switch (at_rule.block) {
| Empty => Builder.pexp_array(~loc=at_rule.loc, [])
| Rule_list(declaration) =>
render_declarations(~loc, declaration)
|> Builder.pexp_array(~loc=at_rule.loc)
| Stylesheet(stylesheet) =>
render_declarations(~loc, stylesheet)
|> Builder.pexp_array(~loc=at_rule.loc)
};
Helper.Exp.apply(
~loc=at_rule.loc,
CSS.selectorMany(~loc=at_rule.loc),
[(Nolabel, query), (Nolabel, rules)],
);
};
}
and render_declaration = (~loc: Ppxlib.location, d: declaration) => {
let (property, name_loc) = d.name;
let (_valueList, value_loc) = d.value;
let (important, _) = d.important;
/* String.trim is a hack, location should be correct and not contain any whitespace */
let value_source = source_code_of_loc(value_loc) |> String.trim;
let declaration_location =
Styled_ppx_css_parser.Parser_location.update_pos_lnum(
{
let offset =
value_loc.loc_start.pos_lnum == 1
? loc.loc_start.pos_cnum - loc.loc_start.pos_bol + 1 : 0;
{
...value_loc,
loc_start: {
...value_loc.loc_start,
pos_cnum: value_loc.loc_start.pos_cnum + offset,
},
loc_end: {
...value_loc.loc_end,
pos_cnum: value_loc.loc_end.pos_cnum + offset,
},
};
},
loc,
);
let property_location =
Styled_ppx_css_parser.Parser_location.update_pos_lnum(
{
let offset =
name_loc.loc_end.pos_lnum == 1
? loc.loc_start.pos_cnum - loc.loc_start.pos_bol + 1 : 0;
{
...name_loc,
loc_start: {
...name_loc.loc_start,
pos_lnum: name_loc.loc_end.pos_lnum,
pos_bol: name_loc.loc_end.pos_bol,
pos_cnum:
name_loc.loc_end.pos_cnum + offset - String.length(property),
},
loc_end: {
...name_loc.loc_end,
pos_cnum: name_loc.loc_end.pos_cnum + offset,
},
};
},
loc,
);
switch (
Property_to_runtime.render(
~loc=declaration_location,
property,
value_source,
important,
)
) {
| Ok(exprs) => exprs
| Error(`Property_not_found) => [
Error.expr(
~loc=property_location,
"Unknown property '" ++ property ++ "'",
),
]
| Error(`Impossible_state) => [
Error.expr(
~loc=declaration_location,
"This is a broken state of the CSS parser and probably a bug. Please report back!",
),
]
| Error(`Invalid_value(reason)) => [
Error.expr(
~loc=declaration_location,
Format.sprintf(
"@[Property@ '%s'@ has@ an@ invalid@ value:@ '%s',@ %s@]",
property,
value_source,
reason,
),
),
]
};
}
and render_declarations = (~loc: Ppxlib.location, (ds, _d_loc)) => {
ds
|> List.concat_map(declaration =>
switch (declaration) {
| Declaration(decl) => render_declaration(~loc, decl)
| At_rule(ar) => [render_at_rule(~loc, ar)]
| Style_rule(style_rules) => [render_style_rule(~loc, style_rules)]
}
);
}
and render_variable_as_string = variable => {
"$(" ++ String.concat(".", variable) ++ ")";
}
and render_selector = (~loc, selector: selector) => {
let rec render_simple_selector =
fun
| Ampersand => "&"
| Universal => "*"
| Type(v) => v
| Subclass(v) => render_subclass_selector(v)
| Variable(v) => render_variable_as_string(v)
| Percentage(v) => Printf.sprintf("%s%%", v)
and render_subclass_selector =
fun
| Id(v) => Printf.sprintf("#%s", v)
| Class(v) => Printf.sprintf(".%s", v)
| ClassVariable(v) => "." ++ render_variable_as_string(v)
| Attribute(Attr_value(v)) => Printf.sprintf("[%s]", v)
| Attribute(To_equal({name, kind, value})) => {
let value =
switch (value) {
| Attr_ident(ident) => ident
| Attr_string(ident) => {|"|} ++ ident ++ {|"|}
};
Printf.sprintf("[%s%s%s]", name, kind, value);
}
| Pseudo_class(psc) => render_pseudo_selector(psc)
and render_nth =
fun
| Even => "even"
| Odd => "odd"
| A(a) => Int.to_string(a)
| AN(an) when an == 1 => "n"
| AN(an) when an == (-1) => "-n"
| AN(an) => Int.to_string(an) ++ "n"
| ANB(a, op, b) when a == 1 => "n" ++ op ++ Int.to_string(b)
| ANB(a, op, b) when a == (-1) => "-n" ++ op ++ Int.to_string(b)
| ANB(a, op, b) => Int.to_string(a) ++ "n" ++ op ++ Int.to_string(b)
and render_nth_payload =
fun
| Nth(nth) => render_nth(nth)
| NthSelector(v) =>
v |> List.map(render_complex_selector) |> String.concat(", ")
and render_pseudoclass =
fun
| PseudoIdent(i) => ":" ++ i
| NthFunction({name, payload: (payload, _loc)}) =>
":"
++ name
++ "("
++ (render_nth_payload(payload) |> String.trim)
++ ")"
| Function({name, payload: (payload, _loc)}) => {
":"
++ name
++ "("
++ (
render_selectors(~loc, payload)
|> String.concat(", ")
|> String.trim
)
++ ")";
}
and render_pseudo_selector =
fun
| Pseudoelement(v) => "::" ++ v
| Pseudoclass(pc) => render_pseudoclass(pc)
and render_compound_selector = compound_selector => {
let simple_selector =
Option.fold(
~none="",
~some=render_simple_selector,
compound_selector.type_selector,
);
let subclass_selectors =
List.map(render_subclass_selector, compound_selector.subclass_selectors)
|> String.concat("");
let pseudo_selectors =
List.map(render_pseudo_selector, compound_selector.pseudo_selectors)
|> String.concat("");
simple_selector ++ subclass_selectors ++ pseudo_selectors;
}
and render_complex_selector = complex => {
switch (complex) {
| Combinator({left, right}) =>
let left = render_selector(~loc, left);
let right = render_right_combinator(right);
left ++ right;
| Selector(selector) => render_selector(~loc, selector)
};
}
and render_right_combinator = right => {
right
|> List.map(((combinator, selector)) => {
Option.fold(~none=" ", ~some=o => " " ++ o ++ " ", combinator)
++ render_selector(~loc, selector)
})
|> String.concat("");
}
and render_relative_selector = ({combinator, complex_selector}) => {
Option.fold(~none="", ~some=o => o ++ " ", combinator)
++ render_complex_selector(complex_selector);
};
switch (selector) {
| SimpleSelector(simple) => simple |> render_simple_selector
| ComplexSelector(complex) => complex |> render_complex_selector
| CompoundSelector(compound) => compound |> render_compound_selector
| RelativeSelector(relative) => relative |> render_relative_selector
};
}
and render_selectors = (~loc, selectors) => {
selectors
|> List.map(((selector, _loc)) => render_selector(~loc, selector));
}
and render_style_rule = (~loc, rule: style_rule) => {
let (prelude, prelude_loc) = rule.prelude;
let selector_location =
Styled_ppx_css_parser.Parser_location.intersection(loc, prelude_loc);
let selector_expr =
render_declarations(~loc, rule.block)
|> Builder.pexp_array(~loc=selector_location);
let (delimiter, attrs) =
Platform_attributes.string_delimiter(~loc=selector_location);
let selector_name =
prelude
|> render_selectors(~loc=selector_location)
|> List.map(String.trim)
|> List.map(
String_interpolation.transform(
~attrs,
~delimiter,
~loc=selector_location,
),
)
|> Builder.pexp_array(~loc=selector_location);
Helper.Exp.apply(
~loc=selector_location,
CSS.selectorMany(~loc=selector_location),
[(Nolabel, selector_name), (Nolabel, selector_expr)],
);
};
let addLabel = (~loc, label, emotionExprs) => [
Helper.Exp.apply(
CSS.label(~loc),
[(Nolabel, Helper.Exp.constant(Pconst_string(label, loc, None)))],
),
...emotionExprs,
];
let render_style_call = (~loc, declaration_list) => {
Helper.Exp.apply(~loc, CSS.style(~loc), [(Nolabel, declaration_list)]);
};
let render_keyframes = (~loc, declarations: rule_list) => {
let (declarations, declarations_loc) = declarations;
let invalid_selector = {|
keyframe selector can be `from`, `to` or <percentage>`
Like following:
[%keyframe "
from { opacity: 1; }
to { opacity: 0; }
"];
|};
let invalid_percentage_value = value =>
Printf.sprintf(
"'%s' isn't valid. Only accept percentage with integers",
value,
);
let invalid_prelude_value = value =>
Printf.sprintf("'%s' isn't a valid keyframe value", value);
let invalid_prelude_value_opaque = "This isn't a valid keyframe value";
let render_select_as_keyframe = prelude => {
switch (prelude |> fst) {
| SimpleSelector(selector) =>
switch (selector) {
// https://drafts.csswg.org/css-animations/#keyframes
// `from` is equivalent to the value 0%
| Type(v) when v == "from" => Builder.eint(~loc=declarations_loc, 0)
// `to` is equivalent to the value 100%
| Type(v) when v == "to" => Builder.eint(~loc=declarations_loc, 100)
| Type(t) =>
Error.expr(~loc=declarations_loc, invalid_prelude_value(t))
| Percentage(n) =>
switch (int_of_string_opt(n)) {
| Some(n) when n >= 0 && n <= 100 =>
Builder.eint(~loc=declarations_loc, n)
| _ => Error.expr(~loc=declarations_loc, invalid_percentage_value(n))
}
| Ampersand =>
Error.expr(~loc=declarations_loc, invalid_prelude_value("&"))
| Universal =>
Error.expr(~loc=declarations_loc, invalid_prelude_value("*"))
| Subclass(_) =>
Error.expr(~loc=declarations_loc, invalid_prelude_value_opaque)
| Variable(_) =>
Error.expr(~loc=declarations_loc, invalid_prelude_value_opaque)
}
| _ => Error.expr(~loc=declarations_loc, invalid_selector)
};
};
let keyframes =
declarations
|> List.map(declaration => {
switch (declaration) {
| Style_rule({prelude: (prelude, _), block, loc: style_loc}) =>
let percentages = prelude |> List.map(render_select_as_keyframe);
let rules =
render_declarations(~loc, block)
|> Builder.pexp_array(~loc=declarations_loc);
percentages
|> List.map(p => Builder.pexp_tuple(~loc=style_loc, [p, rules]));
| _ => [Error.expr(~loc=declarations_loc, invalid_selector)]
}
})
|> List.flatten
|> Builder.pexp_array(~loc=declarations_loc);
Builder.eapply(
~loc=declarations_loc,
CSS.keyframes(~loc=declarations_loc),
[keyframes],
);
};
let render_global = (~loc, (ruleList, stylesheet_loc): stylesheet) => {
let onlyStyleRulesAndAtRulesSupported = {|Declarations does not make sense in global styles. Global should consists of style rules or at-rules (e.g @media, @print, etc.)
If your intent is to apply the declaration to all elements, use the universal selector
* {
/* Your declarations here */
}|};
let styles =
ruleList
|> List.map(rule => {
switch (rule) {
| Style_rule(style_rule) => render_style_rule(~loc, style_rule)
| At_rule(at_rule) => render_at_rule(~loc, at_rule)
| _ =>
Error.expr(~loc=stylesheet_loc, onlyStyleRulesAndAtRulesSupported)
}
})
|> Builder.pexp_array(~loc=stylesheet_loc);
let expr =
Helper.Exp.apply(
~loc=stylesheet_loc,
CSS.global(~loc=stylesheet_loc),
[(Nolabel, styles)],
);
[%expr ignore([%e expr])];
};