github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/attr_remove.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  // ATTR_REMOVE removes single or more attribute(s) of a given element.
    13  // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target node.
    14  // @param {String, repeated} attrNames - Attribute name(s).
    15  func AttributeRemove(ctx context.Context, args ...core.Value) (core.Value, error) {
    16  	err := core.ValidateArgs(args, 2, core.MaxArgs)
    17  
    18  	if err != nil {
    19  		return values.None, err
    20  	}
    21  
    22  	el, err := drivers.ToElement(args[0])
    23  
    24  	if err != nil {
    25  		return values.None, err
    26  	}
    27  
    28  	attrs := args[1:]
    29  	attrsStr := make([]values.String, 0, len(attrs))
    30  
    31  	for _, attr := range attrs {
    32  		str, ok := attr.(values.String)
    33  
    34  		if !ok {
    35  			return values.None, core.TypeError(attr.Type(), types.String)
    36  		}
    37  
    38  		attrsStr = append(attrsStr, str)
    39  	}
    40  
    41  	return values.None, el.RemoveAttribute(ctx, attrsStr...)
    42  }