summaryrefslogtreecommitdiffstats
path: root/modules/highlight
diff options
context:
space:
mode:
authorsilverwind <me@silverwind.io>2022-06-10 15:45:28 +0200
committerGitHub <noreply@github.com>2022-06-10 21:45:28 +0800
commit527e5bd1b29c1087d4b5375beac519d0b74294eb (patch)
treee5f96a202f7e17b3d117032260cb96848c4b11b1 /modules/highlight
parent4d8e9f3b849223ede0f11cf7a0a9296d0bce048c (diff)
downloadgitea-527e5bd1b29c1087d4b5375beac519d0b74294eb.tar.gz
gitea-527e5bd1b29c1087d4b5375beac519d0b74294eb.zip
Fix copy/paste of empty lines (#19798)
* Fix copy/paste of empty newlines again Fixes: https://github.com/go-gitea/gitea/issues/19331 Regressed by: https://github.com/go-gitea/gitea/pull/18270 Needed to do another newline addition to the Chroma output HTML to get copy/paste work again. The previous replacement conditions are probably obsolete, but as I'm not 100% sure, I opted to keep them. Specifically, the Chroma HTML change mentioned in https://github.com/go-gitea/gitea/pull/18270#issuecomment-1013350246 broke our previous newline replacement for such empty lines. Also included are a few changes to make the test more pleasant to work with. * run go mod tidy * add util.Dedent * copy in the code Co-authored-by: techknowlogick <techknowlogick@gitea.io> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'modules/highlight')
-rw-r--r--modules/highlight/highlight.go2
-rw-r--r--modules/highlight/highlight_test.go124
2 files changed, 65 insertions, 61 deletions
diff --git a/modules/highlight/highlight.go b/modules/highlight/highlight.go
index 344be78144..a72f26d5f0 100644
--- a/modules/highlight/highlight.go
+++ b/modules/highlight/highlight.go
@@ -203,6 +203,8 @@ func File(numLines int, fileName, language string, code []byte) []string {
content = "\n"
} else if content == `</span><span class="w">` {
content += "\n</span>"
+ } else if content == `</span></span><span class="line"><span class="cl">` {
+ content += "\n"
}
content = strings.TrimSuffix(content, `<span class="w">`)
content = strings.TrimPrefix(content, `</span>`)
diff --git a/modules/highlight/highlight_test.go b/modules/highlight/highlight_test.go
index 2f305bb589..e5dfedd2b3 100644
--- a/modules/highlight/highlight_test.go
+++ b/modules/highlight/highlight_test.go
@@ -5,11 +5,13 @@
package highlight
import (
- "reflect"
+ "strings"
"testing"
"code.gitea.io/gitea/modules/setting"
+ "code.gitea.io/gitea/modules/util"
+ "github.com/stretchr/testify/assert"
"gopkg.in/ini.v1"
)
@@ -20,83 +22,83 @@ func TestFile(t *testing.T) {
numLines int
fileName string
code string
- want []string
+ want string
}{
{
name: ".drone.yml",
numLines: 12,
fileName: ".drone.yml",
- code: `kind: pipeline
-name: default
+ code: 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
-`,
- want: []string{
- `<span class="line"><span class="cl"><span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">pipeline</span>`,
- `</span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">default</span>`,
- `</span></span><span class="line"><span class="cl">`,
- `</span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">steps</span><span class="p">:</span>`,
- `</span></span><span class="line"><span class="cl"><span class="w"></span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">test</span>`,
- `</span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">image</span><span class="p">:</span><span class="w"> </span><span class="l">golang:1.13</span>`,
- `</span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">environment</span><span class="p">:</span>`,
- `</span></span><span class="line"><span class="cl"><span class="w"></span><span class="w"> </span><span class="nt">GOPROXY</span><span class="p">:</span><span class="w"> </span><span class="l">https://goproxy.cn</span>`,
- `</span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">commands</span><span class="p">:</span>`,
- `</span></span><span class="line"><span class="cl"><span class="w"></span><span class="w"> </span>- <span class="l">go get -u</span>`,
- `</span></span><span class="line"><span class="cl"><span class="w"> </span>- <span class="l">go build -v</span>`,
- `</span></span><span class="line"><span class="cl"><span class="w"> </span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span><span class="w">
-</span></span></span>`,
- `<span class="w">
-</span>`,
- },
+ 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
+ `),
+ want: util.Dedent(`
+ <span class="line"><span class="cl"><span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">pipeline</span>
+ </span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">default</span>
+ </span></span><span class="line"><span class="cl">
+ </span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">steps</span><span class="p">:</span>
+ </span></span><span class="line"><span class="cl"><span class="w"></span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">test</span>
+ </span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">image</span><span class="p">:</span><span class="w"> </span><span class="l">golang:1.13</span>
+ </span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">environment</span><span class="p">:</span>
+ </span></span><span class="line"><span class="cl"><span class="w"></span><span class="w"> </span><span class="nt">GOPROXY</span><span class="p">:</span><span class="w"> </span><span class="l">https://goproxy.cn</span>
+ </span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">commands</span><span class="p">:</span>
+ </span></span><span class="line"><span class="cl"><span class="w"></span><span class="w"> </span>- <span class="l">go get -u</span>
+ </span></span><span class="line"><span class="cl"><span class="w"> </span>- <span class="l">go build -v</span>
+ </span></span><span class="line"><span class="cl"><span class="w"> </span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span></span></span>
+ `),
},
{
name: ".drone.yml - trailing space",
numLines: 13,
fileName: ".drone.yml",
- code: `kind: pipeline
-name: default ` + `
+ code: strings.Replace(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
- `,
- want: []string{
- `<span class="line"><span class="cl"><span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">pipeline</span>`,
- `</span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">default </span>`,
- `</span></span><span class="line"><span class="cl">`,
- `</span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">steps</span><span class="p">:</span>`,
- `</span></span><span class="line"><span class="cl"><span class="w"></span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">test</span>`,
- `</span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">image</span><span class="p">:</span><span class="w"> </span><span class="l">golang:1.13</span>`,
- `</span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">environment</span><span class="p">:</span>`,
- `</span></span><span class="line"><span class="cl"><span class="w"></span><span class="w"> </span><span class="nt">GOPROXY</span><span class="p">:</span><span class="w"> </span><span class="l">https://goproxy.cn</span>`,
- `</span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">commands</span><span class="p">:</span>`,
- `</span></span><span class="line"><span class="cl"><span class="w"></span><span class="w"> </span>- <span class="l">go get -u</span>`,
- `</span></span><span class="line"><span class="cl"><span class="w"> </span>- <span class="l">go build -v</span>`,
- `</span></span><span class="line"><span class="cl"><span class="w"> </span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span>`,
- `</span></span><span class="line"><span class="cl"><span class="w"> </span></span></span>`,
- },
+ 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
+ `)+"\n", "name: default", "name: default ", 1),
+ want: util.Dedent(`
+ <span class="line"><span class="cl"><span class="nt">kind</span><span class="p">:</span><span class="w"> </span><span class="l">pipeline</span>
+ </span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">default </span>
+ </span></span><span class="line"><span class="cl">
+ </span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">steps</span><span class="p">:</span>
+ </span></span><span class="line"><span class="cl"><span class="w"></span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">test</span>
+ </span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">image</span><span class="p">:</span><span class="w"> </span><span class="l">golang:1.13</span>
+ </span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">environment</span><span class="p">:</span>
+ </span></span><span class="line"><span class="cl"><span class="w"></span><span class="w"> </span><span class="nt">GOPROXY</span><span class="p">:</span><span class="w"> </span><span class="l">https://goproxy.cn</span>
+ </span></span><span class="line"><span class="cl"><span class="w"> </span><span class="nt">commands</span><span class="p">:</span>
+ </span></span><span class="line"><span class="cl"><span class="w"></span><span class="w"> </span>- <span class="l">go get -u</span>
+ </span></span><span class="line"><span class="cl"><span class="w"> </span>- <span class="l">go build -v</span>
+ </span></span><span class="line"><span class="cl"><span class="w"> </span>- <span class="l">go test -v -race -coverprofile=coverage.txt -covermode=atomic</span>
+ </span></span>
+ <span class="w">
+ </span>
+ `),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- if got := File(tt.numLines, tt.fileName, "", []byte(tt.code)); !reflect.DeepEqual(got, tt.want) {
- t.Errorf("File() = %v, want %v", got, tt.want)
- }
+ got := strings.Join(File(tt.numLines, tt.fileName, "", []byte(tt.code)), "\n")
+ assert.Equal(t, tt.want, got)
})
}
}