Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support CRLF when splitting code lines for display #1862

Merged
merged 5 commits into from
Jun 10, 2017

Conversation

silverwind
Copy link
Member

@silverwind silverwind commented Jun 3, 2017

This removes the whitspace between <li> elements and adds them to the tag content, where it was originally removed. display:block because with the previous display:inline-block it would all end up on one line.

Fixes: #1857

@lunny lunny added this to the 1.2.0 milestone Jun 3, 2017
@lunny lunny added the topic/ui Change the appearance of the Gitea UI label Jun 3, 2017
@silverwind silverwind changed the title Don't append newline to each code line, adapt css Don't append newline to each code line element, adapt css Jun 3, 2017
@@ -212,7 +212,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
var output bytes.Buffer
lines := strings.Split(fileContent, "\n")
for index, line := range lines {
output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s</li>`, index+1, index+1, gotemplate.HTMLEscapeString(line)) + "\n")
output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s</li>`, index+1, index+1, gotemplate.HTMLEscapeString(line) + "\n"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we instead do

output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s\n</li>`, index+1, index+1, gotemplate.HTMLEscapeString(line)))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly, but I think I'd rather avoid newlines in HTML entirely.

@silverwind
Copy link
Member Author

I'll have to revise this, I think it should be possible to avoid newlines in HTML (line termination should be done through block-level elements). Notably this issue only affects files without syntax highlighting.

@tboerger tboerger added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jun 4, 2017
@silverwind silverwind force-pushed the no-newlines branch 8 times, most recently from 39d9b78 to 69284f0 Compare June 7, 2017 19:51
@silverwind silverwind changed the title Don't append newline to each code line element, adapt css Support CRLF when splitting code lines for display Jun 7, 2017
@silverwind silverwind force-pushed the no-newlines branch 2 times, most recently from 153ece3 to 3d21d95 Compare June 7, 2017 20:05
@silverwind
Copy link
Member Author

Updated. The actual issue with CRLF copying was that lines were always split only on \n which left a lone \r on the line, which browsers interpreted as a separate newline (it wasn't visible as such because of CSS, but it became visible on copy/paste).

I've now updated the line splitting to detect files containing \r\n and split them on this sequence and join them back after rendering the HTML. I'd have loved to eliminate line terminators from HTML completely, but the syntax highlighting broke when I did, so these will need to stay for now.

For files that contain both \n and \r\n, it will render the whole file as \n.

@ethantkoenig
Copy link
Member

@silverwind The CI build failed, run make fmt

} else {
terminator = "\n"
}
lines := strings.Split(fileContent, terminator)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To deal with mixed newlines it would be better to split using:
lines := regexp.MustCompile("\\r?\\n").Split(fileContent, -1)

Copy link
Member Author

@silverwind silverwind Jun 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but how do I know whether to join the lines with LF or CRLF afterwards in the loop? I'd like to preserve CRLF if it's there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave detection code, just replace strings split with regexp split

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, actually I'll try your suggestion. I can keep the detection code above to know.

@@ -210,9 +211,26 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
}

var output bytes.Buffer
lines := strings.Split(fileContent, "\n")
var terminator string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By using a single terminator, we aren't able to correctly render files that sometime use \r\n and in other places use \n? Is there a reason we can't just do

lines := strings.Split(fileContent, "\n")
for index, line := range lines {
	if index < len(lines) - 1 {
		line += "\n"
	}
	output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s</li>`, index+1, index+1, line))
}

Since both \r\n and \n end in \n, it seems to me this would work, and it would handle the case where a file mixes \r\n and \n

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, yes, this looks to work. My first approach was similar to this one, but for some reason I stopped pursuing it. Anyways, this copies fine, preserves newlines in mixed files and also highlights fine, looking good!

@ethantkoenig
Copy link
Member

LGTM

@tboerger tboerger added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Jun 8, 2017
@lunny
Copy link
Member

lunny commented Jun 10, 2017

LGTM

@lunny
Copy link
Member

lunny commented Jun 10, 2017

make L-G-T-M work

@tboerger tboerger added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Jun 10, 2017
@lunny lunny merged commit f2fcd9d into go-gitea:master Jun 10, 2017
@go-gitea go-gitea locked and limited conversation to collaborators Nov 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. topic/ui Change the appearance of the Gitea UI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

problems with copying CRLF files in the browser
5 participants