util: Implement fixMarkup in util as opposed to messageList

We want to be able to import it in the markup unit test without
bringing in a UI dependency.

https://bugzilla.gnome.org/show_bug.cgi?id=783738

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/3164>
This commit is contained in:
Sam Spilsbury
2017-06-14 00:50:03 +08:00
committed by Marge Bot
parent 4711f6eee4
commit 3d9c40783f
3 changed files with 35 additions and 34 deletions

View File

@ -2,6 +2,7 @@
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import Pango from 'gi://Pango';
import Shell from 'gi://Shell';
import St from 'gi://St';
import GnomeDesktop from 'gi://GnomeDesktop';
@ -189,6 +190,35 @@ function _handleSpawnError(command, err) {
({notifyError}) => notifyError(title, err.message));
}
/**
* Fix up embedded markup so that it can be displayed correctly in
* UI elements such as the message list. In some cases, we might want to
* keep some of the embedded markup, so specify allowMarkup for that case
*
* @param {string} text containing markup to escape and parse
* @param {boolean} allowMarkup to allow embedded markup or just escape it all
* @returns the escaped string
*/
export function fixMarkup(text, allowMarkup) {
if (allowMarkup) {
// Support &amp;, &quot;, &apos;, &lt; and &gt;, escape all other
// occurrences of '&'.
let _text = text.replace(/&(?!amp;|quot;|apos;|lt;|gt;)/g, '&amp;');
// Support <b>, <i>, and <u>, escape anything else
// so it displays as raw markup.
_text = _text.replace(/<(?!\/?[biu]>)/g, '&lt;');
try {
Pango.parse_markup(_text, -1, '');
return _text;
} catch (e) {}
}
// !allowMarkup, or invalid markup
return GLib.markup_escape_text(text, -1);
}
/**
* Returns an {@link St.Label} with the date passed formatted
* using {@link formatTime}