Fix some markup characters not being unescaped

This commit is contained in:
Erik Reider
2022-05-08 19:37:34 +02:00
parent ec01b49319
commit 19beafa528

View File

@@ -58,7 +58,15 @@ namespace SwayNotificationCenter {
private static Regex tag_regex; private static Regex tag_regex;
private static Regex tag_replace_regex; private static Regex tag_replace_regex;
private static Regex tag_unescape_regex;
private const string[] TAGS = { "b", "u", "i" }; private const string[] TAGS = { "b", "u", "i" };
private const string[] UNESCAPE_CHARS = {
"lt;", "#60;", "#x3C;", "#x3c;", // <
"gt;", "#62;", "#x3E;", "#x3e;", // >
"apos;", "#39;", // '
"quot;", "#34;", // "
"amp;" // &
};
private Notification () {} private Notification () {}
@@ -89,6 +97,8 @@ namespace SwayNotificationCenter {
string joined_tags = string.joinv ("|", TAGS); string joined_tags = string.joinv ("|", TAGS);
tag_regex = new Regex ("&lt;/?(%s)&gt;".printf (joined_tags)); tag_regex = new Regex ("&lt;/?(%s)&gt;".printf (joined_tags));
tag_replace_regex = new Regex ("&lt;/?|&gt;"); tag_replace_regex = new Regex ("&lt;/?|&gt;");
string unescaped = string.joinv ("|", UNESCAPE_CHARS);
tag_unescape_regex = new Regex ("&amp;(?=%s)".printf (unescaped));
} catch (Error e) { } catch (Error e) {
stderr.printf ("Invalid regex: %s", e.message); stderr.printf ("Invalid regex: %s", e.message);
} }
@@ -255,6 +265,10 @@ namespace SwayNotificationCenter {
0, 0,
RegexMatchFlags.NOTEMPTY, RegexMatchFlags.NOTEMPTY,
this.regex_tag_eval_cb); this.regex_tag_eval_cb);
// Unescape a few characters that may have been double escaped
// Sending "<" in Discord would result in "&amp;lt;" without this
// &amp;lt; -> &lt;
escaped = tag_unescape_regex.replace_literal (escaped, escaped.length, 0, "&");
// Turns it back to markdown, defaults to original if not valid // Turns it back to markdown, defaults to original if not valid
Pango.AttrList ? attr = null; Pango.AttrList ? attr = null;
@@ -316,10 +330,10 @@ namespace SwayNotificationCenter {
private void set_style_urgency () { private void set_style_urgency () {
switch (param.urgency) { switch (param.urgency) {
case UrgencyLevels.LOW : case UrgencyLevels.LOW:
base_box.get_style_context ().add_class ("low"); base_box.get_style_context ().add_class ("low");
break; break;
case UrgencyLevels.NORMAL : case UrgencyLevels.NORMAL:
default: default:
base_box.get_style_context ().add_class ("normal"); base_box.get_style_context ().add_class ("normal");
break; break;