mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-23 20:14:05 +00:00
121 lines
2 KiB
C++
121 lines
2 KiB
C++
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
#include "htmlescape.hh"
|
|
|
|
namespace Html {
|
|
|
|
string escape( string const & str )
|
|
{
|
|
string result( str );
|
|
|
|
for( size_t x = result.size(); x--; )
|
|
switch ( result[ x ] )
|
|
{
|
|
case '&':
|
|
result.erase( x, 1 );
|
|
result.insert( x, "&" );
|
|
break;
|
|
|
|
case '<':
|
|
result.erase( x, 1 );
|
|
result.insert( x, "<" );
|
|
break;
|
|
|
|
case '>':
|
|
result.erase( x, 1 );
|
|
result.insert( x, ">" );
|
|
break;
|
|
|
|
case '"':
|
|
result.erase( x, 1 );
|
|
result.insert( x, """ );
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
string preformat( string const & str )
|
|
{
|
|
string escaped = escape( str ), result;
|
|
|
|
result.reserve( escaped.size() );
|
|
|
|
bool leading = true;
|
|
|
|
for( char const * nextChar = escaped.c_str(); *nextChar; ++nextChar )
|
|
{
|
|
if ( leading )
|
|
{
|
|
if ( *nextChar == ' ' )
|
|
{
|
|
result += " ";
|
|
continue;
|
|
}
|
|
else
|
|
if ( *nextChar == '\t' )
|
|
{
|
|
result += " ";
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if ( *nextChar == '\n' )
|
|
{
|
|
result += "<br/>";
|
|
leading = true;
|
|
continue;
|
|
}
|
|
|
|
if ( *nextChar == '\r' )
|
|
continue; // Just skip all \r
|
|
|
|
result.push_back( *nextChar );
|
|
|
|
leading = false;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
string escapeForJavaScript( string const & str )
|
|
{
|
|
string result( str );
|
|
|
|
for( size_t x = result.size(); x--; )
|
|
switch ( result[ x ] )
|
|
{
|
|
case '\\':
|
|
case '"':
|
|
case '\'':
|
|
result.insert( x, 1, '\\' );
|
|
break;
|
|
|
|
case '\n':
|
|
result.erase( x, 1 );
|
|
result.insert( x, "\\n" );
|
|
break;
|
|
|
|
case '\r':
|
|
result.erase( x, 1 );
|
|
result.insert( x, "\\r" );
|
|
break;
|
|
|
|
case '\t':
|
|
result.erase( x, 1 );
|
|
result.insert( x, "\\t" );
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
}
|