Notification: properly display/escape html tags

This commit is contained in:
Lysec
2026-02-15 16:53:00 +01:00
parent 12b5dea5d1
commit 070c3b523c
3 changed files with 42 additions and 10 deletions
+4 -4
View File
@@ -636,7 +636,7 @@ Variants {
pointSize: Style.fontSizeM
font.weight: Style.fontWeightMedium
color: Color.mOnSurface
textFormat: Text.PlainText
textFormat: Text.StyledText
wrapMode: Text.WrapAtWordBoundaryOrAnywhere
maximumLineCount: 3
elide: Text.ElideRight
@@ -649,7 +649,7 @@ Variants {
text: model.body || ""
pointSize: Style.fontSizeM
color: Color.mOnSurface
textFormat: Text.PlainText
textFormat: Text.StyledText
wrapMode: Text.WrapAtWordBoundaryOrAnywhere
maximumLineCount: 5
@@ -758,7 +758,7 @@ Variants {
pointSize: Style.fontSizeM
font.weight: Style.fontWeightMedium
color: Color.mOnSurface
textFormat: Text.PlainText
textFormat: Text.StyledText
maximumLineCount: 1
elide: Text.ElideRight
Layout.fillWidth: true
@@ -770,7 +770,7 @@ Variants {
text: model.body || ""
pointSize: Style.fontSizeS
color: Color.mOnSurfaceVariant
textFormat: Text.PlainText
textFormat: Text.StyledText
wrapMode: Text.Wrap
maximumLineCount: 2
elide: Text.ElideRight
@@ -825,7 +825,7 @@ SmartPanel {
text: model.summary || I18n.tr("common.no-summary")
pointSize: Style.fontSizeM
color: Color.mOnSurface
textFormat: Text.PlainText
textFormat: Text.StyledText
wrapMode: Text.Wrap
maximumLineCount: notificationDelegate.isExpanded ? 999 : 2
elide: Text.ElideRight
@@ -838,7 +838,7 @@ SmartPanel {
text: model.body || ""
pointSize: Style.fontSizeS
color: Color.mOnSurfaceVariant
textFormat: Text.PlainText
textFormat: Text.StyledText
wrapMode: Text.Wrap
maximumLineCount: notificationDelegate.isExpanded ? 999 : 3
elide: Text.ElideRight
+36 -4
View File
@@ -426,8 +426,8 @@ Singleton {
return {
"id": id,
"summary": n.summary || "",
"body": stripTags(n.body || ""),
"summary": processNotificationText(n.summary || ""),
"body": processNotificationText(n.body || ""),
"appName": getAppName(n.appName || n.desktopEntry || ""),
"urgency": n.urgency < 0 || n.urgency > 2 ? 1 : n.urgency,
"expireTimeout": n.expireTimeout,
@@ -737,8 +737,40 @@ Singleton {
return ThemeIcons.iconFromName(icon);
}
function stripTags(text) {
return text.replace(/<[^>]*>?/gm, '');
function escapeHtml(text) {
if (!text)
return "";
return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}
function processNotificationText(text) {
if (!text)
return "";
// Split by tags to process segments separately
const parts = text.split(/(<[^>]+>)/);
let result = "";
const allowedTags = ["b", "i", "u", "a", "br"];
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
if (part.startsWith("<") && part.endsWith(">")) {
const content = part.substring(1, part.length - 1);
const firstWord = content.split(/[\s/]/).filter(s => s.length > 0)[0]?.toLowerCase();
if (allowedTags.includes(firstWord)) {
// Preserve valid HTML tag
result += part;
} else {
// Unknown tag: strip brackets and escape inner content
result += escapeHtml(content);
}
} else {
// Normal text: escape everything
result += escapeHtml(part);
}
}
return result;
}
function generateImageId(notification, image) {