diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/gitblit/utils/StringUtils.java | 10 | ||||
-rw-r--r-- | src/test/java/com/gitblit/tests/StringUtilsTest.java | 34 |
2 files changed, 40 insertions, 4 deletions
diff --git a/src/main/java/com/gitblit/utils/StringUtils.java b/src/main/java/com/gitblit/utils/StringUtils.java index 442acbbf..0e23d637 100644 --- a/src/main/java/com/gitblit/utils/StringUtils.java +++ b/src/main/java/com/gitblit/utils/StringUtils.java @@ -105,6 +105,8 @@ public class StringUtils { 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("&");
@@ -117,12 +119,18 @@ public class StringUtils { } else if (changeSpace && inStr.charAt(i) == ' ') {
retStr.append(" ");
} else if (changeSpace && inStr.charAt(i) == '\t') {
- for (int j = 0; j < tabLength; j++) {
+ for (int j = 0; j < tabLength - l; j++) {
retStr.append(" ");
}
+ l = -1;
} else {
retStr.append(inStr.charAt(i));
}
+
+ l = (l + 1) % tabLength;
+ if (inStr.charAt(i) == '\n') {
+ l = 0;
+ }
i++;
}
return retStr.toString();
diff --git a/src/test/java/com/gitblit/tests/StringUtilsTest.java b/src/test/java/com/gitblit/tests/StringUtilsTest.java index 3dae66f4..cc579888 100644 --- a/src/test/java/com/gitblit/tests/StringUtilsTest.java +++ b/src/test/java/com/gitblit/tests/StringUtilsTest.java @@ -61,11 +61,39 @@ public class StringUtilsTest extends GitblitUnitTest { @Test
public void testEscapeForHtml() throws Exception {
- String input = "& < > \" \t";
- String outputNoChange = "& < > " \t";
- String outputChange = "& < > " ";
+ String input = "\t & < > \"";
+ String outputNoChange = "\t & < > "";
+ String outputChange = " & < > "";
assertEquals(outputNoChange, StringUtils.escapeForHtml(input, false));
assertEquals(outputChange, StringUtils.escapeForHtml(input, true));
+
+ input = "a\tb";
+ outputNoChange = "a\tb";
+ outputChange = "a b";
+ assertEquals(outputNoChange, StringUtils.escapeForHtml(input, false));
+ assertEquals(outputChange, StringUtils.escapeForHtml(input, true));
+
+ input = "\ta b\t";
+ outputNoChange = "\ta b\t";
+ outputChange = " a b ";
+ assertEquals(outputNoChange, StringUtils.escapeForHtml(input, false));
+ assertEquals(outputChange, StringUtils.escapeForHtml(input, true));
+
+ input = "\t <> \t";
+ outputNoChange = "\t <> \t";
+ outputChange = " <> ";
+ 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; // = 6 chars
+ assertEquals(expectedLength, StringUtils.escapeForHtml(tabs, true).length());
+ tabs = " " + tabs;
+ }
}
@Test
|