1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/stdlib/html/element.go

52 lines
1.2 KiB
Go
Raw Normal View History

2018-09-18 22:42:38 +02:00
package html
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
func Element(_ context.Context, inputs ...core.Value) (core.Value, error) {
el, selector, err := elementArgs(inputs)
if err != nil {
return values.None, err
}
return el.QuerySelector(selector), nil
}
func Elements(_ context.Context, inputs ...core.Value) (core.Value, error) {
el, selector, err := elementArgs(inputs)
if err != nil {
return values.None, err
}
return el.QuerySelectorAll(selector), nil
}
2018-10-06 01:40:09 +02:00
func elementArgs(inputs []core.Value) (values.HTMLNode, values.String, error) {
2018-09-18 22:42:38 +02:00
if len(inputs) == 0 {
return nil, values.EmptyString, core.Error(core.ErrMissedArgument, "element and arg2")
}
if len(inputs) == 1 {
return nil, values.EmptyString, core.Error(core.ErrMissedArgument, "arg2")
}
arg1 := inputs[0]
arg2 := inputs[1]
2018-10-06 01:40:09 +02:00
if arg1.Type() != core.HTMLDocumentType &&
arg1.Type() != core.HTMLElementType {
return nil, values.EmptyString, core.TypeError(arg1.Type(), core.HTMLDocumentType, core.HTMLElementType)
2018-09-18 22:42:38 +02:00
}
if arg2.Type() != core.StringType {
return nil, values.EmptyString, core.TypeError(arg2.Type(), core.StringType)
}
2018-10-06 01:40:09 +02:00
return arg1.(values.HTMLNode), arg2.(values.String), nil
2018-09-18 22:42:38 +02:00
}