From 24ee5208607bd10be7d0588a04acc92c9b87152b Mon Sep 17 00:00:00 2001 From: ArchNemsys Date: Tue, 23 Feb 2021 17:22:10 +0000 Subject: [PATCH] Quality of life enhancments --- QGumboParser/qgumbodocument.cpp | 9 +++++++-- QGumboParser/qgumbodocument.h | 3 ++- QGumboParser/qgumbonode.cpp | 13 ++++++++++++- QGumboParser/qgumbonode.h | 1 + 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/QGumboParser/qgumbodocument.cpp b/QGumboParser/qgumbodocument.cpp index 60726aa..d481519 100644 --- a/QGumboParser/qgumbodocument.cpp +++ b/QGumboParser/qgumbodocument.cpp @@ -22,8 +22,13 @@ QGumboDocument QGumboDocument::parse(QByteArray data) return QGumboDocument(data); } -QGumboDocument::QGumboDocument(QByteArray arr) : - options_(&kGumboDefaultOptions), +QGumboDocument QGumboDocument::parse(QByteArray data, const GumboOptions& opt) +{ + return QGumboDocument(data, opt); +} + +QGumboDocument::QGumboDocument(QByteArray arr, const GumboOptions& opt) : + options_(&opt), sourceData_(arr) { gumboOutput_ = gumbo_parse_with_options(options_, diff --git a/QGumboParser/qgumbodocument.h b/QGumboParser/qgumbodocument.h index fa88970..8a70765 100644 --- a/QGumboParser/qgumbodocument.h +++ b/QGumboParser/qgumbodocument.h @@ -13,6 +13,7 @@ class QGumboDocument static QGumboDocument parse(const QString& htmlText); static QGumboDocument parse(const char* utf8data); static QGumboDocument parse(QByteArray utf8data); + static QGumboDocument parse(QByteArray utf8data, const GumboOptions& opt); public: ~QGumboDocument(); @@ -21,7 +22,7 @@ class QGumboDocument QGumboNode rootNode() const; private: - QGumboDocument(QByteArray); + QGumboDocument(QByteArray, const GumboOptions& opt=kGumboDefaultOptions); QGumboDocument(const QGumboDocument&) = delete; QGumboDocument& operator=(const QGumboDocument&) = delete; diff --git a/QGumboParser/qgumbonode.cpp b/QGumboParser/qgumbonode.cpp index b9abb87..7ab2a33 100644 --- a/QGumboParser/qgumbonode.cpp +++ b/QGumboParser/qgumbonode.cpp @@ -124,7 +124,6 @@ QGumboNodes QGumboNode::getElementsByClassName(const QString& name) const const QVector parts = value.splitRef(QChar(' '), Qt::SkipEmptyParts, Qt::CaseInsensitive); #endif - for (const QStringRef& part: parts) { if (part.compare(name, Qt::CaseInsensitive) == 0) { nodes.emplace_back(QGumboNode(node)); @@ -335,6 +334,18 @@ void QGumboNode::forEach(std::function func) const iterateTree(ptr_, functor); } +void QGumboNode::forEachChild(std::function func) const +{ + Q_ASSERT(ptr_); + + auto functor = [&func](GumboNode* node) { + func(QGumboNode(node)); + return false; + }; + + iterateChildren(ptr_, functor); +} + QGumboNode::operator bool() const { return ptr_; diff --git a/QGumboParser/qgumbonode.h b/QGumboParser/qgumbonode.h index 2c4545f..33f031e 100644 --- a/QGumboParser/qgumbonode.h +++ b/QGumboParser/qgumbonode.h @@ -47,6 +47,7 @@ class QGumboNode QGumboAttributes allAttributes() const; void forEach(std::function) const; + void forEachChild(std::function) const; explicit operator bool() const;