1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-12 11:15:14 +02:00

Added BLUR function

This commit is contained in:
Tim Voronov 2019-09-07 12:59:37 -04:00
parent 926cc305df
commit d144c618eb
13 changed files with 129 additions and 2 deletions

View File

@ -0,0 +1,14 @@
LET url = @dynamic + "/#/events"
LET page = DOCUMENT(url, true)
LET input = ELEMENT(page, "#focus-input")
FOCUS(input)
WAIT_CLASS(page, "#focus-content", "alert-success")
BLUR(input)
WAIT_NO_CLASS(page, "#focus-content", "alert-success")
RETURN ""

View File

@ -0,0 +1,12 @@
LET url = @dynamic + "/#/events"
LET page = DOCUMENT(url, true)
FOCUS(page, "#focus-input")
WAIT_CLASS(page, "#focus-content", "alert-success")
BLUR(page, "#focus-input")
WAIT_NO_CLASS(page, "#focus-content", "alert-success")
RETURN ""

View File

@ -0,0 +1,8 @@
LET url = @dynamic + "/#/events"
LET page = DOCUMENT(url, true)
FOCUS(page, "#focus-input")
WAIT_CLASS(page, "#focus-content", "alert-success")
RETURN ""

View File

@ -1086,6 +1086,14 @@ func (el *HTMLElement) FocusBySelector(ctx context.Context, selector values.Stri
return el.input.FocusBySelector(ctx, el.id.nodeID, selector.String())
}
func (el *HTMLElement) Blur(ctx context.Context) error {
return el.input.Blur(ctx, el.id.objectID)
}
func (el *HTMLElement) BlurBySelector(ctx context.Context, selector values.String) error {
return el.input.BlurBySelector(ctx, el.id.objectID, selector.String())
}
func (el *HTMLElement) Hover(ctx context.Context) error {
return el.input.MoveMouse(ctx, el.id.objectID)
}

View File

@ -107,6 +107,18 @@ func (m *Manager) FocusBySelector(ctx context.Context, parentNodeID dom.NodeID,
return m.client.DOM.Focus(ctx, dom.NewFocusArgs().SetNodeID(found.NodeID))
}
func (m *Manager) Blur(ctx context.Context, objectID runtime.RemoteObjectID) error {
return m.exec.EvalWithArguments(ctx, templates.Blur(), runtime.CallArgument{
ObjectID: &objectID,
})
}
func (m *Manager) BlurBySelector(ctx context.Context, parentObjectID runtime.RemoteObjectID, selector string) error {
return m.exec.EvalWithArguments(ctx, templates.BlurBySelector(selector), runtime.CallArgument{
ObjectID: &parentObjectID,
})
}
func (m *Manager) MoveMouse(ctx context.Context, objectID runtime.RemoteObjectID) error {
if err := m.ScrollIntoView(ctx, objectID); err != nil {
return err

View File

@ -0,0 +1,28 @@
package templates
import (
"fmt"
"github.com/MontFerret/ferret/pkg/drivers"
)
func Blur() string {
return `
(el) => {
el.blur()
}
`
}
func BlurBySelector(selector string) string {
return fmt.Sprintf(`
(parent) => {
const el = parent.querySelector('%s');
if (el == null) {
throw new Error('%s')
}
el.blur();
}
`, selector, drivers.ErrNotFound)
}

View File

@ -537,6 +537,14 @@ func (el *HTMLElement) FocusBySelector(_ context.Context, _ values.String) error
return core.ErrNotSupported
}
func (el *HTMLElement) Blur(_ context.Context) error {
return core.ErrNotSupported
}
func (el *HTMLElement) BlurBySelector(_ context.Context, _ values.String) error {
return core.ErrNotSupported
}
func (el *HTMLElement) Hover(_ context.Context) error {
return core.ErrNotSupported
}

View File

@ -117,6 +117,10 @@ type (
FocusBySelector(ctx context.Context, selector values.String) error
Blur(ctx context.Context) error
BlurBySelector(ctx context.Context, selector values.String) error
Hover(ctx context.Context) error
HoverBySelector(ctx context.Context, selector values.String) error

32
pkg/stdlib/html/blur.go Normal file
View File

@ -0,0 +1,32 @@
package html
import (
"context"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/core"
)
// BLUR Calls blur on the element.
// @param target (HTMLPage | HTMLDocument | HTMLElement) - Target node.
// @param selector (String, optional) - Optional CSS selector.
func Blur(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 2)
if err != nil {
return values.None, err
}
el, err := drivers.ToElement(args[0])
if err != nil {
return values.None, err
}
if len(args) == 1 {
return values.None, el.Blur(ctx)
}
return values.None, el.BlurBySelector(ctx, values.ToString(args[1]))
}

View File

@ -8,9 +8,9 @@ import (
"github.com/MontFerret/ferret/pkg/runtime/values"
)
// FOCUS Calls focus on the element.
// FOCUS Sets 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.
// @param selector (String, optional) - Optional CSS selector.
func Focus(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 2)

View File

@ -15,6 +15,7 @@ func RegisterLib(ns core.Namespace) error {
"ATTR_GET": AttributeGet,
"ATTR_REMOVE": AttributeRemove,
"ATTR_SET": AttributeSet,
"BLUR": Blur,
"COOKIE_DEL": CookieDel,
"COOKIE_GET": CookieGet,
"COOKIE_SET": CookieSet,