github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/style_get.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  // STYLE_GET gets single or more style attribute value(s) of a given element.
    12  // @param {HTMLElement} element - Target html element.
    13  // @param {String, repeated} names - Style name(s).
    14  // @return {Object} - Collection of key-value pairs of style values.
    15  func StyleGet(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  	names := args[1:]
    29  	result := values.NewObject()
    30  
    31  	for _, n := range names {
    32  		name := values.NewString(n.String())
    33  		val, err := el.GetStyle(ctx, name)
    34  
    35  		if err != nil {
    36  			return values.None, err
    37  		}
    38  
    39  		if val != values.None {
    40  			result.Set(name, val)
    41  		}
    42  	}
    43  
    44  	return result, nil
    45  }