2018-10-07 04:33:39 +02:00
|
|
|
package html
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-10-08 03:32:30 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/html"
|
|
|
|
"github.com/MontFerret/ferret/pkg/html/static"
|
2018-10-07 04:33:39 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parses a given HTML string and returns a HTML document.
|
|
|
|
* Returned HTML document is always static.
|
|
|
|
* @param html (String) - Target HTML string.
|
|
|
|
* @returns (HTMLDocument) - Parsed HTML static document.
|
|
|
|
*/
|
|
|
|
func DocumentParse(ctx context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateArgs(args, 1, 1)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = core.ValidateType(args[0], core.StringType)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2018-10-08 03:32:30 +02:00
|
|
|
drv, err := html.FromContext(ctx, html.Static)
|
2018-10-07 04:33:39 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
str := args[0].(values.String)
|
|
|
|
|
|
|
|
return drv.(*static.Driver).ParseDocument(ctx, str)
|
|
|
|
}
|