1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/drivers/http/document.go

53 lines
1.0 KiB
Go
Raw Normal View History

package http
2018-09-18 22:42:38 +02:00
import (
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
2018-09-18 22:42:38 +02:00
"github.com/PuerkitoBio/goquery"
)
type HTMLDocument struct {
2018-10-06 01:40:09 +02:00
*HTMLElement
url values.String
2018-09-18 22:42:38 +02:00
}
func NewHTMLDocument(
2018-09-18 22:42:38 +02:00
url string,
node *goquery.Document,
) (*HTMLDocument, error) {
2018-09-18 22:42:38 +02:00
if url == "" {
return nil, core.Error(core.ErrMissedArgument, "document url")
}
if node == nil {
return nil, core.Error(core.ErrMissedArgument, "document root selection")
}
2018-10-06 01:40:09 +02:00
el, err := NewHTMLElement(node.Selection)
2018-09-18 22:42:38 +02:00
if err != nil {
return nil, err
}
return &HTMLDocument{el, values.NewString(url)}, nil
2018-09-18 22:42:38 +02:00
}
func (doc *HTMLDocument) Type() core.Type {
return types.HTMLDocument
2018-09-18 22:42:38 +02:00
}
func (doc *HTMLDocument) Compare(other core.Value) int64 {
if other.Type() == types.HTMLDocument {
otherDoc := other.(values.HTMLDocument)
return doc.url.Compare(otherDoc.URL())
2018-09-18 22:42:38 +02:00
}
return types.Compare(other.Type(), types.HTMLDocument)
2018-09-18 22:42:38 +02:00
}
func (doc *HTMLDocument) URL() core.Value {
return doc.url
}