2018-10-07 04:33:39 +02:00
|
|
|
package html
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-10-14 19:06:27 +02:00
|
|
|
|
2018-10-07 04:33:39 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
|
|
)
|
|
|
|
|
2018-10-14 19:06:27 +02:00
|
|
|
// Elements finds HTML elements by a given CSS selector.
|
|
|
|
// Returns an empty array if element not found.
|
2019-02-20 01:10:18 +02:00
|
|
|
// @param docOrEl (HTMLDocument|HTMLNode) - Parent document or element.
|
2018-10-14 19:06:27 +02:00
|
|
|
// @param selector (String) - CSS selector.
|
|
|
|
// @returns (Array) - Returns an array of found HTML element.
|
2019-02-21 04:24:05 +02:00
|
|
|
func Elements(ctx context.Context, args ...core.Value) (core.Value, error) {
|
2018-10-07 04:33:39 +02:00
|
|
|
el, selector, err := queryArgs(args)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2019-02-21 04:24:05 +02:00
|
|
|
return el.QuerySelectorAll(ctx, selector), nil
|
2018-10-07 04:33:39 +02:00
|
|
|
}
|