From 98584550358c486e7e95ea3a1124f4a35c2746d2 Mon Sep 17 00:00:00 2001 From: Xiao Yi Fang Date: Fri, 19 Jul 2024 13:55:06 +0800 Subject: [PATCH 1/4] fix: stardict crash with access_read_violation --- src/dict/stardict.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dict/stardict.cc b/src/dict/stardict.cc index 46862641..cbc51292 100644 --- a/src/dict/stardict.cc +++ b/src/dict/stardict.cc @@ -545,7 +545,7 @@ string StardictDictionary::handleResource( char type, char const * resource, siz articleNewText.clear(); } - return ( articleText.toUtf8().data() ); + return articleText.toStdString(); } case 'm': // Pure meaning, usually means preformatted text return "
" + Html::preformat( string( resource, size ), isToLanguageRTL() ) + "
"; From cd580596958e2ae910ca61683dddc75012afbabe Mon Sep 17 00:00:00 2001 From: xiaoyifang <105986+xiaoyifang@users.noreply.github.com> Date: Tue, 23 Jul 2024 17:57:57 +0800 Subject: [PATCH 2/4] fix:QCheckBox signal (#1705) * fix:QCheckBox signal * fix:QCheckBox signal --- src/ui/articleview.cc | 12 +++++------- src/ui/articleview.hh | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/ui/articleview.cc b/src/ui/articleview.cc index 3c6d306a..d66ab839 100644 --- a/src/ui/articleview.cc +++ b/src/ui/articleview.cc @@ -137,7 +137,7 @@ ArticleView::ArticleView( QWidget * parent, connect( searchPanel->previous, &QPushButton::clicked, this, &ArticleView::on_searchPrevious_clicked ); connect( searchPanel->next, &QPushButton::clicked, this, &ArticleView::on_searchNext_clicked ); connect( searchPanel->close, &QPushButton::clicked, this, &ArticleView::on_searchCloseButton_clicked ); - connect( searchPanel->caseSensitive, &QPushButton::clicked, this, &ArticleView::on_searchCaseSensitive_clicked ); + connect( searchPanel->caseSensitive, &QCheckBox::toggled, this, &ArticleView::on_searchCaseSensitive_clicked ); connect( searchPanel->lineEdit, &QLineEdit::textEdited, this, &ArticleView::on_searchText_textEdited ); connect( searchPanel->lineEdit, &QLineEdit::returnPressed, this, &ArticleView::on_searchText_returnPressed ); connect( ftsSearchPanel->next, &QPushButton::clicked, this, &ArticleView::on_ftsSearchNext_clicked ); @@ -1963,8 +1963,11 @@ void ArticleView::on_searchCloseButton_clicked() closeSearch(); } -void ArticleView::on_searchCaseSensitive_clicked() +void ArticleView::on_searchCaseSensitive_clicked( bool checked ) { + //clear the previous findText results. + //when the results is empty, the highlight has not been removed.more likely a qt bug. + webview->findText( "" ); performFindOperation( false ); } @@ -2027,11 +2030,6 @@ void ArticleView::performFindOperation( bool backwards ) findText( text, f, [ text, this ]( bool match ) { bool nomatch = !text.isEmpty() && !match; - if ( nomatch ) { - //clear the previous findText results. - //when the results is empty, the highlight has not been removed.more likely a qt bug. - webview->findText( "" ); - } Utils::Widget::setNoResultColor( searchPanel->lineEdit, nomatch ); } ); } diff --git a/src/ui/articleview.hh b/src/ui/articleview.hh index 36b6b20b..2aa82f8f 100644 --- a/src/ui/articleview.hh +++ b/src/ui/articleview.hh @@ -357,7 +357,7 @@ private slots: void on_searchText_textEdited(); void on_searchText_returnPressed(); void on_searchCloseButton_clicked(); - void on_searchCaseSensitive_clicked(); + void on_searchCaseSensitive_clicked( bool ); void on_ftsSearchPrevious_clicked(); void on_ftsSearchNext_clicked(); From 3ef35daec3fa7be0597255102e5db2288023e809 Mon Sep 17 00:00:00 2001 From: xiaoyifang <105986+xiaoyifang@users.noreply.github.com> Date: Wed, 24 Jul 2024 16:54:34 +0800 Subject: [PATCH 3/4] opt: use static regex (#1706) * opt: use static regex * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- src/dict/stardict.cc | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/dict/stardict.cc b/src/dict/stardict.cc index cbc51292..4aaa9623 100644 --- a/src/dict/stardict.cc +++ b/src/dict/stardict.cc @@ -422,10 +422,12 @@ private: } old = s; } - s.replace( QRegularExpression( "&.\\s*\\{", - QRegularExpression::UseUnicodePropertiesOption - | QRegularExpression::DotMatchesEverythingOption ), - "" ); + + static QRegularExpression leadingBrace( "&.\\s*\\{", + QRegularExpression::UseUnicodePropertiesOption + | QRegularExpression::DotMatchesEverythingOption ); + + s.replace( leadingBrace, "" ); s.replace( "}", "" ); } @@ -448,18 +450,18 @@ string StardictDictionary::handleResource( char type, char const * resource, siz { QString articleText = QString( "
" ) + QString::fromUtf8( resource, size ) + "
"; - QRegularExpression imgRe( R"((<\s*(?:img|script)\s+[^>]*src\s*=\s*["']?)(?!(?:data|https?|ftp):))", - QRegularExpression::CaseInsensitiveOption ); - QRegularExpression linkRe( R"((<\s*link\s+[^>]*href\s*=\s*["']?)(?!(?:data|https?|ftp):))", - QRegularExpression::CaseInsensitiveOption ); + static QRegularExpression imgRe( R"((<\s*(?:img|script)\s+[^>]*src\s*=\s*["']?)(?!(?:data|https?|ftp):))", + QRegularExpression::CaseInsensitiveOption ); + static QRegularExpression linkRe( R"((<\s*link\s+[^>]*href\s*=\s*["']?)(?!(?:data|https?|ftp):))", + QRegularExpression::CaseInsensitiveOption ); articleText.replace( imgRe, "\\1bres://" + QString::fromStdString( getId() ) + "/" ) .replace( linkRe, "\\1bres://" + QString::fromStdString( getId() ) + "/" ); // Handle links to articles - QRegularExpression linksReg( R"(]*)href\s*=\s*['"](bword://)?([^'"]+)['"])", - QRegularExpression::CaseInsensitiveOption ); + static QRegularExpression linksReg( R"(]*)href\s*=\s*['"](bword://)?([^'"]+)['"])", + QRegularExpression::CaseInsensitiveOption ); int pos = 0; @@ -508,9 +510,9 @@ string StardictDictionary::handleResource( char type, char const * resource, siz // Handle "audio" tags - QRegularExpression audioRe( R"(<\s*audio\s*src\s*=\s*(["']+)([^"']+)(["'])\s*>(.*))", - QRegularExpression::CaseInsensitiveOption - | QRegularExpression::DotMatchesEverythingOption ); + static QRegularExpression audioRe( R"(<\s*audio\s*src\s*=\s*(["']+)([^"']+)(["'])\s*>(.*))", + QRegularExpression::CaseInsensitiveOption + | QRegularExpression::DotMatchesEverythingOption ); pos = 0; @@ -614,8 +616,8 @@ void StardictDictionary::pangoToHtml( QString & text ) * Attributes "fallback", "lang", "gravity", "gravity_hint" just ignored */ - QRegularExpression spanRegex( "]*)>", QRegularExpression::CaseInsensitiveOption ); - QRegularExpression styleRegex( "(\\w+)=\"([^\"]*)\"" ); + static QRegularExpression spanRegex( "]*)>", QRegularExpression::CaseInsensitiveOption ); + static QRegularExpression styleRegex( "(\\w+)=\"([^\"]*)\"" ); text.replace( "\n", "
" ); @@ -1554,7 +1556,8 @@ void StardictResourceRequest::run() QString id = QString::fromUtf8( dict.getId().c_str() ); int pos = 0; - QRegularExpression links( R"(url\(\s*(['"]?)([^'"]*)(['"]?)\s*\))", QRegularExpression::CaseInsensitiveOption ); + static QRegularExpression links( R"(url\(\s*(['"]?)([^'"]*)(['"]?)\s*\))", + QRegularExpression::CaseInsensitiveOption ); QString newCSS; QRegularExpressionMatchIterator it = links.globalMatch( css ); From e2617f3b9c7159b5af4993fe251ca3e569779c0d Mon Sep 17 00:00:00 2001 From: xiaoyifang <105986+xiaoyifang@users.noreply.github.com> Date: Thu, 25 Jul 2024 16:45:34 +0800 Subject: [PATCH 4/4] fix: stardict qstring modification (#1708) * fix:stardict crash --- src/common/htmlescape.cc | 21 ++++++++++----------- src/common/htmlescape.hh | 4 ++-- src/dict/stardict.cc | 21 ++++++++++----------- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/common/htmlescape.cc b/src/common/htmlescape.cc index b19bed52..aacd5184 100644 --- a/src/common/htmlescape.cc +++ b/src/common/htmlescape.cc @@ -130,26 +130,25 @@ string escapeForJavaScript( string const & str ) return result; } -QString stripHtml( QString & tmp ) +QString stripHtml( QString tmp ) { - tmp.replace( - QRegularExpression( - "<(?:\\s*/?(?:div|h[1-6r]|q|p(?![alr])|br|li(?![ns])|td|blockquote|[uo]l|pre|d[dl]|nav|address))[^>]{0,}>", - QRegularExpression::CaseInsensitiveOption ), - " " ); - tmp.replace( QRegularExpression( "<[^>]*>" ), " " ); + static QRegularExpression htmlRegex( + "<(?:\\s*/?(?:div|h[1-6r]|q|p(?![alr])|br|li(?![ns])|td|blockquote|[uo]l|pre|d[dl]|nav|address))[^>]{0,}>", + QRegularExpression::CaseInsensitiveOption ); + tmp.replace( htmlRegex, " " ); + static QRegularExpression htmlElementRegex( "<[^>]*>" ); + tmp.replace( htmlElementRegex, " " ); return tmp; } -QString unescape( QString const & str, HtmlOption option ) +QString unescape( QString str, HtmlOption option ) { // Does it contain HTML? If it does, we need to strip it if ( str.contains( '<' ) || str.contains( '&' ) ) { - QString tmp = str; if ( option == HtmlOption::Strip ) { - stripHtml( tmp ); + str = stripHtml( str ); } - return QTextDocumentFragment::fromHtml( tmp.trimmed() ).toPlainText(); + return QTextDocumentFragment::fromHtml( str.trimmed() ).toPlainText(); } return str; } diff --git a/src/common/htmlescape.hh b/src/common/htmlescape.hh index 0c3442bd..e437cde1 100644 --- a/src/common/htmlescape.hh +++ b/src/common/htmlescape.hh @@ -25,9 +25,9 @@ string preformat( string const &, bool baseRightToLeft = false ); // Escapes the given string to be included in JavaScript. string escapeForJavaScript( string const & ); -QString stripHtml( QString & tmp ); +QString stripHtml( QString tmp ); // Replace html entities -QString unescape( QString const & str, HtmlOption option = HtmlOption::Strip ); +QString unescape( QString str, HtmlOption option = HtmlOption::Strip ); QString fromHtmlEscaped( QString const & str ); string unescapeUtf8( string const & str, HtmlOption option = HtmlOption::Strip ); diff --git a/src/dict/stardict.cc b/src/dict/stardict.cc index 4aaa9623..6a4b0f3c 100644 --- a/src/dict/stardict.cc +++ b/src/dict/stardict.cc @@ -525,20 +525,19 @@ string StardictDictionary::handleResource( char type, char const * resource, siz QString src = match.captured( 2 ); - if ( src.indexOf( "://" ) >= 0 ) + if ( src.indexOf( "://" ) >= 0 ) { articleNewText += match.captured(); - + } else { - std::string href = "\"gdau://" + getId() + "/" + src.toUtf8().data() + "\""; - QString newTag = QString::fromUtf8( - ( addAudioLink( href, getId() ) + "" ).c_str() ); - newTag += match.captured( 4 ); - if ( match.captured( 4 ).indexOf( ""; + newTag += match.captured( 4 ).toUtf8().constData(); + if ( match.captured( 4 ).indexOf( "Play)"; + } newTag += ""; - articleNewText += newTag; + articleNewText += QString::fromStdString( newTag ); } } if ( pos ) { @@ -546,8 +545,8 @@ string StardictDictionary::handleResource( char type, char const * resource, siz articleText = articleNewText; articleNewText.clear(); } - - return articleText.toStdString(); + auto text = articleText.toUtf8(); + return text.data(); } case 'm': // Pure meaning, usually means preformatted text return "
" + Html::preformat( string( resource, size ), isToLanguageRTL() ) + "
";