From e1383bf909a840fb5ef390f4a32cc7b716029b3e Mon Sep 17 00:00:00 2001 From: silverwind Date: Tue, 14 Jun 2022 21:46:16 +0200 Subject: [PATCH] Really fix empty line rendering Fixes: https://github.com/go-gitea/gitea/issues/19331 Followup to #19798. Trim off both the .line and .cl classes from Chroma's HTML which made the old conditional work again. This fixed Copy of YAML files for me while also reducing the amount of rendered HTML nodes. --- modules/highlight/highlight.go | 17 ++++++--- modules/highlight/highlight_test.go | 53 +++++++++++++++-------------- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go index a72f26d5f075..482ef6eea2ba 100644 --- a/modules/highlight/highlight.go +++ b/modules/highlight/highlight.go @@ -196,18 +196,25 @@ func File(numLines int, fileName, language string, code []byte) []string { } m := make([]string, 0, numLines) - for _, v := range strings.SplitN(htmlbuf.String(), "\n", numLines) { + for i, v := range strings.SplitN(htmlbuf.String(), "\n", numLines) { content := string(v) + + // remove useless wrapper nodes that are always present + content = strings.Replace(content, "", "", 1) + content = strings.TrimPrefix(content, ``) + + // if there's no final newline, closing tags will be on last line + if (!finalNewLine && i == numLines - 1) { + content = strings.TrimSuffix(content, ``) + } + // need to keep lines that are only \n so copy/paste works properly in browser if content == "" { content = "\n" } else if content == `` { - content += "\n" - } else if content == `` { content += "\n" } - content = strings.TrimSuffix(content, ``) - content = strings.TrimPrefix(content, ``) + m = append(m, content) } if finalNewLine { diff --git a/modules/highlight/highlight_test.go b/modules/highlight/highlight_test.go index e5dfedd2b3c8..bbe1b716b83a 100644 --- a/modules/highlight/highlight_test.go +++ b/modules/highlight/highlight_test.go @@ -43,18 +43,19 @@ func TestFile(t *testing.T) { - go test -v -race -coverprofile=coverage.txt -covermode=atomic `), want: util.Dedent(` - kind: pipeline - name: default - - steps: - - name: test - image: golang:1.13 - environment: - GOPROXY: https://goproxy.cn - commands: - - go get -u - - go build -v - - go test -v -race -coverprofile=coverage.txt -covermode=atomic + kind: pipeline + name: default + + + steps: + - name: test + image: golang:1.13 + environment: + GOPROXY: https://goproxy.cn + commands: + - go get -u + - go build -v + - go test -v -race -coverprofile=coverage.txt -covermode=atomic `), }, { @@ -76,19 +77,20 @@ func TestFile(t *testing.T) { - go test -v -race -coverprofile=coverage.txt -covermode=atomic `)+"\n", "name: default", "name: default ", 1), want: util.Dedent(` - kind: pipeline - name: default - - steps: - - name: test - image: golang:1.13 - environment: - GOPROXY: https://goproxy.cn - commands: - - go get -u - - go build -v - - go test -v -race -coverprofile=coverage.txt -covermode=atomic - + kind: pipeline + name: default + + + steps: + - name: test + image: golang:1.13 + environment: + GOPROXY: https://goproxy.cn + commands: + - go get -u + - go build -v + - go test -v -race -coverprofile=coverage.txt -covermode=atomic + `), @@ -99,6 +101,7 @@ func TestFile(t *testing.T) { t.Run(tt.name, func(t *testing.T) { got := strings.Join(File(tt.numLines, tt.fileName, "", []byte(tt.code)), "\n") assert.Equal(t, tt.want, got) + assert.Equal(t, strings.Count(got, "")) }) } }