2018-12-22 06:14:41 +02:00
|
|
|
package http
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
2018-09-28 01:05:56 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
2019-02-13 19:31:18 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
2018-09-18 22:42:38 +02:00
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
)
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
type HTMLDocument struct {
|
2018-10-06 01:40:09 +02:00
|
|
|
*HTMLElement
|
2018-09-28 01:05:56 +02:00
|
|
|
url values.String
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func NewHTMLDocument(
|
2018-09-18 22:42:38 +02:00
|
|
|
url string,
|
|
|
|
node *goquery.Document,
|
2018-10-05 22:35:08 +02:00
|
|
|
) (*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
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
return &HTMLDocument{el, values.NewString(url)}, nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
func (doc *HTMLDocument) Type() core.Type {
|
2019-02-13 19:31:18 +02:00
|
|
|
return types.HTMLDocument
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-13 19:31:18 +02:00
|
|
|
func (doc *HTMLDocument) Compare(other core.Value) int64 {
|
|
|
|
if other.Type() == types.HTMLDocument {
|
2018-10-05 22:35:08 +02:00
|
|
|
otherDoc := other.(values.HTMLDocument)
|
2018-09-28 01:05:56 +02:00
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
return doc.url.Compare(otherDoc.URL())
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
2019-02-13 19:31:18 +02:00
|
|
|
|
|
|
|
return types.Compare(other.Type(), types.HTMLDocument)
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
2018-09-28 01:05:56 +02:00
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
func (doc *HTMLDocument) URL() core.Value {
|
|
|
|
return doc.url
|
|
|
|
}
|