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

295 lines
5.7 KiB
Go
Raw Normal View History

package http
2018-09-18 22:42:38 +02:00
import (
"encoding/json"
"hash/fnv"
"github.com/MontFerret/ferret/pkg/drivers/common"
2018-09-18 22:42:38 +02:00
"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 HTMLElement struct {
2018-09-18 22:42:38 +02:00
selection *goquery.Selection
attrs *values.Object
children *values.Array
}
func NewHTMLElement(node *goquery.Selection) (*HTMLElement, error) {
2018-09-18 22:42:38 +02:00
if node == nil {
return nil, core.Error(core.ErrMissedArgument, "element selection")
}
return &HTMLElement{node, nil, nil}, nil
2018-09-18 22:42:38 +02:00
}
func (el *HTMLElement) MarshalJSON() ([]byte, error) {
return json.Marshal(el.InnerText().String())
2018-09-18 22:42:38 +02:00
}
func (el *HTMLElement) Type() core.Type {
return types.HTMLElement
2018-09-18 22:42:38 +02:00
}
func (el *HTMLElement) String() string {
return el.InnerHTML().String()
2018-09-18 22:42:38 +02:00
}
func (el *HTMLElement) Compare(other core.Value) int64 {
if other.Type() == types.HTMLElement {
2018-09-18 22:42:38 +02:00
// TODO: complete the comparison
return -1
}
return types.Compare(other.Type(), types.HTMLElement)
2018-09-18 22:42:38 +02:00
}
func (el *HTMLElement) Unwrap() interface{} {
2018-09-18 22:42:38 +02:00
return el.selection
}
func (el *HTMLElement) Hash() uint64 {
2018-09-18 22:42:38 +02:00
str, err := el.selection.Html()
if err != nil {
return 0
}
2018-10-05 21:17:22 +02:00
h := fnv.New64a()
2018-09-18 22:42:38 +02:00
2018-10-05 21:17:22 +02:00
h.Write([]byte(el.Type().String()))
h.Write([]byte(":"))
h.Write([]byte(str))
2018-09-18 22:42:38 +02:00
2018-10-05 21:17:22 +02:00
return h.Sum64()
2018-09-18 22:42:38 +02:00
}
func (el *HTMLElement) Copy() core.Value {
c, _ := NewHTMLElement(el.selection.Clone())
2018-09-27 17:53:26 +02:00
return c
}
func (el *HTMLElement) NodeType() values.Int {
2018-09-18 22:42:38 +02:00
nodes := el.selection.Nodes
if len(nodes) == 0 {
return 0
}
2018-10-06 01:40:09 +02:00
return values.NewInt(common.ToHTMLType(nodes[0].Type))
2018-09-18 22:42:38 +02:00
}
func (el *HTMLElement) NodeName() values.String {
2018-09-18 22:42:38 +02:00
return values.NewString(goquery.NodeName(el.selection))
}
func (el *HTMLElement) Length() values.Int {
2018-09-18 22:42:38 +02:00
if el.children == nil {
el.children = el.parseChildren()
}
return el.children.Length()
}
func (el *HTMLElement) Value() core.Value {
2018-09-18 22:42:38 +02:00
val, ok := el.selection.Attr("value")
if ok {
return values.NewString(val)
}
return values.EmptyString
}
func (el *HTMLElement) InnerText() values.String {
2018-09-18 22:42:38 +02:00
return values.NewString(el.selection.Text())
}
func (el *HTMLElement) InnerHTML() values.String {
2018-09-18 22:42:38 +02:00
h, err := el.selection.Html()
if err != nil {
return values.EmptyString
}
return values.NewString(h)
}
func (el *HTMLElement) GetAttributes() core.Value {
2018-09-18 22:42:38 +02:00
if el.attrs == nil {
el.attrs = el.parseAttrs()
}
return el.attrs
}
func (el *HTMLElement) GetAttribute(name values.String) core.Value {
2018-09-18 22:42:38 +02:00
v, ok := el.selection.Attr(name.String())
if ok {
return values.NewString(v)
}
return values.None
}
func (el *HTMLElement) GetChildNodes() core.Value {
2018-09-18 22:42:38 +02:00
if el.children == nil {
el.children = el.parseChildren()
}
return el.children
}
func (el *HTMLElement) GetChildNode(idx values.Int) core.Value {
2018-09-18 22:42:38 +02:00
if el.children == nil {
el.children = el.parseChildren()
}
return el.children.Get(idx)
}
func (el *HTMLElement) QuerySelector(selector values.String) core.Value {
2018-09-18 22:42:38 +02:00
selection := el.selection.Find(selector.String())
if selection == nil {
return values.None
}
res, err := NewHTMLElement(selection)
2018-09-18 22:42:38 +02:00
if err != nil {
return values.None
}
return res
}
func (el *HTMLElement) QuerySelectorAll(selector values.String) core.Value {
2018-09-18 22:42:38 +02:00
selection := el.selection.Find(selector.String())
if selection == nil {
return values.None
}
arr := values.NewArray(selection.Length())
selection.Each(func(i int, selection *goquery.Selection) {
el, err := NewHTMLElement(selection)
2018-09-18 22:42:38 +02:00
if err == nil {
arr.Push(el)
}
})
return arr
}
func (el *HTMLElement) InnerHTMLBySelector(selector values.String) values.String {
selection := el.selection.Find(selector.String())
str, err := selection.Html()
// TODO: log error
if err != nil {
return values.EmptyString
}
return values.NewString(str)
}
func (el *HTMLElement) InnerHTMLBySelectorAll(selector values.String) *values.Array {
selection := el.selection.Find(selector.String())
arr := values.NewArray(selection.Length())
selection.Each(func(_ int, selection *goquery.Selection) {
str, err := selection.Html()
// TODO: log error
if err == nil {
arr.Push(values.NewString(str))
}
})
return arr
}
func (el *HTMLElement) InnerTextBySelector(selector values.String) values.String {
selection := el.selection.Find(selector.String())
return values.NewString(selection.Text())
}
func (el *HTMLElement) InnerTextBySelectorAll(selector values.String) *values.Array {
selection := el.selection.Find(selector.String())
arr := values.NewArray(selection.Length())
selection.Each(func(_ int, selection *goquery.Selection) {
arr.Push(values.NewString(selection.Text()))
})
return arr
}
func (el *HTMLElement) CountBySelector(selector values.String) values.Int {
selection := el.selection.Find(selector.String())
if selection == nil {
return values.ZeroInt
}
return values.NewInt(selection.Size())
}
func (el *HTMLElement) ExistsBySelector(selector values.String) values.Boolean {
selection := el.selection.Closest(selector.String())
if selection == nil {
return values.False
}
return values.True
}
func (el *HTMLElement) parseAttrs() *values.Object {
2018-09-18 22:42:38 +02:00
obj := values.NewObject()
for _, name := range common.Attributes {
val, ok := el.selection.Attr(name)
if ok {
obj.Set(values.NewString(name), values.NewString(val))
}
}
return obj
}
func (el *HTMLElement) parseChildren() *values.Array {
2018-09-18 22:42:38 +02:00
children := el.selection.Children()
arr := values.NewArray(10)
children.Each(func(i int, selection *goquery.Selection) {
//name := goquery.NodeName(selection)
//if name != "#text" && name != "#comment" {
// child, err := NewHTMLElement(selection)
2018-09-18 22:42:38 +02:00
//
// if err == nil {
// arr.Push(child)
// }
//}
child, err := NewHTMLElement(selection)
2018-09-18 22:42:38 +02:00
if err == nil {
arr.Push(child)
}
})
return arr
}