-
-
Notifications
You must be signed in to change notification settings - Fork 356
/
attribute.go
108 lines (88 loc) · 1.79 KB
/
attribute.go
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
package app
import (
"strconv"
"strings"
)
type attributes map[string]string
func (a attributes) Set(name string, value any) {
switch name {
case "style", "allow":
a[name] += toAttributeValue(value) + ";"
case "class":
if v := strings.TrimSpace(toAttributeValue(value)); v != "" {
s := a[name]
if s != "" {
s += " "
}
a[name] = s + v
}
case "srcset":
s := a[name]
if s != "" {
s += ", "
}
a[name] = s + toAttributeValue(value)
default:
a[name] = toAttributeValue(value)
}
}
type attributeURLResolver func(string) string
func toAttributeValue(v any) string {
return strings.TrimSpace(toString(v))
}
func resolveAttributeURLValue(name, value string, resolve attributeURLResolver) string {
switch name {
case "cite",
"data",
"href",
"src":
return resolve(value)
case "srcset":
srcs := strings.Split(value, ", ")
for i, src := range srcs {
srcs[i] = resolve(src)
}
return strings.Join(srcs, ", ")
default:
return value
}
}
func setJSAttribute(jsElement Value, name, value string) {
toBool := func(v string) bool {
b, _ := strconv.ParseBool(v)
return b
}
switch name {
case "value":
jsElement.Set(name, value)
case "class":
jsElement.Set("className", value)
case "contenteditable":
jsElement.Set("contentEditable", value)
case "ismap":
jsElement.Set("isMap", toBool(value))
case "readonly":
jsElement.Set("readOnly", toBool(value))
case "async",
"autofocus",
"autoplay",
"checked",
"default",
"defer",
"disabled",
"hidden",
"loop",
"multiple",
"muted",
"open",
"required",
"reversed",
"selected":
jsElement.Set(name, toBool(value))
default:
jsElement.Call("setAttribute", name, value)
}
}
func deleteJSAttribute(jsElement Value, name string) {
jsElement.Call("removeAttribute", name)
}