From 48a8245db4d8f9207e06ef10af66184e4d201ec0 Mon Sep 17 00:00:00 2001 From: Fabian Vogt Date: Thu, 13 Jul 2017 15:01:35 +0200 Subject: [PATCH] Treat Button/ToolButton labels as plaintext Summary: The label text gets treated as RichText/StyledText, which is required to display mnemonics underlined. Therefore it is necessary to manually HTML escape the label text, which unfortunately breaks mnemonics as escaped HTML contains ampersands. This commit fixes that by introducing a custom function to stylize mnemonics in HTML escaped text. Test Plan: Ran a modified knotificationdbustest with "

&&a&ction

" and "actio&n2" as actions. Result: http://i.imgur.com/xHifDBu.png Reviewers: #plasma, broulik Subscribers: plasma-devel, #frameworks Tags: #plasma, #frameworks Differential Revision: https://phabricator.kde.org/D6679 --- .../plasmastyle/ButtonStyle.qml | 4 +- .../plasmastyle/ToolButtonStyle.qml | 4 +- .../plasmastyle/private/Util.js | 49 +++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/declarativeimports/plasmastyle/private/Util.js diff --git a/src/declarativeimports/plasmastyle/ButtonStyle.qml b/src/declarativeimports/plasmastyle/ButtonStyle.qml index ffcb6b748..5de2e2b38 100644 --- a/src/declarativeimports/plasmastyle/ButtonStyle.qml +++ b/src/declarativeimports/plasmastyle/ButtonStyle.qml @@ -28,6 +28,7 @@ import org.kde.plasma.extras 2.0 as PlasmaExtras import org.kde.plasma.components 2.0 as PlasmaComponents import "private" as Private +import "private/Util.js" as Util QtQuickControlStyle.ButtonStyle { id: style @@ -72,7 +73,8 @@ QtQuickControlStyle.ButtonStyle { PlasmaComponents.Label { id: label anchors.verticalCenter: parent.verticalCenter - text: QtQuickControlsPrivate.StyleHelpers.stylizeMnemonics(control.text) + text: Util.stylizeEscapedMnemonics(Util.toHtmlEscaped(control.text)) + textFormat: Text.StyledText font: control.font || theme.defaultFont visible: control.text != "" Layout.fillWidth: true diff --git a/src/declarativeimports/plasmastyle/ToolButtonStyle.qml b/src/declarativeimports/plasmastyle/ToolButtonStyle.qml index 76bacaad5..77bc169de 100644 --- a/src/declarativeimports/plasmastyle/ToolButtonStyle.qml +++ b/src/declarativeimports/plasmastyle/ToolButtonStyle.qml @@ -28,6 +28,7 @@ import org.kde.plasma.extras 2.0 as PlasmaExtras import org.kde.plasma.components 2.0 as PlasmaComponents import "private" as Private +import "private/Util.js" as Util QtQuickControlStyle.ButtonStyle { id: style @@ -95,7 +96,8 @@ QtQuickControlStyle.ButtonStyle { id: label anchors.verticalCenter: parent.verticalCenter Layout.minimumWidth: implicitWidth - text: QtQuickControlsPrivate.StyleHelpers.stylizeMnemonics(control.text) + text: Util.stylizeEscapedMnemonics(Util.toHtmlEscaped(control.text)) + textFormat: Text.StyledText font: control.font || theme.defaultFont visible: control.text != "" Layout.fillWidth: true diff --git a/src/declarativeimports/plasmastyle/private/Util.js b/src/declarativeimports/plasmastyle/private/Util.js new file mode 100644 index 000000000..8afba999f --- /dev/null +++ b/src/declarativeimports/plasmastyle/private/Util.js @@ -0,0 +1,49 @@ +/* + * Copyright 2017 by Fabian Vogt + * Copyright 2017 by Kai Uwe Broulik + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 2.010-1301, USA. + */ + +.pragma library + +/* Like QString::toHtmlEscaped */ +function toHtmlEscaped(s) { + return s.replace(/[&<>]/g, function (tag) { + return { + '&': '&', + '<': '<', + '>': '>' + }[tag] || tag + }); +} + +function underlineAmpersands(match, p) { + if(p == "&") + return p; + + return "" + p + ""; +} + +/* This function is a replacement for the flawed + * QtQuickControlsPrivate.StyleHelpers.stylizeMnemonics. + * It scans the passed text for mnemonics, to put them into HTML + * tags. This means it emits HTML, but accepts only plaintext. + * Simply passing HTML escaped plaintext won't work, as it would then + * replace < with lt; so we need to implement it ourselves. */ +function stylizeEscapedMnemonics(text) { + return text.replace(/&(&|.)/g, underlineAmpersands); +}