github.com/MontFerret/ferret@v0.18.0/pkg/drivers/cdp/templates/helpers.go (about)

     1  package templates
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/MontFerret/ferret/pkg/drivers"
     7  	"github.com/MontFerret/ferret/pkg/drivers/cdp/eval"
     8  )
     9  
    10  var (
    11  	notFoundErrorFragment = fmt.Sprintf(`
    12  		if (found == null) {
    13  			throw new Error(%s);
    14  		}
    15  `, ParamErr(drivers.ErrNotFound))
    16  )
    17  
    18  func ParamErr(err error) string {
    19  	return EscapeString(err.Error())
    20  }
    21  
    22  func EscapeString(value string) string {
    23  	return "`" + value + "`"
    24  }
    25  
    26  func toFunction(selector drivers.QuerySelector, cssTmpl, xPathTmpl string) *eval.Function {
    27  	if selector.Kind() == drivers.CSSSelector {
    28  		return eval.F(cssTmpl)
    29  	}
    30  
    31  	return eval.F(xPathTmpl)
    32  }