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"
|
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 {
|
2018-10-05 22:35:08 +02:00
|
|
|
return core.HTMLDocumentType
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
func (doc *HTMLDocument) Compare(other core.Value) int {
|
2018-09-18 22:42:38 +02:00
|
|
|
switch other.Type() {
|
2018-10-05 22:35:08 +02:00
|
|
|
case core.HTMLDocumentType:
|
|
|
|
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
|
|
|
default:
|
2018-10-05 22:35:08 +02:00
|
|
|
if other.Type() > core.HTMLDocumentType {
|
2018-09-18 22:42:38 +02:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|