fix: extended field names
This commit is contained in:
@@ -11,7 +11,21 @@ import (
|
|||||||
var (
|
var (
|
||||||
indexRegex = regexp.MustCompile(`^-?\d+$`)
|
indexRegex = regexp.MustCompile(`^-?\d+$`)
|
||||||
sliceRegex = regexp.MustCompile(`^((\-?\d+:\-?\d*)|(\-?\d*:\-?\d+))$`)
|
sliceRegex = regexp.MustCompile(`^((\-?\d+:\-?\d*)|(\-?\d*:\-?\d+))$`)
|
||||||
fieldRegex = regexp.MustCompile(`^\.[a-zA-Z_-]*?$`)
|
|
||||||
|
// According to ECMAScript 2024, identifiers can include:
|
||||||
|
// - Unicode letters
|
||||||
|
// - $, _
|
||||||
|
// - Unicode combining marks
|
||||||
|
// - Unicode digits
|
||||||
|
// - Unicode connector punctuation
|
||||||
|
// Additional characters allowed for compatibility:
|
||||||
|
// - hyphen (-)
|
||||||
|
// \p{L} - any kind of letter from any language
|
||||||
|
// \p{Mn}\p{Mc} - combining marks and spacing combining marks
|
||||||
|
// \p{Nd} - decimal numbers
|
||||||
|
// \p{Pc} - connector punctuation (like underscore)
|
||||||
|
// \p{Sm}\p{So} - math symbols and other symbols
|
||||||
|
fieldRegex = regexp.MustCompile(`^\.[a-zA-Z_\p{L}][a-zA-Z$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\p{Sm}\p{So}-]*?$`)
|
||||||
)
|
)
|
||||||
|
|
||||||
func Parse(str string) (Selector, error) {
|
func Parse(str string) (Selector, error) {
|
||||||
|
|||||||
@@ -572,4 +572,48 @@ func TestParse(t *testing.T) {
|
|||||||
_, err := Parse(".[foo]")
|
_, err := Parse(".[foo]")
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("extended field names", func(t *testing.T) {
|
||||||
|
validFields := []string{
|
||||||
|
".basic",
|
||||||
|
".user_name",
|
||||||
|
".user-name",
|
||||||
|
".userName$special",
|
||||||
|
".αβγ", // Greek letters
|
||||||
|
".użytkownik", // Polish characters
|
||||||
|
".用户", // Chinese characters
|
||||||
|
".사용자", // Korean characters
|
||||||
|
"._private",
|
||||||
|
".number123",
|
||||||
|
".camelCase",
|
||||||
|
".snake_case",
|
||||||
|
".kebab-case",
|
||||||
|
".mixed_kebab-case",
|
||||||
|
".with$dollar",
|
||||||
|
".MIXED_Case_123",
|
||||||
|
".unicode⌘",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, field := range validFields {
|
||||||
|
sel, err := Parse(field)
|
||||||
|
require.NoError(t, err, "field: %s", field)
|
||||||
|
require.NotNil(t, sel)
|
||||||
|
}
|
||||||
|
|
||||||
|
invalidFields := []string{
|
||||||
|
".123number", // Can't start with digit
|
||||||
|
".@special", // @ not allowed
|
||||||
|
".space name", // No spaces
|
||||||
|
".#hashtag", // No #
|
||||||
|
".name!", // No !
|
||||||
|
".{brackets}", // No brackets
|
||||||
|
".name/with/slashes", // No slashes
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, field := range invalidFields {
|
||||||
|
sel, err := Parse(field)
|
||||||
|
require.Error(t, err, "field: %s", field)
|
||||||
|
require.Nil(t, sel)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user