Skip to content

Commit e17863c

Browse files
XhmikosRversat
authored andcommitted
Fix #9574 htmlreport: fix sticky sidebar (cppcheck-opensource#2495)
* rename JS functions * Use the native font stack * Update cppcheck-htmlreport * Use a class for hiding content and toggle that instead. This should improve performance with huge reports. * reorder selectors * use classes for header and footer * remove unneeded clear property (we don't have floats anymore) * fix wrong `font-size` and `margin` properties * remove `dir=rtl` Related ticket: https://trac.cppcheck.net/ticket/9574
1 parent d635ea4 commit e17863c

1 file changed

Lines changed: 106 additions & 114 deletions

File tree

htmlreport/cppcheck-htmlreport

Lines changed: 106 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ with syntax highlighted source code.
2424

2525
STYLE_FILE = """
2626
body {
27-
font: 13px Arial, Verdana, Sans-Serif;
27+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
28+
font-size: 13px;
29+
line-height: 1.5;
2830
margin: 0;
2931
width: auto;
3032
}
@@ -33,10 +35,72 @@ h1 {
3335
margin: 10px;
3436
}
3537
36-
#footer > p {
38+
.header {
39+
border-bottom: thin solid #aaa;
40+
}
41+
42+
.footer {
43+
border-top: thin solid #aaa;
44+
font-size: 90%;
45+
margin-top: 5px;
46+
}
47+
48+
.footer ul {
49+
list-style-type: none;
50+
padding-left: 0;
51+
}
52+
53+
.footer > p {
3754
margin: 4px;
3855
}
3956
57+
.wrapper {
58+
display: -webkit-box;
59+
display: -ms-flexbox;
60+
display: flex;
61+
-webkit-box-pack: justify;
62+
-ms-flex-pack: justify;
63+
justify-content: space-between;
64+
}
65+
66+
#menu,
67+
#menu_index {
68+
text-align: left;
69+
width: 350px;
70+
height: 90vh;
71+
min-height: 200px;
72+
overflow: auto;
73+
position: -webkit-sticky;
74+
position: sticky;
75+
top: 0;
76+
padding: 0 15px 15px 15px;
77+
}
78+
79+
#menu > a {
80+
display: block;
81+
margin-left: 10px;
82+
font-size: 12px;
83+
z-index: 1;
84+
}
85+
86+
#content,
87+
#content_index {
88+
background-color: #fff;
89+
-webkit-box-sizing: content-box;
90+
-moz-box-sizing: content-box;
91+
box-sizing: content-box;
92+
padding: 0 15px 15px 15px;
93+
width: calc(100% - 350px);
94+
height: 100%;
95+
overflow-x: auto;
96+
}
97+
98+
#filename {
99+
margin-left: 10px;
100+
font-size: 12px;
101+
z-index: 1;
102+
}
103+
40104
.error {
41105
background-color: #ffb7b7;
42106
}
@@ -58,13 +122,13 @@ h1 {
58122
margin-left: 4px;
59123
}
60124
61-
div.verbose {
125+
.verbose {
62126
display: inline-block;
63127
vertical-align: top;
64128
cursor: help;
65129
}
66130
67-
div.verbose div.content {
131+
.verbose .content {
68132
display: none;
69133
position: absolute;
70134
padding: 10px;
@@ -80,77 +144,11 @@ div.verbose div.content {
80144
padding: 1px;
81145
}
82146
83-
#header {
84-
border-bottom: thin solid #aaa;
85-
}
86-
87-
#menu {
88-
float: left;
89-
margin-top: 5px;
90-
text-align: left;
91-
width: 150px;
92-
/*height: 75%;*/
93-
position: fixed;
94-
overflow: auto;
95-
z-index: 1;
96-
}
97-
98-
#menu_index {
99-
float: left;
100-
margin-top: 5px;
101-
padding-left: 5px;
102-
text-align: left;
103-
width: 300px;
104-
/*height: 75%;*/
105-
position: fixed;
106-
overflow: auto;
107-
z-index: 1;
108-
}
109-
110-
#menu > a {
111-
display: block;
112-
margin-left: 10px;
113-
font: 12px;
114-
z-index: 1;
115-
}
116-
117-
#filename {
118-
margin-left: 10px;
119-
font: 12px;
120-
z-index: 1;
121-
}
122-
123147
.highlighttable {
124148
background-color: #fff;
125149
z-index: 10;
126150
position: relative;
127-
margin: -10 px;
128-
}
129-
130-
#content {
131-
background-color: #fff;
132-
-webkit-box-sizing: content-box;
133-
-moz-box-sizing: content-box;
134-
box-sizing: content-box;
135-
float: left;
136-
margin: 5px;
137-
margin-left: 10px;
138-
padding: 0 10px 10px 10px;
139-
width: 80%;
140-
padding-left: 150px;
141-
}
142-
143-
#content_index {
144-
background-color: #fff;
145-
-webkit-box-sizing: content-box;
146-
-moz-box-sizing: content-box;
147-
box-sizing: content-box;
148-
float: left;
149-
margin: 5px;
150-
margin-left: 10px;
151-
padding: 0 10px 10px 10px;
152-
width: 80%;
153-
padding-left: 300px;
151+
margin: -10px;
154152
}
155153
156154
.linenos {
@@ -159,16 +157,8 @@ div.verbose div.content {
159157
padding-right: 6px;
160158
}
161159
162-
#footer {
163-
border-top: thin solid #aaa;
164-
clear: both;
165-
font-size: 90%;
166-
margin-top: 5px;
167-
}
168-
169-
#footer ul {
170-
list-style-type: none;
171-
padding-left: 0;
160+
.d-none {
161+
display: none;
172162
}
173163
"""
174164

@@ -185,10 +175,13 @@ HTML_HEAD = """
185175
<script>
186176
function getStyle(el, styleProp) {
187177
var y;
188-
if (el.currentStyle)
178+
179+
if (el.currentStyle) {
189180
y = el.currentStyle[styleProp];
190-
else if (window.getComputedStyle)
181+
} else if (window.getComputedStyle) {
191182
y = document.defaultView.getComputedStyle(el, null).getPropertyValue(styleProp);
183+
}
184+
192185
return y;
193186
}
194187
@@ -205,11 +198,11 @@ HTML_HEAD = """
205198
}
206199
}
207200
208-
function init_expandables() {
209-
var elts = document.querySelectorAll(".expandable");
201+
function initExpandables() {
202+
var elements = document.querySelectorAll(".expandable");
210203
211-
for (var i = 0, len = elts.length; i < len; i++) {
212-
var el = elts[i];
204+
for (var i = 0, len = elements.length; i < len; i++) {
205+
var el = elements[i];
213206
var clickable = el.querySelector("span");
214207
var marker = clickable.querySelector(".marker");
215208
var content = el.querySelector(".content");
@@ -221,42 +214,40 @@ HTML_HEAD = """
221214
}
222215
}
223216
224-
function set_class_display(c, st) {
225-
var elements = document.querySelectorAll("." + c);
217+
function toggleDisplay(id) {
218+
var elements = document.querySelectorAll("." + id);
226219
227220
for (var i = 0, len = elements.length; i < len; i++) {
228-
elements[i].style.display = st;
221+
elements[i].classList.toggle("d-none");
229222
}
230223
}
231224
232-
function toggle_class_visibility(id) {
233-
var box = document.getElementById(id);
234-
set_class_display(id, box.checked ? "" : "none");
235-
}
225+
function toggleAll() {
226+
var elements = document.querySelectorAll("input");
236227
237-
function toggle_all() {
238-
var elts = document.querySelectorAll("input");
228+
// starting from 1 since 0 is the "toggle all" input
229+
for (var i = 1, len = elements.length; i < len; i++) {
230+
var el = elements[i];
239231
240-
for (var i = 1; i < elts.length; i++) {
241-
var el = elts[i];
242232
if (el.checked) {
243233
el.checked = false;
244234
} else {
245235
el.checked = true;
246236
}
247-
toggle_class_visibility(el.id);
237+
238+
toggleDisplay(el.id);
248239
}
249240
}
250-
251-
window.addEventListener("load", init_expandables);
241+
window.addEventListener("load", initExpandables);
252242
</script>
253243
</head>
254244
<body>
255-
<div id="header">
256-
<h1>Cppcheck report - %s: %s </h1>
245+
<div id="header" class="header">
246+
<h1>Cppcheck report - %s: %s</h1>
257247
</div>
258-
<div id="menu" dir="rtl">
259-
<p id="filename"><a href="index.html">Defects:</a> %s</p>
248+
<div class="wrapper">
249+
<div id="menu">
250+
<p id="filename"><a href="index.html">Defects:</a> %s</p>
260251
"""
261252

262253
HTML_HEAD_END = """
@@ -265,13 +256,14 @@ HTML_HEAD_END = """
265256
"""
266257

267258
HTML_FOOTER = """
259+
</div> <!-- /.wrapper -->
268260
</div>
269-
<div id="footer">
261+
<div id="footer" class="footer">
270262
<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>
263+
Cppcheck %s - a tool for static C/C++ code analysis<br>
264+
<br>
265+
Internet: <a href="http://cppcheck.net">http://cppcheck.net</a><br>
266+
IRC: <a href="irc://irc.freenode.net/cppcheck">irc://irc.freenode.net/cppcheck</a><br>
275267
</p>
276268
</div>
277269
</body>
@@ -586,13 +578,13 @@ if __name__ == '__main__':
586578
except IndexError:
587579
cnt_min = 0
588580

589-
stat_fmt = "\n <tr><td><input type=\"checkbox\" onclick=\"toggle_class_visibility(this.id)\" id=\"{}\" name=\"{}\" checked></td><td>{}</td><td>{}</td></tr>"
581+
stat_fmt = "\n <tr><td><input type=\"checkbox\" onclick=\"toggleDisplay(this.id)\" id=\"{}\" name=\"{}\" checked></td><td>{}</td><td>{}</td></tr>"
590582
for occurrences in reversed(range(cnt_min, cnt_max + 1)):
591583
for _id in [k for k, v in sorted(counter.items()) if v == occurrences]:
592584
stat_html.append(stat_fmt.format(_id, _id, dict(counter.most_common())[_id], _id))
593585

594-
output_file.write(HTML_HEAD.replace('id="menu" dir="rtl"', 'id="menu_index"', 1).replace("Defects:", "Defect summary;", 1) % (options.title, '', options.title, '', ''))
595-
output_file.write('\n <label><input type="checkbox" onclick="toggle_all()" checked> Toggle all</label>')
586+
output_file.write(HTML_HEAD.replace('id="menu"', 'id="menu_index"', 1).replace("Defects:", "Defect summary;", 1) % (options.title, '', options.title, '', ''))
587+
output_file.write('\n <label><input type="checkbox" onclick="toggleAll()" checked> Toggle all</label>')
596588
output_file.write('\n <table>')
597589
output_file.write('\n <tr><th>Show</th><th>#</th><th>Defect ID</th></tr>')
598590
output_file.write(''.join(stat_html))
@@ -679,7 +671,7 @@ if __name__ == '__main__':
679671

680672
with io.open(os.path.join(options.report_dir, 'stats.html'), 'w') as stats_file:
681673

682-
stats_file.write(HTML_HEAD.replace('id="menu" dir="rtl"', 'id="menu_index"', 1).replace("Defects:", "Back to summary", 1) % (options.title, '', options.title, 'Statistics', ''))
674+
stats_file.write(HTML_HEAD.replace('id="menu"', 'id="menu_index"', 1).replace("Defects:", "Back to summary", 1) % (options.title, '', options.title, 'Statistics', ''))
683675
stats_file.write(HTML_HEAD_END.replace("content", "content_index", 1))
684676

685677
for sev in SEVERITIES:

0 commit comments

Comments
 (0)