All files / bind / boolean.ts

100.00% Branches 23/23
100.00% Lines 46/46
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
 
 
 
 
 
x9
x172
x179
x179
x172
x199
x199
x301
 
x172
x175
x172
x178
x172
x182
x191
x191
 
x172
x179
x172
x175
x172
x175
 
 
x172
x175
x172
x187
x190
x190
 
x172
x196
x172
x175
 
 
x172
x181
x172
x181
x172
x175
x172
x175
x172
x172
x178
x172
x207
x172























































/**
 * Checks whether an HTML attribute is a boolean attribute for the specified tag name.
 *
 * See {@link https://html.spec.whatwg.org/#attributes-3 | HTML attributes reference}.
 */
export function boolean(tagname: string, attribute: string) {
  if (/^autofocus|inert|itemscope$/.test(attribute)) {
    return true
  }
  if (attribute === "disabled") {
    return /^(?:BUTTON|INPUT|OPTGROUP|OPTION|SELECT|TEXTAREA|FIELDSET|LINK)$/.test(tagname)
  }
  switch (tagname) {
    // Form elements
    case "FORM":
      return attribute === "novalidate"
    case "TEXTAREA":
      return /^(?:readonly|required)$/.test(attribute)
    case "INPUT":
      if (/^(?:checked|formnovalidate|readonly)$/.test(attribute)) {
        return true
      }
      // fallthrough
    case "SELECT":
      return /^(?:multiple|required)$/.test(attribute)
    case "OPTION":
      return attribute === "selected"
    case "BUTTON":
      return attribute === "formnovalidate"

    // Media elements
    case "IMG":
      return attribute === "ismap"
    case "VIDEO":
      if (attribute === "playsinline") {
        return true
      }
      // fallthrough
    case "AUDIO":
      return /^(?:autoplay|controls|loop|muted)$/.test(attribute)
    case "TRACK":
      return attribute === "default"

    // Other elements
    case "TEMPLATE":
      return /^shadowroot(?:delegatesfocus|clonable|serializable)$/.test(attribute)
    case "SCRIPT":
      return /^(?:async|defer|nomodule)$/.test(attribute)
    case "IFRAME":
      return attribute === "allowfullscreen"
    case "OL":
      return attribute === "reversed"
    case "DETAILS":
    case "DIALOG":
      return attribute === "open"
  }
  return false
}