Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text-serializer-legacy: Properly passthrough invalid RGB codes #333

Merged
merged 1 commit into from Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -155,20 +155,34 @@ final class LegacyComponentSerializerImpl implements LegacyComponentSerializer {
return null;
}
if(foundFormat == FormatCodeType.KYORI_HEX) {
return new DecodedFormat(foundFormat, TextColor.fromHexString('#' + input.substring(pos, pos + 6)));
final @Nullable TextColor parsed = tryParseHexColor(input.substring(pos, pos + 6));
if(parsed != null) {
return new DecodedFormat(foundFormat, parsed);
}
} else if(foundFormat == FormatCodeType.MOJANG_LEGACY) {
return new DecodedFormat(foundFormat, FORMATS.get(LEGACY_CHARS.indexOf(legacy)));
} else if(foundFormat == FormatCodeType.BUNGEECORD_UNUSUAL_HEX) {
final StringBuilder foundHex = new StringBuilder();
final StringBuilder foundHex = new StringBuilder(6);
for(int i = pos - 1; i >= pos - 11; i -= 2) {
foundHex.append(input.charAt(i));
}
foundHex.append('#');
return new DecodedFormat(foundFormat, TextColor.fromHexString(foundHex.reverse().toString()));
final @Nullable TextColor parsed = tryParseHexColor(foundHex.reverse().toString());
if(parsed != null) {
return new DecodedFormat(foundFormat, parsed);
}
}
return null;
}

private static @Nullable TextColor tryParseHexColor(final String hexDigits) {
try {
final int color = Integer.parseInt(hexDigits, 16);
return TextColor.color(color);
} catch(final NumberFormatException ex) {
return null;
}
}

private static boolean isHexTextColor(final TextFormat format) {
return format instanceof TextColor && !(format instanceof NamedTextColor);
}
Expand Down
Expand Up @@ -310,4 +310,12 @@ void testNoRedundantReset() {
final String roundtripped = LegacyComponentSerializer.legacyAmpersand().serialize(deserialized);
assertEquals(text, roundtripped);
}

@Test
void testInvalidHexStringsPassedThrough() {
final String text = "Hello&#hellos world";
final Component deserialized = LegacyComponentSerializer.legacyAmpersand().deserialize(text);

assertEquals(Component.text(text), deserialized);
}
}