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

     1  package html
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/MontFerret/ferret/pkg/drivers"
     7  	"github.com/MontFerret/ferret/pkg/runtime/core"
     8  	"github.com/MontFerret/ferret/pkg/runtime/values"
     9  )
    10  
    11  // SELECT selects a value from an underlying select element.
    12  // @param {HTMLElement} element - Target html element.
    13  // @param {String | String[]} valueOrSelector - Selector or a an array of strings as a value.
    14  // @param {String[]} value - Target value. Optional.
    15  // @return {String[]} - Array of selected values.
    16  func Select(ctx context.Context, args ...core.Value) (core.Value, error) {
    17  	err := core.ValidateArgs(args, 2, 4)
    18  
    19  	if err != nil {
    20  		return values.None, err
    21  	}
    22  
    23  	el, err := drivers.ToElement(args[0])
    24  
    25  	if err != nil {
    26  		return values.None, err
    27  	}
    28  
    29  	if len(args) == 2 {
    30  		arr := values.ToArray(ctx, args[1])
    31  
    32  		return el.Select(ctx, arr)
    33  	}
    34  
    35  	selector, err := drivers.ToQuerySelector(args[1])
    36  
    37  	if err != nil {
    38  		return values.None, err
    39  	}
    40  
    41  	arr := values.ToArray(ctx, args[2])
    42  
    43  	return el.SelectBySelector(ctx, selector, arr)
    44  }