aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/format/css/selector.lux
blob: df2df8fbf03dac72516179e4d032563ea66bcb88 (plain)
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
(.require
 [library
  [lux (.except Label or and for same? not)
   ["[0]" locale (.only Locale)]
   [data
    ["[0]" text (.only)
     ["%" \\format (.only format)]]]
   [math
    [number
     ["i" int]]]
   [meta
    [macro
     ["[0]" template]]
    [type
     ["[0]" primitive (.except def)]]]]]
 ["[0]" //
  ["[1][0]" id (.only ID)]
  ["[1][0]" class (.only Class)]])

(type .public Label Text)

(type .public Tag Label)
(type .public Attribute Label)

(primitive.def .public (Generic brand) Any)

(with_template [<generic> <brand>]
  [(primitive.def <brand> Any)
   (type .public <generic> (Generic <brand>))]

  [Can_Chain Can_Chain']
  [Cannot_Chain Cannot_Chain']
  )

(primitive.def .public Unique Any)
(primitive.def .public Specific Any)
(primitive.def .public Composite Any)

(primitive.def .public (Selector kind)
  Text

  (def .public selector
    (-> (Selector Any) Text)
    (|>> representation))

  (def .public any
    (Selector Cannot_Chain)
    (abstraction "*"))

  (def .public tag
    (-> Tag (Selector Cannot_Chain))
    (|>> abstraction))

  (with_template [<name> <type> <prefix> <kind> <out>]
    [(def .public <name>
       (-> <type> (Selector <kind>))
       (|>> <out> (format <prefix>) abstraction))]

    [id ID "#" Unique //id.id]
    [class Class "." Can_Chain //class.class]
    )

  (with_template [<right> <left> <combinator>+]
    [(`` (with_template [<combinator> <name>]
           [(def .public (<name> right left)
              (-> (Selector <right>) (Selector <left>) (Selector Composite))
              (abstraction (format (representation left)
                                   <combinator>
                                   (representation right))))]

           (,, (template.spliced <combinator>+))))]

    [Can_Chain (Generic Any)
     [["" and]]]
    [Specific (Generic Any)
     [["" at]]]
    [Unique (Generic Any)
     [["" for]]]
    [Any Any
     [["," or]
      [" " in]
      [">" sub]
      ["+" next]
      ["~" later]]]
    )

  (type .public (Specializer kind)
    (-> (Selector kind) (Selector (Generic Any)) (Selector Composite)))

  (type .public Combinator
    (-> (Selector Any) (Selector Any) (Selector Composite)))

  (def .public (with? attribute)
    (-> Attribute (Selector Can_Chain))
    (abstraction (format "[" attribute "]")))

  (with_template [<check> <name>]
    [(def .public (<name> attribute value)
       (-> Attribute Text (Selector Can_Chain))
       (abstraction (format "[" attribute <check> value "]")))]

    ["=" same?]
    ["~=" has?]
    ["|=" has_start?]
    ["^=" starts?]
    ["$=" ends?]
    ["*=" contains?]
    )

  (with_template [<kind> <pseudo>+]
    [(`` (with_template [<name> <pseudo>]
           [(def .public <name>
              (Selector <kind>)
              (abstraction <pseudo>))]

           (,, (template.spliced <pseudo>+))))]

    [Can_Chain
     [[active ":active"]
      [checked ":checked"]
      [default ":default"]
      [disabled ":disabled"]
      [empty ":empty"]
      [enabled ":enabled"]
      [first_child ":first-child"]
      [first_of_type ":first-of-type"]
      [focused ":focus"]
      [hovered ":hover"]
      [in_range ":in-range"]
      [indeterminate ":indeterminate"]
      [invalid ":invalid"]
      [last_child ":last-child"]
      [last_of_type ":last-of-type"]
      [link ":link"]
      [only_of_type ":only-of-type"]
      [only_child ":only-child"]
      [optional ":optional"]
      [out_of_range ":out-of-range"]
      [read_only ":read-only"]
      [read_write ":read-write"]
      [required ":required"]
      [root ":root"]
      [target ":target"]
      [valid ":valid"]
      [visited ":visited"]]]
    
    [Specific
     [[after "::after"]
      [before "::before"]
      [first_letter "::first-letter"]
      [first_line "::first-line"]
      [placeholder "::placeholder"]
      [selection "::selection"]]]
    )

  (def .public (language locale)
    (-> Locale (Selector Can_Chain))
    (|> locale
        locale.code
        (text.enclosed ["(" ")"])
        (format ":lang")
        abstraction))

  (def .public not
    (-> (Selector Any) (Selector Can_Chain))
    (|>> representation
         (text.enclosed ["(" ")"])
         (format ":not")
         abstraction))

  (primitive.def .public Index
    Text

    (def .public index
      (-> Nat Index)
      (|>> %.nat abstraction))

    (with_template [<name> <index>]
      [(def .public <name> Index (abstraction <index>))]
      
      [odd "odd"]
      [even "even"]
      )

    (type .public Formula
      (Record
       [#constant Int
        #variable Int]))

    (def .public (formula input)
      (-> Formula Index)
      (let [(open "_[0]") input]
        (abstraction (format (if (i.< +0 _#variable)
                               (%.int _#variable)
                               (%.nat (.nat _#variable)))
                             (%.int _#constant)))))
    
    (with_template [<name> <pseudo>]
      [(def .public (<name> index)
         (-> Index (Selector Can_Chain))
         (|> (representation index)
             (text.enclosed ["(" ")"])
             (format <pseudo>)
             (abstraction Selector)))]

      [nth_child ":nth-child"]
      [nth_last_child ":nth-last-child"]
      [nth_of_type ":nth-of-type"]
      [nth_last_of_type ":nth-last-of-type"]
      )
    )
  )