Skip to content

Commit

Permalink
bug(#788): disable html escaping and use uppercase hex colour codes
Browse files Browse the repository at this point in the history
fixes #788
  • Loading branch information
kashike committed Jun 19, 2022
1 parent bef0c45 commit 376a44f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
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 @@ -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());
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

0 comments on commit 376a44f

Please sign in to comment.