/
raw.go
179 lines (143 loc) · 3.06 KB
/
raw.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package app
import (
"context"
"io"
"net/url"
"strings"
"github.com/maxence-charriere/go-app/v7/pkg/errors"
)
// Raw returns a ui element from the given raw value. HTML raw value must have a
// single root.
//
// It is not recommended to use this kind of node since there is no check on the
// raw string content.
func Raw(v string) UI {
v = strings.TrimSpace(v)
tag := rawRootTagName(v)
if tag == "" {
v = "<div></div>"
}
return &raw{
value: v,
tag: tag,
}
}
type raw struct {
jsvalue Value
parentElem UI
tag string
value string
}
func (r *raw) Kind() Kind {
return RawHTML
}
func (r *raw) JSValue() Value {
return r.jsvalue
}
func (r *raw) Mounted() bool {
return r.jsvalue != nil
}
func (r *raw) name() string {
return "raw." + r.tag
}
func (r *raw) self() UI {
return r
}
func (r *raw) setSelf(UI) {
}
func (r *raw) context() context.Context {
return nil
}
func (r *raw) attributes() map[string]string {
return nil
}
func (r *raw) eventHandlers() map[string]eventHandler {
return nil
}
func (r *raw) parent() UI {
return r.parentElem
}
func (r *raw) setParent(p UI) {
r.parentElem = p
}
func (r *raw) children() []UI {
return nil
}
func (r *raw) mount() error {
if r.Mounted() {
return errors.New("mounting raw html element failed").
Tag("reason", "already mounted").
Tag("name", r.name()).
Tag("kind", r.Kind())
}
wrapper := Window().Get("document").Call("createElement", "div")
wrapper.Set("innerHTML", r.value)
value := wrapper.Get("firstChild")
if !value.Truthy() {
return errors.New("mounting raw html element failed").
Tag("reason", "converting raw html to html elements returned nil").
Tag("name", r.name()).
Tag("kind", r.Kind()).
Tag("raw-html", r.value)
}
wrapper.Call("removeChild", value)
r.jsvalue = value
return nil
}
func (r *raw) dismount() {
r.jsvalue = nil
}
func (r *raw) update(n UI) error {
if !r.Mounted() {
return nil
}
if n.Kind() != r.Kind() || r.name() != r.name() {
return errors.New("updating raw html element failed").
Tag("replace", true).
Tag("reason", "different element types").
Tag("current-kind", r.Kind()).
Tag("current-name", r.name()).
Tag("updated-kind", n.Kind()).
Tag("updated-name", n.name())
}
if v := n.(*raw).value; r.value != v {
return errors.New("updating raw html element failed").
Tag("replace", true).
Tag("reason", "different raw values").
Tag("current-value", r.value).
Tag("new-value", v)
}
return nil
}
func (r *raw) onNav(*url.URL) {
}
func (r *raw) onAppUpdate() {
}
func (r *raw) html(w io.Writer) {
r.htmlWithIndent(w, 0)
}
func (r *raw) htmlWithIndent(w io.Writer, indent int) {
writeIndent(w, indent)
w.Write(stob(r.value))
w.Write(ln())
}
func rawRootTagName(raw string) string {
raw = strings.TrimSpace(raw)
if strings.HasPrefix(raw, "</") || !strings.HasPrefix(raw, "<") {
return ""
}
end := -1
for i := 1; i < len(raw); i++ {
if raw[i] == ' ' ||
raw[i] == '\t' ||
raw[i] == '\n' ||
raw[i] == '>' {
end = i
break
}
}
if end <= 0 {
return ""
}
return raw[1:end]
}