Strip HTML color codes from description parsing

This commit is contained in:
Kevin Tindall
2022-01-28 20:19:34 -06:00
parent ba2156b618
commit 2f5704acab
2 changed files with 65 additions and 3 deletions

View File

@@ -253,10 +253,13 @@ class PingResponse:
for entry in entries: for entry in entries:
for style_key, style_val in STYLE_MAP.items(): for style_key, style_val in STYLE_MAP.items():
if entry.get(style_key): if entry.get(style_key):
try:
if isinstance(style_val, dict): if isinstance(style_val, dict):
style_val = style_val[entry[style_key]] style_val = style_val[entry[style_key]]
description += f"§{style_val}" description += f"§{style_val}"
except KeyError:
pass # ignoring these key errors strips out html color codes
description += entry.get("text", "") description += entry.get("text", "")
return description + end return description + end

View File

@@ -110,6 +110,65 @@ class TestPingResponse:
} }
) )
def test_parse_description_strips_html_color_codes(self):
out = PingResponse._parse_description(
{
"extra": [
{"text": " "},
{"strikethrough": True, "color": "#b3eeff", "text": "="},
{"strikethrough": True, "color": "#b9ecff", "text": "="},
{"strikethrough": True, "color": "#c0eaff", "text": "="},
{"strikethrough": True, "color": "#c7e8ff", "text": "="},
{"strikethrough": True, "color": "#cee6ff", "text": "="},
{"strikethrough": True, "color": "#d5e4ff", "text": "="},
{"strikethrough": True, "color": "#dce2ff", "text": "="},
{"strikethrough": True, "color": "#e3e0ff", "text": "="},
{"strikethrough": True, "color": "#eadeff", "text": "="},
{"strikethrough": True, "color": "#f1dcff", "text": "="},
{"strikethrough": True, "color": "#f8daff", "text": "="},
{"strikethrough": True, "color": "#ffd9ff", "text": "="},
{"strikethrough": True, "color": "#f4dcff", "text": "="},
{"strikethrough": True, "color": "#f9daff", "text": "="},
{"strikethrough": True, "color": "#ffd9ff", "text": "="},
{"color": "white", "text": " "},
{"bold": True, "color": "#66ff99", "text": "C"},
{"bold": True, "color": "#75f5a2", "text": "r"},
{"bold": True, "color": "#84ebab", "text": "e"},
{"bold": True, "color": "#93e2b4", "text": "a"},
{"bold": True, "color": "#a3d8bd", "text": "t"},
{"bold": True, "color": "#b2cfc6", "text": "i"},
{"bold": True, "color": "#c1c5cf", "text": "v"},
{"bold": True, "color": "#d1bbd8", "text": "e"},
{"bold": True, "color": "#e0b2e1", "text": "F"},
{"bold": True, "color": "#efa8ea", "text": "u"},
{"bold": True, "color": "#ff9ff4", "text": "n "},
{"strikethrough": True, "color": "#b3eeff", "text": "="},
{"strikethrough": True, "color": "#b9ecff", "text": "="},
{"strikethrough": True, "color": "#c0eaff", "text": "="},
{"strikethrough": True, "color": "#c7e8ff", "text": "="},
{"strikethrough": True, "color": "#cee6ff", "text": "="},
{"strikethrough": True, "color": "#d5e4ff", "text": "="},
{"strikethrough": True, "color": "#dce2ff", "text": "="},
{"strikethrough": True, "color": "#e3e0ff", "text": "="},
{"strikethrough": True, "color": "#eadeff", "text": "="},
{"strikethrough": True, "color": "#f1dcff", "text": "="},
{"strikethrough": True, "color": "#f8daff", "text": "="},
{"strikethrough": True, "color": "#ffd9ff", "text": "="},
{"strikethrough": True, "color": "#f4dcff", "text": "="},
{"strikethrough": True, "color": "#f9daff", "text": "="},
{"strikethrough": True, "color": "#ffd9ff", "text": "="},
{"color": "white", "text": " \n "},
{"bold": True, "color": "#E5E5E5", "text": "The server has been updated to "},
{"bold": True, "color": "#97ABFF", "text": "1.17.1"},
],
"text": "",
}
)
assert (
out
== " ===============§f §lC§lr§le§la§lt§li§lv§le§lF§lu§ln ===============§f \n §lThe server has been updated to §l1.17.1"
)
def test_parse_description(self): def test_parse_description(self):
out = PingResponse._parse_description("test §2description") out = PingResponse._parse_description("test §2description")
assert out == "test §2description" assert out == "test §2description"