chore: improve CSS selector escaping for control chars
This commit is contained in:
parent
8abd428227
commit
70a3096518
2 changed files with 12 additions and 9 deletions
|
|
@ -79,18 +79,13 @@ func escapeCSSString(s string) string {
|
|||
case '\\', '"':
|
||||
b.WriteByte('\\')
|
||||
b.WriteRune(r)
|
||||
case '\n':
|
||||
b.WriteString(`\A `)
|
||||
case '\r':
|
||||
b.WriteString(`\D `)
|
||||
case '\f':
|
||||
b.WriteString(`\C `)
|
||||
case '\t':
|
||||
b.WriteString(`\9 `)
|
||||
default:
|
||||
if r < 0x20 || r == 0x7f {
|
||||
b.WriteByte('\\')
|
||||
b.WriteString(strings.ToUpper(strconv.FormatInt(int64(r), 16)))
|
||||
esc := strings.ToUpper(strconv.FormatInt(int64(r), 16))
|
||||
for i := 0; i < len(esc); i++ {
|
||||
b.WriteByte(esc[i])
|
||||
}
|
||||
b.WriteByte(' ')
|
||||
continue
|
||||
}
|
||||
|
|
|
|||
|
|
@ -126,3 +126,11 @@ func TestVariantSelector_Escapes_Good(t *testing.T) {
|
|||
t.Fatalf("VariantSelector escaping = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVariantSelector_ControlChars_Escape_Good(t *testing.T) {
|
||||
got := VariantSelector("a\tb\nc\u0007")
|
||||
want := `[data-variant="a\\9 b\\A \\7 "]`
|
||||
if got != want {
|
||||
t.Fatalf("VariantSelector control escapes = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue