github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/focus.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  // FOCUS Sets focus on the element.
    12  // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node.
    13  // @param {String} [selector] - CSS selector.
    14  func Focus(ctx context.Context, args ...core.Value) (core.Value, error) {
    15  	err := core.ValidateArgs(args, 1, 2)
    16  
    17  	if err != nil {
    18  		return values.None, err
    19  	}
    20  
    21  	el, err := drivers.ToElement(args[0])
    22  
    23  	if err != nil {
    24  		return values.None, err
    25  	}
    26  
    27  	if len(args) == 1 {
    28  		return values.True, el.Focus(ctx)
    29  	}
    30  
    31  	selector, err := drivers.ToQuerySelector(args[1])
    32  
    33  	if err != nil {
    34  		return values.None, err
    35  	}
    36  
    37  	return values.True, el.FocusBySelector(ctx, selector)
    38  }