fix(html): harden selector list splitting
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-03 23:08:50 +00:00
parent fde2f9b884
commit 82ddc736a9
2 changed files with 21 additions and 3 deletions

View file

@ -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

View file

@ -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)
}
}