fix(html): harden selector list splitting
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
fde2f9b884
commit
82ddc736a9
2 changed files with 21 additions and 3 deletions
|
|
@ -106,19 +106,29 @@ func splitSelectorList(selector string) []string {
|
|||
parts := make([]string, 0, 1)
|
||||
var b strings.Builder
|
||||
var quote rune
|
||||
escaped := false
|
||||
depthParen := 0
|
||||
depthBracket := 0
|
||||
depthBrace := 0
|
||||
|
||||
for _, r := range selector {
|
||||
if escaped {
|
||||
b.WriteRune(r)
|
||||
escaped = false
|
||||
continue
|
||||
}
|
||||
|
||||
if r == '\\' {
|
||||
b.WriteRune(r)
|
||||
escaped = true
|
||||
continue
|
||||
}
|
||||
|
||||
switch {
|
||||
case quote != 0:
|
||||
b.WriteRune(r)
|
||||
if r == quote {
|
||||
quote = 0
|
||||
} else if r == '\\' {
|
||||
// Keep escaped characters inside quoted strings intact.
|
||||
continue
|
||||
}
|
||||
case r == '"' || r == '\'':
|
||||
quote = r
|
||||
|
|
|
|||
|
|
@ -243,3 +243,11 @@ func TestScopeVariant_PreservesNestedCommas(t *testing.T) {
|
|||
t.Fatalf("ScopeVariant should preserve nested commas = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScopeVariant_PreservesEscapedSelectorCharacters(t *testing.T) {
|
||||
got := ScopeVariant("desktop", `.nav\,primary, [data-state="open\,expanded"]`)
|
||||
want := `[data-variant="desktop"] .nav\,primary, [data-variant="desktop"] [data-state="open\,expanded"]`
|
||||
if got != want {
|
||||
t.Fatalf("ScopeVariant should preserve escaped selector characters = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue