github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/clear.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  // INPUT_CLEAR clears a value from an underlying input element.
    12  // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node.
    13  // @param {String} [selector] - CSS selector.
    14  func InputClear(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  	// CLEAR(el)
    28  	if len(args) == 1 {
    29  		return values.None, el.Clear(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.ClearBySelector(ctx, selector)
    39  }