1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-11-23 21:54:45 +02:00

Added scroll options (#471)

* Added scroll options

* Updated dependencies

* Updates after code review

* Updates after review

* Added comments
This commit is contained in:
Tim Voronov
2020-04-25 15:06:00 -04:00
committed by GitHub
parent 64fc010e6e
commit c9dfb79641
13 changed files with 419 additions and 100 deletions

View File

@@ -105,3 +105,35 @@ func waitTimeout(ctx context.Context, value values.Int) (context.Context, contex
time.Duration(value)*time.Millisecond,
)
}
func toScrollOptions(value core.Value) (drivers.ScrollOptions, error) {
result := drivers.ScrollOptions{}
err := core.ValidateType(value, types.Object)
if err != nil {
return result, err
}
obj := value.(*values.Object)
behavior, exists := obj.Get("behavior")
if exists {
result.Behavior = drivers.NewScrollBehavior(behavior.String())
}
block, exists := obj.Get("block")
if exists {
result.Block = drivers.NewScrollVerticalAlignment(block.String())
}
inline, exists := obj.Get("inline")
if exists {
result.Inline = drivers.NewScrollHorizontalAlignment(inline.String())
}
return result, nil
}

View File

@@ -10,8 +10,9 @@ import (
// SCROLL_BOTTOM scrolls the document's window to its bottom.
// @param doc (HTMLDocument) - Target document.
// @param options (ScrollOptions) - Scroll options. Optional.
func ScrollBottom(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
err := core.ValidateArgs(args, 1, 2)
if err != nil {
return values.None, err
@@ -23,5 +24,15 @@ func ScrollBottom(ctx context.Context, args ...core.Value) (core.Value, error) {
return values.None, err
}
return values.None, doc.ScrollBottom(ctx)
var opts drivers.ScrollOptions
if len(args) > 1 {
opts, err = toScrollOptions(args[1])
if err != nil {
return values.None, err
}
}
return values.None, doc.ScrollBottom(ctx, opts)
}

View File

@@ -3,6 +3,8 @@ package html
import (
"context"
"github.com/pkg/errors"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
@@ -11,44 +13,90 @@ import (
// SCROLL_ELEMENT scrolls an element on.
// @param docOrEl (HTMLDocument|HTMLElement) - Target document or element.
// @param selector (String, options) - If document is passed, this param must represent an element selector.
// @param selector (String) - If document is passed, this param must represent an element selector.
// @param options (ScrollOptions) - Scroll options. Optional.
func ScrollInto(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 2)
err := core.ValidateArgs(args, 1, 3)
if err != nil {
return values.None, err
}
if len(args) == 2 {
doc, err := drivers.ToDocument(args[0])
var doc drivers.HTMLDocument
var el drivers.HTMLElement
var selector values.String
var opts drivers.ScrollOptions
if len(args) == 3 {
if err = core.ValidateType(args[1], types.String); err != nil {
return values.None, errors.Wrap(err, "selector")
}
if err = core.ValidateType(args[2], types.Object); err != nil {
return values.None, errors.Wrap(err, "options")
}
doc, err = drivers.ToDocument(args[0])
if err != nil {
return values.None, errors.Wrap(err, "document")
}
selector = values.ToString(args[1])
o, err := toScrollOptions(args[2])
if err != nil {
return values.None, errors.Wrap(err, "options")
}
opts = o
} else if len(args) == 2 {
if err = core.ValidateType(args[1], types.String, types.Object); err != nil {
return values.None, err
}
err = core.ValidateType(args[1], types.String)
if args[1].Type() == types.String {
doc, err = drivers.ToDocument(args[0])
if err != nil {
return values.None, errors.Wrap(err, "document")
}
selector = values.ToString(args[1])
} else {
el, err = drivers.ToElement(args[0])
o, err := toScrollOptions(args[1])
if err != nil {
return values.None, errors.Wrap(err, "options")
}
opts = o
}
} else {
el, err = drivers.ToElement(args[0])
if err != nil {
return values.None, err
return values.None, errors.Wrap(err, "element")
}
}
if doc != nil {
if selector != values.EmptyString {
return values.None, doc.ScrollBySelector(ctx, selector, opts)
}
selector := args[1].(values.String)
return values.None, doc.ScrollBySelector(ctx, selector)
return values.None, doc.GetElement().ScrollIntoView(ctx, opts)
}
err = core.ValidateType(args[0], drivers.HTMLElementType)
if err != nil {
return values.None, err
if el != nil {
return values.None, el.ScrollIntoView(ctx, opts)
}
// GetElement
el, err := drivers.ToElement(args[0])
if err != nil {
return values.None, err
}
return values.None, el.ScrollIntoView(ctx)
return values.None, core.TypeError(
args[0].Type(),
drivers.HTMLPageType,
drivers.HTMLDocumentType,
drivers.HTMLElementType,
)
}

View File

@@ -10,8 +10,9 @@ import (
// SCROLL_TOP scrolls the document's window to its top.
// @param doc (HTMLDocument) - Target document.
// @param options (ScrollOptions) - Scroll options. Optional.
func ScrollTop(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
err := core.ValidateArgs(args, 1, 2)
if err != nil {
return values.None, err
@@ -23,5 +24,15 @@ func ScrollTop(ctx context.Context, args ...core.Value) (core.Value, error) {
return values.None, err
}
return values.None, doc.ScrollTop(ctx)
var opts drivers.ScrollOptions
if len(args) > 1 {
opts, err = toScrollOptions(args[1])
if err != nil {
return values.None, err
}
}
return values.None, doc.ScrollTop(ctx, opts)
}

View File

@@ -13,8 +13,9 @@ import (
// @param doc (HTMLDocument) - HTML document.
// @param x (Int|Float) - X coordinate.
// @param y (Int|Float) - Y coordinate.
// @param options (ScrollOptions) - Scroll options. Optional.
func ScrollXY(ctx context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 3, 3)
err := core.ValidateArgs(args, 3, 4)
if err != nil {
return values.None, err
@@ -41,5 +42,15 @@ func ScrollXY(ctx context.Context, args ...core.Value) (core.Value, error) {
x := values.ToFloat(args[1])
y := values.ToFloat(args[2])
return values.None, doc.ScrollByXY(ctx, x, y)
var opts drivers.ScrollOptions
if len(args) > 3 {
opts, err = toScrollOptions(args[3])
if err != nil {
return values.None, err
}
}
return values.None, doc.ScrollByXY(ctx, x, y, opts)
}