2018-09-27 04:03:06 +02:00
|
|
|
package static
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HtmlDocument struct {
|
|
|
|
*HtmlElement
|
2018-09-28 01:05:56 +02:00
|
|
|
url values.String
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-09-28 01:05:56 +02:00
|
|
|
return &HtmlDocument{el, values.NewString(url)}, nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HtmlDocument) Type() core.Type {
|
|
|
|
return core.HtmlDocumentType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HtmlDocument) Compare(other core.Value) int {
|
|
|
|
switch other.Type() {
|
|
|
|
case core.HtmlDocumentType:
|
2018-09-28 01:05:56 +02:00
|
|
|
otherDoc := other.(values.HtmlDocument)
|
|
|
|
|
|
|
|
return el.url.Compare(otherDoc.Url())
|
2018-09-18 22:42:38 +02:00
|
|
|
default:
|
|
|
|
if other.Type() > core.HtmlDocumentType {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
2018-09-28 01:05:56 +02:00
|
|
|
|
|
|
|
func (el *HtmlDocument) Url() core.Value {
|
|
|
|
return el.url
|
|
|
|
}
|