Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit c7cfc9a

Browse files
committed
Refactor PageScreenshotOptions.Parse
1 parent e926a36 commit c7cfc9a

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

common/page_options.go

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -100,45 +100,47 @@ func NewPageScreenshotOptions() *PageScreenshotOptions {
100100
}
101101

102102
// Parse parses the page screenshot options.
103-
func (o *PageScreenshotOptions) Parse(ctx context.Context, opts sobek.Value) error {
103+
func (o *PageScreenshotOptions) Parse(ctx context.Context, opts sobek.Value) error { //nolint:cyclop
104+
if !sobekValueExists(opts) {
105+
return nil
106+
}
107+
104108
rt := k6ext.Runtime(ctx)
105-
if opts != nil && !sobek.IsUndefined(opts) && !sobek.IsNull(opts) {
106-
formatSpecified := false
107-
opts := opts.ToObject(rt)
108-
for _, k := range opts.Keys() {
109-
switch k {
110-
case "clip":
111-
var c map[string]float64
112-
if rt.ExportTo(opts.Get(k), &c) != nil {
113-
o.Clip = &page.Viewport{
114-
X: c["x"],
115-
Y: c["y"],
116-
Width: c["width"],
117-
Height: c["height"],
118-
Scale: 1,
119-
}
120-
}
121-
case "fullPage":
122-
o.FullPage = opts.Get(k).ToBoolean()
123-
case "omitBackground":
124-
o.OmitBackground = opts.Get(k).ToBoolean()
125-
case "path":
126-
o.Path = opts.Get(k).String()
127-
case "quality":
128-
o.Quality = opts.Get(k).ToInteger()
129-
case "type":
130-
if f, ok := imageFormatToID[opts.Get(k).String()]; ok {
131-
o.Format = f
132-
formatSpecified = true
109+
formatSpecified := false
110+
obj := opts.ToObject(rt)
111+
for _, k := range obj.Keys() {
112+
switch k {
113+
case "clip":
114+
var c map[string]float64
115+
if rt.ExportTo(obj.Get(k), &c) != nil {
116+
o.Clip = &page.Viewport{
117+
X: c["x"],
118+
Y: c["y"],
119+
Width: c["width"],
120+
Height: c["height"],
121+
Scale: 1,
133122
}
134123
}
124+
case "fullPage":
125+
o.FullPage = obj.Get(k).ToBoolean()
126+
case "omitBackground":
127+
o.OmitBackground = obj.Get(k).ToBoolean()
128+
case "path":
129+
o.Path = obj.Get(k).String()
130+
case "quality":
131+
o.Quality = obj.Get(k).ToInteger()
132+
case "type":
133+
if f, ok := imageFormatToID[obj.Get(k).String()]; ok {
134+
o.Format = f
135+
formatSpecified = true
136+
}
135137
}
138+
}
136139

137-
// Infer file format by path if format not explicitly specified (default is PNG)
138-
if o.Path != "" && !formatSpecified {
139-
if strings.HasSuffix(o.Path, ".jpg") || strings.HasSuffix(o.Path, ".jpeg") {
140-
o.Format = ImageFormatJPEG
141-
}
140+
// Infer file format by path if format not explicitly specified (default is PNG)
141+
if o.Path != "" && !formatSpecified {
142+
if strings.HasSuffix(o.Path, ".jpg") || strings.HasSuffix(o.Path, ".jpeg") {
143+
o.Format = ImageFormatJPEG
142144
}
143145
}
144146

0 commit comments

Comments
 (0)