github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/click_all.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  	"github.com/MontFerret/ferret/pkg/runtime/values/types"
    10  )
    11  
    12  // CLICK_ALL dispatches click event on all matched element
    13  // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node.
    14  // @param {String} selector - CSS selector.
    15  // @param {Int} [clicks=1] - Optional count of clicks.
    16  // @return {Boolean} - True if matched at least one element.
    17  func ClickAll(ctx context.Context, args ...core.Value) (core.Value, error) {
    18  	err := core.ValidateArgs(args, 2, 3)
    19  
    20  	if err != nil {
    21  		return values.False, err
    22  	}
    23  
    24  	el, err := drivers.ToElement(args[0])
    25  
    26  	if err != nil {
    27  		return values.False, err
    28  	}
    29  
    30  	selector, err := drivers.ToQuerySelector(args[1])
    31  
    32  	if err != nil {
    33  		return values.None, err
    34  	}
    35  
    36  	exists, err := el.ExistsBySelector(ctx, selector)
    37  
    38  	if err != nil {
    39  		return values.False, err
    40  	}
    41  
    42  	if !exists {
    43  		return values.False, nil
    44  	}
    45  
    46  	count := values.NewInt(1)
    47  
    48  	if len(args) == 3 {
    49  		err := core.ValidateType(args[2], types.Int)
    50  
    51  		if err != nil {
    52  			return values.False, err
    53  		}
    54  
    55  		count = values.ToInt(args[2])
    56  	}
    57  
    58  	return values.True, el.ClickBySelectorAll(ctx, selector, count)
    59  }