diff options
Diffstat (limited to 'gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettyFormatter.java')
-rw-r--r-- | gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettyFormatter.java | 49 |
1 files changed, 31 insertions, 18 deletions
diff --git a/gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettyFormatter.java b/gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettyFormatter.java index 151149b52e..511b056c86 100644 --- a/gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettyFormatter.java +++ b/gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettyFormatter.java @@ -135,11 +135,21 @@ public abstract class PrettyFormatter implements SparseHtmlFile { // confuse the parser. // html = html.replaceAll("'", "'"); - html = prettify(html, getFileType()); + // If a line is modified at its end and the line ending is changed from + // '\n' to '\r\n' then the '\r' of the new line is part of the modified + // text. If intraline diffs are highlighted the modified text is + // surrounded by a 'span' tag. As result '\r' and '\n' of the new line get + // separated by '</span>'. For the prettify parser this now looks like two + // separate line endings. This messes up the line counting below. + // Drop any '\r' to avoid this problem. + html = html.replace("\r</span>\n", "</span>\n"); + + html = html.replace("\n", " \n"); + html = prettify(html, getFileType()); + html = html.replace(" \n", "\n"); } else { html = expandTabs(html); - html = html.replaceAll("\n", "<br />"); } int pos = 0; @@ -152,8 +162,9 @@ public abstract class PrettyFormatter implements SparseHtmlFile { buf = new StringBuilder(); while (pos <= html.length()) { int tagStart = html.indexOf('<', pos); + int lf = html.indexOf('\n', pos); - if (tagStart < 0) { + if (tagStart < 0 && lf < 0) { // No more tags remaining. What's left is plain text. // assert lastTag == Tag.NULL; @@ -167,6 +178,22 @@ public abstract class PrettyFormatter implements SparseHtmlFile { break; } + // Line end occurs before the next HTML tag. Break the line. + if (0 <= lf && (lf < tagStart || tagStart < 0)) { + if (textChunkStart < lf) { + lastTag.open(buf, html); + htmlText(html.substring(textChunkStart, lf)); + } + pos = lf + 1; + textChunkStart = pos; + + lastTag.close(buf, html); + content.addLine(src.mapIndexToLine(lineIdx++), buf.toString()); + buf = new StringBuilder(); + col = 0; + continue; + } + // Assume no attribute contains '>' and that all tags // within the HTML will be well-formed. // @@ -183,14 +210,7 @@ public abstract class PrettyFormatter implements SparseHtmlFile { } textChunkStart = pos; - if (isBR(html, tagStart, tagEnd)) { - lastTag.close(buf, html); - content.addLine(src.mapIndexToLine(lineIdx), buf.toString()); - buf = new StringBuilder(); - col = 0; - lineIdx++; - - } else if (html.charAt(tagStart + 1) == '/') { + if (html.charAt(tagStart + 1) == '/') { lastTag = lastTag.pop(buf, html); } else if (html.charAt(tagEnd - 1) != '/') { @@ -245,13 +265,6 @@ public abstract class PrettyFormatter implements SparseHtmlFile { /** Run the prettify engine over the text and return the result. */ protected abstract String prettify(String html, String type); - private static boolean isBR(String html, int tagStart, int tagEnd) { - return tagEnd - tagStart == 5 // - && html.charAt(tagStart + 1) == 'b' // - && html.charAt(tagStart + 2) == 'r' // - && html.charAt(tagStart + 3) == ' '; - } - private static class Tag { static final Tag NULL = new Tag(null, 0, 0) { @Override |