-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.go
More file actions
247 lines (202 loc) · 5.35 KB
/
Copy pathnode.go
File metadata and controls
247 lines (202 loc) · 5.35 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
package htmlx
import (
"io"
"net/http"
"os"
"strings"
"github.com/mdigger/wstat"
"golang.org/x/net/html"
"golang.org/x/net/html/charset"
)
// Node expands html.node with additional methods.
type Node struct {
*html.Node
}
// New warps the representation of html.node by adding it a new functionality.
func New(node *html.Node) Node {
return Node{Node: node}
}
// Parse returns a parsed HTML tree representation.
func Parse(r io.Reader) (n Node, err error) {
doc, err := html.Parse(r)
return New(doc), err
}
// String returns a parsed HTML tree representation from the string.
func String(data string) (Node, error) {
return Parse(strings.NewReader(data))
}
// Load loads and parses an HTML document from the file.
func Load(path string) (Node, error) {
f, err := os.Open(path)
if err != nil {
return New(nil), err
}
defer f.Close()
return Parse(f)
}
// Get loads and parses an HTML document at the specified url address.
func Get(url string) (Node, error) {
resp, err := http.Get(url)
if err != nil {
return New(nil), err
}
defer resp.Body.Close()
r, err := charset.NewReader(resp.Body, resp.Header.Get("Content-Type"))
if err != nil {
return New(nil), err
}
return Parse(r)
}
// IsEmpty returns true if the node is not specified.
func (n Node) IsEmpty() bool {
return n.Node == nil
}
// Rename renames HTML element.
func (n Node) Rename(name string) {
Rename(n.Node, name)
}
// Remove removes the specified element from the HTML tree.
func (n Node) Remove() {
Remove(n.Node)
}
// RemoveChilds removes all child elements if they are exists.
func (n Node) RemoveChilds() {
RemoveChilds(n.Node)
}
// Parent returns the parent element.
func (n Node) Parent() Node {
if n.IsEmpty() {
return n
}
return New(n.Node.Parent)
}
// FirstChild returns the first child element.
func (n Node) FirstChild() Node {
if n.IsEmpty() {
return n
}
return New(n.Node.FirstChild)
}
// LastChild returns the last child element.
func (n Node) LastChild() Node {
if n.IsEmpty() {
return n
}
return New(n.Node.LastChild)
}
// PrevSibling returns the previous sibling element.
func (n Node) PrevSibling() Node {
if n.IsEmpty() {
return n
}
return New(n.Node.PrevSibling)
}
// NextSibling returns the previous sibling element.
func (n Node) NextSibling() Node {
if n.IsEmpty() {
return n
}
return New(n.Node.NextSibling)
}
// Find returns the first element that is suitable for the specified conditions.
func (n Node) Find(m Matcher) Node {
return New(Find(n.Node, m))
}
// FindNext finds the first siblin element.
func (n Node) FindNext(m Matcher) Node {
return New(FindNext(n.Node, m))
}
// FindPrev finds the previous siblin element.
func (n Node) FindPrev(m Matcher) Node {
return New(FindPrev(n.Node, m))
}
// FindAll returns all the elements suitable for the specified conditions.
func (n Node) FindAll(m Matcher) []Node {
nodes := FindAll(n.Node, m)
if len(nodes) == 0 {
return nil
}
result := make([]Node, len(nodes))
for i, node := range nodes {
result[i] = New(node)
}
return result
}
// HTML returns a string with inner HTML representation.
func (n Node) InnerHTML() (string, error) {
return HTML(n.Node, false)
}
// OuterHTML returns a string with HTML representation< include self tag.
func (n Node) OuterHTML() (string, error) {
return HTML(n.Node, true)
}
// String returns a string with HTML representation.
// Possible error is ignored.
func (n Node) String() string {
str, _ := n.OuterHTML()
return str
}
// SetHTML parses an HTML fragment in the context of the current element and
// replaces them the child elements.
func (n Node) SetHTML(data string) error {
return SetHTML(n.Node, data)
}
// Text returns only a text representation, without HTML elements.
func (n Node) Text() string {
return Text(n.Node)
}
// SetText replaces the text of the element to the new and removes possible
// child items.
func (n Node) SetText(text string) error {
return SetText(n.Node, text)
}
// Stats returns statistics on the text.
func (n Node) Stats() (c wstat.Counter) {
return Stats(n.Node)
}
// Predefined attribute names.
const (
AttrID = "id"
AttrClass = "class"
)
// Attr returns the attribute value with the specified name and the flag that
// the attribute was specified for this item.
func (n Node) Attr(name string) (val string, ok bool) {
if n.IsEmpty() || n.Type != html.ElementNode {
return
}
return AttrVal(n.Node.Attr, name)
}
// ID returns the unique identifier of the element.
func (n Node) ID() string {
val, _ := n.Attr(AttrID)
return val
}
// SetAttr set the new attribute value with the specified name.
func (n Node) SetAttr(name, value string) {
if n.IsEmpty() || n.Type != html.ElementNode {
return
}
n.Node.Attr = SetAttr(n.Node.Attr, name, value)
}
// RemoveAttr removes the attribute value with the specified name.
func (n Node) RemoveAttr(name, value string) {
if n.IsEmpty() || n.Type != html.ElementNode {
return
}
n.Node.Attr = RemoveAttr(n.Node.Attr, name)
}
// HasClass returns true if the item is specified with the specified name.
func (n Node) HasClass(name string) (ok bool) {
if n.IsEmpty() || n.Type != html.ElementNode {
return
}
return HasAttrWord(n.Node.Attr, AttrClass, name)
}
// AddClass adds a new style name to the element attribute list.
func (n Node) AddClass(name string) {
if n.IsEmpty() || n.Type != html.ElementNode {
return
}
n.Node.Attr = AddAttrWord(n.Node.Attr, AttrClass, name)
}