Skip to content

Commit f0dce60

Browse files
committed
Merge branch 'QuentinC-fix-tab-escape' into master
The contribution branch was rebased to current master. This should close #1065 as merged.
2 parents d6f6ac3 + 9667d5e commit f0dce60

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

src/main/java/com/gitblit/utils/StringUtils.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ public static String escapeForHtml(String inStr, boolean changeSpace) {
105105
public static String escapeForHtml(String inStr, boolean changeSpace, int tabLength) {
106106
StringBuilder retStr = new StringBuilder();
107107
int i = 0;
108+
int l = 0;
109+
108110
while (i < inStr.length()) {
109111
if (inStr.charAt(i) == '&') {
110112
retStr.append("&amp;");
@@ -117,12 +119,18 @@ public static String escapeForHtml(String inStr, boolean changeSpace, int tabLen
117119
} else if (changeSpace && inStr.charAt(i) == ' ') {
118120
retStr.append("&nbsp;");
119121
} else if (changeSpace && inStr.charAt(i) == '\t') {
120-
for (int j = 0; j < tabLength; j++) {
122+
for (int j = 0; j < tabLength - l; j++) {
121123
retStr.append("&nbsp;");
122124
}
125+
l = -1;
123126
} else {
124127
retStr.append(inStr.charAt(i));
125128
}
129+
130+
l = (l + 1) % tabLength;
131+
if (inStr.charAt(i) == '\n') {
132+
l = 0;
133+
}
126134
i++;
127135
}
128136
return retStr.toString();

src/test/java/com/gitblit/tests/StringUtilsTest.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,39 @@ public void testEncodeUrl() throws Exception {
6161

6262
@Test
6363
public void testEscapeForHtml() throws Exception {
64-
String input = "& < > \" \t";
65-
String outputNoChange = "&amp; &lt; &gt; &quot; \t";
66-
String outputChange = "&amp;&nbsp;&lt;&nbsp;&gt;&nbsp;&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
64+
String input = "\t & < > \"";
65+
String outputNoChange = "\t &amp; &lt; &gt; &quot;";
66+
String outputChange = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&nbsp;&lt;&nbsp;&gt;&nbsp;&quot;";
6767
assertEquals(outputNoChange, StringUtils.escapeForHtml(input, false));
6868
assertEquals(outputChange, StringUtils.escapeForHtml(input, true));
69+
70+
input = "a\tb";
71+
outputNoChange = "a\tb";
72+
outputChange = "a&nbsp;&nbsp;&nbsp;b";
73+
assertEquals(outputNoChange, StringUtils.escapeForHtml(input, false));
74+
assertEquals(outputChange, StringUtils.escapeForHtml(input, true));
75+
76+
input = "\ta b\t";
77+
outputNoChange = "\ta b\t";
78+
outputChange = "&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;b&nbsp;";
79+
assertEquals(outputNoChange, StringUtils.escapeForHtml(input, false));
80+
assertEquals(outputChange, StringUtils.escapeForHtml(input, true));
81+
82+
input = "\t <> \t";
83+
outputNoChange = "\t &lt;&gt; \t";
84+
outputChange = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
85+
assertEquals(outputNoChange, StringUtils.escapeForHtml(input, false));
86+
assertEquals(outputChange, StringUtils.escapeForHtml(input, true));
87+
88+
String tabs = "\t";
89+
int tabSpaces;
90+
int expectedLength;
91+
for (int i = 0; i < 50; i++) {
92+
tabSpaces = 4 - i % 4;
93+
expectedLength = (i + tabSpaces) * 6; // &nbsp; = 6 chars
94+
assertEquals(expectedLength, StringUtils.escapeForHtml(tabs, true).length());
95+
tabs = " " + tabs;
96+
}
6997
}
7098

7199
@Test

0 commit comments

Comments
 (0)