-
-
Notifications
You must be signed in to change notification settings - Fork 366
/
text.go
79 lines (62 loc) · 1.15 KB
/
text.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
package app
import (
"fmt"
"html"
"io"
"reflect"
)
type textNode interface {
UI
text() string
mount() error
update(t textNode)
}
// Text returns a text node.
func Text(v string) UI {
return &text{textValue: v}
}
type text struct {
parentNode UI
jsValue Value
textValue string
}
func (t *text) nodeType() reflect.Type {
return reflect.TypeOf(t)
}
func (t *text) JSValue() Value {
return t.jsValue
}
func (t *text) parent() UI {
return t.parentNode
}
func (t *text) setParent(p UI) {
t.parentNode = p
}
func (t *text) dismount() {
t.jsValue = nil
}
func (t *text) text() string {
return t.textValue
}
func (t *text) mount() error {
if t.jsValue != nil {
return fmt.Errorf("node already mounted: %+v", t)
}
t.jsValue = Window().
Get("document").
Call("createTextNode", t.textValue)
return nil
}
func (t *text) update(n textNode) {
if text := n.text(); text != t.textValue {
t.textValue = text
t.jsValue.Set("nodeValue", text)
}
}
func (t *text) html(w io.Writer) {
t.htmlWithIndent(w, 0)
}
func (t *text) htmlWithIndent(w io.Writer, indent int) {
writeIndent(w, indent)
w.Write(stob(html.EscapeString(t.textValue)))
}