From 9d8a3eb370195321d224c0a51a6752c62404ac1b Mon Sep 17 00:00:00 2001 From: Jake Stanger Date: Sun, 2 Jun 2024 15:23:30 +0100 Subject: [PATCH 1/2] fix: correctly escape pango markup --- src/dynamic_value/dynamic_string.rs | 2 +- src/gtk_helpers.rs | 17 +++++++++++++++-- src/modules/clock.rs | 4 ++-- src/modules/custom/button.rs | 3 ++- src/modules/custom/label.rs | 3 ++- src/modules/label.rs | 3 ++- src/modules/music/mod.rs | 8 ++++---- src/modules/script.rs | 3 ++- src/modules/sysinfo.rs | 4 ++-- src/modules/upower.rs | 6 +++--- 10 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/dynamic_value/dynamic_string.rs b/src/dynamic_value/dynamic_string.rs index 6c0cfb52..b33711f1 100644 --- a/src/dynamic_value/dynamic_string.rs +++ b/src/dynamic_value/dynamic_string.rs @@ -22,7 +22,7 @@ enum DynamicStringSegment { /// /// ```rs /// dynamic_string(&text, move |string| { -/// label.set_markup(&string); +/// label.set_markup_escaped(&string); /// }); /// ``` pub fn dynamic_string(input: &str, mut f: F) diff --git a/src/gtk_helpers.rs b/src/gtk_helpers.rs index cc88822b..c6d2fb65 100644 --- a/src/gtk_helpers.rs +++ b/src/gtk_helpers.rs @@ -1,6 +1,6 @@ -use glib::IsA; +use glib::{markup_escape_text, IsA}; use gtk::prelude::*; -use gtk::{Orientation, Widget}; +use gtk::{Label, Orientation, Widget}; /// Represents a widget's size /// and location relative to the bar's start edge. @@ -75,3 +75,16 @@ impl> IronbarGtkExt for W { unsafe { self.set_data(key, value) } } } + +pub trait IronbarLabelExt { + /// Sets a label's pango markup, escaping it in the process. + /// This should be used for any label values + /// which could contain special characters, for example `&`. + fn set_markup_escaped(&self, text: &str); +} + +impl IronbarLabelExt for Label { + fn set_markup_escaped(&self, text: &str) { + self.set_markup(markup_escape_text(text).as_str()) + } +} diff --git a/src/modules/clock.rs b/src/modules/clock.rs index 6af5e884..07d40366 100644 --- a/src/modules/clock.rs +++ b/src/modules/clock.rs @@ -9,7 +9,7 @@ use tokio::sync::{broadcast, mpsc}; use tokio::time::sleep; use crate::config::{CommonConfig, ModuleOrientation}; -use crate::gtk_helpers::IronbarGtkExt; +use crate::gtk_helpers::{IronbarGtkExt, IronbarLabelExt}; use crate::modules::{ Module, ModuleInfo, ModuleParts, ModulePopup, ModuleUpdateEvent, PopupButton, WidgetContext, }; @@ -143,7 +143,7 @@ impl Module