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 <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2010-07-16 19:35:51 +02:00
parent 62a0189f05
commit 5487906487
1 changed files with 12 additions and 1 deletions

View File

@ -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('<span font_desc="%s"><b>%s</b>%s</span>', font, title, text:gsub("<br.->", "\n"))
textbox.valign = "middle"
local function setText(pattern, replacements)
textbox.text = string.format('<span font_desc="%s"><b>%s</b>%s</span>', font, title, text:gsub(pattern, replacements))
end
-- First try to set the text while only interpreting <br>.
-- (Setting a textbox' .text to an invalid pattern throws a lua error)
if not pcall(setText, "<br.->", "\n") then
-- That failed, escape everything which might cause an error from pango
setText("[<>&]", { ['<'] = "&lt;", ['>'] = "&gt;", ['&'] = "&amp;" })
end
-- create iconbox
local iconbox = nil
if icon then