-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.go
173 lines (137 loc) · 3.88 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
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
package dom
import (
"fmt"
"strings"
)
// domText. We don't 'inherit' from CharacterData, that's a bit too convoluted...
// Maybe we'll implement that some other time.
type domText struct {
localName string
parentNode Node
ownerDocument Document
// Text specific things
data string
}
func newText(owner Document) Text {
t := &domText{}
t.ownerDocument = owner
return t
}
func (dt *domText) GetNodeName() string {
return "#text"
}
func (dt *domText) GetNodeType() NodeType {
return TextNode
}
// NodeValue returns the same as GetData, the content of the text node.
func (dt *domText) GetNodeValue() string {
return dt.GetText()
}
func (dt *domText) GetLocalName() string {
return ""
}
func (dt *domText) GetChildNodes() []Node {
return nil
}
func (dt *domText) GetParentNode() Node {
return dt.parentNode
}
func (dt *domText) GetFirstChild() Node {
return nil
}
func (dt *domText) GetLastChild() Node {
return nil
}
func (dt *domText) GetAttributes() NamedNodeMap {
return nil
}
func (dt *domText) HasAttributes() bool {
return false
}
func (dt *domText) GetOwnerDocument() Document {
return dt.ownerDocument
}
func (dt *domText) AppendChild(child Node) error {
return fmt.Errorf("%v: %v does not allow child nodes", ErrorHierarchyRequest, TextNode)
}
func (dt *domText) RemoveChild(oldChild Node) (Node, error) {
return nil, fmt.Errorf("%v: %v does not allow child nodes - nothing to remove", ErrorHierarchyRequest, TextNode)
}
func (dt *domText) ReplaceChild(newChild, oldChild Node) (Node, error) {
return nil, fmt.Errorf("%v: %v does not allow child nodes - nothing to replace", ErrorHierarchyRequest, TextNode)
}
func (dt *domText) InsertBefore(newChild, refChild Node) (Node, error) {
return nil, fmt.Errorf("%v: %v does not allow child nodes - nothing to insert", ErrorHierarchyRequest, TextNode)
}
func (dt *domText) HasChildNodes() bool {
return false
}
func (dt *domText) GetPreviousSibling() Node {
return getPreviousSibling(dt)
}
func (dt *domText) GetNextSibling() Node {
return getNextSibling(dt)
}
// GetNamespaceURI returns an empty string for Text nodes.
func (dt *domText) GetNamespaceURI() string {
return ""
}
// GetNamespacePrefix returns an empty string for Text nodes.
func (dt *domText) GetNamespacePrefix() string {
return ""
}
func (dt *domText) LookupPrefix(namespace string) (string, bool) {
return "", false
}
func (dt *domText) LookupNamespaceURI(pfx string) (string, bool) {
return "", false
}
func (dt *domText) IsDefaultNamespace(namespace string) bool {
// TODO ?
return false
}
func (dt *domText) GetTextContent() string {
return dt.GetNodeValue()
}
func (dt *domText) SetTextContent(content string) {
dt.SetText(content)
}
func (dt *domText) CloneNode(deep bool) Node {
cloneText := dt.ownerDocument.CreateText(dt.data)
return cloneText
}
func (dt *domText) ImportNode(n Node, deep bool) Node {
return importNode(dt.ownerDocument, n, deep)
}
// Private functions:
func (dt *domText) setParentNode(parent Node) {
dt.parentNode = parent
}
func (dt *domText) setOwnerDocument(doc Document) {
dt.ownerDocument = doc
}
// Text specifics:
// GetText returns the character data of this text node, unescaped.
func (dt *domText) GetText() string {
return dt.data
}
// SetText sets the character data of the XML node. The data can be unescaped
// XML, since GetText() will take care of conversion.
func (dt *domText) SetText(data string) {
dt.data = data
}
// IsElementContentWhitespace returns true when the Text node contains ignorable
// whitespace, like any combinations of \t, \n, \r and space characters.
func (dt *domText) IsElementContentWhitespace() bool {
return strings.TrimSpace(dt.GetText()) == ""
}
func (dt *domText) String() string {
maxlen := 30
var d string
if len(dt.data) > maxlen {
d = strings.TrimSpace(dt.data[0:maxlen] + " [...]")
} else {
d = strings.TrimSpace(dt.GetText())
}
return fmt.Sprintf("%s: '%s'", dt.GetNodeType(), d)
}