Skip to content

Commit 1ccaeea

Browse files
alecthomasclaude
andcommitted
fix: harden compressStyle and drop double compression
compressStyle assumed any fragment containing # ended in six hex digits: shorter customCSS values panicked on the negative slice and non-colour uses of # could be silently mangled. It now only compresses a # followed by exactly six trailing characters. Also, styleCache.get ran compressStyle twice on the default inline-style path (!classes and compress both true); compress once if either applies. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent b359e38 commit 1ccaeea

1 file changed

Lines changed: 4 additions & 9 deletions

File tree

formatters/html/html.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -629,10 +629,10 @@ func compressStyle(s string) string {
629629
for _, p := range parts {
630630
p = strings.Join(strings.Fields(p), " ")
631631
p = strings.Replace(p, ": ", ":", 1)
632-
if strings.Contains(p, "#") {
633-
c := p[len(p)-6:]
632+
if i := strings.LastIndexByte(p, '#'); i >= 0 && len(p)-i == 7 {
633+
c := p[i+1:]
634634
if c[0] == c[1] && c[2] == c[3] && c[4] == c[5] {
635-
p = p[:len(p)-6] + c[0:1] + c[2:3] + c[4:5]
635+
p = p[:i+1] + c[0:1] + c[2:3] + c[4:5]
636636
}
637637
}
638638
out = append(out, p)
@@ -681,12 +681,7 @@ func (l *styleCache) get(style *chroma.Style, compress bool) map[chroma.TokenTyp
681681

682682
// No entry, create one.
683683
cached := l.f.styleToCSS(style)
684-
if !l.f.classes {
685-
for t, style := range cached {
686-
cached[t] = compressStyle(style)
687-
}
688-
}
689-
if compress {
684+
if !l.f.classes || compress {
690685
for t, style := range cached {
691686
cached[t] = compressStyle(style)
692687
}

0 commit comments

Comments
 (0)