2019-02-19 18:10:18 -05: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"
|
|
|
|
)
|
|
|
|
|
2021-09-08 21:01:22 -04:00
|
|
|
func SetInPage(ctx context.Context, path []core.Value, page drivers.HTMLPage, value core.Value) core.PathError {
|
2019-05-04 00:10:34 +03:00
|
|
|
if len(path) == 0 {
|
2019-02-19 18:10:18 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-08 21:01:22 -04:00
|
|
|
return SetInDocument(ctx, path, page.GetMainFrame(), value)
|
2019-06-19 17:58:56 -04:00
|
|
|
}
|
2019-03-15 19:59:05 -04:00
|
|
|
|
2021-09-08 21:01:22 -04:00
|
|
|
func SetInDocument(ctx context.Context, path []core.Value, doc drivers.HTMLDocument, value core.Value) core.PathError {
|
2019-06-19 17:58:56 -04:00
|
|
|
if len(path) == 0 {
|
|
|
|
return nil
|
2019-02-19 18:10:18 -05:00
|
|
|
}
|
|
|
|
|
2021-09-08 21:01:22 -04:00
|
|
|
return SetInNode(ctx, path, doc, value)
|
2019-02-19 18:10:18 -05:00
|
|
|
}
|
|
|
|
|
2021-09-08 21:01:22 -04:00
|
|
|
func SetInElement(ctx context.Context, path []core.Value, el drivers.HTMLElement, value core.Value) core.PathError {
|
2019-05-04 00:10:34 +03:00
|
|
|
if len(path) == 0 {
|
2019-02-19 18:10:18 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-08 21:01:22 -04:00
|
|
|
segmentIdx := 0
|
|
|
|
segment := path[segmentIdx]
|
2019-02-19 18:10:18 -05:00
|
|
|
|
|
|
|
if segment.Type() == types.String {
|
|
|
|
segment := segment.(values.String)
|
|
|
|
|
|
|
|
switch segment {
|
|
|
|
case "attributes":
|
|
|
|
if len(path) > 1 {
|
|
|
|
attrName := path[1]
|
2021-09-08 21:01:22 -04:00
|
|
|
err := el.SetAttribute(ctx, values.NewString(attrName.String()), values.NewString(value.String()))
|
2019-02-19 18:10:18 -05:00
|
|
|
|
2021-09-08 21:01:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return core.NewPathError(err, segmentIdx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-02-19 18:10:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
err := core.ValidateType(value, types.Object)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-09-08 21:01:22 -04:00
|
|
|
return core.NewPathError(err, segmentIdx)
|
2019-02-19 18:10:18 -05:00
|
|
|
}
|
|
|
|
|
2019-09-06 21:02:41 -04:00
|
|
|
curr, err := el.GetAttributes(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-09-08 21:01:22 -04:00
|
|
|
return core.NewPathError(err, segmentIdx)
|
2019-09-06 21:02:41 -04:00
|
|
|
}
|
2019-03-13 14:51:30 -04:00
|
|
|
|
|
|
|
// remove all previous attributes
|
|
|
|
err = el.RemoveAttribute(ctx, curr.Keys()...)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-09-08 21:01:22 -04:00
|
|
|
return core.NewPathError(err, segmentIdx)
|
2019-03-13 14:51:30 -04:00
|
|
|
}
|
|
|
|
|
2019-02-19 18:10:18 -05:00
|
|
|
obj := value.(*values.Object)
|
|
|
|
obj.ForEach(func(value core.Value, key string) bool {
|
2019-02-20 21:24:05 -05:00
|
|
|
err = el.SetAttribute(ctx, values.NewString(key), values.NewString(value.String()))
|
2019-02-19 18:10:18 -05:00
|
|
|
|
|
|
|
return err == nil
|
|
|
|
})
|
|
|
|
|
2021-09-08 21:01:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return core.NewPathError(err, segmentIdx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-03-13 14:51:30 -04:00
|
|
|
case "style":
|
|
|
|
if len(path) > 1 {
|
|
|
|
attrName := path[1]
|
|
|
|
|
2021-09-08 21:01:22 -04:00
|
|
|
err := el.SetStyle(ctx, values.NewString(attrName.String()), values.NewString(value.String()))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return core.NewPathError(err, segmentIdx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-03-13 14:51:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err := core.ValidateType(value, types.Object)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-09-08 21:01:22 -04:00
|
|
|
return core.NewPathError(err, segmentIdx)
|
2019-03-13 14:51:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
styles, err := el.GetStyles(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-09-08 21:01:22 -04:00
|
|
|
return core.NewPathError(err, segmentIdx)
|
2019-03-13 14:51:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err = el.RemoveStyle(ctx, styles.Keys()...)
|
|
|
|
|
|
|
|
obj := value.(*values.Object)
|
|
|
|
obj.ForEach(func(value core.Value, key string) bool {
|
2020-11-20 20:09:21 -05:00
|
|
|
err = el.SetStyle(ctx, values.NewString(key), values.NewString(value.String()))
|
2019-03-13 14:51:30 -04:00
|
|
|
|
|
|
|
return err == nil
|
|
|
|
})
|
|
|
|
|
2021-09-08 21:01:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return core.NewPathError(err, segmentIdx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-02-19 18:10:18 -05:00
|
|
|
case "value":
|
|
|
|
if len(path) > 1 {
|
2021-09-08 21:01:22 -04:00
|
|
|
return core.NewPathError(ErrInvalidPath, segmentIdx+1)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := el.SetValue(ctx, value)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return core.NewPathError(err, segmentIdx)
|
2019-02-19 18:10:18 -05:00
|
|
|
}
|
|
|
|
|
2021-09-08 21:01:22 -04:00
|
|
|
return nil
|
2019-02-19 18:10:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 21:01:22 -04:00
|
|
|
return SetInNode(ctx, path, el, value)
|
2019-02-19 18:10:18 -05:00
|
|
|
}
|
|
|
|
|
2021-09-08 21:01:22 -04:00
|
|
|
func SetInNode(_ context.Context, path []core.Value, _ drivers.HTMLNode, _ core.Value) core.PathError {
|
2019-05-04 00:10:34 +03:00
|
|
|
if len(path) == 0 {
|
2019-02-19 18:10:18 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-08 21:01:22 -04:00
|
|
|
return core.NewPathError(ErrReadOnly, 0)
|
2019-02-19 18:10:18 -05:00
|
|
|
}
|