Skip to content

Commit 103532b

Browse files
versatXhmikosR
andauthored
Update cppcheck-htmlreport (danmar#2494)
* Try to use double quotes consistently * minor CSS consistency changes * fix HTML errors * fix a few JS issues and switch to `textContent` * use `addEventListener` instead of the onload event * use `querySelector` and `querySelectorAll` Co-authored-by: XhmikosR <[email protected]>
1 parent 7c7b0e5 commit 103532b

File tree

1 file changed

+81
-67
lines changed

1 file changed

+81
-67
lines changed

htmlreport/cppcheck-htmlreport

Lines changed: 81 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ h1 {
4848
}
4949
5050
.inconclusive {
51-
background-color: #B6B6B4;
51+
background-color: #b6b6b4;
5252
}
5353
5454
.inconclusive2 {
55-
background-color: #B6B6B4;
56-
border: 1px dotted black;
55+
background-color: #b6b6b4;
56+
border: 1px dotted #000;
5757
display: inline-block;
5858
margin-left: 4px;
5959
}
@@ -71,8 +71,8 @@ div.verbose div.content {
7171
margin: 4px;
7272
max-width: 40%;
7373
white-space: pre-wrap;
74-
border: 1px solid black;
75-
background-color: #FFFFCC;
74+
border: 1px solid #000;
75+
background-color: #ffffcc;
7676
cursor: auto;
7777
}
7878
@@ -114,21 +114,21 @@ div.verbose div.content {
114114
z-index: 1;
115115
}
116116
117-
#filename {
117+
#filename {
118118
margin-left: 10px;
119119
font: 12px;
120120
z-index: 1;
121121
}
122122
123123
.highlighttable {
124-
background-color:white;
124+
background-color: #fff;
125125
z-index: 10;
126126
position: relative;
127127
margin: -10 px;
128128
}
129129
130130
#content {
131-
background-color: white;
131+
background-color: #fff;
132132
-webkit-box-sizing: content-box;
133133
-moz-box-sizing: content-box;
134134
box-sizing: content-box;
@@ -141,7 +141,7 @@ div.verbose div.content {
141141
}
142142
143143
#content_index {
144-
background-color: white;
144+
background-color: #fff;
145145
-webkit-box-sizing: content-box;
146146
-moz-box-sizing: content-box;
147147
box-sizing: content-box;
@@ -155,7 +155,7 @@ div.verbose div.content {
155155
156156
.linenos {
157157
border-right: thin solid #aaa;
158-
color: lightgray;
158+
color: #d3d3d3;
159159
padding-right: 6px;
160160
}
161161
@@ -173,7 +173,7 @@ div.verbose div.content {
173173
"""
174174

175175
HTML_HEAD = """
176-
<!DOCTYPE html>
176+
<!doctype html>
177177
<html lang="en">
178178
<head>
179179
<meta charset="utf-8">
@@ -182,93 +182,107 @@ HTML_HEAD = """
182182
<style>
183183
%s
184184
</style>
185-
<script language="javascript">
186-
function getStyle(el,styleProp) {
185+
<script>
186+
function getStyle(el, styleProp) {
187+
var y;
187188
if (el.currentStyle)
188-
var y = el.currentStyle[styleProp];
189+
y = el.currentStyle[styleProp];
189190
else if (window.getComputedStyle)
190-
var y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
191+
y = document.defaultView.getComputedStyle(el, null).getPropertyValue(styleProp);
191192
return y;
192193
}
194+
193195
function toggle() {
194196
var el = this.expandable_content;
195197
var mark = this.expandable_marker;
196-
if (el.style.display == "block") {
198+
199+
if (el.style.display === "block") {
197200
el.style.display = "none";
198-
mark.innerHTML = "[+]";
201+
mark.textContent = "[+]";
199202
} else {
200203
el.style.display = "block";
201-
mark.innerHTML = "[-]";
204+
mark.textContent = "[-]";
202205
}
203206
}
207+
204208
function init_expandables() {
205-
var elts = document.getElementsByClassName("expandable");
206-
for (var i = 0; i < elts.length; i++) {
209+
var elts = document.querySelectorAll(".expandable");
210+
211+
for (var i = 0, len = elts.length; i < len; i++) {
207212
var el = elts[i];
208-
var clickable = el.getElementsByTagName("span")[0];
209-
var marker = clickable.getElementsByClassName("marker")[0];
210-
var content = el.getElementsByClassName("content")[0];
213+
var clickable = el.querySelector("span");
214+
var marker = clickable.querySelector(".marker");
215+
var content = el.querySelector(".content");
211216
var width = clickable.clientWidth - parseInt(getStyle(content, "padding-left")) - parseInt(getStyle(content, "padding-right"));
212217
content.style.width = width + "px";
213218
clickable.expandable_content = content;
214219
clickable.expandable_marker = marker;
215-
clickable.onclick = toggle;
220+
clickable.addEventListener("click", toggle);
216221
}
217222
}
223+
218224
function set_class_display(c, st) {
219-
var elements = document.querySelectorAll('.' + c),
220-
len = elements.length;
221-
for (i = 0; i < len; i++) {
222-
elements[i].style.display = st;
225+
var elements = document.querySelectorAll("." + c);
226+
227+
for (var i = 0, len = elements.length; i < len; i++) {
228+
elements[i].style.display = st;
223229
}
224230
}
231+
225232
function toggle_class_visibility(id) {
226233
var box = document.getElementById(id);
227-
set_class_display(id, box.checked ? '' : 'none');
234+
set_class_display(id, box.checked ? "" : "none");
228235
}
229-
function toggle_all(){
230-
var elts = document.getElementsByTagName("input");
231-
for (var i = 1; i < elts.length; i++) {
236+
237+
function toggle_all() {
238+
var elts = document.querySelectorAll("input");
239+
240+
for (var i = 1; i < elts.length; i++) {
232241
var el = elts[i];
233-
el.checked ? el.checked=false : el.checked=true;
234-
toggle_class_visibility(el.id);
235-
}
242+
if (el.checked) {
243+
el.checked = false;
244+
} else {
245+
el.checked = true;
246+
}
247+
toggle_class_visibility(el.id);
248+
}
236249
}
237250
251+
window.addEventListener("load", init_expandables);
238252
</script>
239253
</head>
240-
<body onload="init_expandables()">
241-
<div id="header">
242-
<h1>Cppcheck report - %s: %s </h1>
243-
</div>
244-
<div id="menu" dir="rtl">
245-
<p id="filename"><a href="index.html">Defects:</a> %s</p>
254+
<body>
255+
<div id="header">
256+
<h1>Cppcheck report - %s: %s </h1>
257+
</div>
258+
<div id="menu" dir="rtl">
259+
<p id="filename"><a href="index.html">Defects:</a> %s</p>
246260
"""
247261

248262
HTML_HEAD_END = """
249-
</div>
250-
<div id="content">
263+
</div>
264+
<div id="content">
251265
"""
252266

253267
HTML_FOOTER = """
254-
</div>
255-
<div id="footer">
256-
<p>
257-
Cppcheck %s - a tool for static C/C++ code analysis</br>
258-
</br>
259-
Internet: <a href="http://cppcheck.net">http://cppcheck.net</a></br>
260-
IRC: <a href="irc://irc.freenode.net/cppcheck">irc://irc.freenode.net/cppcheck</a></br>
261-
</p>
262-
</div>
268+
</div>
269+
<div id="footer">
270+
<p>
271+
Cppcheck %s - a tool for static C/C++ code analysis<br>
272+
<br>
273+
Internet: <a href="http://cppcheck.net">http://cppcheck.net</a><br>
274+
IRC: <a href="irc://irc.freenode.net/cppcheck">irc://irc.freenode.net/cppcheck</a><br>
275+
</p>
276+
</div>
263277
</body>
264278
</html>
265279
"""
266280

267-
HTML_ERROR = "<span class='error2'>&lt;--- %s</span>\n"
268-
HTML_INCONCLUSIVE = "<span class='inconclusive2'>&lt;--- %s</span>\n"
281+
HTML_ERROR = "<span class=\"error2\">&lt;--- %s</span>\n"
282+
HTML_INCONCLUSIVE = "<span class=\"inconclusive2\">&lt;--- %s</span>\n"
269283

270-
HTML_EXPANDABLE_ERROR = "<div class='verbose expandable'><span class='error2'>&lt;--- %s <span class='marker'>[+]</span></span><div class='content'>%s</div></div>\n"""
271-
HTML_EXPANDABLE_INCONCLUSIVE = "<div class='verbose expandable'><span class='inconclusive2'>&lt;--- %s <span class='marker'>[+]</span></span><div class='content'>%s</div></div>\n"""
284+
HTML_EXPANDABLE_ERROR = "<div class=\"verbose expandable\"><span class=\"error2\">&lt;--- %s <span class=\"marker\">[+]</span></span><div class=\"content\">%s</div></div>\n"""
285+
HTML_EXPANDABLE_INCONCLUSIVE = "<div class=\"verbose expandable\"><span class=\"inconclusive2\">&lt;--- %s <span class=\"marker\">[+]</span></span><div class=\"content\">%s</div></div>\n"""
272286

273287
# escape() and unescape() takes care of &, < and >.
274288
html_escape_table = {
@@ -518,7 +532,7 @@ if __name__ == '__main__':
518532
filename.split('/')[-1]))
519533

520534
for error in sorted(errors, key=lambda k: k['line']):
521-
output_file.write("<a href='%s#line-%d'> %s %s</a>" % (data['htmlfile'], error['line'], error['id'], error['line']))
535+
output_file.write("<a href=\"%s#line-%d\"> %s %s</a>" % (data['htmlfile'], error['line'], error['id'], error['line']))
522536

523537
output_file.write(HTML_HEAD_END)
524538
try:
@@ -528,8 +542,8 @@ if __name__ == '__main__':
528542
lexer = guess_lexer(content)
529543
except ClassNotFound:
530544
sys.stderr.write("ERROR: Couldn't determine lexer for the file' " + source_filename + " '. Won't be able to syntax highlight this file.")
531-
output_file.write("\n <tr><td colspan='5'> Could not generated content because pygments failed to retrieve the determine code type.</td></tr>")
532-
output_file.write("\n <tr><td colspan='5'> Sorry about this.</td></tr>")
545+
output_file.write("\n <tr><td colspan=\"5\"> Could not generate content because pygments failed to determine the code type.</td></tr>")
546+
output_file.write("\n <tr><td colspan=\"5\"> Sorry about this.</td></tr>")
533547
continue
534548

535549
if options.source_encoding:
@@ -572,7 +586,7 @@ if __name__ == '__main__':
572586
except IndexError:
573587
cnt_min = 0
574588

575-
stat_fmt = "\n <tr><td><input type='checkbox' onclick='toggle_class_visibility(this.id)' id='{}' name='{}' checked></td><td>{}</td><td>{}</td></tr>"
589+
stat_fmt = "\n <tr><td><input type=\"checkbox\" onclick=\"toggle_class_visibility(this.id)\" id=\"{}\" name=\"{}\" checked></td><td>{}</td><td>{}</td></tr>"
576590
for occurrences in reversed(range(cnt_min, cnt_max + 1)):
577591
for _id in [k for k, v in sorted(counter.items()) if v == occurrences]:
578592
stat_html.append(stat_fmt.format(_id, _id, dict(counter.most_common())[_id], _id))
@@ -591,16 +605,16 @@ if __name__ == '__main__':
591605
output_file.write('\n <tr><th>Line</th><th>Id</th><th>CWE</th><th>Severity</th><th>Message</th></tr>')
592606
for filename, data in sorted(files.items()):
593607
if filename in decode_errors: # don't print a link but a note
594-
output_file.write("\n <tr><td colspan='5'>%s</td></tr>" % (filename))
595-
output_file.write("\n <tr><td colspan='5'> Could not generated due to UnicodeDecodeError</td></tr>")
608+
output_file.write("\n <tr><td colspan=\"5\">%s</td></tr>" % (filename))
609+
output_file.write("\n <tr><td colspan=\"5\"> Could not generated due to UnicodeDecodeError</td></tr>")
596610
else:
597611
if filename.endswith('*'): # assume unmatched suppression
598612
output_file.write(
599-
"\n <tr><td colspan='5'>%s</td></tr>" %
613+
"\n <tr><td colspan=\"5\">%s</td></tr>" %
600614
(filename))
601615
else:
602616
output_file.write(
603-
"\n <tr><td colspan='5'><a href='%s'>%s</a></td></tr>" %
617+
"\n <tr><td colspan=\"5\"><a href=\"%s\">%s</a></td></tr>" %
604618
(data['htmlfile'], filename))
605619

606620
for error in sorted(data['errors'], key=lambda k: k['line']):
@@ -614,7 +628,7 @@ if __name__ == '__main__':
614628

615629
try:
616630
if error['cwe']:
617-
cwe_url = "<a href='https://cwe.mitre.org/data/definitions/" + error['cwe'] + ".html'>" + error['cwe'] + "</a>"
631+
cwe_url = "<a href=\"https://cwe.mitre.org/data/definitions/" + error['cwe'] + ".html\">" + error['cwe'] + "</a>"
618632
except KeyError:
619633
cwe_url = ""
620634

@@ -686,7 +700,7 @@ if __name__ == '__main__':
686700
continue
687701
except KeyError:
688702
continue
689-
stats_file.write("<p>Top 10 files for " + sev + " severity, total findings: " + str(_sum) + "</br>\n")
703+
stats_file.write("<p>Top 10 files for " + sev + " severity, total findings: " + str(_sum) + "<br>\n")
690704

691705
# sort, so that the file with the most severities per type is first
692706
stats_list_sorted = sorted(stats_templist.items(), key=operator.itemgetter(1, 0), reverse=True)
@@ -699,7 +713,7 @@ if __name__ == '__main__':
699713
if (it == 0):
700714
LENGTH = len(str(i[1])) # <- length of longest number, now get the difference and try to make other numbers align to it
701715

702-
stats_file.write("&#160;" * 3 + str(i[1]) + "&#160;" * (1 + LENGTH - len(str(i[1]))) + "<a href=\"" + files[i[0]]['htmlfile'] + "\"> " + i[0] + "</a></br>\n")
716+
stats_file.write("&#160;" * 3 + str(i[1]) + "&#160;" * (1 + LENGTH - len(str(i[1]))) + "<a href=\"" + files[i[0]]['htmlfile'] + "\"> " + i[0] + "</a><br>\n")
703717
it += 1
704718
if (it == 10): # print only the top 10
705719
break

0 commit comments

Comments
 (0)