Skip to content

Commit

Permalink
fix: stardict qstring modification (#1708)
Browse files Browse the repository at this point in the history
* fix:stardict crash
  • Loading branch information
xiaoyifang authored Jul 25, 2024
1 parent 3ef35da commit e2617f3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
21 changes: 10 additions & 11 deletions src/common/htmlescape.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/common/htmlescape.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
21 changes: 10 additions & 11 deletions src/dict/stardict.cc
Original file line number Diff line number Diff line change
Expand Up @@ -525,29 +525,28 @@ 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 ) {
articleNewText += articleText.mid( pos );
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>";
Expand Down

0 comments on commit e2617f3

Please sign in to comment.