Skip to content

Commit

Permalink
Fix handling of non-HTML text values having had links inserted (fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Oct 11, 2023
1 parent 2f04e75 commit b80e868
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/core/utils/stringutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,20 @@ StringUtils::StringUtils( QObject *parent )
{
}


QString StringUtils::insertLinks( const QString &string )
{
return QgsStringUtils::insertLinks( string );
}

bool StringUtils::hasLinks( const QString &string )
{
// These expressions are taken from QgsStringUtils::insertLinks
const thread_local QRegularExpression urlRegEx( QStringLiteral( "(\\b(([\\w-]+://?|www[.])[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^!\"#$%&'()*+,\\-./:;<=>?@[\\\\\\]^_`{|}~\\s]|/))))" ) );
const thread_local QRegularExpression emailRegEx( QStringLiteral( "([\\w._%+-]+@[\\w.-]+\\.[A-Za-z]+)" ) );

return string.contains( urlRegEx ) || string.contains( emailRegEx );
}

QString StringUtils::createUuid()
{
return QUuid::createUuid().toString();
Expand Down
3 changes: 3 additions & 0 deletions src/core/utils/stringutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class QFIELD_CORE_EXPORT StringUtils : public QObject
//! Returns a string with any URL (e.g., http(s)/ftp) and mailto: text converted to valid HTML <a …> links
static Q_INVOKABLE QString insertLinks( const QString &string );

//! Returns whether a string contains one or more URLs
static Q_INVOKABLE bool hasLinks( const QString &string );

//! Returns a new UUID string
static Q_INVOKABLE QString createUuid();

Expand Down
4 changes: 3 additions & 1 deletion src/qml/editorwidgets/TextEdit.qml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ EditorWidgetBase {
color: Theme.mainTextColor
opacity: 0.45
wrapMode: Text.Wrap
textFormat: config['IsMultiline'] === true && config['UseHtml'] ? TextEdit.RichText : TextEdit.AutoText
textFormat: (config['IsMultiline'] === true && config['UseHtml']) || stringUtilities.hasLinks(value)
? TextEdit.RichText
: TextEdit.AutoText

text: value == null ? '' : config['IsMultiline'] === true
? config['UseHtml'] === true ? value : stringUtilities.insertLinks(value)
Expand Down

1 comment on commit b80e868

@qfield-fairy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.