github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/set_inner_text.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  // INNER_TEXT_SET sets inner text string to a given or matched by CSS selector element
    13  // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node.
    14  // @param {String} textOrCssSelector - String of CSS selector.
    15  // @param {String} [text] - String of inner text.
    16  func SetInnerText(ctx context.Context, args ...core.Value) (core.Value, error) {
    17  	err := core.ValidateArgs(args, 2, 3)
    18  
    19  	if err != nil {
    20  		return values.None, err
    21  	}
    22  
    23  	el, err := drivers.ToElement(args[0])
    24  
    25  	if err != nil {
    26  		return values.None, err
    27  	}
    28  
    29  	if len(args) == 2 {
    30  		err := core.ValidateType(args[1], types.String)
    31  
    32  		if err != nil {
    33  			return values.None, err
    34  		}
    35  
    36  		return values.None, el.SetInnerText(ctx, values.ToString(args[1]))
    37  	}
    38  
    39  	err = core.ValidateType(args[2], types.String)
    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  	innerHTML := values.ToString(args[2])
    52  
    53  	return values.None, el.SetInnerTextBySelector(ctx, selector, innerHTML)
    54  }