feat(StringUtils): implement `plainText(htmlFragment)`

- converts an HTML fragment to plain text
- move the impl to C++ so that it can be used directly from QML too
This commit is contained in:
Lukáš Tinkl 2024-07-05 14:48:14 +02:00 committed by Lukáš Tinkl
parent 9662c974a0
commit 5d9c5d46fd
3 changed files with 12 additions and 0 deletions

View File

@ -19,6 +19,8 @@ public:
Q_INVOKABLE QString extractDomainFromLink(const QString& link) const;
Q_INVOKABLE QString plainText(const QString& htmlFragment) const;
private:
QQmlEngine* m_engine{nullptr};
};

View File

@ -16,4 +16,8 @@ QtObject {
function extractDomainFromLink(link) {
return Internal.StringUtils.extractDomainFromLink(link)
}
function plainText(htmlFragment) {
return Internal.StringUtils.plainText(htmlFragment)
}
}

View File

@ -5,6 +5,7 @@
#include <QQmlEngine>
#include <QQmlFileSelector>
#include <QUrl>
#include <QTextDocumentFragment>
StringUtilsInternal::StringUtilsInternal(QQmlEngine* engine, QObject* parent)
: m_engine(engine)
@ -73,3 +74,8 @@ QString StringUtilsInternal::extractDomainFromLink(const QString& link) const
}
return url.host();
}
QString StringUtilsInternal::plainText(const QString &htmlFragment) const
{
return QTextDocumentFragment::fromHtml(htmlFragment).toPlainText();
}