2018-12-22 06:14:41 +02:00
|
|
|
package http
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
import (
|
2019-02-20 01:10:18 +02:00
|
|
|
"context"
|
2018-09-18 22:42:38 +02:00
|
|
|
"encoding/json"
|
2018-10-12 17:58:08 +02:00
|
|
|
"hash/fnv"
|
2019-02-26 04:46:39 +02:00
|
|
|
"strings"
|
2018-10-12 17:58:08 +02:00
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers"
|
2018-12-22 06:14:41 +02:00
|
|
|
"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/PuerkitoBio/goquery"
|
|
|
|
)
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
type HTMLElement struct {
|
2018-09-18 22:42:38 +02:00
|
|
|
selection *goquery.Selection
|
|
|
|
attrs *values.Object
|
|
|
|
children *values.Array
|
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
func NewHTMLElement(node *goquery.Selection) (drivers.HTMLElement, error) {
|
2018-09-18 22:42:38 +02:00
|
|
|
if node == nil {
|
|
|
|
return nil, core.Error(core.ErrMissedArgument, "element selection")
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
return &HTMLElement{node, nil, nil}, nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
func (nd *HTMLElement) MarshalJSON() ([]byte, error) {
|
2019-02-21 04:24:05 +02:00
|
|
|
return json.Marshal(nd.InnerText(context.Background()).String())
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
func (nd *HTMLElement) Type() core.Type {
|
|
|
|
return drivers.HTMLElementType
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
func (nd *HTMLElement) String() string {
|
2019-02-21 04:24:05 +02:00
|
|
|
return nd.InnerHTML(context.Background()).String()
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
func (nd *HTMLElement) Compare(other core.Value) int64 {
|
|
|
|
switch other.Type() {
|
|
|
|
case drivers.HTMLElementType:
|
|
|
|
other := other.(drivers.HTMLElement)
|
2019-02-13 19:31:18 +02:00
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
ctx, fn := drivers.WithDefaultTimeout(context.Background())
|
|
|
|
defer fn()
|
|
|
|
|
|
|
|
return nd.InnerHTML(ctx).Compare(other.InnerHTML(ctx))
|
2019-02-20 01:10:18 +02:00
|
|
|
default:
|
|
|
|
return drivers.Compare(nd.Type(), other.Type())
|
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
func (nd *HTMLElement) Unwrap() interface{} {
|
|
|
|
return nd.selection
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
func (nd *HTMLElement) Hash() uint64 {
|
|
|
|
str, err := nd.selection.Html()
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:17:22 +02:00
|
|
|
h := fnv.New64a()
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
h.Write([]byte(nd.Type().String()))
|
2018-10-05 21:17:22 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
func (nd *HTMLElement) Copy() core.Value {
|
|
|
|
c, _ := NewHTMLElement(nd.selection.Clone())
|
2018-09-27 17:53:26 +02:00
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
func (nd *HTMLElement) NodeType() values.Int {
|
|
|
|
nodes := nd.selection.Nodes
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
func (nd *HTMLElement) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nd *HTMLElement) NodeName() values.String {
|
|
|
|
return values.NewString(goquery.NodeName(nd.selection))
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
func (nd *HTMLElement) Length() values.Int {
|
|
|
|
if nd.children == nil {
|
|
|
|
nd.children = nd.parseChildren()
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
return nd.children.Length()
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (nd *HTMLElement) GetValue(_ context.Context) core.Value {
|
2019-02-20 01:10:18 +02:00
|
|
|
val, ok := nd.selection.Attr("value")
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if ok {
|
|
|
|
return values.NewString(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.EmptyString
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (nd *HTMLElement) SetValue(_ context.Context, value core.Value) error {
|
2019-02-20 01:10:18 +02:00
|
|
|
nd.selection.SetAttr("value", value.String())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (nd *HTMLElement) InnerText(_ context.Context) values.String {
|
2019-02-20 01:10:18 +02:00
|
|
|
return values.NewString(nd.selection.Text())
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (nd *HTMLElement) InnerHTML(_ context.Context) values.String {
|
2019-02-20 01:10:18 +02:00
|
|
|
h, err := nd.selection.Html()
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.NewString(h)
|
|
|
|
}
|
|
|
|
|
2019-02-26 04:46:39 +02:00
|
|
|
func (nd *HTMLElement) GetAttributes(_ context.Context) *values.Object {
|
2019-02-20 01:10:18 +02:00
|
|
|
if nd.attrs == nil {
|
|
|
|
nd.attrs = nd.parseAttrs()
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
return nd.attrs
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (nd *HTMLElement) GetAttribute(_ context.Context, name values.String) core.Value {
|
2019-02-20 01:10:18 +02:00
|
|
|
v, ok := nd.selection.Attr(name.String())
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if ok {
|
|
|
|
return values.NewString(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.None
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (nd *HTMLElement) SetAttribute(_ context.Context, name, value values.String) error {
|
2019-02-20 01:10:18 +02:00
|
|
|
nd.selection.SetAttr(string(name), string(value))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (nd *HTMLElement) GetChildNodes(_ context.Context) core.Value {
|
2019-02-20 01:10:18 +02:00
|
|
|
if nd.children == nil {
|
|
|
|
nd.children = nd.parseChildren()
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
return nd.children
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (nd *HTMLElement) GetChildNode(_ context.Context, idx values.Int) core.Value {
|
2019-02-20 01:10:18 +02:00
|
|
|
if nd.children == nil {
|
|
|
|
nd.children = nd.parseChildren()
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
return nd.children.Get(idx)
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (nd *HTMLElement) QuerySelector(_ context.Context, selector values.String) core.Value {
|
2019-02-20 01:10:18 +02:00
|
|
|
selection := nd.selection.Find(selector.String())
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if selection == nil {
|
|
|
|
return values.None
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
res, err := NewHTMLElement(selection)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (nd *HTMLElement) QuerySelectorAll(_ context.Context, selector values.String) core.Value {
|
2019-02-20 01:10:18 +02:00
|
|
|
selection := nd.selection.Find(selector.String())
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if selection == nil {
|
|
|
|
return values.None
|
|
|
|
}
|
|
|
|
|
|
|
|
arr := values.NewArray(selection.Length())
|
|
|
|
|
|
|
|
selection.Each(func(i int, selection *goquery.Selection) {
|
2018-10-05 22:35:08 +02:00
|
|
|
el, err := NewHTMLElement(selection)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
arr.Push(el)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (nd *HTMLElement) InnerHTMLBySelector(_ context.Context, selector values.String) values.String {
|
2019-02-20 01:10:18 +02:00
|
|
|
selection := nd.selection.Find(selector.String())
|
2018-10-09 02:20:40 +02:00
|
|
|
|
|
|
|
str, err := selection.Html()
|
|
|
|
|
|
|
|
// TODO: log error
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.NewString(str)
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (nd *HTMLElement) InnerHTMLBySelectorAll(_ context.Context, selector values.String) *values.Array {
|
2019-02-20 01:10:18 +02:00
|
|
|
selection := nd.selection.Find(selector.String())
|
2018-10-09 02:20:40 +02:00
|
|
|
arr := values.NewArray(selection.Length())
|
|
|
|
|
|
|
|
selection.Each(func(_ int, selection *goquery.Selection) {
|
|
|
|
str, err := selection.Html()
|
|
|
|
|
|
|
|
// TODO: log error
|
|
|
|
if err == nil {
|
2019-02-26 04:46:39 +02:00
|
|
|
arr.Push(values.NewString(strings.TrimSpace(str)))
|
2018-10-09 02:20:40 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (nd *HTMLElement) InnerTextBySelector(_ context.Context, selector values.String) values.String {
|
2019-02-20 01:10:18 +02:00
|
|
|
selection := nd.selection.Find(selector.String())
|
2018-10-09 02:20:40 +02:00
|
|
|
|
|
|
|
return values.NewString(selection.Text())
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (nd *HTMLElement) InnerTextBySelectorAll(_ context.Context, selector values.String) *values.Array {
|
2019-02-20 01:10:18 +02:00
|
|
|
selection := nd.selection.Find(selector.String())
|
2018-10-09 02:20:40 +02:00
|
|
|
arr := values.NewArray(selection.Length())
|
|
|
|
|
|
|
|
selection.Each(func(_ int, selection *goquery.Selection) {
|
|
|
|
arr.Push(values.NewString(selection.Text()))
|
|
|
|
})
|
|
|
|
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (nd *HTMLElement) CountBySelector(_ context.Context, selector values.String) values.Int {
|
2019-02-20 01:10:18 +02:00
|
|
|
selection := nd.selection.Find(selector.String())
|
2018-10-13 03:52:27 +02:00
|
|
|
|
|
|
|
if selection == nil {
|
|
|
|
return values.ZeroInt
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.NewInt(selection.Size())
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (nd *HTMLElement) ExistsBySelector(_ context.Context, selector values.String) values.Boolean {
|
2019-02-20 01:10:18 +02:00
|
|
|
selection := nd.selection.Closest(selector.String())
|
2018-12-22 06:14:41 +02:00
|
|
|
|
|
|
|
if selection == nil {
|
|
|
|
return values.False
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.True
|
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
func (nd *HTMLElement) GetIn(ctx context.Context, path []core.Value) (core.Value, error) {
|
|
|
|
return common.GetInElement(ctx, nd, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nd *HTMLElement) SetIn(ctx context.Context, path []core.Value, value core.Value) error {
|
|
|
|
return common.SetInElement(ctx, nd, path, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nd *HTMLElement) Iterate(_ context.Context) (core.Iterator, error) {
|
|
|
|
return common.NewIterator(nd)
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (nd *HTMLElement) Click(_ context.Context) (values.Boolean, error) {
|
2019-02-20 01:10:18 +02:00
|
|
|
return false, core.ErrNotSupported
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (nd *HTMLElement) Input(_ context.Context, _ core.Value, _ values.Int) error {
|
2019-02-20 01:10:18 +02:00
|
|
|
return core.ErrNotSupported
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (nd *HTMLElement) Select(_ context.Context, _ *values.Array) (*values.Array, error) {
|
2019-02-20 01:10:18 +02:00
|
|
|
return nil, core.ErrNotSupported
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (nd *HTMLElement) ScrollIntoView(_ context.Context) error {
|
2019-02-20 01:10:18 +02:00
|
|
|
return core.ErrNotSupported
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (nd *HTMLElement) Hover(_ context.Context) error {
|
2019-02-20 01:10:18 +02:00
|
|
|
return core.ErrNotSupported
|
|
|
|
}
|
|
|
|
|
2019-03-07 04:52:41 +02:00
|
|
|
func (nd *HTMLElement) WaitForClass(_ context.Context, _ values.String, _ drivers.WaitEvent) error {
|
2019-02-20 01:10:18 +02:00
|
|
|
return core.ErrNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nd *HTMLElement) parseAttrs() *values.Object {
|
2018-09-18 22:42:38 +02:00
|
|
|
obj := values.NewObject()
|
|
|
|
|
|
|
|
for _, name := range common.Attributes {
|
2019-02-20 01:10:18 +02:00
|
|
|
val, ok := nd.selection.Attr(name)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if ok {
|
|
|
|
obj.Set(values.NewString(name), values.NewString(val))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj
|
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
func (nd *HTMLElement) parseChildren() *values.Array {
|
|
|
|
children := nd.selection.Children()
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
arr := values.NewArray(10)
|
|
|
|
|
|
|
|
children.Each(func(i int, selection *goquery.Selection) {
|
2018-10-05 22:35:08 +02:00
|
|
|
child, err := NewHTMLElement(selection)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
arr.Push(child)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return arr
|
|
|
|
}
|