1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/stdlib/html/input.go

83 lines
1.8 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"
)
2018-10-14 19:06:27 +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(25)
// 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)
err := core.ValidateType(args[1], types.String)
if err != nil {
return values.False, err
}
selector = values.ToString(args[1])
value = args[2]
}
} else {
// INPUT(el, selector, value, delay)
err := core.ValidateType(args[3], types.Int)
if err != nil {
return values.False, err
}
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)
}