github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/scroll_element.go (about) 1 package html 2 3 import ( 4 "context" 5 6 "github.com/pkg/errors" 7 8 "github.com/MontFerret/ferret/pkg/drivers" 9 "github.com/MontFerret/ferret/pkg/runtime/core" 10 "github.com/MontFerret/ferret/pkg/runtime/values" 11 "github.com/MontFerret/ferret/pkg/runtime/values/types" 12 ) 13 14 // SCROLL_ELEMENT scrolls an element on. 15 // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node. 16 // @param {String} selector - If document is passed, this param must represent an element selector. 17 // @param {Object} [params] - Scroll params. 18 // @param {String} [params.behavior="instant"] - Scroll behavior 19 // @param {String} [params.block="center"] - Scroll vertical alignment. 20 // @param {String} [params.inline="center"] - Scroll horizontal alignment. 21 func ScrollInto(ctx context.Context, args ...core.Value) (core.Value, error) { 22 err := core.ValidateArgs(args, 1, 3) 23 24 if err != nil { 25 return values.None, err 26 } 27 28 var doc drivers.HTMLDocument 29 var el drivers.HTMLElement 30 var selector drivers.QuerySelector 31 var opts drivers.ScrollOptions 32 33 if len(args) == 3 { 34 if err = core.ValidateType(args[1], types.String); err != nil { 35 return values.None, errors.Wrap(err, "selector") 36 } 37 38 if err = core.ValidateType(args[2], types.Object); err != nil { 39 return values.None, errors.Wrap(err, "options") 40 } 41 42 doc, err = drivers.ToDocument(args[0]) 43 44 if err != nil { 45 return values.None, errors.Wrap(err, "document") 46 } 47 48 selector, err = drivers.ToQuerySelector(args[1]) 49 50 if err != nil { 51 return values.None, err 52 } 53 54 o, err := toScrollOptions(args[2]) 55 56 if err != nil { 57 return values.None, errors.Wrap(err, "options") 58 } 59 60 opts = o 61 } else if len(args) == 2 { 62 if err = core.ValidateType(args[1], types.String, types.Object); err != nil { 63 return values.None, err 64 } 65 66 if args[1].Type() == types.String { 67 doc, err = drivers.ToDocument(args[0]) 68 69 if err != nil { 70 return values.None, errors.Wrap(err, "document") 71 } 72 73 selector, err = drivers.ToQuerySelector(args[1]) 74 75 if err != nil { 76 return values.None, err 77 } 78 79 } else { 80 el, err = drivers.ToElement(args[0]) 81 o, err := toScrollOptions(args[1]) 82 83 if err != nil { 84 return values.None, errors.Wrap(err, "options") 85 } 86 87 opts = o 88 } 89 } else { 90 el, err = drivers.ToElement(args[0]) 91 92 if err != nil { 93 return values.None, errors.Wrap(err, "element") 94 } 95 } 96 97 if doc != nil { 98 if selector.String() != "" { 99 return values.True, doc.ScrollBySelector(ctx, selector, opts) 100 } 101 102 return values.True, doc.GetElement().ScrollIntoView(ctx, opts) 103 } 104 105 if el != nil { 106 return values.True, el.ScrollIntoView(ctx, opts) 107 } 108 109 return values.None, core.TypeError( 110 args[0].Type(), 111 drivers.HTMLPageType, 112 drivers.HTMLDocumentType, 113 drivers.HTMLElementType, 114 ) 115 }