mirror of
https://github.com/MontFerret/ferret.git
synced 2025-07-05 00:49:00 +02:00
Feature/focus (#340)
* Added and implemented Focus and FocusBySelector methods * Added e2e tests * Updated CHANGELOG * Fixed linting errors
This commit is contained in:
38
pkg/stdlib/html/focus.go
Normal file
38
pkg/stdlib/html/focus.go
Normal file
@ -0,0 +1,38 @@
|
||||
package html
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/MontFerret/ferret/pkg/drivers"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/core"
|
||||
"github.com/MontFerret/ferret/pkg/runtime/values"
|
||||
)
|
||||
|
||||
// FOCUS Calls focus on the element.
|
||||
// @param target (HTMLPage | HTMLDocument | HTMLElement) - Target node.
|
||||
// @param selector (String, optional) - Optional CSS selector. Required when target is HTMLPage or HTMLDocument.
|
||||
func Focus(ctx context.Context, args ...core.Value) (core.Value, error) {
|
||||
err := core.ValidateArgs(args, 1, 2)
|
||||
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
// Document with selector
|
||||
if len(args) == 2 {
|
||||
doc, err := drivers.ToDocument(args[0])
|
||||
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
return values.None, doc.FocusBySelector(ctx, values.ToString(args[1]))
|
||||
}
|
||||
|
||||
el, err := drivers.ToElement(args[0])
|
||||
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
return values.None, el.Focus(ctx)
|
||||
}
|
Reference in New Issue
Block a user