diff --git a/formatters/html/html.go b/formatters/html/html.go index 9d17c327f..8717b9d43 100644 --- a/formatters/html/html.go +++ b/formatters/html/html.go @@ -169,7 +169,7 @@ func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []*chroma fmt.Fprintf(w, "", f.styleAttr(css, chroma.LineHighlight)) } - fmt.Fprintf(w, "%*d", f.styleAttr(css, chroma.LineNumbersTable), lineDigits, line) + fmt.Fprintf(w, "%*d\n", f.styleAttr(css, chroma.LineNumbersTable), lineDigits, line) if highlight { fmt.Fprintf(w, "") @@ -327,7 +327,7 @@ func (f *Formatter) styleToCSS(style *chroma.Style) map[chroma.TokenType]string 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.LineNumbersTable] = lineNumbersStyle + 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: auto; overflow: auto; display: block;" + classes[chroma.LineTable] classes[chroma.LineTableTD] = "vertical-align: top; padding: 0; margin: 0; border: 0;" + classes[chroma.LineTableTD] diff --git a/formatters/html/html_test.go b/formatters/html/html_test.go index 02e53d33b..3e97e0c92 100644 --- a/formatters/html/html_test.go +++ b/formatters/html/html_test.go @@ -92,3 +92,22 @@ func TestClassPrefix(t *testing.T) { t.Error("Stylesheets should have a class prefix") } } + +func TestTableLineNumberNewlines(t *testing.T) { + f := New(WithClasses(), WithLineNumbers(), LineNumbersInTable()) + it, err := lexers.Get("go").Tokenise(nil, "package main\nfunc main()\n{\nprintln(`hello world`)\n}\n") + assert.NoError(t, err) + + var buf bytes.Buffer + err = f.Format(&buf, styles.Fallback, it) + assert.NoError(t, err) + + // Don't bother testing the whole output, just verify it's got line numbers + // in a
-friendly format.
+	// Note: placing the newlines inside the  lets browser selections look
+	// better, instead of "skipping" over the span margin.
+	assert.Contains(t, buf.String(), `2
+3
+4
+`)
+}