-
-
Notifications
You must be signed in to change notification settings - Fork 299
/
setter.go
130 lines (94 loc) · 2.67 KB
/
setter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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 SetInDocument(ctx context.Context, doc drivers.HTMLDocument, path []core.Value, value core.Value) error {
if path == nil || len(path) == 0 {
return nil
}
segment := path[0]
if segment.Type() == types.String {
segment := segment.(values.String)
switch segment {
case "url", "URL":
return doc.SetURL(ctx, values.NewString(value.String()))
case "cookies":
default:
return SetInNode(ctx, doc, path, value)
}
}
return SetInNode(ctx, doc, path, value)
}
func SetInElement(ctx context.Context, el drivers.HTMLElement, path []core.Value, value core.Value) error {
if path == nil || len(path) == 0 {
return nil
}
segment := path[0]
if segment.Type() == types.String {
segment := segment.(values.String)
switch segment {
case "attributes":
if len(path) > 1 {
attrName := path[1]
return el.SetAttribute(ctx, values.NewString(attrName.String()), values.NewString(value.String()))
}
err := core.ValidateType(value, types.Object)
if err != nil {
return err
}
curr := el.GetAttributes(ctx)
// remove all previous attributes
err = el.RemoveAttribute(ctx, curr.Keys()...)
if err != nil {
return err
}
obj := value.(*values.Object)
obj.ForEach(func(value core.Value, key string) bool {
err = el.SetAttribute(ctx, values.NewString(key), values.NewString(value.String()))
return err == nil
})
return err
case "style":
if len(path) > 1 {
attrName := path[1]
return el.SetStyle(ctx, values.NewString(attrName.String()), value)
}
err := core.ValidateType(value, types.Object)
if err != nil {
return err
}
styles, err := el.GetStyles(ctx)
if err != nil {
return err
}
err = el.RemoveStyle(ctx, styles.Keys()...)
obj := value.(*values.Object)
obj.ForEach(func(value core.Value, key string) bool {
err = el.SetStyle(ctx, values.NewString(key), value)
return err == nil
})
return err
case "value":
if len(path) > 1 {
return core.Error(ErrInvalidPath, PathToString(path[1:]))
}
return el.SetValue(ctx, value)
}
}
return SetInNode(ctx, el, path, value)
}
func SetInNode(_ context.Context, _ drivers.HTMLNode, path []core.Value, _ core.Value) error {
if path == nil || len(path) == 0 {
return nil
}
segment := path[0]
st := segment.Type()
if st == types.Int {
return core.Error(core.ErrInvalidOperation, "children are read-only")
}
return core.Error(ErrReadOnly, PathToString(path))
}