mirror of
https://github.com/alecthomas/chroma.git
synced 2025-03-27 21:49:13 +02:00
Fix broken css rules output
Strip leading semicolons in combination with line numbers. fixes #98
This commit is contained in:
parent
c6b01cb2a6
commit
c9f612c194
@ -317,12 +317,13 @@ func (f *Formatter) styleToCSS(style *chroma.Style) map[chroma.TokenType]string
|
||||
classes[t] = StyleEntryToCSS(entry)
|
||||
}
|
||||
classes[chroma.Background] += f.tabWidthStyle()
|
||||
lineNumbersStyle := "; margin-right: 0.4em; padding: 0 0.4em 0 0.4em;"
|
||||
classes[chroma.LineNumbers] += lineNumbersStyle
|
||||
classes[chroma.LineNumbersTable] += lineNumbersStyle + " display: block;"
|
||||
classes[chroma.LineHighlight] += "; display: block; width: 100%"
|
||||
classes[chroma.LineTable] += "; border-spacing: 0; padding: 0; margin: 0; border: 0; width: 100%; overflow: auto; display: block;"
|
||||
classes[chroma.LineTableTD] += "; vertical-align: top; padding: 0; margin: 0; border: 0;"
|
||||
lineNumbersStyle := "margin-right: 0.4em; padding: 0 0.4em 0 0.4em;"
|
||||
// all rules begin with default rules followed by user provided rules
|
||||
classes[chroma.LineNumbers] = lineNumbersStyle + classes[chroma.LineNumbers]
|
||||
classes[chroma.LineNumbersTable] = lineNumbersStyle + " display: block;" + classes[chroma.LineNumbersTable]
|
||||
classes[chroma.LineHighlight] = "display: block; width: 100%;" + classes[chroma.LineHighlight]
|
||||
classes[chroma.LineTable] = "border-spacing: 0; padding: 0; margin: 0; border: 0; width: 100%; overflow: auto; display: block;" + classes[chroma.LineTable]
|
||||
classes[chroma.LineTableTD] = "vertical-align: top; padding: 0; margin: 0; border: 0;" + classes[chroma.LineTableTD]
|
||||
|
||||
return classes
|
||||
}
|
||||
|
@ -3,10 +3,10 @@ package html
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/alecthomas/assert"
|
||||
|
||||
"github.com/alecthomas/chroma"
|
||||
"github.com/alecthomas/chroma/lexers"
|
||||
"github.com/alecthomas/chroma/styles"
|
||||
@ -58,3 +58,20 @@ func TestIteratorPanicRecovery(t *testing.T) {
|
||||
err := New().Format(ioutil.Discard, styles.Fallback, it)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestFormatter_styleToCSS(t *testing.T) {
|
||||
builder := styles.Get("github").Builder()
|
||||
builder.Add(chroma.LineHighlight, "bg:#ffffcc")
|
||||
builder.Add(chroma.LineNumbers, "bold")
|
||||
style, err := builder.Build()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
formatter := New(WithClasses())
|
||||
css := formatter.styleToCSS(style)
|
||||
for _, s := range css {
|
||||
if strings.HasPrefix(strings.TrimSpace(s), ";") {
|
||||
t.Errorf("rule starts with semicolon - expected valid css rule without semicolon: %v", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user