From f6c588ee7e0694a0f630e3e362f751ac0a6d91ae Mon Sep 17 00:00:00 2001 From: xiaoyifang Date: Wed, 20 Apr 2022 20:53:57 +0800 Subject: [PATCH] fix: collapse function does not work expectedly. the website dictionaries do not collapse as it will always treated as 0 . the normal dictionaries html size is not correct calculated due to a qt bug https://bugreports.qt.io/browse/QTBUG-102757 --- article_maker.cc | 20 +++++++++++++++++++- article_maker.hh | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/article_maker.cc b/article_maker.cc index db56ca2b..e18b63fb 100644 --- a/article_maker.cc +++ b/article_maker.cc @@ -596,7 +596,7 @@ void ArticleRequest::bodyFinished() } } - int size = QTextDocumentFragment::fromHtml( text ).toPlainText().length(); + int size = htmlTextSize( text ); if( size > articleSizeLimit ) collapse = true; } @@ -750,6 +750,24 @@ void ArticleRequest::bodyFinished() } } +int ArticleRequest::htmlTextSize( QString html ) +{ + // website dictionary. + if( html.contains( QRegularExpression( "]*>", QRegularExpression::CaseInsensitiveOption ) ) ) + { + //arbitary number; + return 1000; + } + + //https://bugreports.qt.io/browse/QTBUG-102757 + QString stripStyleSheet = + html.remove( QRegularExpression( "]*>", QRegularExpression::CaseInsensitiveOption ) ) + .remove( QRegularExpression( "[\\s\\S]*?<\\/script>", QRegularExpression::CaseInsensitiveOption|QRegularExpression::MultilineOption ) ); + int size = QTextDocumentFragment::fromHtml( stripStyleSheet ).toPlainText().length(); + + return size; +} + void ArticleRequest::stemmedSearchFinished() { // Got stemmed matching results diff --git a/article_maker.hh b/article_maker.hh index c3a017d4..665515f3 100644 --- a/article_maker.hh +++ b/article_maker.hh @@ -148,7 +148,7 @@ private slots: void individualWordFinished(); private: - + int htmlTextSize( QString html ); /// Appends the given string to 'data', with locking its mutex. void appendToData( std::string const & );