Skip to content

Commit

Permalink
Merge branch 'QuentinC-fix-tab-escape' into master
Browse files Browse the repository at this point in the history
The contribution branch was rebased to current master.
This should close gitblit-org#1065 as merged.
  • Loading branch information
flaix committed Aug 2, 2022
2 parents d6f6ac3 + 9667d5e commit f0dce60
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
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 @@ -105,6 +105,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 @@ -117,12 +119,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
34 changes: 31 additions & 3 deletions src/test/java/com/gitblit/tests/StringUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,39 @@ 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 = "\t & < > \"";
String outputNoChange = "\t &amp; &lt; &gt; &quot;";
String outputChange = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&nbsp;&lt;&nbsp;&gt;&nbsp;&quot;";
assertEquals(outputNoChange, StringUtils.escapeForHtml(input, false));
assertEquals(outputChange, StringUtils.escapeForHtml(input, true));

input = "a\tb";
outputNoChange = "a\tb";
outputChange = "a&nbsp;&nbsp;&nbsp;b";
assertEquals(outputNoChange, StringUtils.escapeForHtml(input, false));
assertEquals(outputChange, StringUtils.escapeForHtml(input, true));

input = "\ta b\t";
outputNoChange = "\ta b\t";
outputChange = "&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;b&nbsp;";
assertEquals(outputNoChange, StringUtils.escapeForHtml(input, false));
assertEquals(outputChange, StringUtils.escapeForHtml(input, true));

input = "\t <> \t";
outputNoChange = "\t &lt;&gt; \t";
outputChange = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
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

0 comments on commit f0dce60

Please sign in to comment.