0 | ||| Note: only currently valid attributes as per [MDN website][1] are listed.
  1 | ||| Experimental or deprecated attributes are on purpose not available.
  2 | |||
  3 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements
  4 | module Text.Html.Attributes.Types
  5 |
  6 | import Data.Either
  7 | import Data.String
  8 | import Derive.Prelude
  9 |
 10 | import Text.Html.Types
 11 | import Text.Html.Values
 12 |
 13 | %hide Language.Reflection.TT.Name
 14 |
 15 | %default total
 16 | %language ElabReflection
 17 |
 18 | ||| Escaped attribute value.
 19 | |||
 20 | ||| The constructor is not exported. Escaped attribute values can only be
 21 | ||| created using the `escape` function.
 22 | export
 23 | data EscapedValue = Escaped String
 24 |
 25 | public export
 26 | Eq EscapedValue where
 27 |   (Escaped x) == (Escaped y) = x == y
 28 |
 29 | public export
 30 | Show EscapedValue where
 31 |   show (Escaped str) = str
 32 |
 33 | ||| Escapes a string for use as an attribute value.
 34 | |||
 35 | ||| The following characters are escaped: '"', '\'', '&' and '<'.
 36 | ||| This function is the only way to construct an `EscapedValue` as the
 37 | ||| `EscapedValue` constructor is kept private.
 38 | export
 39 | escape : String -> EscapedValue
 40 | escape = Escaped . pack . escapeHelper . unpack
 41 |   where
 42 |     escapeHelper : List Char -> List Char
 43 |     escapeHelper []           = []
 44 |     escapeHelper ('"' :: cs)  = unpack "&quot;" ++ escapeHelper cs
 45 |     escapeHelper ('\'' :: cs) = unpack "&#39;" ++ escapeHelper cs
 46 |     escapeHelper ('&' :: cs)  = unpack "&amp;" ++ escapeHelper cs
 47 |     escapeHelper ('<' :: cs)  = unpack "&lt;" ++ escapeHelper cs
 48 |     escapeHelper (c :: cs)    = (c :: escapeHelper cs)
 49 |
 50 | ||| HTML element attribute.
 51 | public export
 52 | record Attribute where
 53 |   constructor MkAttribute
 54 |   name : Name
 55 |   value : Maybe EscapedValue
 56 |
 57 | %runElab derive "Attribute" [Eq, Show]
 58 |
 59 | ||| List the attributes' names
 60 | |||
 61 | ||| The names are provided as a standard `List` for simplicity.
 62 | ||| Nevertheless, the list will not have any duplicate value as it derives
 63 | ||| from a `FreshList`.
 64 | public export
 65 | attributesNames : List Attribute -> List String
 66 | attributesNames [] = []
 67 | attributesNames (attr :: attrs) = (attr.name :: attributesNames attrs)
 68 |
 69 | ||| Attributes for a **a** element as per [MDN list][1].
 70 | |||
 71 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/a
 72 | public export
 73 | aAttributes : List String
 74 | aAttributes =
 75 |   [ "download"
 76 |   , "href"
 77 |   , "hreflang"
 78 |   , "ping"
 79 |   , "referrerpolicy"
 80 |   , "rel"
 81 |   , "target"
 82 |   , "type"
 83 |   ]
 84 |
 85 | ||| Attributes for an **area** element as per [MDN list][1].
 86 | |||
 87 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/area
 88 | public export
 89 | areaAttributes : List String
 90 | areaAttributes =
 91 |   [ "alt"
 92 |   , "coords"
 93 |   , "download"
 94 |   , "href"
 95 |   , "ping"
 96 |   , "referrerpolicy"
 97 |   , "rel"
 98 |   , "shape"
 99 |   , "target"
100 |   ]
101 |
102 | ||| Attributes for an **audio** element as per [MDN list][1].
103 | |||
104 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/audio
105 | public export
106 | audioAttributes : List String
107 | audioAttributes =
108 |   [ "autoplay"
109 |   , "controls"
110 |   , "controlslist"
111 |   , "crossorigin"
112 |   , "disableremoteplayback"
113 |   , "loop"
114 |   , "muted"
115 |   , "preload"
116 |   , "src"
117 |   ]
118 |
119 | ||| Attributes for a **base** element as per [MDN list][1].
120 | |||
121 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/base
122 | public export
123 | baseAttributes : List String
124 | baseAttributes = ["href", "target"]
125 |
126 | ||| Attributes for a **blockquote** or **q** element as per [MDN list][1].
127 | |||
128 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/blockquote
129 | public export
130 | citationAttributes : List String
131 | citationAttributes = ["cite"]
132 |
133 | ||| Attributes for a **button** element as per [MDN list][1].
134 | |||
135 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/button
136 | public export
137 | buttonAttributes : List String
138 | buttonAttributes =
139 |   [ "autofocus"
140 |   , "command"
141 |   , "commandfor"
142 |   , "disabled"
143 |   , "form"
144 |   , "formaction"
145 |   , "formenctype"
146 |   , "formmethod"
147 |   , "formnovalidate"
148 |   , "formtarget"
149 |   , "interestfor"
150 |   , "name"
151 |   , "popovertarget"
152 |   , "popovertargetaction"
153 |   , "type"
154 |   , "value"
155 |   ]
156 |
157 | ||| Attributes for a **canvas** element as per [MDN list][1].
158 | |||
159 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/canvas
160 | public export
161 | canvasAttributes : List String
162 | canvasAttributes = ["height", "width"]
163 |
164 | ||| Attributes for a [**del**][1] or [**ins**][2] element.
165 | |||
166 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/del
167 | ||| [2]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/ins
168 | public export
169 | changeAttributes : List String
170 | changeAttributes = ["cite", "datetime"]
171 |
172 | ||| Attributes for a **col** element as per [MDN list][1].
173 | |||
174 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/col
175 | public export
176 | colAttributes : List String
177 | colAttributes = ["span"]
178 |
179 | ||| Attributes for a **details** element as per [MDN list][1].
180 | |||
181 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/details
182 | public export
183 | detailsAttributes : List String
184 | detailsAttributes = ["open", "name"]
185 |
186 | ||| Attributes for a **dialgo** element as per [MDN list][1].
187 | |||
188 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/dialog
189 | public export
190 | dialogAttributes : List String
191 | dialogAttributes = ["closedby", "open"]
192 |
193 | ||| Attributes for a **embed** element as per [MDN list][1].
194 | |||
195 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/embed
196 | public export
197 | embedAttributes : List String
198 | embedAttributes = ["height", "src", "type", "width"]
199 |
200 | ||| Attributes for a **fieldset** element as per [MDN list][1].
201 | |||
202 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/fieldset
203 | public export
204 | fieldsetAttributes : List String
205 | fieldsetAttributes = ["disabled", "form", "name"]
206 |
207 | ||| Attributes for a **form** element as per [MDN list][1].
208 | |||
209 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/form
210 | public export
211 | formAttributes : List String
212 | formAttributes = [
213 |     "accept-charset"
214 |   , "autocapitalize"
215 |   , "autocomplete"
216 |   , "name"
217 |   , "rel"
218 |   , "action"
219 |   , "enctype"
220 |   , "method"
221 |   , "novalidate"
222 |   , "target"
223 |   ]
224 |
225 | ||| Global HTML attributes as per [MDN list][1].
226 | |||
227 | ||| Note: the experimental attributes are not included.
228 | ||| Also, the event attributes are not included as their use is discouraged as
229 | ||| per [MDN recommendations][1].
230 | |||
231 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Reference/Global_attributes
232 | public export
233 | globalAttributes : List String
234 | globalAttributes =
235 |   [ "accesskey"
236 |   , "autocapitalize"
237 |   , "autocorrect"
238 |   , "autofocus"
239 |   , "class"
240 |   , "contenteditable"
241 |   , "dir"
242 |   , "draggable"
243 |   , "enterkeyhint"
244 |   , "exportparts"
245 |   , "hidden"
246 |   , "id"
247 |   , "inert"
248 |   , "inputmode"
249 |   , "is"
250 |   , "itemid"
251 |   , "itemprop"
252 |   , "itemref"
253 |   , "itemscope"
254 |   , "itemtype"
255 |   , "lang"
256 |   , "nonce"
257 |   , "part"
258 |   , "popover"
259 |   , "role"
260 |   , "slot"
261 |   , "spellcheck"
262 |   , "style"
263 |   , "tabindex"
264 |   , "title"
265 |   , "translate"
266 |   , "writingsuggestions"
267 |   ]
268 |
269 | public export
270 | iframeAttributes : List String
271 | iframeAttributes =
272 |   [ "allow"
273 |   , "allowfullscreen"
274 |   , "height"
275 |   , "loading"
276 |   , "name"
277 |   , "referrerpolicy"
278 |   , "sandbox"
279 |   , "src"
280 |   , "srcdoc"
281 |   , "width"
282 |   ]
283 |
284 | public export
285 | imgAttributes : List String
286 | imgAttributes =
287 |   [ "alt"
288 |   , "crossorigin"
289 |   , "decoding"
290 |   , "elementtiming"
291 |   , "fetchpriority"
292 |   , "height"
293 |   , "ismap"
294 |   , "loading"
295 |   , "referrerpolicy"
296 |   , "sizes"
297 |   , "src"
298 |   , "srcset"
299 |   , "width"
300 |   , "usemap"
301 |   ]
302 |
303 | ||| List of attributes for the **input** element.
304 | |||
305 | ||| Note: some attributes specific to a given type of input could be missing.
306 | public export
307 | inputAttributes : List String
308 | inputAttributes =
309 |   [ "accept"
310 |   , "alt"
311 |   , "autocapitalize"
312 |   , "autocomplete"
313 |   , "capture"
314 |   , "checked"
315 |   , "dirname"
316 |   , "disabled"
317 |   , "form"
318 |   , "formaction"
319 |   , "formenctype"
320 |   , "formmethod"
321 |   , "formnovalidate"
322 |   , "formtarget"
323 |   , "height"
324 |   , "list"
325 |   , "max"
326 |   , "maxlength"
327 |   , "min"
328 |   , "minlength"
329 |   , "multiple"
330 |   , "name"
331 |   , "pattern"
332 |   , "placeholder"
333 |   , "popovertarget"
334 |   , "popovertargetaction"
335 |   , "readonly"
336 |   , "required"
337 |   , "size"
338 |   , "src"
339 |   , "step"
340 |   , "type"
341 |   , "value"
342 |   , "width"
343 |   ]
344 |
345 | ||| Attributes for a **label** element as per [MDN list][1].
346 | |||
347 | ||| [1]: https://developer.mozilla.org/docs/Web/HTML/Reference/Elements/label
348 | public export
349 | labelAttributes : List String
350 | labelAttributes = ["for"]
351 |
352 | public export
353 | linkAttributes : List String
354 | linkAttributes =
355 |   [ "as"
356 |   , "blocking"
357 |   , "crossorigin"
358 |   , "disabled"
359 |   , "fetchpriority"
360 |   , "href"
361 |   , "hreflang"
362 |   , "imagesizes"
363 |   , "imagesrcset"
364 |   , "integrity"
365 |   , "media"
366 |   , "referrerpolicy"
367 |   , "rel"
368 |   , "sizes"
369 |   , "title"
370 |   , "type"
371 |   ]
372 |
373 | public export
374 | metaAttributes : List String
375 | metaAttributes =
376 |   [ "charset"
377 |   , "content"
378 |   , "http-equiv"
379 |   , "media"
380 |   , "name"
381 |   ]
382 |
383 | public export
384 | mapAttributes : List String
385 | mapAttributes = ["name"]
386 |
387 | public export
388 | meterAttributes : List String
389 | meterAttributes =
390 |   [ "value"
391 |   , "min"
392 |   , "max"
393 |   , "low"
394 |   , "high"
395 |   , "optimum"
396 |   ]
397 |
398 | public export
399 | objectAttributes : List String
400 | objectAttributes =
401 |   [ "data"
402 |   , "form"
403 |   , "height"
404 |   , "name"
405 |   , "type"
406 |   , "width"
407 |   ]
408 |
409 | public export
410 | olAttributes : List String
411 | olAttributes =
412 |   [ "reversed"
413 |   , "start"
414 |   , "type"
415 |   ]
416 |
417 | public export
418 | optgroupAttributes : List String
419 | optgroupAttributes = ["disabled" , "label"]
420 |
421 | public export
422 | optionAttributes : List String
423 | optionAttributes =
424 |   [ "disabled"
425 |   , "label"
426 |   , "selected"
427 |   , "value"
428 |   ]
429 |
430 | public export
431 | outputAttributes : List String
432 | outputAttributes =
433 |   [ "for"
434 |   , "form"
435 |   , "name"
436 |   ]
437 |
438 | public export
439 | progressAttributes : List String
440 | progressAttributes = ["max" , "value"]
441 |
442 | public export
443 | scriptAttributes : List String
444 | scriptAttributes =
445 |   [ "async"
446 |   , "blocking"
447 |   , "crossorigin"
448 |   , "defer"
449 |   , "fetchpriority"
450 |   , "integrity"
451 |   , "nomodule"
452 |   , "nonce"
453 |   , "referrerpolicy"
454 |   , "src"
455 |   , "type"
456 |   ]
457 |
458 | public export
459 | selectAttributes : List String
460 | selectAttributes =
461 |   [ "autocomplete"
462 |   , "autofocus"
463 |   , "disabled"
464 |   , "form"
465 |   , "multiple"
466 |   , "name"
467 |   , "required"
468 |   , "size"
469 |   ]
470 |
471 | public export
472 | slotAttributes : List String
473 | slotAttributes = ["name"]
474 |
475 | public export
476 | sourceAttributes : List String
477 | sourceAttributes =
478 |   [ "type"
479 |   , "src"
480 |   , "srcset"
481 |   , "sizes"
482 |   , "media"
483 |   , "height"
484 |   , "width"
485 |   ]
486 |
487 | public export
488 | styleAttributes : List String
489 | styleAttributes =
490 |   [ "blocking"
491 |   , "media"
492 |   , "nonce"
493 |   , "title"
494 |   ]
495 |
496 | public export
497 | tdAttributes : List String
498 | tdAttributes =
499 |   [ "colspan"
500 |   , "headers"
501 |   , "rowspan"
502 |   ]
503 |
504 | public export
505 | templateAttributes : List String
506 | templateAttributes =
507 |   [ "shadowrootmode"
508 |   , "shadowrootclonable"
509 |   , "shadowrootcustomelementregistry"
510 |   , "shadowrootdelegatesfocus"
511 |   , "shadowrootserializable"
512 |   ]
513 |
514 | public export
515 | textareaAttributes : List String
516 | textareaAttributes =
517 |   [ "autocapitalize"
518 |   , "autocomplete"
519 |   , "autocorrect"
520 |   , "autofocus"
521 |   , "cols"
522 |   , "dirname"
523 |   , "disabled"
524 |   , "form"
525 |   , "maxlength"
526 |   , "minlength"
527 |   , "name"
528 |   , "placeholder"
529 |   , "readonly"
530 |   , "required"
531 |   , "rows"
532 |   , "spellcheck"
533 |   , "wrap"
534 |   ]
535 |
536 | public export
537 | thAttributes : List String
538 | thAttributes =
539 |   [ "abbr"
540 |   , "colspan"
541 |   , "headers"
542 |   , "rowspan"
543 |   , "scope"
544 |   ]
545 |
546 | public export
547 | timeAttributes : List String
548 | timeAttributes = ["datetime"]
549 |
550 | public export
551 | trackAttributes : List String
552 | trackAttributes =
553 |   [ "default"
554 |   , "kind"
555 |   , "label"
556 |   , "src"
557 |   , "srclang"
558 |   ]
559 |
560 | public export
561 | videoAttributes : List String
562 | videoAttributes =
563 |   [ "autoplay"
564 |   , "controls"
565 |   , "controlslist"
566 |   , "crossorigin"
567 |   , "disablepictureinpicture"
568 |   , "disableremoteplayback"
569 |   , "height"
570 |   , "loop"
571 |   , "muted"
572 |   , "playsinline"
573 |   , "poster"
574 |   , "preload"
575 |   , "src"
576 |   , "width"
577 |   ]
578 |