fix: stardict qstring modification (#1708)

* fix:stardict crash
This commit is contained in:
xiaoyifang 2024-07-25 16:45:34 +08:00 committed by GitHub
parent 3ef35daec3
commit e2617f3b9c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 24 deletions

View file

@ -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;
}

View file

@ -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 );

View file

@ -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() ) + "<span class=\"sdict_h_wav\"><a href=" + href + ">" ).c_str() );
newTag += match.captured( 4 );
if ( match.captured( 4 ).indexOf( "<img " ) < 0 )
std::string href = "\"gdau://" + getId() + "/" + src.toUtf8().data() + "\"";
std::string newTag = addAudioLink( href, getId() ) + "<span class=\"sdict_h_wav\"><a href=" + href + ">";
newTag += match.captured( 4 ).toUtf8().constData();
if ( match.captured( 4 ).indexOf( "<img " ) < 0 ) {
newTag += R"( <img src="qrc:///icons/playsound.png" border="0" alt="Play">)";
}
newTag += "</a></span>";
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 "<div class=\"sdct_m\">" + Html::preformat( string( resource, size ), isToLanguageRTL() ) + "</div>";