Source file padding.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
module Css = Cascade.Css

module Handler = struct
  open Style
  open Css

  type padding_value =
    | Standard of spacing
    | Arbitrary of string * Css.length (* p-[4px], raw kept for round-trip *)
    | Arbitrary_var of string * string (* raw inner, then the var() text *)
    | Arbitrary_raw of string * string
  (* p-[foo]: padding writes one longhand per side, so a bracket no length
     reader took still names that longhand and the value is forwarded verbatim -
     Tailwind's token-stream contract. The browser discards the declaration; the
     selector is what the class was for. *)

  type t = {
    axis : [ `All | `X | `Y | `T | `R | `B | `L | `S | `E | `Bs | `Be ];
    value : padding_value;
  }

  let name = "padding"
  let priority _ = 23

  let to_class { axis; value } =
    let prefix =
      match axis with
      | `All -> "p-"
      | `X -> "px-"
      | `Y -> "py-"
      | `T -> "pt-"
      | `R -> "pr-"
      | `B -> "pb-"
      | `L -> "pl-"
      | `S -> "ps-"
      | `E -> "pe-"
      | `Bs -> "pbs-"
      | `Be -> "pbe-"
    in
    let value_suffix =
      match value with
      | Standard s -> Spacing.pp_spacing_suffix s
      | Arbitrary (raw, _) -> "[" ^ raw ^ "]"
      | Arbitrary_var (raw, _) | Arbitrary_raw (raw, _) -> "[" ^ raw ^ "]"
    in
    prefix ^ value_suffix

  let v_spacing ?theme (prop : length -> declaration) (s : Style.spacing) =
    let decl, len = Spacing.to_decl_len ?theme s in
    style (Option.to_list decl @ [ prop len ])

  let vs_spacing ?theme (prop : length list -> declaration) (s : Style.spacing)
      =
    let decl, len = Spacing.to_decl_len ?theme s in
    style (Option.to_list decl @ [ prop [ len ] ])

  let spacing_value_order = function
    | `Px -> 1
    | `Full -> 10000
    | `Named _ -> 20000
    | `Rem f ->
        let units = f /. 0.25 in
        int_of_float (units *. 10.)

  let value_order = function
    | Standard s -> spacing_value_order s
    | Arbitrary _ -> 20000
    | Arbitrary_var _ -> 20000
    | Arbitrary_raw _ -> 20000

  (* The longhand each axis names, for a value no typed setter can hold. *)
  let property_of_axis = function
    | `All -> "padding"
    | `X -> "padding-inline"
    | `Y -> "padding-block"
    | `T -> "padding-top"
    | `R -> "padding-right"
    | `B -> "padding-bottom"
    | `L -> "padding-left"
    | `S -> "padding-inline-start"
    | `E -> "padding-inline-end"
    | `Bs -> "padding-block-start"
    | `Be -> "padding-block-end"

  let raw_style axis value =
    style
      (Option.to_list (Parse.opaque_declaration (property_of_axis axis) value))

  let apply_prop_list ?theme (prop : length list -> declaration) value =
    match value with
    | Standard s -> vs_spacing ?theme prop s
    | Arbitrary (_, len) -> style [ prop [ len ] ]
    | Arbitrary_raw _ -> style []
    | Arbitrary_var (_, var_str) ->
        let bare_name = Parse.extract_var_name var_str in
        style [ prop [ Var (Var.bracket bare_name) ] ]

  let apply_prop ?theme (prop : length -> declaration) value =
    match value with
    | Standard s -> v_spacing ?theme prop s
    | Arbitrary (_, len) -> style [ prop len ]
    (* [to_style] answers the raw case before either applier is reached. *)
    | Arbitrary_raw _ -> style []
    | Arbitrary_var (_, var_str) ->
        let bare_name = Parse.extract_var_name var_str in
        style [ prop (Var (Var.bracket bare_name)) ]

  let rec to_style theme t =
    match t.value with
    | Arbitrary_raw (_, value) -> raw_style t.axis value
    | Standard _ | Arbitrary _ | Arbitrary_var _ -> to_style_typed theme t

  and to_style_typed theme t =
    let apply_prop_list prop value = apply_prop_list ~theme prop value in
    let apply_prop prop value = apply_prop ~theme prop value in
    match t.axis with
    | `All -> apply_prop_list padding t.value
    | `X -> apply_prop_list padding_inline t.value
    | `Y -> apply_prop_list padding_block t.value
    | `T -> apply_prop padding_top t.value
    | `R -> apply_prop padding_right t.value
    | `B -> apply_prop padding_bottom t.value
    | `L -> apply_prop padding_left t.value
    | `S -> apply_prop padding_inline_start t.value
    | `E -> apply_prop padding_inline_end t.value
    | `Bs -> apply_prop padding_block_start t.value
    | `Be -> apply_prop padding_block_end t.value

  let suborder { axis; value } =
    let side_offset =
      match axis with
      | `All -> 0
      | `X -> 100_000
      | `Y -> 200_000
      | `S -> 300_000
      | `E -> 400_000
      | `Bs -> 500_000
      | `Be -> 600_000
      | `T -> 700_000
      | `R -> 800_000
      | `B -> 900_000
      | `L -> 1_000_000
    in
    side_offset + value_order value

  let parse_arbitrary s : padding_value option =
    if Parse.is_bracket_value s then
      let inner = Parse.bracket_inner s in
      (* A data-type hint chooses the longhand and says nothing about the value.
         Padding writes one longhand per side, so every hint lands here and the
         readers below are handed what follows it; [inner] keeps the hint
         because the class name has to. *)
      match Parse.value_after_hint inner with
      | None -> None
      | Some value -> (
          if Parse.is_var value then Some (Arbitrary_var (inner, value))
          else
            (* Full length grammar (percent, container units, calc), raw kept
               for round-trip. *)
            match Parse.arbitrary_length value with
            | Some l -> Some (Arbitrary (inner, l))
            | None ->
                (* No length reader took it, so it goes to the longhand the
                   class names, which is this family's last resort. *)
                Option.map
                  (fun raw -> Arbitrary_raw (inner, raw))
                  (Parse.arbitrary_declaration_value inner))
    else None

  let padding_axis_of_prefix = function
    | "p" -> Some `All
    | "px" -> Some `X
    | "py" -> Some `Y
    | "pt" -> Some `T
    | "pr" -> Some `R
    | "pb" -> Some `B
    | "pl" -> Some `L
    | "ps" -> Some `S
    | "pe" -> Some `E
    | "pbs" -> Some `Bs
    | "pbe" -> Some `Be
    | _ -> None

  let of_class theme class_name =
    let parts = Parse.split_class class_name in
    match parts with
    | [ prefix; arb ] when String.starts_with ~prefix:"[" arb -> (
        match (padding_axis_of_prefix prefix, parse_arbitrary arb) with
        | Some axis, Some value -> Ok { axis; value }
        | _ -> Utility.unclaimed)
    | _ -> (
        match Spacing.parse_class_parts parts with
        | Some (true, _, _) ->
            (* Padding doesn't support negative values *)
            Utility.unclaimed
        | Some (false, prefix, value) -> (
            if Spacing.is_margin_prefix prefix then Utility.unclaimed
            else
              match Spacing.axis_of_prefix prefix with
              | None -> Utility.unclaimed
              | Some axis -> (
                  match
                    Spacing.parse_value_string ~theme ~allow_auto:false value
                  with
                  | None -> Utility.unclaimed
                  | Some (#spacing as spacing_val) ->
                      Ok { axis; value = Standard spacing_val }
                  | Some `Auto -> Utility.unclaimed))
        | None -> Utility.unclaimed)

  let examples =
    [
      { axis = `All; value = Standard (`Rem 1.) };
      { axis = `X; value = Standard (`Rem 1.) };
      { axis = `Y; value = Standard (`Rem 1.) };
      { axis = `T; value = Standard (`Rem 1.) };
      { axis = `R; value = Standard (`Rem 1.) };
      { axis = `B; value = Standard (`Rem 1.) };
      { axis = `L; value = Standard (`Rem 1.) };
      { axis = `S; value = Standard (`Rem 1.) };
      { axis = `E; value = Standard (`Rem 1.) };
      { axis = `Bs; value = Standard (`Rem 1.) };
      { axis = `Be; value = Standard (`Rem 1.) };
    ]
end

open Handler
module Utility_factory = Utility.Make (Handler)

let utility axis value = Utility_factory.v { axis; value }

(* Padding has no negative form in Tailwind, and the class printer writes a
   size's absolute value, so a negative one would come back as a different,
   valid utility. It is refused instead, as a shade the palette lacks is. *)
let checked_size name n =
  if n < 0 then
    invalid_arg (name ^ ": padding has no negative size, got " ^ string_of_int n)
  else Spacing.int n

let checked_size' name n =
  if n < 0. then
    invalid_arg
      (name ^ ": padding has no negative size, got " ^ Float.to_string n)
  else Spacing.float n

let p n = utility `All (Handler.Standard (checked_size "p" n))
let px n = utility `X (Handler.Standard (checked_size "px" n))
let py n = utility `Y (Handler.Standard (checked_size "py" n))
let pt n = utility `T (Handler.Standard (checked_size "pt" n))
let pr n = utility `R (Handler.Standard (checked_size "pr" n))
let pb n = utility `B (Handler.Standard (checked_size "pb" n))
let pl n = utility `L (Handler.Standard (checked_size "pl" n))
let p' n = utility `All (Handler.Standard (checked_size' "p'" n))
let px' n = utility `X (Handler.Standard (checked_size' "px'" n))
let py' n = utility `Y (Handler.Standard (checked_size' "py'" n))
let pt' n = utility `T (Handler.Standard (checked_size' "pt'" n))
let pr' n = utility `R (Handler.Standard (checked_size' "pr'" n))
let pb' n = utility `B (Handler.Standard (checked_size' "pb'" n))
let pl' n = utility `L (Handler.Standard (checked_size' "pl'" n))
let p_px = utility `All (Handler.Standard `Px)
let p_full = utility `All (Handler.Standard `Full)
let px_px = utility `X (Handler.Standard `Px)
let px_full = utility `X (Handler.Standard `Full)
let py_px = utility `Y (Handler.Standard `Px)
let py_full = utility `Y (Handler.Standard `Full)
let pt_px = utility `T (Handler.Standard `Px)
let pt_full = utility `T (Handler.Standard `Full)
let pr_px = utility `R (Handler.Standard `Px)
let pr_full = utility `R (Handler.Standard `Full)
let pb_px = utility `B (Handler.Standard `Px)
let pb_full = utility `B (Handler.Standard `Full)
let pl_px = utility `L (Handler.Standard `Px)
let pl_full = utility `L (Handler.Standard `Full)