Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main/java/com/gitblit/utils/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public static String escapeForHtml(String inStr, boolean changeSpace) {
public static String escapeForHtml(String inStr, boolean changeSpace, int tabLength) {
StringBuilder retStr = new StringBuilder();
int i = 0;
int l = 0;

while (i < inStr.length()) {
if (inStr.charAt(i) == '&') {
retStr.append("&amp;");
Expand All @@ -106,12 +108,18 @@ public static String escapeForHtml(String inStr, boolean changeSpace, int tabLen
} else if (changeSpace && inStr.charAt(i) == ' ') {
retStr.append("&nbsp;");
} else if (changeSpace && inStr.charAt(i) == '\t') {
for (int j = 0; j < tabLength; j++) {
for (int j = 0; j < tabLength - l; j++) {
retStr.append("&nbsp;");
}
l = -1;
} else {
retStr.append(inStr.charAt(i));
}

l = (l + 1) % tabLength;
if (inStr.charAt(i) == '\n') {
l = 0;
}
i++;
}
return retStr.toString();
Expand Down
16 changes: 13 additions & 3 deletions src/test/java/com/gitblit/tests/StringUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,21 @@ public void testEncodeUrl() throws Exception {

@Test
public void testEscapeForHtml() throws Exception {
String input = "& < > \" \t";
String outputNoChange = "&amp; &lt; &gt; &quot; \t";
String outputChange = "&amp;&nbsp;&lt;&nbsp;&gt;&nbsp;&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
String input = "& < > \"";
String outputNoChange = "&amp; &lt; &gt; &quot;";
String outputChange = "&amp;&nbsp;&lt;&nbsp;&gt;&nbsp;&quot;";
assertEquals(outputNoChange, StringUtils.escapeForHtml(input, false));
assertEquals(outputChange, StringUtils.escapeForHtml(input, true));

String tabs = "\t";
int tabSpaces;
int expectedLength;
for (int i = 0; i < 50; i++) {
tabSpaces = 4 - i % 4;
expectedLength = (i + tabSpaces) * 6; // &nbsp; = 6 chars
assertEquals(expectedLength, StringUtils.escapeForHtml(tabs, true).length());
tabs = " " + tabs;
}
}

@Test
Expand Down