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

bug(#788): disable html escaping and use uppercase hex colour codes #789

Merged
merged 1 commit into from Jun 20, 2022
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 @@ -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
Expand Down
Expand Up @@ -27,6 +27,7 @@
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.util.Locale;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import org.jetbrains.annotations.NotNull;
Expand All @@ -49,10 +50,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(Locale.ROOT, "#%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());
Expand Down
@@ -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\":\"<APPEND\"}],\"text\":\"\"}";
final Component component = GsonComponentSerializer.gson().deserialize(expected);
final String actual = GsonComponentSerializer.gson().serialize(component);
assertEquals(expected, actual);
}
}
Expand Up @@ -80,7 +80,7 @@ void testEmpty() {

@Test
void testHexColor() {
this.test(Style.style(TextColor.color(0x0a1ab9)), object(json -> json.addProperty(StyleSerializer.COLOR, "#0a1ab9")));
this.test(Style.style(TextColor.color(0x0a1ab9)), object(json -> json.addProperty(StyleSerializer.COLOR, "#0A1AB9")));
}

@Test
Expand Down Expand Up @@ -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");
}));
}));
}));
Expand Down