/
Attrs.kt
339 lines (233 loc) · 10 KB
/
Attrs.kt
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
package org.jetbrains.compose.web.attributes
import org.jetbrains.compose.web.attributes.builders.saveControlledInputState
import org.jetbrains.compose.web.css.*
import org.jetbrains.compose.web.events.SyntheticSubmitEvent
import org.w3c.dom.HTMLAnchorElement
import org.w3c.dom.HTMLButtonElement
import org.w3c.dom.HTMLCanvasElement
import org.w3c.dom.HTMLFormElement
import org.w3c.dom.HTMLImageElement
import org.w3c.dom.HTMLInputElement
import org.w3c.dom.HTMLLabelElement
import org.w3c.dom.HTMLOptGroupElement
import org.w3c.dom.HTMLOptionElement
import org.w3c.dom.HTMLSelectElement
import org.w3c.dom.HTMLTableCellElement
import org.w3c.dom.HTMLTableColElement
import org.w3c.dom.HTMLTextAreaElement
fun AttrsScope<HTMLAnchorElement>.href(value: String) =
attr("href", value)
fun AttrsScope<HTMLAnchorElement>.target(value: ATarget = ATarget.Self) =
attr("target", value.targetStr)
fun AttrsScope<HTMLAnchorElement>.ref(value: ARel) =
attr("rel", value.relStr)
fun AttrsScope<HTMLAnchorElement>.ping(value: String) =
attr("ping", value)
fun AttrsScope<HTMLAnchorElement>.ping(vararg urls: String) =
attr("ping", urls.joinToString(" "))
fun AttrsScope<HTMLAnchorElement>.hreflang(value: String) =
attr("hreflang", value)
fun AttrsScope<HTMLAnchorElement>.download(value: String = "") =
attr("download", value)
/* Button attributes */
fun AttrsScope<HTMLButtonElement>.autoFocus() =
attr("autofocus", "")
fun AttrsScope<HTMLButtonElement>.disabled() =
attr("disabled", "")
fun AttrsScope<HTMLButtonElement>.form(formId: String) =
attr("form", formId)
fun AttrsScope<HTMLButtonElement>.formAction(url: String) =
attr("formaction", url)
fun AttrsScope<HTMLButtonElement>.formEncType(value: ButtonFormEncType) =
attr("formenctype", value.typeStr)
fun AttrsScope<HTMLButtonElement>.formMethod(value: ButtonFormMethod) =
attr("formmethod", value.methodStr)
fun AttrsScope<HTMLButtonElement>.formNoValidate() =
attr("formnovalidate", "")
fun AttrsScope<HTMLButtonElement>.formTarget(value: ButtonFormTarget) =
attr("formtarget", value.targetStr)
fun AttrsScope<HTMLButtonElement>.name(value: String) =
attr("name", value)
fun AttrsScope<HTMLButtonElement>.type(value: ButtonType) =
attr("type", value.str)
fun AttrsScope<HTMLButtonElement>.value(value: String) =
attr("value", value)
/* Form attributes */
fun AttrsScope<HTMLFormElement>.action(value: String) =
attr("action", value)
fun AttrsScope<HTMLFormElement>.acceptCharset(value: String) =
attr("accept-charset", value)
fun AttrsScope<HTMLFormElement>.autoComplete(value: Boolean = true) =
attr("autocomplete", if(value) "on" else "off")
fun AttrsScope<HTMLFormElement>.encType(value: FormEncType) =
attr("enctype", value.typeStr)
fun AttrsScope<HTMLFormElement>.method(value: FormMethod) =
attr("method", value.methodStr)
fun AttrsScope<HTMLFormElement>.noValidate() =
attr("novalidate", "")
fun AttrsScope<HTMLFormElement>.target(value: FormTarget) =
attr("target", value.targetStr)
fun AttrsScope<HTMLFormElement>.onSubmit(
listener: (SyntheticSubmitEvent) -> Unit
) {
addEventListener(eventName = EventsListenerScope.SUBMIT, listener = listener)
}
fun AttrsScope<HTMLFormElement>.onReset(
listener: (SyntheticSubmitEvent) -> Unit
) {
addEventListener(eventName = EventsListenerScope.RESET, listener = listener)
}
/* Input attributes */
fun AttrsScope<HTMLInputElement>.type(value: InputType<*>) =
attr("type", value.typeStr)
fun AttrsScope<HTMLInputElement>.accept(value: String) =
attr("accept", value) // type: file only
fun AttrsScope<HTMLInputElement>.alt(value: String) =
attr("alt", value) // type: image only
fun AttrsScope<HTMLInputElement>.autoComplete(value: AutoComplete) =
attr("autocomplete", value.unsafeCast<String>())
fun AttrsScope<HTMLInputElement>.autoFocus() =
attr("autofocus", "")
fun AttrsScope<HTMLInputElement>.capture(value: String) =
attr("capture", value) // type: file only
fun AttrsScope<HTMLInputElement>.dirName(value: String) =
attr("dirname", value) // text, search
fun AttrsScope<HTMLInputElement>.disabled() =
attr("disabled", "")
fun AttrsScope<HTMLInputElement>.form(id: String) =
attr("form", id)
fun AttrsScope<HTMLInputElement>.formAction(url: String) =
attr("formaction", url)
fun AttrsScope<HTMLInputElement>.formEncType(value: InputFormEncType) =
attr("formenctype", value.typeStr)
fun AttrsScope<HTMLInputElement>.formMethod(value: InputFormMethod) =
attr("formmethod", value.methodStr)
fun AttrsScope<HTMLInputElement>.formNoValidate() =
attr("formnovalidate", "")
fun AttrsScope<HTMLInputElement>.formTarget(value: InputFormTarget) =
attr("formtarget", value.targetStr)
fun AttrsScope<HTMLInputElement>.height(value: Int) =
attr("height", value.toString()) // image only
fun AttrsScope<HTMLInputElement>.width(value: Int) =
attr("width", value.toString()) // image only
fun AttrsScope<HTMLInputElement>.list(dataListId: String) =
attr("list", dataListId)
fun AttrsScope<HTMLInputElement>.max(value: String) =
attr("max", value)
fun AttrsScope<HTMLInputElement>.maxLength(value: Int) =
attr("maxlength", value.toString())
fun AttrsScope<HTMLInputElement>.min(value: String) =
attr("min", value)
fun AttrsScope<HTMLInputElement>.minLength(value: Int) =
attr("minlength", value.toString())
fun AttrsScope<HTMLInputElement>.multiple() =
attr("multiple", "")
fun AttrsScope<HTMLInputElement>.name(value: String) =
attr("name", value)
fun AttrsScope<HTMLInputElement>.pattern(value: String) =
attr("pattern", value)
fun AttrsScope<HTMLInputElement>.placeholder(value: String) =
attr("placeholder", value)
fun AttrsScope<HTMLInputElement>.readOnly() =
attr("readonly", "")
fun AttrsScope<HTMLInputElement>.required(value: Boolean = true) =
attr("required", value.toString())
fun AttrsScope<HTMLInputElement>.size(value: Int) =
attr("size", value.toString())
fun AttrsScope<HTMLInputElement>.src(value: String) =
attr("src", value) // image only
fun AttrsScope<HTMLInputElement>.step(value: Number) =
attr("step", value.toString()) // numeric types only
/* Option attributes */
fun AttrsScope<HTMLOptionElement>.value(value: String) =
attr("value", value)
fun AttrsScope<HTMLOptionElement>.disabled() =
attr("disabled", "")
fun AttrsScope<HTMLOptionElement>.selected() =
attr("selected", "")
fun AttrsScope<HTMLOptionElement>.label(value: String) =
attr("label", value)
/* Select attributes */
fun AttrsScope<HTMLSelectElement>.autoComplete(value: AutoComplete) =
attr("autocomplete", value.unsafeCast<String>())
fun AttrsScope<HTMLSelectElement>.autofocus() =
attr("autofocus", "")
fun AttrsScope<HTMLSelectElement>.disabled() =
attr("disabled", "")
fun AttrsScope<HTMLSelectElement>.form(formId: String) =
attr("form", formId)
fun AttrsScope<HTMLSelectElement>.multiple() =
attr("multiple", "")
fun AttrsScope<HTMLSelectElement>.name(value: String) =
attr("name", value)
fun AttrsScope<HTMLSelectElement>.required() =
attr("required", "")
fun AttrsScope<HTMLSelectElement>.size(numberOfRows: Int) =
attr("size", numberOfRows.toString())
/* OptGroup attributes */
fun AttrsScope<HTMLOptGroupElement>.label(value: String) =
attr("label", value)
fun AttrsScope<HTMLOptGroupElement>.disabled() =
attr("disabled", "")
/* TextArea attributes */
fun AttrsScope<HTMLTextAreaElement>.autoComplete(value: AutoComplete) =
attr("autocomplete", value.unsafeCast<String>())
fun AttrsScope<HTMLTextAreaElement>.autoFocus() =
attr("autofocus", "")
fun AttrsScope<HTMLTextAreaElement>.cols(value: Int) =
attr("cols", value.toString())
fun AttrsScope<HTMLTextAreaElement>.disabled() =
attr("disabled", "")
fun AttrsScope<HTMLTextAreaElement>.form(formId: String) =
attr("form", formId)
fun AttrsScope<HTMLTextAreaElement>.maxLength(value: Int) =
attr("maxlength", value.toString())
fun AttrsScope<HTMLTextAreaElement>.minLength(value: Int) =
attr("minlength", value.toString())
fun AttrsScope<HTMLTextAreaElement>.name(value: String) =
attr("name", value)
fun AttrsScope<HTMLTextAreaElement>.placeholder(value: String) =
attr("placeholder", value)
fun AttrsScope<HTMLTextAreaElement>.readOnly() =
attr("readonly", "")
fun AttrsScope<HTMLTextAreaElement>.required() =
attr("required", "")
fun AttrsScope<HTMLTextAreaElement>.rows(value: Int) =
attr("rows", value.toString())
fun AttrsScope<HTMLTextAreaElement>.wrap(value: TextAreaWrap) =
attr("wrap", value.str)
/* Img attributes */
fun AttrsScope<HTMLImageElement>.src(value: String): AttrsScope<HTMLImageElement> =
attr("src", value)
fun AttrsScope<HTMLImageElement>.alt(value: String): AttrsScope<HTMLImageElement> =
attr("alt", value)
internal val setInputValue: (HTMLInputElement, String) -> Unit = { e, v ->
if (v != e.value) {
e.value = v
}
saveControlledInputState(e, v)
}
internal val setTextAreaDefaultValue: (HTMLTextAreaElement, String) -> Unit = { e, v ->
e.innerText = v
}
internal val setCheckedValue: (HTMLInputElement, Boolean) -> Unit = { e, v ->
e.checked = v
saveControlledInputState(e, v)
}
/* Img attributes */
fun AttrsScope<HTMLLabelElement>.forId(value: String): AttrsScope<HTMLLabelElement> =
attr("for", value)
/* Table attributes */
fun AttrsScope<HTMLTableColElement>.span(value: Int): AttrsScope<HTMLTableColElement> =
attr("span", value.toString())
fun AttrsScope<HTMLTableCellElement>.scope(value: Scope): AttrsScope<HTMLTableCellElement> =
attr("scope", value.str)
fun AttrsScope<HTMLTableCellElement>.colspan(value: Int): AttrsScope<HTMLTableCellElement> =
attr("colspan", value.toString())
fun AttrsScope<HTMLTableCellElement>.rowspan(value: Int): AttrsScope<HTMLTableCellElement> =
attr("rowspan", value.toString())
/* Canvas attributes */
fun AttrsScope<HTMLCanvasElement>.width(value: CSSSizeValue<out CSSUnitLengthOrPercentage>) =
attr("width", value.toString())
fun AttrsScope<HTMLCanvasElement>.height(value: CSSSizeValue<out CSSUnitLengthOrPercentage>) =
attr("height", value.toString())