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 "<h1>&&a&ction</h1>" 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
This commit is contained in:
Fabian Vogt 2017-07-13 15:01:35 +02:00
parent 7b394146e5
commit 48a8245db4
3 changed files with 55 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,49 @@
/*
* Copyright 2017 by Fabian Vogt <fabian@ritter-vogt.de>
* Copyright 2017 by Kai Uwe Broulik <kde@privat.broulik.de>
*
* 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 {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;'
}[tag] || tag
});
}
function underlineAmpersands(match, p) {
if(p == "&amp;")
return p;
return "<u>" + p + "</u>";
}
/* This function is a replacement for the flawed
* QtQuickControlsPrivate.StyleHelpers.stylizeMnemonics.
* It scans the passed text for mnemonics, to put them into HTML <u></u>
* tags. This means it emits HTML, but accepts only plaintext.
* Simply passing HTML escaped plaintext won't work, as it would then
* replace &lt; with <u>l</u>t; so we need to implement it ourselves. */
function stylizeEscapedMnemonics(text) {
return text.replace(/&amp;(&amp;|.)/g, underlineAmpersands);
}