2018-10-07 04:33:39 +02:00
|
|
|
package html
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-20 01:10:18 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers"
|
2018-10-07 04:33:39 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
2019-02-13 19:31:18 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
2018-10-07 04:33:39 +02:00
|
|
|
)
|
|
|
|
|
2019-09-07 20:03:17 +02:00
|
|
|
// INPUT types a value to an underlying input element.
|
2019-09-01 22:09:35 +02:00
|
|
|
// @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.
|
2019-09-01 22:09:35 +02:00
|
|
|
// @param delay (Int, optional) - Target value.
|
2018-10-14 19:06:27 +02:00
|
|
|
// @returns (Boolean) - Returns true if an element was found.
|
2019-02-21 04:24:05 +02:00
|
|
|
func Input(ctx context.Context, args ...core.Value) (core.Value, error) {
|
2018-10-09 05:07:08 +02:00
|
|
|
err := core.ValidateArgs(args, 2, 4)
|
2018-10-07 04:33:39 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-09-01 22:09:35 +02:00
|
|
|
return values.False, err
|
2018-10-07 04:33:39 +02:00
|
|
|
}
|
|
|
|
|
2019-09-01 22:09:35 +02:00
|
|
|
el, err := drivers.ToElement(args[0])
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2018-10-09 05:07:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return values.False, err
|
|
|
|
}
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2019-09-07 07:59:32 +02:00
|
|
|
delay := values.NewInt(drivers.DefaultKeyboardDelay)
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2019-09-01 22:09:35 +02:00
|
|
|
// INPUT(el, value)
|
|
|
|
if len(args) == 2 {
|
|
|
|
return values.True, el.Input(ctx, args[1], delay)
|
|
|
|
}
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2019-09-01 22:09:35 +02:00
|
|
|
var selector values.String
|
|
|
|
var value core.Value
|
2018-11-12 21:53:36 +02:00
|
|
|
|
2019-09-01 22:09:35 +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)
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2019-09-01 22:09:35 +02:00
|
|
|
return values.True, el.Input(ctx, value, delay)
|
|
|
|
default:
|
|
|
|
// INPUT(el, selector, value)
|
2019-09-05 18:17:22 +02:00
|
|
|
if err := core.ValidateType(args[1], types.String); err != nil {
|
2018-10-09 05:07:08 +02:00
|
|
|
return values.False, err
|
|
|
|
}
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2019-09-01 22:09:35 +02:00
|
|
|
selector = values.ToString(args[1])
|
|
|
|
value = args[2]
|
2018-10-09 05:07:08 +02:00
|
|
|
}
|
2019-09-01 22:09:35 +02:00
|
|
|
} else {
|
|
|
|
// INPUT(el, selector, value, delay)
|
2019-09-05 18:17:22 +02:00
|
|
|
if err := core.ValidateType(args[1], types.String); err != nil {
|
|
|
|
return values.False, err
|
|
|
|
}
|
2019-07-26 19:22:06 +02:00
|
|
|
|
2019-09-05 18:17:22 +02:00
|
|
|
if err := core.ValidateType(args[3], types.Int); err != nil {
|
2019-07-26 19:22:06 +02:00
|
|
|
return values.False, err
|
|
|
|
}
|
|
|
|
|
2019-09-05 18:17:22 +02:00
|
|
|
selector = values.ToString(args[1])
|
|
|
|
value = args[2]
|
2019-09-01 22:09:35 +02:00
|
|
|
delay = values.ToInt(args[3])
|
2019-02-20 01:10:18 +02:00
|
|
|
}
|
2018-11-12 21:53:36 +02:00
|
|
|
|
2019-09-01 22:09:35 +02:00
|
|
|
exists, err := el.ExistsBySelector(ctx, selector)
|
2019-06-19 23:58:56 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-09-01 22:09:35 +02:00
|
|
|
return values.False, err
|
2018-10-09 05:07:08 +02:00
|
|
|
}
|
2019-02-20 01:10:18 +02:00
|
|
|
|
2019-09-01 22:09:35 +02:00
|
|
|
if !exists {
|
|
|
|
return values.False, nil
|
2019-02-20 01:10:18 +02:00
|
|
|
}
|
|
|
|
|
2019-09-01 22:09:35 +02:00
|
|
|
return values.True, el.InputBySelector(ctx, selector, value, delay)
|
2018-10-07 04:33:39 +02:00
|
|
|
}
|