forked from MontFerret/ferret
/
document.go
56 lines (44 loc) · 1.03 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
package http
import (
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/PuerkitoBio/goquery"
)
type HTMLDocument struct {
*HTMLElement
url values.String
}
func NewHTMLDocument(
url string,
node *goquery.Document,
) (*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
}
return &HTMLDocument{el, values.NewString(url)}, nil
}
func (doc *HTMLDocument) Type() core.Type {
return core.HTMLDocumentType
}
func (doc *HTMLDocument) Compare(other core.Value) int {
switch other.Type() {
case core.HTMLDocumentType:
otherDoc := other.(values.HTMLDocument)
return doc.url.Compare(otherDoc.URL())
default:
if other.Type() > core.HTMLDocumentType {
return -1
}
return 1
}
}
func (doc *HTMLDocument) URL() core.Value {
return doc.url
}