Merge branch 'staged' into dev

This commit is contained in:
Xiao Yi Fang 2024-07-25 16:46:01 +08:00
commit 529871bd58
5 changed files with 47 additions and 48 deletions

View file

@ -130,26 +130,25 @@ string escapeForJavaScript( string const & str )
return result; return result;
} }
QString stripHtml( QString & tmp ) QString stripHtml( QString tmp )
{ {
tmp.replace( static QRegularExpression htmlRegex(
QRegularExpression(
"<(?:\\s*/?(?:div|h[1-6r]|q|p(?![alr])|br|li(?![ns])|td|blockquote|[uo]l|pre|d[dl]|nav|address))[^>]{0,}>", "<(?:\\s*/?(?:div|h[1-6r]|q|p(?![alr])|br|li(?![ns])|td|blockquote|[uo]l|pre|d[dl]|nav|address))[^>]{0,}>",
QRegularExpression::CaseInsensitiveOption ), QRegularExpression::CaseInsensitiveOption );
" " ); tmp.replace( htmlRegex, " " );
tmp.replace( QRegularExpression( "<[^>]*>" ), " " ); static QRegularExpression htmlElementRegex( "<[^>]*>" );
tmp.replace( htmlElementRegex, " " );
return tmp; 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 // Does it contain HTML? If it does, we need to strip it
if ( str.contains( '<' ) || str.contains( '&' ) ) { if ( str.contains( '<' ) || str.contains( '&' ) ) {
QString tmp = str;
if ( option == HtmlOption::Strip ) { if ( option == HtmlOption::Strip ) {
stripHtml( tmp ); str = stripHtml( str );
} }
return QTextDocumentFragment::fromHtml( tmp.trimmed() ).toPlainText(); return QTextDocumentFragment::fromHtml( str.trimmed() ).toPlainText();
} }
return str; return str;
} }

View file

@ -25,9 +25,9 @@ string preformat( string const &, bool baseRightToLeft = false );
// Escapes the given string to be included in JavaScript. // Escapes the given string to be included in JavaScript.
string escapeForJavaScript( string const & ); string escapeForJavaScript( string const & );
QString stripHtml( QString & tmp ); QString stripHtml( QString tmp );
// Replace html entities // 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 ); QString fromHtmlEscaped( QString const & str );
string unescapeUtf8( string const & str, HtmlOption option = HtmlOption::Strip ); string unescapeUtf8( string const & str, HtmlOption option = HtmlOption::Strip );

View file

