1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-21 01:21:34 +02:00
ferret/pkg/stdlib/html/input.go

85 lines
2.0 KiB
Go
Raw Normal View History

package html
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"
)
2019-09-07 20:03:17 +02:00
// INPUT types a value to an underlying input element.
// @param source (HTMLPage | HTMLDocument | HTMLElement) - Event target.
2018-10-14 19:06:27 +02:00
// @param valueOrSelector (String) - Selector or a value.
// @param value (String) - Target value.
// @param delay (Int, optional) - Target value.
2018-10-14 19:06:27 +02:00
// @returns (Boolean) - Returns true if an element was found.
func Input(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 2, 4)
if err != nil {
return values.False, err
}
el, err := drivers.ToElement(args[0])
if err != nil {
return values.False, err
}
delay := values.NewInt(drivers.DefaultKeyboardDelay)
// INPUT(el, value)
if len(args) == 2 {
return values.True, el.Input(ctx, args[1], delay)
}
var selector values.String
var value core.Value
2018-11-12 21:53:36 +02:00
// INPUT(el, valueOrSelector, valueOrOpts)
if len(args) == 3 {
switch v := args[2].(type) {
// INPUT(el, value, delay)
case values.Int, values.Float:
value = args[1]
delay = values.ToInt(v)
return values.True, el.Input(ctx, value, delay)
default:
// INPUT(el, selector, value)
if err := core.ValidateType(args[1], types.String); err != nil {
return values.False, err
}
selector = values.ToString(args[1])
value = args[2]
}
} else {
// INPUT(el, selector, value, delay)
if err := core.ValidateType(args[1], types.String); err != nil {
return values.False, err
}
if err := core.ValidateType(args[3], types.Int); err != nil {
return values.False, err
}
selector = values.ToString(args[1])
value = args[2]
delay = values.ToInt(args[3])
}
2018-11-12 21:53:36 +02:00
exists, err := el.ExistsBySelector(ctx, selector)
if err != nil {
return values.False, err
}
if !exists {
return values.False, nil
}
return values.True, el.InputBySelector(ctx, selector, value, delay)
}