github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/set_inner_html.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_HTML_SET sets inner HTML string to a given or matched by CSS selector element
    13  // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node.
    14  // @param {String} htmlOrSelector - HTML or CSS selector.
    15  // @param {String} [html] - String of inner HTML.
    16  func SetInnerHTML(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.SetInnerHTML(ctx, values.ToString(args[1]))
    37  	}
    38  
    39  	selector, err := drivers.ToQuerySelector(args[1])
    40  
    41  	if err != nil {
    42  		return values.None, err
    43  	}
    44  
    45  	err = core.ValidateType(args[2], types.String)
    46  
    47  	if err != nil {
    48  		return values.None, err
    49  	}
    50  
    51  	innerHTML := values.ToString(args[2])
    52  
    53  	return values.None, el.SetInnerHTMLBySelector(ctx, selector, innerHTML)
    54  }