github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/hover.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  // HOVER fetches an element with selector, scrolls it into view if needed, and then uses page.mouse to hover over the center of the element.
    12  // If there's no element matching selector, the method returns an error.
    13  // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node.
    14  // @param {String} [selector] - If document is passed, this param must represent an element selector.
    15  func Hover(ctx context.Context, args ...core.Value) (core.Value, error) {
    16  	err := core.ValidateArgs(args, 1, 2)
    17  
    18  	if err != nil {
    19  		return values.None, err
    20  	}
    21  
    22  	el, err := drivers.ToElement(args[0])
    23  
    24  	if err != nil {
    25  		return values.None, err
    26  	}
    27  
    28  	if len(args) == 1 {
    29  		return values.True, el.Hover(ctx)
    30  	}
    31  
    32  	selector, err := drivers.ToQuerySelector(args[1])
    33  
    34  	if err != nil {
    35  		return values.None, err
    36  	}
    37  
    38  	return values.True, el.HoverBySelector(ctx, selector)
    39  }