2019-03-13 20:51:30 +02:00
|
|
|
package html
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers"
|
2019-03-13 20:51:30 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
|
|
)
|
|
|
|
|
2019-09-07 20:03:17 +02:00
|
|
|
// ATTR_GET gets single or more attribute(s) of a given element.
|
2019-03-13 20:51:30 +02:00
|
|
|
// @param el (HTMLElement) - Target element.
|
|
|
|
// @param names (...String) - Attribute name(s).
|
|
|
|
// @returns Object - Key-value pairs of attribute values.
|
|
|
|
func AttributeGet(ctx context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateArgs(args, 2, core.MaxArgs)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
el, err := drivers.ToElement(args[0])
|
2019-03-13 20:51:30 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
names := args[1:]
|
|
|
|
result := values.NewObject()
|
2019-09-07 03:02:41 +02:00
|
|
|
attrs, err := el.GetAttributes(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
2019-03-13 20:51:30 +02:00
|
|
|
|
|
|
|
for _, n := range names {
|
|
|
|
name := values.NewString(n.String())
|
|
|
|
val, exists := attrs.Get(name)
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
result.Set(name, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|