Skip to content

Commit 1f0a2dc

Browse files
authored
Browser: Fix getBy* when using quotes inside name (#5374)
1 parent ba2f60c commit 1f0a2dc

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

internal/js/modules/k6/browser/browser/page_mapping.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"reflect"
8+
"strings"
89
"time"
910

1011
"github.com/grafana/sobek"
@@ -749,9 +750,9 @@ func parseStringOrRegex(v sobek.Value, doubleQuote bool) string {
749750
switch v.ExportType() {
750751
case reflect.TypeOf(stringType): // text values require quotes
751752
if doubleQuote {
752-
a = `"` + v.String() + `"`
753+
a = `"` + strings.ReplaceAll(v.String(), `"`, `\"`) + `"`
753754
} else {
754-
a = `'` + v.String() + `'`
755+
a = `'` + strings.ReplaceAll(v.String(), `'`, `\'`) + `'`
755756
}
756757
case reflect.TypeOf(map[string]interface{}(nil)): // JS RegExp
757758
a = v.String() // No quotes

internal/js/modules/k6/browser/browser/page_mapping_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ func TestParseStringOrRegex(t *testing.T) {
3434
{name: "null", input: mk(`null`), doubleQuote: false, want: `null`},
3535
{name: "undefined", input: mk(`undefined`), doubleQuote: false, want: `undefined`},
3636
{name: "undefined", input: mk(``), doubleQuote: false, want: `undefined`},
37+
{name: "string_with_single_quote", input: mk(`'abc\''`), doubleQuote: false, want: `'abc\''`},
38+
{name: "string_with_single_quote", input: mk(`'abc\''`), doubleQuote: true, want: `"abc'"`},
39+
{name: "string_with_double_quote", input: mk(`'abc"'`), doubleQuote: false, want: `'abc"'`},
40+
{name: "string_with_double_quote", input: mk(`'abc"'`), doubleQuote: true, want: `"abc\""`},
3741
}
3842

3943
for _, tc := range tests {

0 commit comments

Comments
 (0)