Add html::EscapeText for std::string; use this function to escape text in message bubbles.

llvm-svn: 48884
This commit is contained in:
Ted Kremenek 2008-03-27 17:15:29 +00:00
parent ed9f054a9c
commit 561dfe3153
3 changed files with 32 additions and 4 deletions

View File

@ -117,8 +117,8 @@ void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic& D) {
std::ostringstream os; std::ostringstream os;
const FileEntry* Entry = SMgr.getFileEntryForID(FileID); const FileEntry* Entry = SMgr.getFileEntryForID(FileID);
os << "<h1>" << Entry->getDir()->getName() << "/" os << "<h1>" << html::EscapeText(Entry->getDir()->getName())
<< Entry->getName() << "</h1>\n"; << "/" << html::EscapeText(Entry->getName()) << "</h1>\n";
R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str()); R.InsertStrBefore(SourceLocation::getFileLoc(FileID, 0), os.str());
} }
@ -200,7 +200,7 @@ void HTMLDiagnostics::HandlePiece(Rewriter& R,
<< "<div class=\"msg\" style=\"margin-left:" << "<div class=\"msg\" style=\"margin-left:"
<< ColNo << "ex\">"; << ColNo << "ex\">";
os << P.getString() << "</div></td></tr>"; os << html::EscapeText(P.getString()) << "</div></td></tr>";
// Insert the new html. // Insert the new html.

View File

@ -16,6 +16,7 @@
#define LLVM_CLANG_HTMLREWRITER_H #define LLVM_CLANG_HTMLREWRITER_H
#include "clang/Basic/SourceLocation.h" #include "clang/Basic/SourceLocation.h"
#include <string>
namespace clang { namespace clang {
@ -24,7 +25,8 @@ class Rewriter;
namespace html { namespace html {
void EscapeText(Rewriter& R, unsigned FileID, bool EscapeSpaces = false); void EscapeText(Rewriter& R, unsigned FileID, bool EscapeSpaces = false);
std::string EscapeText(const std::string& s, bool EscapeSpaces = false);
void AddLineNumbers(Rewriter& R, unsigned FileID); void AddLineNumbers(Rewriter& R, unsigned FileID);
void AddHeaderFooterInternalBuiltinCSS(Rewriter& R, unsigned FileID); void AddHeaderFooterInternalBuiltinCSS(Rewriter& R, unsigned FileID);

View File

@ -46,6 +46,32 @@ void html::EscapeText(Rewriter& R, unsigned FileID, bool EscapeSpaces) {
} }
} }
std::string html::EscapeText(const std::string& s, bool EscapeSpaces) {
unsigned len = s.size();
std::ostringstream os;
for (unsigned i = 0 ; i < len; ++i) {
char c = s[i];
switch (c) {
default:
os << c; break;
case ' ':
if (EscapeSpaces) os << "&#32;";
break;
case '<': os << "&lt;"; break;
case '>': os << "&gt;"; break;
case '&': os << "&amp;"; break;
}
}
return os.str();
}
static void AddLineNumber(Rewriter& R, unsigned LineNo, static void AddLineNumber(Rewriter& R, unsigned LineNo,
SourceLocation B, SourceLocation E) { SourceLocation B, SourceLocation E) {