From 5487906487c33962fc1a6a6a28c9efbf2af78339 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Fri, 16 Jul 2010 19:35:51 +0200 Subject: [PATCH] Naughty: Catch invalid markup in notifications Previously, an invalid markup caused an empty popup. Since the C core now throws a lua error on invalid markup, we have a way to notice that something is wrong. This patch first tries to set the notification's text the same way we did previously. If that fails, everything is escaped and the result is used as the text for the notification. Thanks to farhaven/Gregor Best for the initial version of this and for the string.gsub() call I stole from him. :) Signed-off-by: Uli Schlachter --- lib/naughty.lua.in | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/naughty.lua.in b/lib/naughty.lua.in index 4dbfd929a..7ed210920 100644 --- a/lib/naughty.lua.in +++ b/lib/naughty.lua.in @@ -9,6 +9,7 @@ local pairs = pairs local table = table local type = type local string = string +local pcall = pcall local capi = { screen = screen, awesome = awesome, dbus = dbus, @@ -354,9 +355,19 @@ function notify(args) local textbox = capi.widget({ type = "textbox", align = "flex" }) textbox:buttons(util.table.join(button({ }, 1, run), button({ }, 3, die))) layout.margins[textbox] = { right = margin, left = margin, bottom = margin, top = margin } - textbox.text = string.format('%s%s', font, title, text:gsub("", "\n")) textbox.valign = "middle" + local function setText(pattern, replacements) + textbox.text = string.format('%s%s', font, title, text:gsub(pattern, replacements)) + end + + -- First try to set the text while only interpreting
. + -- (Setting a textbox' .text to an invalid pattern throws a lua error) + if not pcall(setText, "", "\n") then + -- That failed, escape everything which might cause an error from pango + setText("[<>&]", { ['<'] = "<", ['>'] = ">", ['&'] = "&" }) + end + -- create iconbox local iconbox = nil if icon then