github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/press_selector.go (about)

     1  package html
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/MontFerret/ferret/pkg/runtime/values/types"
     7  
     8  	"github.com/MontFerret/ferret/pkg/drivers"
     9  	"github.com/MontFerret/ferret/pkg/runtime/core"
    10  	"github.com/MontFerret/ferret/pkg/runtime/values"
    11  )
    12  
    13  // PRESS_SELECTOR presses a keyboard key.
    14  // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node.
    15  // @param {String} selector - CSS selector.
    16  // @param {String | String[]} key - Target keyboard key(s).
    17  // @param {Int} [presses=1] - Count of presses.
    18  func PressSelector(ctx context.Context, args ...core.Value) (core.Value, error) {
    19  	err := core.ValidateArgs(args, 3, 4)
    20  
    21  	if err != nil {
    22  		return values.False, err
    23  	}
    24  
    25  	el, err := drivers.ToElement(args[0])
    26  
    27  	if err != nil {
    28  		return values.False, err
    29  	}
    30  
    31  	selector, err := drivers.ToQuerySelector(args[1])
    32  
    33  	if err != nil {
    34  		return values.None, err
    35  	}
    36  
    37  	count := values.NewInt(1)
    38  
    39  	if len(args) == 4 {
    40  		countArg := values.ToInt(args[3])
    41  
    42  		if countArg > 0 {
    43  			count = countArg
    44  		}
    45  	}
    46  
    47  	keysArg := args[2]
    48  
    49  	switch keys := keysArg.(type) {
    50  	case values.String:
    51  		return values.True, el.PressBySelector(ctx, selector, []values.String{keys}, count)
    52  	case *values.Array:
    53  		return values.True, el.PressBySelector(ctx, selector, values.ToStrings(keys), count)
    54  	default:
    55  		return values.None, core.TypeError(keysArg.Type(), types.String, types.Array)
    56  	}
    57  }