github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/wait_class_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_CLASS_ALL waits for a class to appear on all matched elements.
    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 WaitClassAll(ctx context.Context, args ...core.Value) (core.Value, error) {
    19  	return waitClassAllWhen(ctx, args, drivers.WaitEventPresence)
    20  }
    21  
    22  // WAIT_NO_CLASS_ALL waits for a class to disappear on all matched elements.
    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 WaitNoClassAll(ctx context.Context, args ...core.Value) (core.Value, error) {
    29  	return waitClassAllWhen(ctx, args, drivers.WaitEventAbsence)
    30  }
    31  
    32  func waitClassAllWhen(ctx context.Context, args []core.Value, when drivers.WaitEvent) (core.Value, error) {
    33  	err := core.ValidateArgs(args, 3, 4)
    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
    46  	selector, err := drivers.ToQuerySelector(args[1])
    47  
    48  	if err != nil {
    49  		return values.None, err
    50  	}
    51  
    52  	// class
    53  	err = core.ValidateType(args[2], types.String)
    54  
    55  	if err != nil {
    56  		return values.None, err
    57  	}
    58  
    59  	class := args[2].(values.String)
    60  	timeout := values.NewInt(drivers.DefaultWaitTimeout)
    61  
    62  	if len(args) == 4 {
    63  		err = core.ValidateType(args[3], types.Int)
    64  
    65  		if err != nil {
    66  			return values.None, err
    67  		}
    68  
    69  		timeout = args[3].(values.Int)
    70  	}
    71  
    72  	ctx, fn := waitTimeout(ctx, timeout)
    73  	defer fn()
    74  
    75  	return values.True, el.WaitForClassBySelectorAll(ctx, selector, class, when)
    76  }