1
0
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:
Tim Voronov
2019-07-23 16:13:04 -04:00
committed by GitHub
parent 996d565191
commit 7e6b3bf15d
12 changed files with 154 additions and 0 deletions

38
pkg/stdlib/html/focus.go Normal file
View 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)
}