github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/get_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  )
    10  
    11  // INNER_HTML returns inner HTML string of a given or matched by CSS selector element
    12  // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node.
    13  // @param {String} [selector] - String of CSS selector.
    14  // @return {String} - Inner HTML string if a matched element, otherwise empty string.
    15  func GetInnerHTML(ctx context.Context, args ...core.Value) (core.Value, error) {
    16  	err := core.ValidateArgs(args, 1, 2)
    17  
    18  	if err != nil {
    19  		return values.EmptyString, 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 el.GetInnerHTML(ctx)
    30  	}
    31  
    32  	selector, err := drivers.ToQuerySelector(args[1])
    33  
    34  	if err != nil {
    35  		return values.None, err
    36  	}
    37  
    38  	return el.GetInnerHTMLBySelector(ctx, selector)
    39  }