github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/style_set.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  // STYLE_SET sets or updates a single or more style attribute value of a given element.
    13  // @param {HTMLElement} element - Target html element.
    14  // @param {String | Object} nameOrObj - Style name or an object representing a key-value pair of attributes.
    15  // @param {String} value - If a second parameter is a string value, this parameter represent a style value.
    16  func StyleSet(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  	switch arg1 := args[1].(type) {
    30  	case values.String:
    31  		// STYLE_SET(el, name, value)
    32  		err = core.ValidateArgs(args, 3, 3)
    33  
    34  		if err != nil {
    35  			return values.None, nil
    36  		}
    37  
    38  		return values.None, el.SetStyle(ctx, arg1, values.NewString(args[2].String()))
    39  	case *values.Object:
    40  		// STYLE_SET(el, values)
    41  		return values.None, el.SetStyles(ctx, arg1)
    42  	default:
    43  		return values.None, core.TypeError(arg1.Type(), types.String, types.Object)
    44  	}
    45  }