2019-02-20 01:10:18 +02:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/MontFerret/ferret/pkg/drivers"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetInDocument(ctx context.Context, doc drivers.HTMLDocument, path []core.Value) (core.Value, error) {
|
|
|
|
if path == nil || len(path) == 0 {
|
|
|
|
return values.None, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
segment := path[0]
|
|
|
|
|
|
|
|
if segment.Type() == types.String {
|
|
|
|
segment := segment.(values.String)
|
|
|
|
|
|
|
|
switch segment {
|
|
|
|
case "url", "URL":
|
|
|
|
return doc.GetURL(), nil
|
|
|
|
case "body":
|
2019-02-21 04:24:05 +02:00
|
|
|
return doc.QuerySelector(ctx, "body"), nil
|
2019-02-20 01:10:18 +02:00
|
|
|
case "head":
|
2019-02-21 04:24:05 +02:00
|
|
|
return doc.QuerySelector(ctx, "head"), nil
|
2019-02-20 01:10:18 +02:00
|
|
|
default:
|
|
|
|
return GetInNode(ctx, doc.DocumentElement(), path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetInNode(ctx, doc.DocumentElement(), path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetInElement(ctx context.Context, el drivers.HTMLElement, path []core.Value) (core.Value, error) {
|
|
|
|
if path == nil || len(path) == 0 {
|
|
|
|
return values.None, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
segment := path[0]
|
|
|
|
|
|
|
|
if segment.Type() == types.String {
|
|
|
|
segment := segment.(values.String)
|
|
|
|
|
|
|
|
switch segment {
|
|
|
|
case "innerText":
|
2019-02-21 04:24:05 +02:00
|
|
|
return el.InnerText(ctx), nil
|
2019-02-20 01:10:18 +02:00
|
|
|
case "innerHTML":
|
2019-02-21 04:24:05 +02:00
|
|
|
return el.InnerHTML(ctx), nil
|
2019-02-20 01:10:18 +02:00
|
|
|
case "value":
|
2019-02-21 04:24:05 +02:00
|
|
|
return el.GetValue(ctx), nil
|
2019-02-20 01:10:18 +02:00
|
|
|
case "attributes":
|
2019-02-26 04:46:39 +02:00
|
|
|
attrs := el.GetAttributes(ctx)
|
|
|
|
|
|
|
|
if len(path) == 1 {
|
|
|
|
return attrs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.GetIn(ctx, attrs, path[1:])
|
2019-03-13 20:51:30 +02:00
|
|
|
case "style":
|
|
|
|
styles, err := el.GetStyles(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(path) == 1 {
|
|
|
|
return styles, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.GetIn(ctx, styles, path[1:])
|
2019-02-20 01:10:18 +02:00
|
|
|
default:
|
|
|
|
return GetInNode(ctx, el, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetInNode(ctx, el, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetInNode(ctx context.Context, node drivers.HTMLNode, path []core.Value) (core.Value, error) {
|
|
|
|
if path == nil || len(path) == 0 {
|
|
|
|
return values.None, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nt := node.Type()
|
|
|
|
segment := path[0]
|
|
|
|
st := segment.Type()
|
|
|
|
|
2019-02-26 04:46:39 +02:00
|
|
|
switch st {
|
|
|
|
case types.Int:
|
2019-02-20 01:10:18 +02:00
|
|
|
if nt == drivers.HTMLElementType || nt == drivers.HTMLDocumentType {
|
|
|
|
re := node.(drivers.HTMLNode)
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
return re.GetChildNode(ctx, segment.(values.Int)), nil
|
2019-02-20 01:10:18 +02:00
|
|
|
}
|
|
|
|
|
2019-02-26 04:46:39 +02:00
|
|
|
return values.GetIn(ctx, node, path[1:])
|
|
|
|
case types.String:
|
2019-02-20 01:10:18 +02:00
|
|
|
segment := segment.(values.String)
|
|
|
|
|
|
|
|
switch segment {
|
|
|
|
case "nodeType":
|
|
|
|
return node.NodeType(), nil
|
|
|
|
case "nodeName":
|
|
|
|
return node.NodeName(), nil
|
|
|
|
case "children":
|
2019-02-26 04:46:39 +02:00
|
|
|
children := node.GetChildNodes(ctx)
|
|
|
|
|
|
|
|
if len(path) == 1 {
|
|
|
|
return children, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.GetIn(ctx, children, path[1:])
|
2019-02-20 01:10:18 +02:00
|
|
|
case "length":
|
|
|
|
return node.Length(), nil
|
|
|
|
default:
|
|
|
|
return values.None, nil
|
|
|
|
}
|
2019-02-26 04:46:39 +02:00
|
|
|
default:
|
|
|
|
return values.None, core.TypeError(st, types.Int, types.String)
|
2019-02-20 01:10:18 +02:00
|
|
|
}
|
|
|
|
}
|