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

57 lines
1.0 KiB
Go
Raw Normal View History

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"
"github.com/MontFerret/ferret/pkg/runtime/values"
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 (el *HTMLDocument) Type() core.Type {
return core.HTMLDocumentType
2018-09-18 22:42:38 +02:00
}
func (el *HTMLDocument) Compare(other core.Value) int {
2018-09-18 22:42:38 +02:00
switch other.Type() {
case core.HTMLDocumentType:
otherDoc := other.(values.HTMLDocument)
return el.url.Compare(otherDoc.URL())
2018-09-18 22:42:38 +02:00
default:
if other.Type() > core.HTMLDocumentType {
2018-09-18 22:42:38 +02:00
return -1
}
return 1
}
}
func (el *HTMLDocument) URL() core.Value {
return el.url
}