@ -422,10 +422,12 @@ private:
} }
old = s; old = s;
} }
s.replace( QRegularExpression( "&.\\s*\\{",
static QRegularExpression leadingBrace( "&.\\s*\\{",
QRegularExpression::UseUnicodePropertiesOption QRegularExpression::UseUnicodePropertiesOption
| QRegularExpression::DotMatchesEverythingOption ), | QRegularExpression::DotMatchesEverythingOption );
"" );
s.replace( leadingBrace, "" );
s.replace( "}", "" ); s.replace( "}", "" );
} }
@ -448,9 +450,9 @@ string StardictDictionary::handleResource( char type, char const * resource, siz
{ {
QString articleText = QString( "<div class=\"sdct_h\">" ) + QString::fromUtf8( resource, size ) + "</div>"; QString articleText = QString( "<div class=\"sdct_h\">" ) + QString::fromUtf8( resource, size ) + "</div>";
QRegularExpression imgRe( R"((<\s*(?:img|script)\s+[^>]*src\s*=\s*["']?)(?!(?:data|https?|ftp):))", static QRegularExpression imgRe( R"((<\s*(?:img|script)\s+[^>]*src\s*=\s*["']?)(?!(?:data|https?|ftp):))",
QRegularExpression::CaseInsensitiveOption ); QRegularExpression::CaseInsensitiveOption );
QRegularExpression linkRe( R"((<\s*link\s+[^>]*href\s*=\s*["']?)(?!(?:data|https?|ftp):))", static QRegularExpression linkRe( R"((<\s*link\s+[^>]*href\s*=\s*["']?)(?!(?:data|https?|ftp):))",
QRegularExpression::CaseInsensitiveOption ); QRegularExpression::CaseInsensitiveOption );
articleText.replace( imgRe, "\\1bres://" + QString::fromStdString( getId() ) + "/" ) articleText.replace( imgRe, "\\1bres://" + QString::fromStdString( getId() ) + "/" )
@ -458,7 +460,7 @@ string StardictDictionary::handleResource( char type, char const * resource, siz
// Handle links to articles // Handle links to articles
QRegularExpression linksReg( R"(<a(\s*[^>]*)href\s*=\s*['"](bword://)?([^'"]+)['"])", static QRegularExpression linksReg( R"(<a(\s*[^>]*)href\s*=\s*['"](bword://)?([^'"]+)['"])",
QRegularExpression::CaseInsensitiveOption ); QRegularExpression::CaseInsensitiveOption );
@ -508,7 +510,7 @@ string StardictDictionary::handleResource( char type, char const * resource, siz
// Handle "audio" tags // Handle "audio" tags
QRegularExpression audioRe( R"(<\s*audio\s*src\s*=\s*(["']+)([^"']+)(["'])\s*>(.*)</audio>)", static QRegularExpression audioRe( R"(<\s*audio\s*src\s*=\s*(["']+)([^"']+)(["'])\s*>(.*)</audio>)",
QRegularExpression::CaseInsensitiveOption QRegularExpression::CaseInsensitiveOption
| QRegularExpression::DotMatchesEverythingOption ); | QRegularExpression::DotMatchesEverythingOption );
@ -523,20 +525,19 @@ string StardictDictionary::handleResource( char type, char const * resource, siz
QString src = match.captured( 2 ); QString src = match.captured( 2 );
if ( src.indexOf( "://" ) >= 0 ) if ( src.indexOf( "://" ) >= 0 ) {
articleNewText += match.captured(); articleNewText += match.captured();
}
else { else {
std::string href = "\"gdau://" + getId() + "/" + src.toUtf8().data() + "\""; std::string href = "\"gdau://" + getId() + "/" + src.toUtf8().data() + "\"";
QString newTag = QString::fromUtf8( std::string newTag = addAudioLink( href, getId() ) + "<span class=\"sdict_h_wav\"><a href=" + href + ">";
( addAudioLink( href, getId() ) + "<span class=\"sdict_h_wav\"><a href=" + href + ">" ).c_str() ); newTag += match.captured( 4 ).toUtf8().constData();
newTag += match.captured( 4 ); if ( match.captured( 4 ).indexOf( "<img " ) < 0 ) {
if ( match.captured( 4 ).indexOf( "<img " ) < 0 )
newTag += R"( <img src="qrc:///icons/playsound.png" border="0" alt="Play">)"; newTag += R"( <img src="qrc:///icons/playsound.png" border="0" alt="Play">)";
}
newTag += "</a></span>"; newTag += "</a></span>";
articleNewText += newTag; articleNewText += QString::fromStdString( newTag );
} }
} }
if ( pos ) { if ( pos ) {
@ -544,8 +545,8 @@ string StardictDictionary::handleResource( char type, char const * resource, siz
articleText = articleNewText; articleText = articleNewText;
articleNewText.clear(); articleNewText.clear();
} }
auto text = articleText.toUtf8();
return ( articleText.toUtf8().data() ); return text.data();
} }
case 'm': // Pure meaning, usually means preformatted text case 'm': // Pure meaning, usually means preformatted text
return "<div class=\"sdct_m\">" + Html::preformat( string( resource, size ), isToLanguageRTL() ) + "</div>"; return "<div class=\"sdct_m\">" + Html::preformat( string( resource, size ), isToLanguageRTL() ) + "</div>";
@ -614,8 +615,8 @@ void StardictDictionary::pangoToHtml( QString & text )
* Attributes "fallback", "lang", "gravity", "gravity_hint" just ignored * Attributes "fallback", "lang", "gravity", "gravity_hint" just ignored
*/ */
QRegularExpression spanRegex( "<span\\s*([^>]*)>", QRegularExpression::CaseInsensitiveOption ); static QRegularExpression spanRegex( "<span\\s*([^>]*)>", QRegularExpression::CaseInsensitiveOption );
QRegularExpression styleRegex( "(\\w+)=\"([^\"]*)\"" ); static QRegularExpression styleRegex( "(\\w+)=\"([^\"]*)\"" );
text.replace( "\n", "<br>" ); text.replace( "\n", "<br>" );
@ -1554,7 +1555,8 @@ void StardictResourceRequest::run()
QString id = QString::fromUtf8( dict.getId().c_str() ); QString id = QString::fromUtf8( dict.getId().c_str() );
int pos = 0; int pos = 0;
QRegularExpression links( R"(url\(\s*(['"]?)([^'"]*)(['"]?)\s*\))", QRegularExpression::CaseInsensitiveOption ); static QRegularExpression links( R"(url\(\s*(['"]?)([^'"]*)(['"]?)\s*\))",
QRegularExpression::CaseInsensitiveOption );
QString newCSS; QString newCSS;
QRegularExpressionMatchIterator it = links.globalMatch( css ); QRegularExpressionMatchIterator it = links.globalMatch( css );

View file

@ -137,7 +137,7 @@ ArticleView::ArticleView( QWidget * parent,
connect( searchPanel->previous, &QPushButton::clicked, this, &ArticleView::on_searchPrevious_clicked ); connect( searchPanel->previous, &QPushButton::clicked, this, &ArticleView::on_searchPrevious_clicked );
connect( searchPanel->next, &QPushButton::clicked, this, &ArticleView::on_searchNext_clicked ); connect( searchPanel->next, &QPushButton::clicked, this, &ArticleView::on_searchNext_clicked );
connect( searchPanel->close, &QPushButton::clicked, this, &ArticleView::on_searchCloseButton_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::textEdited, this, &ArticleView::on_searchText_textEdited );
connect( searchPanel->lineEdit, &QLineEdit::returnPressed, this, &ArticleView::on_searchText_returnPressed ); connect( searchPanel->lineEdit, &QLineEdit::returnPressed, this, &ArticleView::on_searchText_returnPressed );
connect( ftsSearchPanel->next, &QPushButton::clicked, this, &ArticleView::on_ftsSearchNext_clicked ); connect( ftsSearchPanel->next, &QPushButton::clicked, this, &ArticleView::on_ftsSearchNext_clicked );
@ -1963,8 +1963,11 @@ void ArticleView::on_searchCloseButton_clicked()
closeSearch(); 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 ); performFindOperation( false );
} }
@ -2027,11 +2030,6 @@ void ArticleView::performFindOperation( bool backwards )
findText( text, f, [ text, this ]( bool match ) { findText( text, f, [ text, this ]( bool match ) {
bool nomatch = !text.isEmpty() && !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 ); Utils::Widget::setNoResultColor( searchPanel->lineEdit, nomatch );
} ); } );
} }

View file

@ -357,7 +357,7 @@ private slots:
void on_searchText_textEdited(); void on_searchText_textEdited();
void on_searchText_returnPressed(); void on_searchText_returnPressed();
void on_searchCloseButton_clicked(); void on_searchCloseButton_clicked();
void on_searchCaseSensitive_clicked(); void on_searchCaseSensitive_clicked( bool );
void on_ftsSearchPrevious_clicked(); void on_ftsSearchPrevious_clicked();
void on_ftsSearchNext_clicked(); void on_ftsSearchNext_clicked();