github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/wait_attr_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  // WAIT_ATTR_ALL waits for an attribute to appear on all matched elements with a given value.
    13  // Stops the execution until the navigation ends or operation times out.
    14  // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node.
    15  // @param {String} selector - String of CSS selector.
    16  // @param {String} class - String of target CSS class.
    17  // @param {Int} [timeout=5000] - Wait timeout.
    18  func WaitAttributeAll(ctx context.Context, args ...core.Value) (core.Value, error) {
    19  	return waitAttributeAllWhen(ctx, args, drivers.WaitEventPresence)
    20  }
    21  
    22  // WAIT_NO_ATTR_ALL waits for an attribute to disappear on all matched elements by a given value.
    23  // Stops the execution until the navigation ends or operation times out.
    24  // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node.
    25  // @param {String} selector - String of CSS selector.
    26  // @param {String} class - String of target CSS class.
    27  // @param {Int} [timeout=5000] - Wait timeout.
    28  func WaitNoAttributeAll(ctx context.Context, args ...core.Value) (core.Value, error) {
    29  	return waitAttributeAllWhen(ctx, args, drivers.WaitEventAbsence)
    30  }
    31  
    32  func waitAttributeAllWhen(ctx context.Context, args []core.Value, when drivers.WaitEvent) (core.Value, error) {
    33  	err := core.ValidateArgs(args, 4, 5)
    34  
    35  	if err != nil {
    36  		return values.None, err
    37  	}
    38  
    39  	el, err := drivers.ToElement(args[0])
    40  
    41  	if err != nil {
    42  		return values.None, err
    43  	}
    44  
    45  	selector, err := drivers.ToQuerySelector(args[1])
    46  
    47  	if err != nil {
    48  		return values.None, err
    49  	}
    50  
    51  	// attr name
    52  	err = core.ValidateType(args[2], types.String)
    53  
    54  	if err != nil {
    55  		return values.None, err
    56  	}
    57  
    58  	name := args[2].(values.String)
    59  	value := args[3]
    60  	timeout := values.NewInt(drivers.DefaultWaitTimeout)
    61  
    62  	if len(args) == 5 {
    63  		err = core.ValidateType(args[4], types.Int)
    64  
    65  		if err != nil {
    66  			return values.None, err
    67  		}
    68  
    69  		timeout = args[4].(values.Int)
    70  	}
    71  
    72  	ctx, fn := waitTimeout(ctx, timeout)
    73  	defer fn()
    74  
    75  	return values.True, el.WaitForAttributeBySelectorAll(ctx, selector, name, value, when)
    76  }