From 402bfc61d960a8fd4319eb2b29b561ed94154447 Mon Sep 17 00:00:00 2001 From: kotontrion <141950090+kotontrion@users.noreply.github.com> Date: Mon, 18 Sep 2023 21:16:22 +0200 Subject: [PATCH] fix: check for invalid markup on label (#99) --- src/widgets/label.ts | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/widgets/label.ts b/src/widgets/label.ts index b3dbc64..31ec833 100644 --- a/src/widgets/label.ts +++ b/src/widgets/label.ts @@ -1,10 +1,16 @@ import GObject from 'gi://GObject'; import Gtk from 'gi://Gtk?version=3.0'; +import GLib from 'gi://GLib'; import Pango from 'gi://Pango'; const justification = ['left', 'center', 'right', 'fill']; const truncate = ['none', 'start', 'middle', 'end']; +interface Params { + label?: string + [key: string]: unknown +} + export default class AgsLabel extends Gtk.Label { static { GObject.registerClass({ @@ -24,8 +30,28 @@ export default class AgsLabel extends Gtk.Label { }, this); } - constructor(params: object | string) { - super(typeof params === 'string' ? { label: params } : params); + constructor(params: Params | string) { + const label = typeof params === 'string' ? params : params.label; + if (typeof params === 'object') + delete params.label; + + super(typeof params === 'string' ? {} : params); + this.label = label || ''; + } + + set label(label: string) { + if (this.useMarkup) { + try { + // @ts-expect-error + Pango.parse_markup(label, label.length, '0'); + } catch (e) { + if (e instanceof GLib.MarkupError) + label = GLib.markup_escape_text(label, label.length); + else + logError(e as Error); + } + } + super.label = label; } get truncate() { return truncate[this.ellipsize]; }