All files / bind / boolean.ts

100.00% Branches 46/46
100.00% Functions 1/1
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
x164
x7
x7
x164
x27
x27
x130
 
x164
x3
x164
x6
x164
x10
x9
x9
 
x164
x7
x164
x3
x164
x3
 
 
x164
x3
x164
x15
x3
x3
 
x164
x24
x164
x3
 
 
x164
x9
x164
x9
x164
x3
x164
x3
x164
x164
x6
x164
x36
x164























































/**
 * 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
}