From 376a44f65665e60a5dcf0e5877046d4e7d3b3126 Mon Sep 17 00:00:00 2001 From: Riley Park Date: Sun, 19 Jun 2022 12:02:54 -0700 Subject: [PATCH] bug(#788): disable html escaping and use uppercase hex colour codes fixes #788 --- .../gson/GsonComponentSerializerImpl.java | 5 ++- .../serializer/gson/TextColorSerializer.java | 6 ++- .../text/serializer/gson/Issue788Test.java | 39 +++++++++++++++++++ .../text/serializer/gson/StyleTest.java | 4 +- 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 text-serializer-gson/src/test/java/net/kyori/adventure/text/serializer/gson/Issue788Test.java diff --git a/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/GsonComponentSerializerImpl.java b/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/GsonComponentSerializerImpl.java index 3cc4dea589..d31f97d83c 100644 --- a/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/GsonComponentSerializerImpl.java +++ b/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/GsonComponentSerializerImpl.java @@ -66,7 +66,10 @@ static final class Instances { builder.registerTypeAdapterFactory(new SerializerFactory(downsampleColor, legacyHoverSerializer, emitLegacyHover)); return builder; }; - this.serializer = this.populator.apply(new GsonBuilder()).create(); + this.serializer = this.populator.apply( + new GsonBuilder() + .disableHtmlEscaping() // to be consistent with vanilla + ).create(); } @Override diff --git a/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/TextColorSerializer.java b/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/TextColorSerializer.java index e75ad21f9b..6b5cfd5544 100644 --- a/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/TextColorSerializer.java +++ b/text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/TextColorSerializer.java @@ -49,10 +49,14 @@ public void write(final JsonWriter out, final TextColor value) throws IOExceptio } else if (this.downsampleColor) { out.value(NamedTextColor.NAMES.key(NamedTextColor.nearestTo(value))); } else { - out.value(value.asHexString()); + out.value(asUpperCaseHexString(value)); } } + private static String asUpperCaseHexString(final TextColor color) { + return String.format("#%06X", color.value()); // to be consistent with vanilla + } + @Override public @Nullable TextColor read(final JsonReader in) throws IOException { final @Nullable TextColor color = fromString(in.nextString()); diff --git a/text-serializer-gson/src/test/java/net/kyori/adventure/text/serializer/gson/Issue788Test.java b/text-serializer-gson/src/test/java/net/kyori/adventure/text/serializer/gson/Issue788Test.java new file mode 100644 index 0000000000..42ed4436df --- /dev/null +++ b/text-serializer-gson/src/test/java/net/kyori/adventure/text/serializer/gson/Issue788Test.java @@ -0,0 +1,39 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2022 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kyori.adventure.text.serializer.gson; + +import net.kyori.adventure.text.Component; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class Issue788Test { + @Test + void test() { + final String expected = "{\"extra\":[{\"color\":\"#FF00FF\",\"text\":\"PREPEND>\"},{\"text\":\"/sign test\"},{\"color\":\"#FF00FF\",\"text\":\" json.addProperty(StyleSerializer.COLOR, "#0a1ab9"))); + this.test(Style.style(TextColor.color(0x0a1ab9)), object(json -> json.addProperty(StyleSerializer.COLOR, "#0A1AB9"))); } @Test @@ -159,7 +159,7 @@ void testShowEntityHoverEvent() { contents.addProperty(ShowEntitySerializer.ID, dolores.toString()); contents.add(ShowEntitySerializer.NAME, object(name -> { name.addProperty(ComponentSerializerImpl.TEXT, "Dolores"); - name.addProperty(StyleSerializer.COLOR, "#0a1ab9"); + name.addProperty(StyleSerializer.COLOR, "#0A1AB9"); })); })); }));