From c936d03fa05ba6a726724cfe5208cd99ad57f3a0 Mon Sep 17 00:00:00 2001 From: Igor Kushnir Date: Fri, 1 Jul 2022 18:43:43 +0300 Subject: [PATCH 1/3] Replace a fruitless check with an assertion This reduces CPU time waste and simplifies adding Qt WebEngine support. --- articleview.cc | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/articleview.cc b/articleview.cc index 3d10e37b..898a7d1e 100644 --- a/articleview.cc +++ b/articleview.cc @@ -2596,12 +2596,9 @@ void ArticleView::highlightFTSResults() sptr< AccentMarkHandler > marksHandler = ignoreDiacritics ? new DiacriticsHandler : new AccentMarkHandler; - // Clear any current selection - if ( ui.definition->selectedText().size() ) - { - ui.definition->page()->currentFrame()-> - evaluateJavaScript( "window.getSelection().removeAllRanges();_=0;" ); - } + // The code below relies on empty current selection. There must be no selection because this + // function is called only in one place - when a page loading finishes. + Q_ASSERT( !ui.definition->hasSelection() ); QString pageText = ui.definition->page()->currentFrame()->toPlainText(); marksHandler->setText( pageText ); From 1e43bdf18c2918e8dc391cd80cf923bb35ac87dc Mon Sep 17 00:00:00 2001 From: Igor Kushnir Date: Fri, 1 Jul 2022 19:48:42 +0300 Subject: [PATCH 2/3] highlightFTSResults: return earlier if the regexp is empty A default-constructed QRegExp is passed to ArticleView::showDefinition() when a user navigates from an FTS-result page by clicking a link or double-clicking a word. QRegExp().pattern() returns an empty string, which is stored in the "regexp" query item of an URL. When this URL is loaded, ArticleView::loadFinished() calls highlightFTSResults(), which then calls closeSearch(), performs multiple string and regular expression operations and early-returns because the regular expression pattern is empty. Returning earlier skips useless work in this case. An alternative optimization is not calling highlightFTSResults() at all when the regular expression is empty, but that would skip the closeSearch() call and keep the FTS search frame visible on a page with an empty regexp. --- articleview.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/articleview.cc b/articleview.cc index 898a7d1e..66074eb5 100644 --- a/articleview.cc +++ b/articleview.cc @@ -2556,9 +2556,10 @@ void ArticleView::highlightFTSResults() const QUrl & url = ui.definition->url(); - bool ignoreDiacritics = Qt4x5::Url::hasQueryItem( url, "ignore_diacritics" ); - QString regString = Qt4x5::Url::queryItemValue( url, "regexp" ); + if( regString.isEmpty() ) + return; + const bool ignoreDiacritics = Qt4x5::Url::hasQueryItem( url, "ignore_diacritics" ); if( ignoreDiacritics ) regString = gd::toQString( Folding::applyDiacriticsOnly( gd::toWString( regString ) ) ); else From d8d30023294b084ff4ba5d5bc2c77b9662f81212 Mon Sep 17 00:00:00 2001 From: Igor Kushnir Date: Sat, 2 Jul 2022 10:58:54 +0300 Subject: [PATCH 3/3] Revert "Replace a fruitless check with an assertion" The assertion failure can be triggered by full-text-searching for a common word, selecting a result with many large articles and quickly selecting text in the first article while the page is still loading. This reverts commit c936d03fa05ba6a726724cfe5208cd99ad57f3a0. --- articleview.cc | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/articleview.cc b/articleview.cc index 66074eb5..a43f9ac6 100644 --- a/articleview.cc +++ b/articleview.cc @@ -2597,9 +2597,12 @@ void ArticleView::highlightFTSResults() sptr< AccentMarkHandler > marksHandler = ignoreDiacritics ? new DiacriticsHandler : new AccentMarkHandler; - // The code below relies on empty current selection. There must be no selection because this - // function is called only in one place - when a page loading finishes. - Q_ASSERT( !ui.definition->hasSelection() ); + // Clear any current selection + if ( ui.definition->selectedText().size() ) + { + ui.definition->page()->currentFrame()-> + evaluateJavaScript( "window.getSelection().removeAllRanges();_=0;" ); + } QString pageText = ui.definition->page()->currentFrame()->toPlainText(); marksHandler->setText( pageText );