github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/wait_style.go (about) 1 package html 2 3 import ( 4 "context" 5 6 "github.com/MontFerret/ferret/pkg/drivers" 7 "github.com/MontFerret/ferret/pkg/runtime/core" 8 "github.com/MontFerret/ferret/pkg/runtime/values" 9 "github.com/MontFerret/ferret/pkg/runtime/values/types" 10 ) 11 12 // WAIT_STYLE waits until a target style value appears 13 // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node. 14 // @param {String} styleNameOrSelector - Style name or CSS selector. 15 // @param {String | Any} valueOrStyleName - Style value or name. 16 // @param {Any | Int} [valueOrTimeout] - Style value or wait timeout. 17 // @param {Int} [timeout=5000] - Wait timeout. 18 func WaitStyle(ctx context.Context, args ...core.Value) (core.Value, error) { 19 return waitStyleWhen(ctx, args, drivers.WaitEventPresence) 20 } 21 22 // WAIT_NO_STYLE waits until a target style value disappears 23 // @param {HTMLPage | HTMLDocument | HTMLElement} node - Target html node. 24 // @param {String} styleNameOrSelector - Style name or CSS selector. 25 // @param {String | Any} valueOrStyleName - Style value or name. 26 // @param {Any | Int} [valueOrTimeout] - Style value or wait timeout. 27 // @param {Int} [timeout=5000] - Wait timeout. 28 func WaitNoStyle(ctx context.Context, args ...core.Value) (core.Value, error) { 29 return waitStyleWhen(ctx, args, drivers.WaitEventAbsence) 30 } 31 32 func waitStyleWhen(ctx context.Context, args []core.Value, when drivers.WaitEvent) (core.Value, error) { 33 err := core.ValidateArgs(args, 3, 5) 34 35 if err != nil { 36 return values.None, err 37 } 38 39 // document or element 40 arg1 := args[0] 41 err = core.ValidateType(arg1, drivers.HTMLPageType, drivers.HTMLDocumentType, drivers.HTMLElementType) 42 43 if err != nil { 44 return values.None, err 45 } 46 47 timeout := values.NewInt(drivers.DefaultWaitTimeout) 48 49 // if a document is passed 50 // WAIT_ATTR(doc, selector, attrName, attrValue, timeout) 51 if arg1.Type() == drivers.HTMLPageType || arg1.Type() == drivers.HTMLDocumentType { 52 // revalidate args with more accurate amount 53 err := core.ValidateArgs(args, 4, 5) 54 55 if err != nil { 56 return values.None, err 57 } 58 59 selector, err := drivers.ToQuerySelector(args[1]) 60 61 if err != nil { 62 return values.None, err 63 } 64 65 // attr name 66 err = core.ValidateType(args[2], types.String) 67 68 if err != nil { 69 return values.None, err 70 } 71 72 el, err := drivers.ToElement(arg1) 73 74 if err != nil { 75 return values.None, err 76 } 77 78 name := args[2].(values.String) 79 value := args[3] 80 81 if len(args) == 5 { 82 err = core.ValidateType(args[4], types.Int) 83 84 if err != nil { 85 return values.None, err 86 } 87 88 timeout = args[4].(values.Int) 89 } 90 91 ctx, fn := waitTimeout(ctx, timeout) 92 defer fn() 93 94 return values.True, el.WaitForStyleBySelector(ctx, selector, name, value, when) 95 } 96 97 el := arg1.(drivers.HTMLElement) 98 name := args[1].(values.String) 99 value := args[2] 100 101 if len(args) == 4 { 102 err = core.ValidateType(args[3], types.Int) 103 104 if err != nil { 105 return values.None, err 106 } 107 108 timeout = args[3].(values.Int) 109 } 110 111 ctx, fn := waitTimeout(ctx, timeout) 112 defer fn() 113 114 return values.True, el.WaitForStyle(ctx, name, value, when) 115 }