-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnode_leaf.go
More file actions
255 lines (213 loc) · 6.05 KB
/
Copy pathnode_leaf.go
File metadata and controls
255 lines (213 loc) · 6.05 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package helium
import "github.com/lestrrat-go/pdebug"
// CDATASection represents a CDATA section node in the DOM tree
// (libxml2: xmlNode with type XML_CDATA_SECTION_NODE).
type CDATASection struct {
node
}
func newCDATASection(b []byte) *CDATASection {
c := CDATASection{}
c.etype = CDATASectionNode
c.content = make([]byte, len(b))
copy(c.content, b)
c.name = "(CDATA)"
return &c
}
func (n *CDATASection) AddChild(cur Node) error {
return ErrInvalidOperation
}
func (n *CDATASection) AppendText(b []byte) error {
n.content = append(n.content, b...)
return nil
}
func (n *CDATASection) AddSibling(cur Node) error {
return addSibling(n, cur)
}
func (n CDATASection) Content() []byte {
return n.content
}
func (n *CDATASection) Replace(nodes ...Node) error {
return replaceNode(n, nodes...)
}
func (n *CDATASection) SetTreeDoc(doc *Document) {
setTreeDoc(n, doc)
}
// Comment represents an XML comment node (libxml2: xmlNode with type XML_COMMENT_NODE).
type Comment struct {
node
}
func newComment(b []byte) *Comment {
t := Comment{}
t.etype = CommentNode
t.content = b
return &t
}
func (n *Comment) AddChild(cur Node) error {
// Reject a nil or typed-nil operand BEFORE the type assertion / comment-merge
// fast path so the call returns ErrNilNode instead of panicking and leaves
// the tree untouched.
if isNilNode(cur) {
return ErrNilNode
}
// Run the shared self/cycle guard and auto-unlink BEFORE the comment-merge
// fast path. Otherwise comment.AddChild(comment) would double its own
// content, and merging an already-linked comment node would copy its
// content while leaving it linked under its old parent.
if t, ok := cur.(*Comment); ok {
if err := addChildPreflight(n, cur); err != nil {
return err
}
return n.AppendText(t.content)
}
return ErrInvalidOperation
}
func (n *Comment) AppendText(b []byte) error {
n.content = append(n.content, b...)
return nil
}
// AddSibling adds a new sibling to the end of the sibling nodes.
func (n *Comment) AddSibling(cur Node) error {
return addSibling(n, cur)
}
func (n Comment) Content() []byte {
return n.content
}
func (n *Comment) Replace(nodes ...Node) error {
return replaceNode(n, nodes...)
}
func (n *Comment) SetTreeDoc(doc *Document) {
setTreeDoc(n, doc)
}
// ProcessingInstruction represents an XML processing instruction node
// (libxml2: xmlNode with type XML_PI_NODE).
type ProcessingInstruction struct {
docnode
target string
data string
}
func (p *ProcessingInstruction) Name() string {
return p.target
}
func (p *ProcessingInstruction) Content() []byte {
return []byte(p.data)
}
func (p *ProcessingInstruction) Type() ElementType {
return ProcessingInstructionNode
}
func (p *ProcessingInstruction) AddChild(cur Node) error {
return addChild(p, cur)
}
func (p *ProcessingInstruction) AppendText(b []byte) error {
return appendText(p, b)
}
func (p *ProcessingInstruction) AddSibling(cur Node) error {
return addSibling(p, cur)
}
func (p *ProcessingInstruction) Replace(nodes ...Node) error {
return replaceNode(p, nodes...)
}
func (p *ProcessingInstruction) SetTreeDoc(doc *Document) {
setTreeDoc(p, doc)
}
// EntityRef represents an entity reference node in the DOM tree
// (libxml2: xmlNode with type XML_ENTITY_REF_NODE).
type EntityRef struct {
node
}
func newEntityRef() *EntityRef {
n := &EntityRef{}
n.etype = EntityRefNode
return n
}
func (e *EntityRef) AddChild(cur Node) error {
return addChild(e, cur)
}
func (e *EntityRef) AppendText(b []byte) error {
return appendText(e, b)
}
func (e *EntityRef) AddSibling(cur Node) error {
return addSibling(e, cur)
}
func (e *EntityRef) Replace(nodes ...Node) error {
return replaceNode(e, nodes...)
}
func (n *EntityRef) SetTreeDoc(doc *Document) {
setTreeDoc(n, doc)
}
// Text represents an XML text node (libxml2: xmlNode with type XML_TEXT_NODE).
type Text struct {
node
}
const textNodeName = "(text)"
func newText(b []byte) *Text {
t := Text{}
t.etype = TextNode
t.content = make([]byte, len(b))
copy(t.content, b)
t.name = textNodeName
return &t
}
func (n *Text) AddChild(cur Node) error {
// Reject a nil or typed-nil operand BEFORE the type assertion / text-merge
// fast path so the call returns ErrNilNode instead of panicking and leaves
// the tree untouched.
if isNilNode(cur) {
return ErrNilNode
}
// Run the shared self/cycle guard and auto-unlink BEFORE the text-merge
// fast path. Otherwise txt.AddChild(txt) would double its own content, and
// merging an already-linked text node would copy its content while leaving
// it linked under its old parent.
if t, ok := cur.(*Text); ok {
if err := addChildPreflight(n, cur); err != nil {
return err
}
return n.AppendText(t.content)
}
return ErrInvalidOperation
}
func (n *Text) AppendText(b []byte) error {
if pdebug.Enabled {
g := pdebug.IPrintf("START Text.AppendText '%s' (%p)", b, n)
defer func() {
g.IRelease("END Text.AppendText '%s'", n.content)
}()
}
if doc := n.doc; doc != nil {
n.content = doc.growOwnedTextContent(n.content, len(b))
}
n.content = append(n.content, b...)
return nil
}
func (n *Text) AddSibling(cur Node) error {
// Reject a nil or typed-nil operand BEFORE the debug log (which dereferences
// cur) and the text-merge fast path so the call returns ErrNilNode instead of
// panicking and leaves the tree untouched.
if isNilNode(cur) {
return ErrNilNode
}
if pdebug.Enabled {
g := pdebug.IPrintf("START Text.AddSibling '%s'", cur.Content())
defer g.IRelease("END Text.AddSibling")
}
// Run the shared self/cycle guard and auto-unlink BEFORE the text-merge
// fast path. Otherwise t.AddSibling(t) would double its own content, and
// merging an already-linked text node would copy its content while leaving
// it linked under its old parent.
if err := addSiblingPreflight(n, cur); err != nil {
return err
}
if cur.Type() == TextNode {
return n.AppendText(cur.Content())
}
return addSibling(n, cur)
}
func (n Text) Content() []byte {
return n.content
}
func (n *Text) Replace(nodes ...Node) error {
return replaceNode(n, nodes...)
}
func (n *Text) SetTreeDoc(doc *Document) {
setTreeDoc(n, doc)
}