2018-10-07 04:33:39 +02:00
|
|
|
package html
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-10-09 05:07:08 +02:00
|
|
|
|
2018-10-08 03:32:30 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/html/dynamic"
|
2018-10-07 04:33:39 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
|
|
)
|
|
|
|
|
2018-10-14 19:06:27 +02:00
|
|
|
// Input types a value to an underlying input element.
|
|
|
|
// @param source (Document | Element) - Event target.
|
|
|
|
// @param valueOrSelector (String) - Selector or a value.
|
|
|
|
// @param value (String) - Target value.
|
|
|
|
// @param delay (Int, optional) - Waits delay milliseconds between keystrokes
|
|
|
|
// @returns (Boolean) - Returns true if an element was found.
|
2018-10-07 04:33:39 +02:00
|
|
|
func Input(_ 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 {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2018-10-09 05:07:08 +02:00
|
|
|
arg1 := args[0]
|
|
|
|
err = core.ValidateType(arg1, core.HTMLDocumentType, core.HTMLElementType)
|
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
|
|
|
|
2018-10-09 05:07:08 +02:00
|
|
|
switch args[0].(type) {
|
|
|
|
case *dynamic.HTMLDocument:
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2018-10-09 05:07:08 +02:00
|
|
|
doc, ok := arg1.(*dynamic.HTMLDocument)
|
2018-10-07 04:33:39 +02:00
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return values.False, core.Errors(core.ErrInvalidType, ErrNotDynamic)
|
|
|
|
}
|
|
|
|
|
2018-10-09 05:07:08 +02:00
|
|
|
// selector
|
|
|
|
arg2 := args[1]
|
|
|
|
err = core.ValidateType(arg2, core.StringType)
|
2018-10-07 04:33:39 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.False, err
|
|
|
|
}
|
|
|
|
|
2018-10-09 05:07:08 +02:00
|
|
|
delay := values.Int(0)
|
|
|
|
if len(args) == 4 {
|
|
|
|
arg4 := args[3]
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2018-10-09 05:07:08 +02:00
|
|
|
err = core.ValidateType(arg4, core.IntType)
|
|
|
|
if err != nil {
|
|
|
|
return values.False, err
|
|
|
|
}
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2018-10-09 05:07:08 +02:00
|
|
|
delay = arg4.(values.Int)
|
|
|
|
}
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2018-10-09 05:07:08 +02:00
|
|
|
return doc.InputBySelector(arg2.(values.String), args[2], delay)
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2018-10-09 05:07:08 +02:00
|
|
|
case *dynamic.HTMLElement:
|
|
|
|
el, ok := arg1.(*dynamic.HTMLElement)
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2018-10-09 05:07:08 +02:00
|
|
|
if !ok {
|
|
|
|
return values.False, core.Errors(core.ErrInvalidType, ErrNotDynamic)
|
|
|
|
}
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2018-10-09 05:07:08 +02:00
|
|
|
delay := values.Int(0)
|
|
|
|
if len(args) == 3 {
|
|
|
|
arg3 := args[2]
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2018-10-09 05:07:08 +02:00
|
|
|
err = core.ValidateType(arg3, core.IntType)
|
|
|
|
if err != nil {
|
|
|
|
return values.False, err
|
|
|
|
}
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2018-10-09 05:07:08 +02:00
|
|
|
delay = arg3.(values.Int)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = el.Input(args[1], delay)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.False, err
|
|
|
|
}
|
2018-10-07 04:33:39 +02:00
|
|
|
|
2018-10-09 05:07:08 +02:00
|
|
|
return values.True, nil
|
|
|
|
default:
|
|
|
|
return values.False, core.Errors(core.ErrInvalidArgument)
|
|
|
|
}
|
2018-10-07 04:33:39 +02:00
|
|
|
}
|