github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/xpath.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  // XPATH evaluates the XPath expression.
    12  // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node.
    13  // @param {String} expression - XPath expression.
    14  // @return {Any} - Returns result of a given XPath expression.
    15  func XPath(ctx context.Context, args ...core.Value) (core.Value, error) {
    16  	err := core.ValidateArgs(args, 2, 2)
    17  
    18  	if err != nil {
    19  		return values.None, err
    20  	}
    21  
    22  	element, err := drivers.ToElement(args[0])
    23  
    24  	if err != nil {
    25  		return values.None, err
    26  	}
    27  
    28  	expr := values.ToString(args[1])
    29  
    30  	return element.XPath(ctx, expr)
    31  }