aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/format/css/selector.lux
blob: f5a33e833b723d7cf3e0d5e3c449ec2a9367cda6 (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
(.module:
  [lux (#- and id is? not)
   [data
    ["." text
     format]]
   [type
    abstract]
   [world
    [internationalization
     ["." locale (#+ Locale)]]]])

(type: #export Tag Text)
(type: #export ID Text)
(type: #export Class Text)
(type: #export Attribute Text)

(abstract: #export Selector
  {}
  
  Text

  (def: #export selector
    (-> Selector Text)
    (|>> :representation))

  (def: #export any
    Selector
    (:abstraction "*"))

  (def: #export tag
    (-> Tag Selector)
    (|>> :abstraction))

  (do-template [<name> <type> <prefix>]
    [(def: #export <name>
       (-> <type> Selector)
       (|>> (format <prefix>) :abstraction))]

    [id ID "#"]
    [class Class "."]
    )

  (type: #export Combinator
    (-> Selector Selector Selector))

  (do-template [<name> <combinator>]
    [(def: #export (<name> left right)
       Combinator
       (:abstraction (format (:representation left)
                             <combinator>
                             (:representation right))))]

    [and ","]
    [in " "]
    [sub ">"]
    [after "+"]
    [later "~"]
    )

  (def: #export (with attribute)
    (-> Attribute Selector)
    (:abstraction (format "[" attribute "]")))

  (do-template [<name> <check>]
    [(def: #export (<name> attribute value base)
       (-> Attribute Text Selector Selector)
       (:abstraction (format (:representation base) "[" attribute <check> value "]")))]

    [is? "="]
    [has? "~="]
    [has-start? "|="]
    [starts? "^="]
    [ends? "$="]
    [contains? "*="]
    )

  (do-template [<name> <pseudo>]
    [(def: #export (<name> base)
       (-> Selector Selector)
       (:abstraction (format (:representation base) <pseudo>)))]

    [active ":active"]
    [after! "::after"]
    [before! "::before"]

    [checked ":checked"]
    [default ":default"]
    [disabled ":disabled"]
    [empty ":empty"]
    [enabled ":enabled"]
    [first-child ":first-child"]
    [first-letter "::first-letter"]
    [first-line "::first-line"]
    [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"]
    [placeholder "::placeholder"]
    [read-only ":read-only"]
    [read-write ":read-write"]
    [required ":required"]
    [root ":root"]
    [selection "::selection"]
    [target ":target"]
    [valid ":valid"]
    [visited ":visited"]
    )

  (def: #export (language locale base)
    (-> Locale Selector Selector)
    (|> locale
        locale.code
        (text.enclose ["(" ")"])
        (format (:representation base) ":lang")
        :abstraction))

  (def: #export not
    (-> Selector Selector)
    (|>> :representation
         (text.enclose ["(" ")"])
         (format ":not")
         :abstraction))

  (abstract: #export Index
    {}
    
    Text

    (def: #export simple
      (-> Nat Index)
      (|>> %n :abstraction))

    (do-template [<name> <index>]
      [(def: #export <name> Index (:abstraction <index>))]
      
      [odd "odd"]
      [even "even"]
      )

    (type: #export Formula
      {#constant Int
       #variable Int})

    (def: #export (formula input)
      (-> Formula Index)
      (let [(^slots [#constant #variable]) input]
        (:abstraction (format (if (i/< +0 variable)
                                (%i variable)
                                (%n (.nat variable)))
                              (%i constant)))))
    
    (do-template [<name> <pseudo>]
      [(def: #export (<name> index base)
         (-> Index Selector Selector)
         (|> (:representation index)
             (text.enclose ["(" ")"])
             (format (:representation Selector base) <pseudo>)
             (:abstraction Selector)))]

      [nth-child ":nth-child"]
      [nth-last-child ":nth-last-child"]
      [nth-last-of-type ":nth-last-of-type"]
      [nth-of-type ":nth-of-type"]
      )
    )
  )