Fixed Html#colorsToSpan for escaped html

This commit is contained in:
Rsl1122 2019-08-26 10:46:23 +03:00
parent 667dbe471f
commit 5ebe33e293
2 changed files with 13 additions and 2 deletions

View File

@ -96,6 +96,8 @@ public enum Html {
}; };
Map<Character, String> colorMap = new HashMap<>(); Map<Character, String> colorMap = new HashMap<>();
String splitWith = string.contains("&sect;") ? "&sect;" : "§";
for (Html html : replacer) { for (Html html : replacer) {
colorMap.put(Character.toLowerCase(html.name().charAt(6)), html.parse()); colorMap.put(Character.toLowerCase(html.name().charAt(6)), html.parse());
colorMap.put('k', ""); colorMap.put('k', "");
@ -106,9 +108,9 @@ public enum Html {
} }
StringBuilder result = new StringBuilder(string.length()); StringBuilder result = new StringBuilder(string.length());
String[] split = string.split("§"); String[] split = string.split(splitWith);
// Skip first part if it does not start with § // Skip first part if it does not start with §
boolean skipFirst = !string.startsWith("§"); boolean skipFirst = !string.startsWith(splitWith);
int placedSpans = 0; int placedSpans = 0;
for (String part : split) { for (String part : split) {

View File

@ -16,6 +16,7 @@
*/ */
package com.djrapitops.plan.utilities.html; package com.djrapitops.plan.utilities.html;
import org.apache.commons.text.StringEscapeUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform; import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -54,6 +55,14 @@ class HtmlTest {
assertEquals(expected, result); assertEquals(expected, result);
} }
@Test
void colorsToSpanSwapsEscapedHtmlColors() {
String testString = "§fHello, §aPerson§r - How Are you?";
String expected = Html.COLOR_F.parse() + "Hello, " + Html.COLOR_A.parse() + "Person</span></span> - How Are you?";
String result = Html.swapColorCodesToSpan(StringEscapeUtils.escapeHtml4(testString));
assertEquals(expected, result);
}
@Test @Test
void swapColorCodesDoesNotReplaceNonColors() { void swapColorCodesDoesNotReplaceNonColors() {
String testString = "1test§2yes"; String testString = "1test§2yes";