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
x173
x180
x180
x173
x200
x200
x303
 
x173
x176
x173
x179
x173
x183
x192
x192
 
x173
x180
x173
x176
x173
x176
 
 
x173
x176
x173
x188
x191
x191
 
x173
x197
x173
x176
 
 
x173
x182
x173
x182
x173
x176
x173
x176
x173
x173
x179
x173
x209
x173























































/**
 * 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): boolean {
  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
}