/
document.go
229 lines (173 loc) · 5.51 KB
/
document.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
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
package http
import (
"context"
"hash/fnv"
"github.com/PuerkitoBio/goquery"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/drivers/common"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
type HTMLDocument struct {
doc *goquery.Document
element drivers.HTMLElement
url values.String
parent drivers.HTMLDocument
children *values.Array
}
func NewRootHTMLDocument(
node *goquery.Document,
url string,
) (*HTMLDocument, error) {
return NewHTMLDocument(node, url, nil)
}
func NewHTMLDocument(
node *goquery.Document,
url string,
parent drivers.HTMLDocument,
) (*HTMLDocument, error) {
if url == "" {
return nil, core.Error(core.ErrMissedArgument, "document url")
}
if node == nil {
return nil, core.Error(core.ErrMissedArgument, "document root selection")
}
el, err := NewHTMLElement(node.Selection)
if err != nil {
return nil, err
}
doc := new(HTMLDocument)
doc.doc = node
doc.element = el
doc.parent = parent
doc.url = values.NewString(url)
doc.children = values.NewArray(10)
frames := node.Find("iframe")
frames.Each(func(i int, selection *goquery.Selection) {
child, _ := NewHTMLDocument(goquery.NewDocumentFromNode(selection.Nodes[0]), selection.AttrOr("src", url), doc)
doc.children.Push(child)
})
return doc, nil
}
func (doc *HTMLDocument) MarshalJSON() ([]byte, error) {
return doc.element.MarshalJSON()
}
func (doc *HTMLDocument) Type() core.Type {
return drivers.HTMLDocumentType
}
func (doc *HTMLDocument) String() string {
str, err := doc.doc.Html()
if err != nil {
return ""
}
return str
}
func (doc *HTMLDocument) Compare(other core.Value) int64 {
switch other.Type() {
case drivers.HTMLElementType:
otherDoc := other.(drivers.HTMLDocument)
return doc.url.Compare(otherDoc.GetURL())
default:
return drivers.Compare(doc.Type(), other.Type())
}
}
func (doc *HTMLDocument) Unwrap() interface{} {
return doc.doc
}
func (doc *HTMLDocument) Hash() uint64 {
h := fnv.New64a()
h.Write([]byte(doc.Type().String()))
h.Write([]byte(":"))
h.Write([]byte(doc.url))
return h.Sum64()
}
func (doc *HTMLDocument) Copy() core.Value {
cp, err := NewHTMLDocument(doc.doc, string(doc.url), doc.parent)
if err != nil {
return values.None
}
return cp
}
func (doc *HTMLDocument) Clone() core.Cloneable {
cloned, err := NewHTMLDocument(doc.doc, doc.url.String(), doc.parent)
if err != nil {
return values.None
}
return cloned
}
func (doc *HTMLDocument) Length() values.Int {
return values.NewInt(doc.doc.Length())
}
func (doc *HTMLDocument) Iterate(_ context.Context) (core.Iterator, error) {
return common.NewIterator(doc.element)
}
func (doc *HTMLDocument) GetIn(ctx context.Context, path []core.Value) (core.Value, core.PathError) {
return common.GetInDocument(ctx, path, doc)
}
func (doc *HTMLDocument) SetIn(ctx context.Context, path []core.Value, value core.Value) core.PathError {
return common.SetInDocument(ctx, path, doc, value)
}
func (doc *HTMLDocument) GetNodeType(_ context.Context) (values.Int, error) {
return 9, nil
}
func (doc *HTMLDocument) GetNodeName(_ context.Context) (values.String, error) {
return "#document", nil
}
func (doc *HTMLDocument) GetChildNodes(ctx context.Context) (*values.Array, error) {
return doc.element.GetChildNodes(ctx)
}
func (doc *HTMLDocument) GetChildNode(ctx context.Context, idx values.Int) (core.Value, error) {
return doc.element.GetChildNode(ctx, idx)
}
func (doc *HTMLDocument) QuerySelector(ctx context.Context, selector drivers.QuerySelector) (core.Value, error) {
return doc.element.QuerySelector(ctx, selector)
}
func (doc *HTMLDocument) QuerySelectorAll(ctx context.Context, selector drivers.QuerySelector) (*values.Array, error) {
return doc.element.QuerySelectorAll(ctx, selector)
}
func (doc *HTMLDocument) CountBySelector(ctx context.Context, selector drivers.QuerySelector) (values.Int, error) {
return doc.element.CountBySelector(ctx, selector)
}
func (doc *HTMLDocument) ExistsBySelector(ctx context.Context, selector drivers.QuerySelector) (values.Boolean, error) {
return doc.element.ExistsBySelector(ctx, selector)
}
func (doc *HTMLDocument) XPath(ctx context.Context, expression values.String) (core.Value, error) {
return doc.element.XPath(ctx, expression)
}
func (doc *HTMLDocument) GetTitle() values.String {
title := doc.doc.Find("head > title")
return values.NewString(title.Text())
}
func (doc *HTMLDocument) GetChildDocuments(_ context.Context) (*values.Array, error) {
return doc.children.Clone().(*values.Array), nil
}
func (doc *HTMLDocument) GetURL() values.String {
return doc.url
}
func (doc *HTMLDocument) GetElement() drivers.HTMLElement {
return doc.element
}
func (doc *HTMLDocument) GetName() values.String {
return ""
}
func (doc *HTMLDocument) GetParentDocument(_ context.Context) (drivers.HTMLDocument, error) {
return doc.parent, nil
}
func (doc *HTMLDocument) ScrollTop(_ context.Context, _ drivers.ScrollOptions) error {
return core.ErrNotSupported
}
func (doc *HTMLDocument) ScrollBottom(_ context.Context, _ drivers.ScrollOptions) error {
return core.ErrNotSupported
}
func (doc *HTMLDocument) ScrollBySelector(_ context.Context, _ drivers.QuerySelector, _ drivers.ScrollOptions) error {
return core.ErrNotSupported
}
func (doc *HTMLDocument) Scroll(_ context.Context, _ drivers.ScrollOptions) error {
return core.ErrNotSupported
}
func (doc *HTMLDocument) MoveMouseByXY(_ context.Context, _, _ values.Float) error {
return core.ErrNotSupported
}
func (doc *HTMLDocument) Close() error {
return nil
}