mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-04 13:50:39 +00:00
Ensure empty lines are copiable and final new line too (#16678)
* Ensure empty lines are copiable and final new line too When files are highlighted the newline character needs to be added in a whitespace compliant mode. Also ensure the final empty newline is rendered. Fix #16434 * Add test and ensure spans closed Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
parent
a4962a9440
commit
23a87a003e
2 changed files with 116 additions and 0 deletions
|
@ -166,6 +166,11 @@ func File(numLines int, fileName string, code []byte) map[int]string {
|
|||
}
|
||||
|
||||
htmlw.Flush()
|
||||
finalNewLine := false
|
||||
if len(code) > 0 {
|
||||
finalNewLine = code[len(code)-1] == '\n'
|
||||
}
|
||||
|
||||
m := make(map[int]string, numLines)
|
||||
for k, v := range strings.SplitN(htmlbuf.String(), "\n", numLines) {
|
||||
line := k + 1
|
||||
|
@ -173,9 +178,17 @@ func File(numLines int, fileName string, code []byte) map[int]string {
|
|||
//need to keep lines that are only \n so copy/paste works properly in browser
|
||||
if content == "" {
|
||||
content = "\n"
|
||||
} else if content == `</span><span class="w">` {
|
||||
content += "\n</span>"
|
||||
}
|
||||
content = strings.TrimSuffix(content, `<span class="w">`)
|
||||
content = strings.TrimPrefix(content, `</span>`)
|
||||
m[line] = content
|
||||
}
|
||||
if finalNewLine {
|
||||
m[numLines+1] = "<span class=\"w\">\n</span>"
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue