Skip to content

Commit

Permalink
Merge pull request #789 from KyoriPowered/4/issue/788
Browse files Browse the repository at this point in the history
bug(#788): disable html escaping and use uppercase hex colour codes
  • Loading branch information
zml2008 authored Jun 20, 2022
2 parents 687b8d5 + 53a0637 commit 167e46d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
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

0 comments on commit 167e46d

Please sign in to comment.