Skip to content

Commit

Permalink
feat(cshtml): use CSS instead of inlining styles
Browse files Browse the repository at this point in the history
Fixes: csutils#174
Closes: csutils#181
  • Loading branch information
jamacku committed May 14, 2024
1 parent 63871e2 commit 5f3b933
Show file tree
Hide file tree
Showing 3 changed files with 2,707 additions and 2,654 deletions.
59 changes: 39 additions & 20 deletions src/lib/writer-html.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,27 @@ namespace HtmlLib {

str << "<!DOCTYPE html>\n\
<html>\n\
<head><title>" << title << "</title></head>\n\
<body style='background: white;'>\n<h1>" << title << "</h1>\n";
<head>\n\
<title>" << title
<< "</title>\n\
<style>\n\
body { background-color: white; }\n\
pre { white-space: pre-wrap; }\n\
.checker { background:#C0FF00; }\n\
.ctxLine { color:#000000; }\n\
.infoEvent { color:#808080; }\n\
.infoEventComment { color:#00C0C0; }\n\
.traceEvent { color: #C0C0C0; }\n\
.newDefect { color: #00FF00; }\n\
.parseWarning { color: #FF0000; }\n\
.impFlag { color: #FF0000; font-weight: bold; }\n\
#scanProps { font-family: monospace; }\n\
.oddRow { background-color: #EEE; }\n\
.scanPropName { padding-right: 8px; white-space: nowrap; }\n\
</style>\n\
</head>\n\
<body>\n<h1>"
<< title << "</h1>\n";
}

void writeLink(
Expand All @@ -74,7 +93,7 @@ namespace HtmlLib {
}

void initPre(std::ostream &str) {
str << "<pre style='white-space: pre-wrap;'>\n";
str << "<pre>\n";
}

void finalizePre(std::ostream &str) {
Expand Down Expand Up @@ -133,7 +152,7 @@ void writeParseWarnings(std::ostream &str, const TScanProps &props) {
const int count = boost::lexical_cast<int>(itCount->second);
const int ratio = boost::lexical_cast<float>(itRatio->second);
if (ratio < parsingRatioThr)
str << "<p><b style='color: #FF0000;'>warning:</b> "
str << "<p><b class='parseWarning'>warning:</b> "
"low parsing ratio: " << ratio << "%</p>\n";

itCount = props.find("diffbase-cov-compilation-unit-count");
Expand All @@ -149,12 +168,12 @@ void writeParseWarnings(std::ostream &str, const TScanProps &props) {
const int baseCount = boost::lexical_cast<int>(itCount->second);
const int baseRatio = boost::lexical_cast<float>(itRatio->second);
if (baseRatio < parsingRatioThr && baseRatio < ratio)
str << "<p><b style='color: #FF0000;'>warning:</b> "
str << "<p><b class='parseWarning'>warning:</b> "
"low parsing ratio in diff base: "
<< baseRatio << "%</p>\n";

if (!count || 100 * baseCount / count < parsingOldToNewRatioThr)
str << "<p><b style='color: #FF0000;'>warning:</b> "
str << "<p><b class='parseWarning'>warning:</b> "
"low count of parsed units in diff base: "
<< baseCount << "</p>\n";
}
Expand All @@ -169,16 +188,16 @@ void writeScanProps(std::ostream &str, const TScanProps &props) {

HtmlLib::initSection(str, "Scan Properties");

str << "<table style='font-family: monospace;'>\n";
str << "<table id='scanProps'>\n";
int i = 0;

for (TScanProps::const_reference item : props) {
const char *trStyle = "";
const char *trClass = "";
if (++i & 1)
trStyle = " style='background-color: #EEE;'";
trClass = " class='oddRow'";

const char *tdStyle0 = "padding-right: 8px; white-space: nowrap;";
str << "<tr" << trStyle << "><td style='" << tdStyle0 << "'>"
const char *tdClass0 = "scanPropName";
str << "<tr" << trClass << "><td class='" << tdClass0 << "'>"
<< item.first << "</td><td>" << item.second << "</td></tr>\n";
}

Expand Down Expand Up @@ -445,7 +464,7 @@ void HtmlWriter::Private::writeNewDefWarning(const Defect &def)
return;

// a newly introduced defect
this->str << " <span style='color: #00FF00;'>[<b>warning:</b> "
this->str << " <span class='newDefect'>[<b>warning:</b> "
<< this->newDefMsg << "]</span>";
}

Expand All @@ -456,7 +475,7 @@ void HtmlWriter::handleDef(const Defect &def)
// HTML anchor
d->str << "<a id='def" << ++(d->defCnt) << "'></a>";

d->str << "<b>Error: <span style='background: #C0FF00;'>"
d->str << "<b>Error: <span class='checker'>"
<< HtmlLib::escapeTextInline(def.checker) << "</span>";

const int cwe = def.cwe;
Expand All @@ -482,7 +501,7 @@ void HtmlWriter::handleDef(const Defect &def)

if (0 < def.imp) {
// highlight the "imp" flag
d->str << " <span style='color: #FF0000; font-weight: bold;'>"
d->str << " <span class='impFlag'>"
"[important]</span>";
}

Expand All @@ -498,13 +517,13 @@ void HtmlWriter::handleDef(const Defect &def)
switch (evt.verbosityLevel) {
case 1:
if (isComment)
d->str << "<span style='color: #00C0C0;'>";
d->str << "<span class='infoEventComment'>";
else
d->str << "<span style='color: #808080;'>";
d->str << "<span class='infoEvent'>";
break;

case 2:
d->str << "<span style='color: #C0C0C0;'>";
d->str << "<span class='traceEvent'>";
break;
}

Expand Down Expand Up @@ -544,9 +563,9 @@ void HtmlWriter::handleDef(const Defect &def)
const bool isCtxLine = detector.isAnyCtxLine(evt);
if (isCtxLine) {
const char *color = (detector.isKeyCtxLine(evt))
? "000000"
: "C0C0C0";
d->str << "<span style='color: #" << color << ";'>";
? "ctxLine"
: "traceEvent";
d->str << "<span class='" << color << "'>";
}

// translate message text
Expand Down
Loading

0 comments on commit 5f3b933

Please sign in to comment.