2018-12-22 06:14:41 +02:00
|
|
|
package cdp
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2018-10-11 18:39:03 +02:00
|
|
|
"fmt"
|
2018-10-09 05:07:08 +02:00
|
|
|
"hash/fnv"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2019-09-05 18:17:22 +02:00
|
|
|
"time"
|
2018-10-09 05:07: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/cdp/eval"
|
|
|
|
"github.com/MontFerret/ferret/pkg/drivers/cdp/events"
|
2019-06-20 19:21:48 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers/cdp/input"
|
2019-07-11 23:16:34 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers/cdp/templates"
|
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"
|
2019-02-13 19:31:18 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
2019-06-20 19:21:48 +02:00
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
"github.com/mafredri/cdp"
|
|
|
|
"github.com/mafredri/cdp/protocol/dom"
|
2018-10-11 18:39:03 +02:00
|
|
|
"github.com/mafredri/cdp/protocol/runtime"
|
2019-07-11 23:16:34 +02:00
|
|
|
"github.com/pkg/errors"
|
2018-09-28 06:28:33 +02:00
|
|
|
"github.com/rs/zerolog"
|
2019-07-11 23:16:34 +02:00
|
|
|
"golang.org/x/net/html"
|
2018-09-18 22:42:38 +02:00
|
|
|
)
|
|
|
|
|
2018-10-11 18:39:03 +02:00
|
|
|
var emptyNodeID = dom.NodeID(0)
|
|
|
|
|
|
|
|
type (
|
|
|
|
HTMLElementIdentity struct {
|
2019-07-03 20:05:02 +02:00
|
|
|
nodeID dom.NodeID
|
|
|
|
objectID runtime.RemoteObjectID
|
2018-10-11 18:39:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HTMLElement struct {
|
2018-10-15 23:17:15 +02:00
|
|
|
mu sync.Mutex
|
2018-10-11 18:39:03 +02:00
|
|
|
logger *zerolog.Logger
|
|
|
|
client *cdp.Client
|
|
|
|
events *events.EventBroker
|
2019-06-20 19:21:48 +02:00
|
|
|
input *input.Manager
|
2019-06-19 23:58:56 +02:00
|
|
|
exec *eval.ExecutionContext
|
2018-10-11 18:39:03 +02:00
|
|
|
connected values.Boolean
|
2019-06-19 23:58:56 +02:00
|
|
|
id HTMLElementIdentity
|
|
|
|
nodeType html.NodeType
|
2018-10-11 18:39:03 +02:00
|
|
|
nodeName values.String
|
2019-07-11 23:16:34 +02:00
|
|
|
innerHTML *common.LazyValue
|
2018-10-11 18:39:03 +02:00
|
|
|
innerText *common.LazyValue
|
|
|
|
value core.Value
|
|
|
|
attributes *common.LazyValue
|
2019-03-13 20:51:30 +02:00
|
|
|
style *common.LazyValue
|
2019-06-19 23:58:56 +02:00
|
|
|
children []HTMLElementIdentity
|
2018-10-11 18:39:03 +02:00
|
|
|
loadedChildren *common.LazyValue
|
|
|
|
}
|
|
|
|
)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func LoadHTMLElement(
|
2018-10-11 18:39:03 +02:00
|
|
|
ctx context.Context,
|
2018-09-28 06:28:33 +02:00
|
|
|
logger *zerolog.Logger,
|
2018-09-18 22:42:38 +02:00
|
|
|
client *cdp.Client,
|
2018-09-27 04:03:06 +02:00
|
|
|
broker *events.EventBroker,
|
2019-06-20 19:21:48 +02:00
|
|
|
input *input.Manager,
|
2019-06-19 23:58:56 +02:00
|
|
|
exec *eval.ExecutionContext,
|
2018-10-11 18:39:03 +02:00
|
|
|
nodeID dom.NodeID,
|
2018-10-05 22:35:08 +02:00
|
|
|
) (*HTMLElement, error) {
|
2018-09-18 22:42:38 +02:00
|
|
|
if client == nil {
|
|
|
|
return nil, core.Error(core.ErrMissedArgument, "client")
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:39:03 +02:00
|
|
|
// getting a remote object that represents the current DOM Node
|
2019-07-03 20:05:02 +02:00
|
|
|
args := dom.NewResolveNodeArgs().SetNodeID(nodeID).SetExecutionContextID(exec.ID())
|
2018-10-11 18:39:03 +02:00
|
|
|
|
|
|
|
obj, err := client.DOM.ResolveNode(ctx, args)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-10-11 18:39:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if obj.Object.ObjectID == nil {
|
|
|
|
return nil, core.Error(core.ErrNotFound, fmt.Sprintf("element %d", nodeID))
|
|
|
|
}
|
|
|
|
|
2019-07-03 20:05:02 +02:00
|
|
|
id := HTMLElementIdentity{}
|
|
|
|
id.nodeID = nodeID
|
|
|
|
id.objectID = *obj.Object.ObjectID
|
|
|
|
|
|
|
|
return LoadHTMLElementWithID(
|
|
|
|
ctx,
|
|
|
|
logger,
|
|
|
|
client,
|
|
|
|
broker,
|
|
|
|
input,
|
|
|
|
exec,
|
|
|
|
id,
|
|
|
|
)
|
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2019-07-03 20:05:02 +02:00
|
|
|
func LoadHTMLElementWithID(
|
|
|
|
ctx context.Context,
|
|
|
|
logger *zerolog.Logger,
|
|
|
|
client *cdp.Client,
|
|
|
|
broker *events.EventBroker,
|
|
|
|
input *input.Manager,
|
|
|
|
exec *eval.ExecutionContext,
|
|
|
|
id HTMLElementIdentity,
|
|
|
|
) (*HTMLElement, error) {
|
2018-09-18 22:42:38 +02:00
|
|
|
node, err := client.DOM.DescribeNode(
|
|
|
|
ctx,
|
|
|
|
dom.
|
|
|
|
NewDescribeNodeArgs().
|
2019-07-03 20:05:02 +02:00
|
|
|
SetObjectID(id.objectID).
|
2018-09-27 06:26:56 +02:00
|
|
|
SetDepth(1),
|
2018-09-18 22:42:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-07-03 20:05:02 +02:00
|
|
|
return nil, core.Error(err, strconv.Itoa(int(id.nodeID)))
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:39:03 +02:00
|
|
|
var val string
|
|
|
|
|
|
|
|
if node.Node.Value != nil {
|
|
|
|
val = *node.Node.Value
|
2018-09-27 04:03:06 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
return NewHTMLElement(
|
2018-09-28 06:28:33 +02:00
|
|
|
logger,
|
2018-09-27 04:03:06 +02:00
|
|
|
client,
|
|
|
|
broker,
|
2019-06-20 19:21:48 +02:00
|
|
|
input,
|
2019-06-19 23:58:56 +02:00
|
|
|
exec,
|
2018-09-27 04:03:06 +02:00
|
|
|
id,
|
2018-10-11 18:39:03 +02:00
|
|
|
node.Node.NodeType,
|
|
|
|
node.Node.NodeName,
|
|
|
|
val,
|
|
|
|
createChildrenArray(node.Node.Children),
|
2018-09-27 04:03:06 +02:00
|
|
|
), nil
|
2018-09-25 17:43:58 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func NewHTMLElement(
|
2018-09-28 06:28:33 +02:00
|
|
|
logger *zerolog.Logger,
|
2018-09-25 17:43:58 +02:00
|
|
|
client *cdp.Client,
|
2018-09-27 04:03:06 +02:00
|
|
|
broker *events.EventBroker,
|
2019-06-20 19:21:48 +02:00
|
|
|
input *input.Manager,
|
2019-06-19 23:58:56 +02:00
|
|
|
exec *eval.ExecutionContext,
|
|
|
|
id HTMLElementIdentity,
|
2018-10-11 18:39:03 +02:00
|
|
|
nodeType int,
|
|
|
|
nodeName string,
|
|
|
|
value string,
|
2019-06-19 23:58:56 +02:00
|
|
|
children []HTMLElementIdentity,
|
2018-10-05 22:35:08 +02:00
|
|
|
) *HTMLElement {
|
|
|
|
el := new(HTMLElement)
|
2018-09-28 06:28:33 +02:00
|
|
|
el.logger = logger
|
2018-09-25 17:43:58 +02:00
|
|
|
el.client = client
|
2018-10-11 18:39:03 +02:00
|
|
|
el.events = broker
|
2019-06-20 19:21:48 +02:00
|
|
|
el.input = input
|
2019-06-19 23:58:56 +02:00
|
|
|
el.exec = exec
|
2018-09-27 06:45:07 +02:00
|
|
|
el.connected = values.True
|
2018-09-25 17:43:58 +02:00
|
|
|
el.id = id
|
2019-06-19 23:58:56 +02:00
|
|
|
el.nodeType = common.ToHTMLType(nodeType)
|
2018-10-11 18:39:03 +02:00
|
|
|
el.nodeName = values.NewString(nodeName)
|
2019-07-13 18:55:00 +02:00
|
|
|
el.innerHTML = common.NewLazyValue(el.loadInnerHTML)
|
2018-09-28 06:28:33 +02:00
|
|
|
el.innerText = common.NewLazyValue(el.loadInnerText)
|
|
|
|
el.attributes = common.NewLazyValue(el.loadAttrs)
|
2019-03-13 20:51:30 +02:00
|
|
|
el.style = common.NewLazyValue(el.parseStyle)
|
2018-09-27 06:45:07 +02:00
|
|
|
el.value = values.EmptyString
|
2018-09-28 06:28:33 +02:00
|
|
|
el.loadedChildren = common.NewLazyValue(el.loadChildren)
|
2018-10-11 18:39:03 +02:00
|
|
|
el.value = values.NewString(value)
|
|
|
|
el.children = children
|
2018-09-25 17:43:58 +02:00
|
|
|
|
2018-10-15 23:17:15 +02:00
|
|
|
broker.AddEventListener(events.EventReload, el.handlePageReload)
|
|
|
|
broker.AddEventListener(events.EventAttrModified, el.handleAttrModified)
|
|
|
|
broker.AddEventListener(events.EventAttrRemoved, el.handleAttrRemoved)
|
|
|
|
broker.AddEventListener(events.EventChildNodeCountUpdated, el.handleChildrenCountChanged)
|
|
|
|
broker.AddEventListener(events.EventChildNodeInserted, el.handleChildInserted)
|
|
|
|
broker.AddEventListener(events.EventChildNodeRemoved, el.handleChildRemoved)
|
2018-09-25 17:43:58 +02:00
|
|
|
|
|
|
|
return el
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) Close() error {
|
2018-10-15 23:17:15 +02:00
|
|
|
el.mu.Lock()
|
|
|
|
defer el.mu.Unlock()
|
2018-09-27 17:32:52 +02:00
|
|
|
|
|
|
|
// already closed
|
|
|
|
if !el.connected {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-15 23:17:15 +02:00
|
|
|
el.connected = values.False
|
|
|
|
el.events.RemoveEventListener(events.EventReload, el.handlePageReload)
|
|
|
|
el.events.RemoveEventListener(events.EventAttrModified, el.handleAttrModified)
|
|
|
|
el.events.RemoveEventListener(events.EventAttrRemoved, el.handleAttrRemoved)
|
|
|
|
el.events.RemoveEventListener(events.EventChildNodeCountUpdated, el.handleChildrenCountChanged)
|
|
|
|
el.events.RemoveEventListener(events.EventChildNodeInserted, el.handleChildInserted)
|
|
|
|
el.events.RemoveEventListener(events.EventChildNodeRemoved, el.handleChildRemoved)
|
2018-09-27 17:32:52 +02:00
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) Type() core.Type {
|
2019-02-20 01:10:18 +02:00
|
|
|
return drivers.HTMLElementType
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) MarshalJSON() ([]byte, error) {
|
2019-07-11 23:16:34 +02:00
|
|
|
return json.Marshal(el.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) String() string {
|
2019-09-05 18:17:22 +02:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(drivers.DefaultWaitTimeout)*time.Millisecond)
|
2019-07-11 23:16:34 +02:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
res, err := el.GetInnerHTML(ctx)
|
2018-09-28 01:05:56 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-07-11 23:16:34 +02:00
|
|
|
el.logError(errors.Wrap(err, "HTMLElement.String"))
|
2018-09-28 01:05:56 +02:00
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
return ""
|
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
return res.String()
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-13 19:31:18 +02:00
|
|
|
func (el *HTMLElement) Compare(other core.Value) int64 {
|
2019-02-20 01:10:18 +02:00
|
|
|
switch other.Type() {
|
|
|
|
case drivers.HTMLElementType:
|
|
|
|
other := other.(drivers.HTMLElement)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
return int64(strings.Compare(el.String(), other.String()))
|
2019-02-20 01:10:18 +02:00
|
|
|
default:
|
|
|
|
return drivers.Compare(el.Type(), other.Type())
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
func (el *HTMLElement) Unwrap() interface{} {
|
2018-09-25 17:43:58 +02:00
|
|
|
return el
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 23:42:28 +02:00
|
|
|
func (el *HTMLElement) Hash() uint64 {
|
2018-10-05 21:17:22 +02:00
|
|
|
h := fnv.New64a()
|
2018-09-28 06:28:33 +02:00
|
|
|
|
2018-10-05 21:17:22 +02:00
|
|
|
h.Write([]byte(el.Type().String()))
|
|
|
|
h.Write([]byte(":"))
|
2019-07-11 23:16:34 +02:00
|
|
|
h.Write([]byte(strconv.Itoa(int(el.id.nodeID))))
|
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 (el *HTMLElement) Copy() core.Value {
|
|
|
|
return values.None
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) Iterate(_ context.Context) (core.Iterator, error) {
|
|
|
|
return common.NewIterator(el)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) GetIn(ctx context.Context, path []core.Value) (core.Value, error) {
|
|
|
|
return common.GetInElement(ctx, el, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) SetIn(ctx context.Context, path []core.Value, value core.Value) error {
|
|
|
|
return common.SetInElement(ctx, el, path, value)
|
|
|
|
}
|
|
|
|
|
2019-09-07 03:02:41 +02:00
|
|
|
func (el *HTMLElement) GetValue(ctx context.Context) (core.Value, error) {
|
2019-06-19 23:58:56 +02:00
|
|
|
if el.IsDetached() {
|
2019-09-07 03:02:41 +02:00
|
|
|
return values.None, drivers.ErrDetached
|
2018-09-27 23:19:55 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
val, err := el.exec.ReadProperty(ctx, el.id.objectID, "value")
|
2018-09-27 23:19:55 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-09-07 03:02:41 +02:00
|
|
|
return values.None, err
|
2018-09-27 23:19:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
el.value = val
|
|
|
|
|
2019-09-07 03:02:41 +02:00
|
|
|
return val, nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (el *HTMLElement) SetValue(ctx context.Context, value core.Value) error {
|
2019-06-19 23:58:56 +02:00
|
|
|
if el.IsDetached() {
|
2019-09-07 03:02:41 +02:00
|
|
|
return drivers.ErrDetached
|
2019-02-20 01:10:18 +02:00
|
|
|
}
|
2018-09-27 17:53:26 +02:00
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
return el.client.DOM.SetNodeValue(ctx, dom.NewSetNodeValueArgs(el.id.nodeID, value.String()))
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func (el *HTMLElement) GetNodeType() values.Int {
|
|
|
|
return values.NewInt(common.FromHTMLType(el.nodeType))
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func (el *HTMLElement) GetNodeName() values.String {
|
2018-09-25 17:43:58 +02:00
|
|
|
return el.nodeName
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
func (el *HTMLElement) Length() values.Int {
|
|
|
|
return values.NewInt(len(el.children))
|
|
|
|
}
|
|
|
|
|
2019-03-13 20:51:30 +02:00
|
|
|
func (el *HTMLElement) GetStyles(ctx context.Context) (*values.Object, error) {
|
|
|
|
val, err := el.style.Read(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.NewObject(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
if val == values.None {
|
|
|
|
return values.NewObject(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
styles := val.(*values.Object)
|
|
|
|
|
|
|
|
return styles.Copy().(*values.Object), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) GetStyle(ctx context.Context, name values.String) (core.Value, error) {
|
|
|
|
styles, err := el.style.Read(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
val, found := styles.(*values.Object).Get(name)
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return values.None, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) SetStyles(ctx context.Context, styles *values.Object) error {
|
|
|
|
if styles == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := el.style.Read(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
currentStyles := val.(*values.Object)
|
|
|
|
|
|
|
|
styles.ForEach(func(value core.Value, key string) bool {
|
|
|
|
currentStyles.Set(values.NewString(key), value)
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
str := common.SerializeStyles(ctx, currentStyles)
|
|
|
|
|
|
|
|
return el.SetAttribute(ctx, "style", str)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) SetStyle(ctx context.Context, name values.String, value core.Value) error {
|
|
|
|
val, err := el.style.Read(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
styles := val.(*values.Object)
|
|
|
|
styles.Set(name, value)
|
|
|
|
|
|
|
|
str := common.SerializeStyles(ctx, styles)
|
|
|
|
|
|
|
|
return el.SetAttribute(ctx, "style", str)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) RemoveStyle(ctx context.Context, names ...values.String) error {
|
|
|
|
if len(names) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := el.style.Read(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
styles := val.(*values.Object)
|
|
|
|
|
|
|
|
for _, name := range names {
|
|
|
|
styles.Remove(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
str := common.SerializeStyles(ctx, styles)
|
|
|
|
|
|
|
|
return el.SetAttribute(ctx, "style", str)
|
|
|
|
}
|
|
|
|
|
2019-09-07 03:02:41 +02:00
|
|
|
func (el *HTMLElement) GetAttributes(ctx context.Context) (*values.Object, error) {
|
2019-02-21 04:24:05 +02:00
|
|
|
val, err := el.attributes.Read(ctx)
|
2018-09-27 04:03:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-09-07 03:02:41 +02:00
|
|
|
return values.NewObject(), err
|
2018-09-27 04:03:06 +02:00
|
|
|
}
|
|
|
|
|
2019-02-26 04:46:39 +02:00
|
|
|
attrs := val.(*values.Object)
|
|
|
|
|
2018-09-27 17:53:26 +02:00
|
|
|
// returning shallow copy
|
2019-09-07 03:02:41 +02:00
|
|
|
return attrs.Copy().(*values.Object), nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 03:02:41 +02:00
|
|
|
func (el *HTMLElement) GetAttribute(ctx context.Context, name values.String) (core.Value, error) {
|
2019-02-21 04:24:05 +02:00
|
|
|
attrs, err := el.attributes.Read(ctx)
|
2018-09-27 04:03:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-09-07 03:02:41 +02:00
|
|
|
return values.None, err
|
2018-09-27 04:03:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
val, found := attrs.(*values.Object).Get(name)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if !found {
|
2019-09-07 03:02:41 +02:00
|
|
|
return values.None, nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 03:02:41 +02:00
|
|
|
return val, nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-03-13 20:51:30 +02:00
|
|
|
func (el *HTMLElement) SetAttributes(ctx context.Context, attrs *values.Object) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
attrs.ForEach(func(value core.Value, key string) bool {
|
|
|
|
err = el.SetAttribute(ctx, values.NewString(key), values.NewString(value.String()))
|
|
|
|
|
|
|
|
return err == nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (el *HTMLElement) SetAttribute(ctx context.Context, name, value values.String) error {
|
2019-02-20 01:10:18 +02:00
|
|
|
return el.client.DOM.SetAttributeValue(
|
2019-02-21 04:24:05 +02:00
|
|
|
ctx,
|
2019-02-20 01:10:18 +02:00
|
|
|
dom.NewSetAttributeValueArgs(el.id.nodeID, string(name), string(value)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-03-13 20:51:30 +02:00
|
|
|
func (el *HTMLElement) RemoveAttribute(ctx context.Context, names ...values.String) error {
|
|
|
|
for _, name := range names {
|
|
|
|
err := el.client.DOM.RemoveAttribute(
|
|
|
|
ctx,
|
|
|
|
dom.NewRemoveAttributeArgs(el.id.nodeID, name.String()),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-07 03:02:41 +02:00
|
|
|
func (el *HTMLElement) GetChildNodes(ctx context.Context) (*values.Array, error) {
|
2019-02-21 04:24:05 +02:00
|
|
|
val, err := el.loadedChildren.Read(ctx)
|
2018-09-27 04:03:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-09-07 03:02:41 +02:00
|
|
|
return values.NewArray(0), err
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 03:02:41 +02:00
|
|
|
return val.Copy().(*values.Array), nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 03:02:41 +02:00
|
|
|
func (el *HTMLElement) GetChildNode(ctx context.Context, idx values.Int) (core.Value, error) {
|
2019-02-21 04:24:05 +02:00
|
|
|
val, err := el.loadedChildren.Read(ctx)
|
2018-09-27 04:03:06 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-09-07 03:02:41 +02:00
|
|
|
return values.None, err
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 03:02:41 +02:00
|
|
|
return val.(*values.Array).Get(idx), nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 03:02:41 +02:00
|
|
|
func (el *HTMLElement) QuerySelector(ctx context.Context, selector values.String) (core.Value, error) {
|
2019-06-19 23:58:56 +02:00
|
|
|
if el.IsDetached() {
|
2019-09-07 03:02:41 +02:00
|
|
|
return values.None, drivers.ErrDetached
|
2018-09-27 06:45:07 +02:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:39:03 +02:00
|
|
|
selectorArgs := dom.NewQuerySelectorArgs(el.id.nodeID, selector.String())
|
2018-09-18 22:42:38 +02:00
|
|
|
found, err := el.client.DOM.QuerySelector(ctx, selectorArgs)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-09-07 03:02:41 +02:00
|
|
|
return values.None, err
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:39:03 +02:00
|
|
|
if found.NodeID == emptyNodeID {
|
2019-09-07 03:02:41 +02:00
|
|
|
return values.None, nil
|
2018-10-11 18:39:03 +02:00
|
|
|
}
|
|
|
|
|
2019-07-03 20:05:02 +02:00
|
|
|
res, err := LoadHTMLElement(ctx, el.logger, el.client, el.events, el.input, el.exec, found.NodeID)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-09-07 03:02:41 +02:00
|
|
|
return values.None, nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 03:02:41 +02:00
|
|
|
return res, nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 03:02:41 +02:00
|
|
|
func (el *HTMLElement) QuerySelectorAll(ctx context.Context, selector values.String) (*values.Array, error) {
|
2019-06-19 23:58:56 +02:00
|
|
|
if el.IsDetached() {
|
2019-09-07 03:02:41 +02:00
|
|
|
return values.NewArray(0), drivers.ErrDetached
|
2018-09-27 06:45:07 +02:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:39:03 +02:00
|
|
|
selectorArgs := dom.NewQuerySelectorAllArgs(el.id.nodeID, selector.String())
|
2018-09-18 22:42:38 +02:00
|
|
|
res, err := el.client.DOM.QuerySelectorAll(ctx, selectorArgs)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-09-07 03:02:41 +02:00
|
|
|
return values.NewArray(0), err
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
arr := values.NewArray(len(res.NodeIDs))
|
|
|
|
|
|
|
|
for _, id := range res.NodeIDs {
|
2018-10-11 18:39:03 +02:00
|
|
|
if id == emptyNodeID {
|
|
|
|
el.logError(err).
|
|
|
|
Str("selector", selector.String()).
|
|
|
|
Msg("failed to find a node by selector. returned 0 NodeID")
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-03 20:05:02 +02:00
|
|
|
childEl, err := LoadHTMLElement(ctx, el.logger, el.client, el.events, el.input, el.exec, id)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2018-10-08 03:04:36 +02:00
|
|
|
// close elements that are already loaded, but won't be used because of the error
|
|
|
|
if arr.Length() > 0 {
|
|
|
|
arr.ForEach(func(e core.Value, _ int) bool {
|
|
|
|
e.(*HTMLElement).Close()
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-07 03:02:41 +02:00
|
|
|
return values.NewArray(0), err
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
arr.Push(childEl)
|
|
|
|
}
|
|
|
|
|
2019-09-07 03:02:41 +02:00
|
|
|
return arr, nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-07-03 20:05:02 +02:00
|
|
|
func (el *HTMLElement) XPath(ctx context.Context, expression values.String) (result core.Value, err error) {
|
|
|
|
exp, err := expression.MarshalJSON()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2019-07-17 00:17:42 +02:00
|
|
|
out, err := el.exec.EvalWithArgumentsAndReturnReference(ctx, templates.XPath(),
|
2019-07-03 20:05:02 +02:00
|
|
|
runtime.CallArgument{
|
|
|
|
ObjectID: &el.id.objectID,
|
|
|
|
},
|
|
|
|
runtime.CallArgument{
|
|
|
|
Value: json.RawMessage(exp),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
typeName := out.Type
|
|
|
|
|
|
|
|
// checking whether it's actually an array
|
|
|
|
if typeName == "object" {
|
2019-07-17 00:17:42 +02:00
|
|
|
if out.ClassName != nil && *out.ClassName == "Array" {
|
2019-07-03 20:05:02 +02:00
|
|
|
typeName = "array"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch typeName {
|
|
|
|
case "string", "number", "boolean":
|
|
|
|
return eval.Unmarshal(&out)
|
|
|
|
case "array":
|
|
|
|
if out.ObjectID == nil {
|
|
|
|
return values.None, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
props, err := el.client.Runtime.GetProperties(ctx, runtime.NewGetPropertiesArgs(*out.ObjectID).SetOwnProperties(true))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if props.ExceptionDetails != nil {
|
|
|
|
exception := *props.ExceptionDetails
|
|
|
|
|
|
|
|
return values.None, errors.New(exception.Text)
|
|
|
|
}
|
|
|
|
|
|
|
|
result := values.NewArray(len(props.Result))
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
result.ForEach(func(value core.Value, idx int) bool {
|
|
|
|
el, ok := value.(*HTMLElement)
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
el.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for _, descr := range props.Result {
|
|
|
|
if !descr.Enumerable {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if descr.Value == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
repl, err := el.client.DOM.RequestNode(ctx, dom.NewRequestNodeArgs(*descr.Value.ObjectID))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
el, err := LoadHTMLElementWithID(
|
|
|
|
ctx,
|
|
|
|
el.logger,
|
|
|
|
el.client,
|
|
|
|
el.events,
|
|
|
|
el.input,
|
|
|
|
el.exec,
|
|
|
|
HTMLElementIdentity{
|
|
|
|
nodeID: repl.NodeID,
|
|
|
|
objectID: *descr.Value.ObjectID,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result.Push(el)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
case "object":
|
|
|
|
if out.ObjectID == nil {
|
|
|
|
return values.None, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
repl, err := el.client.DOM.RequestNode(ctx, dom.NewRequestNodeArgs(*out.ObjectID))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return LoadHTMLElementWithID(
|
|
|
|
ctx,
|
|
|
|
el.logger,
|
|
|
|
el.client,
|
|
|
|
el.events,
|
|
|
|
el.input,
|
|
|
|
el.exec,
|
|
|
|
HTMLElementIdentity{
|
|
|
|
nodeID: repl.NodeID,
|
|
|
|
objectID: *out.ObjectID,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
default:
|
|
|
|
return values.None, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
func (el *HTMLElement) GetInnerText(ctx context.Context) (values.String, error) {
|
2019-02-21 04:24:05 +02:00
|
|
|
val, err := el.innerText.Read(ctx)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-07-11 23:16:34 +02:00
|
|
|
return values.EmptyString, err
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-11-22 05:45:00 +02:00
|
|
|
if val == values.None {
|
2019-07-11 23:16:34 +02:00
|
|
|
return values.EmptyString, nil
|
2018-11-22 05:45:00 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
return val.(values.String), nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
func (el *HTMLElement) SetInnerText(ctx context.Context, innerText values.String) error {
|
2019-06-19 23:58:56 +02:00
|
|
|
if el.IsDetached() {
|
2019-07-11 23:16:34 +02:00
|
|
|
return drivers.ErrDetached
|
2018-10-09 02:20:40 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
el.innerText.Reset()
|
2018-10-09 02:20:40 +02:00
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
return setInnerText(ctx, el.client, el.exec, el.id, innerText)
|
|
|
|
}
|
2018-10-09 02:20:40 +02:00
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
func (el *HTMLElement) GetInnerTextBySelector(ctx context.Context, selector values.String) (values.String, error) {
|
|
|
|
if el.IsDetached() {
|
|
|
|
return values.EmptyString, drivers.ErrDetached
|
2018-10-09 02:20:40 +02:00
|
|
|
}
|
|
|
|
|
2019-08-06 01:57:02 +02:00
|
|
|
sel, err := selector.MarshalJSON()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := el.exec.EvalWithArgumentsAndReturnValue(
|
|
|
|
ctx,
|
|
|
|
templates.GetInnerTextBySelector(),
|
|
|
|
runtime.CallArgument{
|
|
|
|
ObjectID: &el.id.objectID,
|
|
|
|
},
|
|
|
|
runtime.CallArgument{
|
|
|
|
Value: sel,
|
|
|
|
},
|
|
|
|
)
|
2018-10-11 18:39:03 +02:00
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
2018-10-11 18:39:03 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
return values.NewString(out.String()), nil
|
|
|
|
}
|
2018-10-11 18:39:03 +02:00
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
func (el *HTMLElement) SetInnerTextBySelector(ctx context.Context, selector, innerText values.String) error {
|
|
|
|
if el.IsDetached() {
|
|
|
|
return drivers.ErrDetached
|
2018-10-11 18:39:03 +02:00
|
|
|
}
|
|
|
|
|
2019-08-06 01:57:02 +02:00
|
|
|
sel, err := selector.MarshalJSON()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := innerText.MarshalJSON()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return el.exec.EvalWithArguments(
|
|
|
|
ctx,
|
|
|
|
templates.SetInnerTextBySelector(),
|
|
|
|
runtime.CallArgument{
|
|
|
|
ObjectID: &el.id.objectID,
|
|
|
|
},
|
|
|
|
runtime.CallArgument{
|
|
|
|
Value: sel,
|
|
|
|
},
|
|
|
|
runtime.CallArgument{
|
|
|
|
Value: val,
|
|
|
|
},
|
|
|
|
)
|
2019-07-11 23:16:34 +02:00
|
|
|
}
|
2018-10-11 18:39:03 +02:00
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
func (el *HTMLElement) GetInnerTextBySelectorAll(ctx context.Context, selector values.String) (*values.Array, error) {
|
|
|
|
if el.IsDetached() {
|
|
|
|
return values.NewArray(0), drivers.ErrDetached
|
2018-10-11 18:39:03 +02:00
|
|
|
}
|
|
|
|
|
2019-08-06 01:57:02 +02:00
|
|
|
sel, err := selector.MarshalJSON()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := el.exec.EvalWithArgumentsAndReturnValue(
|
|
|
|
ctx,
|
|
|
|
templates.GetInnerTextBySelectorAll(),
|
|
|
|
runtime.CallArgument{
|
|
|
|
ObjectID: &el.id.objectID,
|
|
|
|
},
|
|
|
|
runtime.CallArgument{
|
|
|
|
Value: sel,
|
|
|
|
},
|
|
|
|
)
|
2018-10-11 18:39:03 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-07-11 23:16:34 +02:00
|
|
|
return values.NewArray(0), err
|
|
|
|
}
|
|
|
|
|
|
|
|
arr, ok := out.(*values.Array)
|
2018-10-09 02:20:40 +02:00
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
if !ok {
|
|
|
|
return values.NewArray(0), errors.New("unexpected output")
|
2018-10-09 02:20:40 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
return arr, nil
|
2018-10-09 02:20:40 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
func (el *HTMLElement) GetInnerHTML(ctx context.Context) (values.String, error) {
|
|
|
|
val, err := el.innerHTML.Read(ctx)
|
2018-10-09 02:20:40 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-07-11 23:16:34 +02:00
|
|
|
return values.EmptyString, err
|
2018-10-09 02:20:40 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
if val == values.None {
|
|
|
|
return values.EmptyString, nil
|
2018-10-09 02:20:40 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
return val.(values.String), nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
func (el *HTMLElement) SetInnerHTML(ctx context.Context, innerHTML values.String) error {
|
2019-06-19 23:58:56 +02:00
|
|
|
if el.IsDetached() {
|
2019-07-11 23:16:34 +02:00
|
|
|
return drivers.ErrDetached
|
2018-10-09 02:20:40 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
el.innerHTML.Reset()
|
2018-10-09 02:20:40 +02:00
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
return setInnerHTML(ctx, el.client, el.exec, el.id, innerHTML)
|
|
|
|
}
|
2018-10-09 02:20:40 +02:00
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
func (el *HTMLElement) GetInnerHTMLBySelector(ctx context.Context, selector values.String) (values.String, error) {
|
|
|
|
if el.IsDetached() {
|
|
|
|
return values.EmptyString, drivers.ErrDetached
|
2018-10-09 02:20:40 +02:00
|
|
|
}
|
|
|
|
|
2019-08-06 01:57:02 +02:00
|
|
|
sel, err := selector.MarshalJSON()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := el.exec.EvalWithArgumentsAndReturnValue(
|
|
|
|
ctx,
|
|
|
|
templates.GetInnerHTMLBySelector(),
|
|
|
|
runtime.CallArgument{
|
|
|
|
ObjectID: &el.id.objectID,
|
|
|
|
},
|
|
|
|
runtime.CallArgument{
|
|
|
|
Value: sel,
|
|
|
|
},
|
|
|
|
)
|
2018-10-09 02:20:40 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-07-11 23:16:34 +02:00
|
|
|
return values.EmptyString, err
|
2018-10-09 02:20:40 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
return values.NewString(out.String()), nil
|
2018-10-09 02:20:40 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
func (el *HTMLElement) SetInnerHTMLBySelector(ctx context.Context, selector, innerHTML values.String) error {
|
|
|
|
if el.IsDetached() {
|
|
|
|
return drivers.ErrDetached
|
|
|
|
}
|
2018-10-09 02:20:40 +02:00
|
|
|
|
2019-08-06 01:57:02 +02:00
|
|
|
sel, err := selector.MarshalJSON()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := innerHTML.MarshalJSON()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return el.exec.EvalWithArguments(
|
|
|
|
ctx,
|
|
|
|
templates.SetInnerHTMLBySelector(),
|
|
|
|
runtime.CallArgument{
|
|
|
|
ObjectID: &el.id.objectID,
|
|
|
|
},
|
|
|
|
runtime.CallArgument{
|
|
|
|
Value: sel,
|
|
|
|
},
|
|
|
|
runtime.CallArgument{
|
|
|
|
Value: val,
|
|
|
|
},
|
|
|
|
)
|
2019-07-11 23:16:34 +02:00
|
|
|
}
|
2018-10-09 02:20:40 +02:00
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
func (el *HTMLElement) GetInnerHTMLBySelectorAll(ctx context.Context, selector values.String) (*values.Array, error) {
|
|
|
|
if el.IsDetached() {
|
|
|
|
return values.NewArray(0), drivers.ErrDetached
|
2018-10-09 02:20:40 +02:00
|
|
|
}
|
|
|
|
|
2019-08-06 01:57:02 +02:00
|
|
|
sel, err := selector.MarshalJSON()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.NewArray(0), err
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := el.exec.EvalWithArgumentsAndReturnValue(
|
|
|
|
ctx,
|
|
|
|
templates.GetInnerHTMLBySelectorAll(),
|
|
|
|
runtime.CallArgument{
|
|
|
|
ObjectID: &el.id.objectID,
|
|
|
|
},
|
|
|
|
runtime.CallArgument{
|
|
|
|
Value: sel,
|
|
|
|
},
|
|
|
|
)
|
2018-10-09 02:20:40 +02:00
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return values.NewArray(0), err
|
|
|
|
}
|
2018-10-09 02:20:40 +02:00
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
arr, ok := out.(*values.Array)
|
2018-10-09 02:20:40 +02:00
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
if !ok {
|
|
|
|
return values.NewArray(0), errors.New("unexpected output")
|
2018-10-09 02:20:40 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
return arr, nil
|
2018-10-09 02:20:40 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 03:02:41 +02:00
|
|
|
func (el *HTMLElement) CountBySelector(ctx context.Context, selector values.String) (values.Int, error) {
|
2019-06-19 23:58:56 +02:00
|
|
|
if el.IsDetached() {
|
2019-09-07 03:02:41 +02:00
|
|
|
return values.ZeroInt, drivers.ErrDetached
|
2018-10-13 03:52:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
selectorArgs := dom.NewQuerySelectorAllArgs(el.id.nodeID, selector.String())
|
|
|
|
res, err := el.client.DOM.QuerySelectorAll(ctx, selectorArgs)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-09-07 03:02:41 +02:00
|
|
|
return values.ZeroInt, err
|
2018-10-13 03:52:27 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 03:02:41 +02:00
|
|
|
return values.NewInt(len(res.NodeIDs)), nil
|
2018-10-13 03:52:27 +02:00
|
|
|
}
|
|
|
|
|
2019-07-26 19:22:06 +02:00
|
|
|
func (el *HTMLElement) ExistsBySelector(ctx context.Context, selector values.String) (values.Boolean, error) {
|
2019-06-19 23:58:56 +02:00
|
|
|
if el.IsDetached() {
|
2019-07-26 19:22:06 +02:00
|
|
|
return values.False, drivers.ErrDetached
|
2018-12-22 06:14:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Can we use RemoteObjectID or BackendID instead of NodeId?
|
|
|
|
selectorArgs := dom.NewQuerySelectorArgs(el.id.nodeID, selector.String())
|
|
|
|
res, err := el.client.DOM.QuerySelector(ctx, selectorArgs)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-07-26 19:22:06 +02:00
|
|
|
return values.False, err
|
2018-12-22 06:14:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if res.NodeID == 0 {
|
2019-07-26 19:22:06 +02:00
|
|
|
return values.False, nil
|
2018-12-22 06:14:41 +02:00
|
|
|
}
|
|
|
|
|
2019-07-26 19:22:06 +02:00
|
|
|
return values.True, nil
|
2018-12-22 06:14:41 +02:00
|
|
|
}
|
|
|
|
|
2019-03-07 04:52:41 +02:00
|
|
|
func (el *HTMLElement) WaitForClass(ctx context.Context, class values.String, when drivers.WaitEvent) error {
|
2018-11-15 21:33:53 +02:00
|
|
|
task := events.NewWaitTask(
|
2019-02-21 04:24:05 +02:00
|
|
|
func(ctx2 context.Context) (core.Value, error) {
|
2019-09-07 03:02:41 +02:00
|
|
|
current, err := el.GetAttribute(ctx2, "class")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, nil
|
|
|
|
}
|
2018-11-15 21:33:53 +02:00
|
|
|
|
2019-02-13 19:31:18 +02:00
|
|
|
if current.Type() != types.String {
|
2018-11-15 21:33:53 +02:00
|
|
|
return values.None, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
str := current.(values.String)
|
|
|
|
classStr := string(class)
|
|
|
|
classes := strings.Split(string(str), " ")
|
|
|
|
|
2019-03-07 04:52:41 +02:00
|
|
|
if when != drivers.WaitEventAbsence {
|
|
|
|
for _, c := range classes {
|
|
|
|
if c == classStr {
|
|
|
|
// The value does not really matter if it's not None
|
|
|
|
// None indicates that operation needs to be repeated
|
|
|
|
return values.True, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var found values.Boolean
|
|
|
|
|
|
|
|
for _, c := range classes {
|
|
|
|
if c == classStr {
|
|
|
|
found = values.True
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found == values.False {
|
|
|
|
// The value does not really matter if it's not None
|
|
|
|
// None indicates that operation needs to be repeated
|
|
|
|
return values.False, nil
|
2018-11-15 21:33:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.None, nil
|
|
|
|
},
|
|
|
|
events.DefaultPolling,
|
|
|
|
)
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
_, err := task.Run(ctx)
|
2018-11-15 21:33:53 +02:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-14 04:50:29 +02:00
|
|
|
func (el *HTMLElement) WaitForAttribute(
|
|
|
|
ctx context.Context,
|
|
|
|
name values.String,
|
|
|
|
value core.Value,
|
|
|
|
when drivers.WaitEvent,
|
|
|
|
) error {
|
2019-03-15 04:10:15 +02:00
|
|
|
task := events.NewValueWaitTask(when, value, func(ctx context.Context) (core.Value, error) {
|
2019-09-07 03:02:41 +02:00
|
|
|
return el.GetAttribute(ctx, name)
|
2019-03-15 04:10:15 +02:00
|
|
|
}, events.DefaultPolling)
|
2019-03-14 04:50:29 +02:00
|
|
|
|
2019-03-15 04:10:15 +02:00
|
|
|
_, err := task.Run(ctx)
|
2019-03-14 04:50:29 +02:00
|
|
|
|
2019-03-15 04:10:15 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) WaitForStyle(ctx context.Context, name values.String, value core.Value, when drivers.WaitEvent) error {
|
|
|
|
task := events.NewValueWaitTask(when, value, func(ctx context.Context) (core.Value, error) {
|
|
|
|
return el.GetStyle(ctx, name)
|
|
|
|
}, events.DefaultPolling)
|
2019-03-14 04:50:29 +02:00
|
|
|
|
|
|
|
_, err := task.Run(ctx)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-07 07:59:32 +02:00
|
|
|
func (el *HTMLElement) Click(ctx context.Context, count values.Int) error {
|
|
|
|
return el.input.Click(ctx, el.id.objectID, int(count))
|
2018-09-25 17:43:58 +02:00
|
|
|
}
|
2018-09-27 06:26:56 +02:00
|
|
|
|
2019-09-07 07:59:32 +02:00
|
|
|
func (el *HTMLElement) ClickBySelector(ctx context.Context, selector values.String, count values.Int) error {
|
|
|
|
return el.input.ClickBySelector(ctx, el.id.nodeID, selector.String(), int(count))
|
2019-08-25 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 07:59:32 +02:00
|
|
|
func (el *HTMLElement) ClickBySelectorAll(ctx context.Context, selector values.String, count values.Int) error {
|
|
|
|
return el.input.ClickBySelectorAll(ctx, el.id.nodeID, selector.String(), int(count))
|
2019-08-25 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (el *HTMLElement) Input(ctx context.Context, value core.Value, delay values.Int) error {
|
2019-06-20 19:21:48 +02:00
|
|
|
if el.GetNodeName() != "INPUT" {
|
|
|
|
return core.Error(core.ErrInvalidOperation, "element is not an <input> element.")
|
2018-10-09 05:07:08 +02:00
|
|
|
}
|
|
|
|
|
2019-09-01 22:09:35 +02:00
|
|
|
return el.input.Type(ctx, el.id.objectID, input.TypeParams{
|
2019-09-05 18:17:22 +02:00
|
|
|
Text: value.String(),
|
2019-09-01 22:09:35 +02:00
|
|
|
Clear: false,
|
2019-09-05 18:17:22 +02:00
|
|
|
Delay: time.Duration(delay) * time.Millisecond,
|
2019-09-01 22:09:35 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) InputBySelector(ctx context.Context, selector values.String, value core.Value, delay values.Int) error {
|
2019-09-05 18:17:22 +02:00
|
|
|
return el.input.TypeBySelector(ctx, el.id.nodeID, selector.String(), input.TypeParams{
|
|
|
|
Text: value.String(),
|
2019-09-01 22:09:35 +02:00
|
|
|
Clear: false,
|
2019-09-05 18:17:22 +02:00
|
|
|
Delay: time.Duration(delay) * time.Millisecond,
|
2019-09-01 22:09:35 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) Clear(ctx context.Context) error {
|
|
|
|
return el.input.Clear(ctx, el.id.objectID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (el *HTMLElement) ClearBySelector(ctx context.Context, selector values.String) error {
|
2019-09-05 18:17:22 +02:00
|
|
|
return el.input.ClearBySelector(ctx, el.id.nodeID, selector.String())
|
2018-09-28 01:05:56 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (el *HTMLElement) Select(ctx context.Context, value *values.Array) (*values.Array, error) {
|
2019-07-17 00:17:42 +02:00
|
|
|
return el.input.Select(ctx, el.id.objectID, value)
|
2018-11-12 21:53:36 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 05:15:27 +02:00
|
|
|
func (el *HTMLElement) SelectBySelector(ctx context.Context, selector values.String, value *values.Array) (*values.Array, error) {
|
|
|
|
return el.input.SelectBySelector(ctx, el.id.nodeID, selector.String(), value)
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (el *HTMLElement) ScrollIntoView(ctx context.Context) error {
|
2019-07-17 00:17:42 +02:00
|
|
|
return el.input.ScrollIntoView(ctx, el.id.objectID)
|
2018-11-15 21:33:53 +02:00
|
|
|
}
|
|
|
|
|
2019-07-23 22:13:04 +02:00
|
|
|
func (el *HTMLElement) Focus(ctx context.Context) error {
|
|
|
|
return el.input.Focus(ctx, el.id.objectID)
|
|
|
|
}
|
|
|
|
|
2019-09-07 05:15:27 +02:00
|
|
|
func (el *HTMLElement) FocusBySelector(ctx context.Context, selector values.String) error {
|
|
|
|
return el.input.FocusBySelector(ctx, el.id.nodeID, selector.String())
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (el *HTMLElement) Hover(ctx context.Context) error {
|
2019-07-17 00:17:42 +02:00
|
|
|
return el.input.MoveMouse(ctx, el.id.objectID)
|
2018-11-15 21:33:53 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 05:15:27 +02:00
|
|
|
func (el *HTMLElement) HoverBySelector(ctx context.Context, selector values.String) error {
|
|
|
|
return el.input.MoveMouseBySelector(ctx, el.id.nodeID, selector.String())
|
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
func (el *HTMLElement) IsDetached() values.Boolean {
|
2018-10-15 23:17:15 +02:00
|
|
|
el.mu.Lock()
|
|
|
|
defer el.mu.Unlock()
|
2018-09-27 06:45:07 +02:00
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
return !el.connected
|
2018-09-27 06:26:56 +02:00
|
|
|
}
|
2018-09-27 17:32:52 +02:00
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
func (el *HTMLElement) loadInnerHTML(ctx context.Context) (core.Value, error) {
|
|
|
|
if el.IsDetached() {
|
|
|
|
return values.None, drivers.ErrDetached
|
|
|
|
}
|
|
|
|
|
|
|
|
return getInnerHTML(ctx, el.client, el.exec, el.id, el.nodeType)
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (el *HTMLElement) loadInnerText(ctx context.Context) (core.Value, error) {
|
2019-06-19 23:58:56 +02:00
|
|
|
if !el.IsDetached() {
|
2019-07-11 23:16:34 +02:00
|
|
|
text, err := getInnerText(ctx, el.client, el.exec, el.id, el.nodeType)
|
2018-10-11 18:39:03 +02:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return text, nil
|
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
el.logError(err).Msg("failed to get inner text from remote object")
|
2018-10-11 18:39:03 +02:00
|
|
|
|
2018-11-22 05:45:00 +02:00
|
|
|
// and just parse cached innerHTML
|
2018-10-11 18:39:03 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
h, err := el.GetInnerHTML(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
el.logError(err).Msg("failed to get inner html from remote object")
|
|
|
|
|
|
|
|
return values.None, err
|
|
|
|
}
|
2018-09-28 06:28:33 +02:00
|
|
|
|
|
|
|
if h == values.EmptyString {
|
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:39:03 +02:00
|
|
|
parsed, err := parseInnerText(h.String())
|
2018-09-28 06:28:33 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2018-10-11 18:39:03 +02:00
|
|
|
el.logError(err).Msg("failed to parse inner html")
|
2018-09-28 06:28:33 +02:00
|
|
|
|
|
|
|
return values.EmptyString, err
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:39:03 +02:00
|
|
|
return parsed, nil
|
2018-09-28 06:28:33 +02:00
|
|
|
}
|
|
|
|
|
2019-03-13 20:51:30 +02:00
|
|
|
func (el *HTMLElement) loadAttrs(ctx context.Context) (core.Value, error) {
|
|
|
|
repl, err := el.client.DOM.GetAttributes(ctx, dom.NewGetAttributesArgs(el.id.nodeID))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseAttrs(repl.Attributes), nil
|
2018-09-28 06:28:33 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (el *HTMLElement) loadChildren(ctx context.Context) (core.Value, error) {
|
2019-06-19 23:58:56 +02:00
|
|
|
if el.IsDetached() {
|
2018-09-28 06:28:33 +02:00
|
|
|
return values.NewArray(0), nil
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:39:03 +02:00
|
|
|
loaded := values.NewArray(len(el.children))
|
|
|
|
|
|
|
|
for _, childID := range el.children {
|
2019-06-19 23:58:56 +02:00
|
|
|
child, err := LoadHTMLElement(
|
2018-10-11 18:39:03 +02:00
|
|
|
ctx,
|
|
|
|
el.logger,
|
|
|
|
el.client,
|
|
|
|
el.events,
|
2019-06-20 19:21:48 +02:00
|
|
|
el.input,
|
2019-06-19 23:58:56 +02:00
|
|
|
el.exec,
|
2018-10-11 18:39:03 +02:00
|
|
|
childID.nodeID,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-02-20 01:10:18 +02:00
|
|
|
el.logError(err).Msg("failed to load child elements")
|
2018-10-11 18:39:03 +02:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
2018-09-28 06:28:33 +02:00
|
|
|
|
2018-10-11 18:39:03 +02:00
|
|
|
loaded.Push(child)
|
2018-09-28 06:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return loaded, nil
|
|
|
|
}
|
|
|
|
|
2019-03-13 20:51:30 +02:00
|
|
|
func (el *HTMLElement) parseStyle(ctx context.Context) (core.Value, error) {
|
2019-09-07 03:02:41 +02:00
|
|
|
value, err := el.GetAttribute(ctx, "style")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
2019-03-13 20:51:30 +02:00
|
|
|
|
|
|
|
if value == values.None {
|
|
|
|
return values.NewObject(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if value.Type() != types.String {
|
|
|
|
return values.NewObject(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return common.DeserializeStyles(value.(values.String))
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (el *HTMLElement) handlePageReload(_ context.Context, _ interface{}) {
|
2018-09-27 17:32:52 +02:00
|
|
|
el.Close()
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (el *HTMLElement) handleAttrModified(ctx context.Context, message interface{}) {
|
2018-09-27 17:32:52 +02:00
|
|
|
reply, ok := message.(*dom.AttributeModifiedReply)
|
|
|
|
|
|
|
|
// well....
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
// it's not for this el
|
2018-10-11 18:39:03 +02:00
|
|
|
if reply.NodeID != el.id.nodeID {
|
2018-09-27 17:32:52 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-13 20:51:30 +02:00
|
|
|
// they are not event loaded
|
|
|
|
// just ignore the event
|
|
|
|
if !el.attributes.Ready() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-01 22:09:35 +02:00
|
|
|
if el.IsDetached() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-16 16:34:13 +02:00
|
|
|
el.attributes.Mutate(ctx, func(v core.Value, err error) {
|
2018-10-07 04:33:39 +02:00
|
|
|
if err != nil {
|
2019-02-20 01:10:18 +02:00
|
|
|
el.logError(err).Msg("failed to update element")
|
2018-09-27 17:32:52 +02:00
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
return
|
|
|
|
}
|
2018-09-27 17:53:26 +02:00
|
|
|
|
2019-03-13 20:51:30 +02:00
|
|
|
if reply.Name == "style" {
|
|
|
|
el.style.Reset()
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
attrs, ok := v.(*values.Object)
|
2018-09-27 17:32:52 +02:00
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2018-09-27 17:32:52 +02:00
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
attrs.Set(values.NewString(reply.Name), values.NewString(reply.Value))
|
|
|
|
})
|
2018-09-27 17:32:52 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (el *HTMLElement) handleAttrRemoved(ctx context.Context, message interface{}) {
|
2018-09-27 17:32:52 +02:00
|
|
|
reply, ok := message.(*dom.AttributeRemovedReply)
|
|
|
|
|
|
|
|
// well....
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-20 01:10:18 +02:00
|
|
|
// it's not for this el
|
2018-10-11 18:39:03 +02:00
|
|
|
if reply.NodeID != el.id.nodeID {
|
2018-09-27 17:32:52 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// they are not event loaded
|
|
|
|
// just ignore the event
|
|
|
|
if !el.attributes.Ready() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-01 22:09:35 +02:00
|
|
|
if el.IsDetached() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-16 16:34:13 +02:00
|
|
|
el.attributes.Mutate(ctx, func(v core.Value, err error) {
|
2018-10-07 04:33:39 +02:00
|
|
|
if err != nil {
|
2019-02-20 01:10:18 +02:00
|
|
|
el.logError(err).Msg("failed to update element")
|
2018-09-27 17:32:52 +02:00
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
return
|
|
|
|
}
|
2018-09-27 17:53:26 +02:00
|
|
|
|
2019-03-13 20:51:30 +02:00
|
|
|
if reply.Name == "style" {
|
|
|
|
el.style.Reset()
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
attrs, ok := v.(*values.Object)
|
2018-09-27 17:32:52 +02:00
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2018-09-27 17:32:52 +02:00
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
attrs.Remove(values.NewString(reply.Name))
|
|
|
|
})
|
2018-09-27 17:32:52 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (el *HTMLElement) handleChildrenCountChanged(ctx context.Context, message interface{}) {
|
2018-09-27 17:32:52 +02:00
|
|
|
reply, ok := message.(*dom.ChildNodeCountUpdatedReply)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:39:03 +02:00
|
|
|
if reply.NodeID != el.id.nodeID {
|
2018-09-27 17:32:52 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-01 22:09:35 +02:00
|
|
|
if el.IsDetached() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
el.mu.Lock()
|
|
|
|
defer el.mu.Unlock()
|
|
|
|
|
2018-10-11 18:39:03 +02:00
|
|
|
node, err := el.client.DOM.DescribeNode(
|
|
|
|
ctx,
|
|
|
|
dom.NewDescribeNodeArgs().SetObjectID(el.id.objectID),
|
|
|
|
)
|
2018-09-27 17:32:52 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-02-20 01:10:18 +02:00
|
|
|
el.logError(err).Msg("failed to update element")
|
2018-09-28 06:28:33 +02:00
|
|
|
|
2018-09-27 17:32:52 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
el.children = createChildrenArray(node.Node.Children)
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (el *HTMLElement) handleChildInserted(ctx context.Context, message interface{}) {
|
2018-09-27 17:32:52 +02:00
|
|
|
reply, ok := message.(*dom.ChildNodeInsertedReply)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:39:03 +02:00
|
|
|
if reply.ParentNodeID != el.id.nodeID {
|
2018-09-27 17:32:52 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
targetIDx := -1
|
|
|
|
prevID := reply.PreviousNodeID
|
|
|
|
nextID := reply.Node.NodeID
|
2018-09-27 17:32:52 +02:00
|
|
|
|
2019-09-01 22:09:35 +02:00
|
|
|
if el.IsDetached() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-15 23:17:15 +02:00
|
|
|
el.mu.Lock()
|
|
|
|
defer el.mu.Unlock()
|
2018-09-27 17:32:52 +02:00
|
|
|
|
|
|
|
for idx, id := range el.children {
|
2018-10-11 18:39:03 +02:00
|
|
|
if id.nodeID == prevID {
|
2018-10-05 22:35:08 +02:00
|
|
|
targetIDx = idx
|
2018-09-27 17:32:52 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
if targetIDx == -1 {
|
2018-09-27 17:32:52 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
nextIdentity := HTMLElementIdentity{
|
2019-07-03 20:05:02 +02:00
|
|
|
nodeID: reply.Node.NodeID,
|
2018-10-11 18:39:03 +02:00
|
|
|
}
|
|
|
|
|
2018-09-27 17:32:52 +02:00
|
|
|
arr := el.children
|
2019-06-19 23:58:56 +02:00
|
|
|
el.children = append(arr[:targetIDx], append([]HTMLElementIdentity{nextIdentity}, arr[targetIDx:]...)...)
|
2018-09-27 17:32:52 +02:00
|
|
|
|
|
|
|
if !el.loadedChildren.Ready() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-16 16:34:13 +02:00
|
|
|
el.loadedChildren.Mutate(ctx, func(v core.Value, _ error) {
|
2018-10-07 04:33:39 +02:00
|
|
|
loadedArr := v.(*values.Array)
|
2019-07-03 20:05:02 +02:00
|
|
|
loadedEl, err := LoadHTMLElement(ctx, el.logger, el.client, el.events, el.input, el.exec, nextID)
|
2018-09-27 17:32:52 +02:00
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
if err != nil {
|
2019-02-20 01:10:18 +02:00
|
|
|
el.logError(err).Msg("failed to load an inserted element")
|
2018-09-28 06:28:33 +02:00
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
return
|
|
|
|
}
|
2018-09-27 17:32:52 +02:00
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
loadedArr.Insert(values.NewInt(targetIDx), loadedEl)
|
2018-09-27 17:32:52 +02:00
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
el.innerHTML.Reset()
|
2018-10-11 18:39:03 +02:00
|
|
|
el.innerText.Reset()
|
2018-10-07 04:33:39 +02:00
|
|
|
})
|
2018-09-27 17:32:52 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
func (el *HTMLElement) handleChildRemoved(ctx context.Context, message interface{}) {
|
2018-09-27 17:32:52 +02:00
|
|
|
reply, ok := message.(*dom.ChildNodeRemovedReply)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:39:03 +02:00
|
|
|
if reply.ParentNodeID != el.id.nodeID {
|
2018-09-27 17:32:52 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
targetIDx := -1
|
|
|
|
targetID := reply.NodeID
|
2018-09-27 17:32:52 +02:00
|
|
|
|
2019-09-01 22:09:35 +02:00
|
|
|
if el.IsDetached() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-15 23:17:15 +02:00
|
|
|
el.mu.Lock()
|
|
|
|
defer el.mu.Unlock()
|
2018-09-27 17:32:52 +02:00
|
|
|
|
|
|
|
for idx, id := range el.children {
|
2018-10-11 18:39:03 +02:00
|
|
|
if id.nodeID == targetID {
|
2018-10-05 22:35:08 +02:00
|
|
|
targetIDx = idx
|
2018-09-27 17:32:52 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:35:08 +02:00
|
|
|
if targetIDx == -1 {
|
2018-09-27 17:32:52 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
arr := el.children
|
2018-10-05 22:35:08 +02:00
|
|
|
el.children = append(arr[:targetIDx], arr[targetIDx+1:]...)
|
2018-09-27 17:32:52 +02:00
|
|
|
|
|
|
|
if !el.loadedChildren.Ready() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-16 16:34:13 +02:00
|
|
|
el.loadedChildren.Mutate(ctx, func(v core.Value, err error) {
|
2018-10-07 04:33:39 +02:00
|
|
|
if err != nil {
|
|
|
|
el.logger.Error().
|
|
|
|
Timestamp().
|
|
|
|
Err(err).
|
2018-10-11 18:39:03 +02:00
|
|
|
Int("nodeID", int(el.id.nodeID)).
|
2019-02-20 01:10:18 +02:00
|
|
|
Msg("failed to update element")
|
2018-09-27 17:32:52 +02:00
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
return
|
|
|
|
}
|
2018-09-27 17:32:52 +02:00
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
loadedArr := v.(*values.Array)
|
|
|
|
loadedArr.RemoveAt(values.NewInt(targetIDx))
|
2018-09-27 17:32:52 +02:00
|
|
|
|
2019-07-11 23:16:34 +02:00
|
|
|
el.innerHTML.Reset()
|
2018-10-11 18:39:03 +02:00
|
|
|
el.innerText.Reset()
|
2018-10-07 04:33:39 +02:00
|
|
|
})
|
2018-09-27 17:32:52 +02:00
|
|
|
}
|
2018-10-11 18:39:03 +02:00
|
|
|
|
|
|
|
func (el *HTMLElement) logError(err error) *zerolog.Event {
|
|
|
|
return el.logger.
|
|
|
|
Error().
|
|
|
|
Timestamp().
|
|
|
|
Int("nodeID", int(el.id.nodeID)).
|
|
|
|
Str("objectID", string(el.id.objectID)).
|
|
|
|
Err(err)
|
|
|
|
}
|