Skip to content

Commit 63f3e49

Browse files
committed
style: clean up #1587
1 parent a5dc011 commit 63f3e49

File tree

10 files changed

+43
-37
lines changed

10 files changed

+43
-37
lines changed

CHANGES.rst

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ development at the same time, such as 4.5.x and 5.0.
2020
Unreleased
2121
----------
2222

23-
- Performance: HTML reports with context information are now much more compact,
24-
with file sizes typically as small as one-third the previous size. This
25-
closes `issue 1584`_ thanks to `Oleh Krehel <pull 1587_>`_.
23+
- Performance: HTML reports with context information are now much more compact.
24+
File sizes are typically as small as one-third the previous size, but can be
25+
dramatically smaller. This closes `issue 1584`_ thanks to `Oleh Krehel
26+
<pull 1587_>`_.
2627

2728
.. _issue 1584: https://github.com/nedbat/coveragepy/issues/1584
2829
.. _pull 1587: https://github.com/nedbat/coveragepy/pull/1587

coverage/html.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
from __future__ import annotations
77

8+
import collections
89
import datetime
910
import json
1011
import os
1112
import re
1213
import shutil
13-
from collections import Counter
1414

1515
from dataclasses import dataclass
1616
from typing import Any, Dict, Iterable, List, Optional, Tuple, TYPE_CHECKING, cast
@@ -370,9 +370,12 @@ def write_html_file(self, ftr: FileToReport, prev_html: str, next_html: str) ->
370370
# Write the HTML page for this file.
371371
file_data = self.datagen.data_for_file(ftr.fr, ftr.analysis)
372372

373-
contexts = Counter(c for cline in file_data.lines for c in cline.contexts)
373+
contexts = collections.Counter(c for cline in file_data.lines for c in cline.contexts)
374374
context_codes = {y: i for (i, y) in enumerate(x[0] for x in contexts.most_common())}
375-
contexts_json = json.dumps({v: k for (k, v) in context_codes.items()}, indent=2)
375+
if context_codes:
376+
contexts_json = json.dumps({v: k for (k, v) in context_codes.items()}, indent=2)
377+
else:
378+
contexts_json = None
376379

377380
for ldata in file_data.lines:
378381
# Build the HTML for the line.
@@ -382,13 +385,11 @@ def write_html_file(self, ftr: FileToReport, prev_html: str, next_html: str) ->
382385
html_parts.append(escape(tok_text))
383386
else:
384387
tok_html = escape(tok_text) or '&nbsp;'
385-
html_parts.append(
386-
f'<span class="{tok_type}">{tok_html}</span>'
387-
)
388+
html_parts.append(f'<span class="{tok_type}">{tok_html}</span>')
388389
ldata.html = ''.join(html_parts)
389-
390390
ldata.context_str = ",".join(
391-
str(context_codes[c_context]) for c_context in ldata.context_list)
391+
str(context_codes[c_context]) for c_context in ldata.context_list
392+
)
392393

393394
if ldata.short_annotations:
394395
# 202F is NARROW NO-BREAK SPACE.
@@ -422,13 +423,10 @@ def write_html_file(self, ftr: FileToReport, prev_html: str, next_html: str) ->
422423
)
423424
ldata.css_class = ' '.join(css_classes) or "pln"
424425

425-
if context_codes:
426-
file_data.__dict__["contexts_json"] = contexts_json
427-
else:
428-
file_data.__dict__["contexts_json"] = None
429426
html_path = os.path.join(self.directory, ftr.html_filename)
430427
html = self.source_tmpl.render({
431428
**file_data.__dict__,
429+
"contexts_json": contexts_json,
432430
'prev_html': prev_html,
433431
'next_html': next_html,
434432
})

coverage/htmlfiles/coverage_html.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,7 @@ coverage.showContexts = function (e) {
604604
span = e.target.nextElementSibling.nextElementSibling;
605605
span_text = span.textContent;
606606

607-
if (/^[0-9,]+$/.test(span_text))
608-
{
607+
if (/^[0-9,]+$/.test(span_text)) {
609608
span.textContent = "";
610609
span_text.split(",").forEach(function(s) {
611610
ctx = contexts[s];

coverage/htmlfiles/pyfile.html

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
{% if contexts_json %}
1616
<script type="text/javascript">
17-
contexts = {{ contexts_json }}
17+
contexts = {{ contexts_json }}
1818
</script>
1919
{% endif %}
2020

@@ -125,9 +125,7 @@ <h2>
125125
</span>
126126
{# Things that should appear below the line. #}
127127
{% if line.context_str %}
128-
<span class="ctxs">
129-
{{ line.context_str }}
130-
</span>
128+
<span class="ctxs">{{ line.context_str }}</span>
131129
{% endif %}
132130
</p>
133131
{% endjoined %}

coverage/htmlfiles/style.css

-2
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,6 @@ kbd { border: 1px solid black; border-color: #888 #333 #333 #888; padding: .1em
262262

263263
@media (prefers-color-scheme: dark) { #source p .ctxs { background: #056; } }
264264

265-
#source p .ctxs span { display: block; text-align: right; }
266-
267265
#index { font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; font-size: 0.875em; }
268266

269267
#index table.index { margin-left: -.5em; }

coverage/htmlfiles/style.scss

-4
Original file line numberDiff line numberDiff line change
@@ -623,10 +623,6 @@ $border-indicator-width: .2em;
623623
border-radius: .25em;
624624
margin-right: 1.75em;
625625
text-align: right;
626-
span {
627-
display: block;
628-
text-align: right;
629-
}
630626
}
631627
}
632628
}

tests/gold/html/contexts/two_tests_py.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<link rel="icon" sizes="32x32" href="favicon_32.png">
77
<link rel="stylesheet" href="style.css" type="text/css">
88
<script type="text/javascript">
9-
contexts = {
9+
contexts = {
1010
"0": "(empty)",
1111
"1": "two_tests.test_two",
1212
"2": "two_tests.test_one"
@@ -72,7 +72,7 @@ <h2>
7272
<a id="nextFileLink" class="nav" href="index.html">&#xbb; next</a>
7373
&nbsp; &nbsp; &nbsp;
7474
<a class="nav" href="https://coverage.readthedocs.io/en/7.2.3a0.dev1">coverage.py v7.2.3a0.dev1</a>,
75-
created at 2023-03-22 16:13 +0100
75+
created at 2023-03-22 13:16 -0400
7676
</p>
7777
<aside class="hidden">
7878
<button type="button" class="button_next_chunk" data-shortcut="j"/>
@@ -118,7 +118,7 @@ <h2>
118118
<a id="nextFileLink" class="nav" href="index.html">&#xbb; next</a>
119119
&nbsp; &nbsp; &nbsp;
120120
<a class="nav" href="https://coverage.readthedocs.io/en/7.2.3a0.dev1">coverage.py v7.2.3a0.dev1</a>,
121-
created at 2023-03-22 16:13 +0100
121+
created at 2023-03-22 13:16 -0400
122122
</p>
123123
</div>
124124
</footer>

tests/gold/html/styled/style.css

-2
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,6 @@ kbd { border: 1px solid black; border-color: #888 #333 #333 #888; padding: .1em
262262

263263
@media (prefers-color-scheme: dark) { #source p .ctxs { background: #056; } }
264264

265-
#source p .ctxs span { display: block; text-align: right; }
266-
267265
#index { font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; font-size: 0.875em; }
268266

269267
#index table.index { margin-left: -.5em; }

tests/gold/html/support/coverage_html.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ coverage.wire_up_filter = function () {
166166

167167
// Trigger change event on setup, to force filter on page refresh
168168
// (filter value may still be present).
169-
document.getElementById("filter").dispatchEvent(new Event("change"));
169+
document.getElementById("filter").dispatchEvent(new Event("input"));
170170
};
171171

172172
coverage.INDEX_SORT_STORAGE = "COVERAGE_INDEX_SORT_2";
@@ -212,6 +212,11 @@ coverage.index_ready = function () {
212212
coverage.LINE_FILTERS_STORAGE = "COVERAGE_LINE_FILTERS";
213213

214214
coverage.pyfile_ready = function () {
215+
cboxes = document.querySelectorAll('[id^=ctxs]')
216+
cboxes.forEach(function(cbox) {
217+
cbox.addEventListener("click", coverage.showContexts)
218+
});
219+
215220
// If we're directed to a particular line number, highlight the line.
216221
var frag = location.hash;
217222
if (frag.length > 2 && frag[1] === 't') {
@@ -553,7 +558,7 @@ coverage.build_scroll_markers = function () {
553558
'p.show_run, p.show_mis, p.show_exc, p.show_exc, p.show_par'
554559
).forEach(element => {
555560
const line_top = Math.floor(element.offsetTop * marker_scale);
556-
const line_number = parseInt(element.id.substr(1));
561+
const line_number = parseInt(element.querySelector(".n a").id.substr(1));
557562

558563
if (line_number === previous_line + 1) {
559564
// If this solid missed block just make previous mark higher.
@@ -595,10 +600,25 @@ coverage.wire_up_sticky_header = function () {
595600
updateHeader();
596601
};
597602

603+
coverage.showContexts = function (e) {
604+
span = e.target.nextElementSibling.nextElementSibling;
605+
span_text = span.textContent;
606+
607+
if (/^[0-9,]+$/.test(span_text)) {
608+
span.textContent = "";
609+
span_text.split(",").forEach(function(s) {
610+
ctx = contexts[s];
611+
span.appendChild(document.createTextNode(ctx));
612+
span.appendChild(document.createElement("br"));
613+
})
614+
}
615+
};
616+
598617
document.addEventListener("DOMContentLoaded", () => {
599618
if (document.body.classList.contains("indexfile")) {
600619
coverage.index_ready();
601620
} else {
602621
coverage.pyfile_ready();
603622
}
623+
604624
});

tests/gold/html/support/style.css

+1-3
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,10 @@ kbd { border: 1px solid black; border-color: #888 #333 #333 #888; padding: .1em
258258

259259
@media (prefers-color-scheme: dark) { #source p label.ctx { color: #777; } }
260260

261-
#source p .ctxs { display: block; max-height: 0; overflow-y: hidden; transition: all .2s; padding: 0 .5em; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; white-space: nowrap; background: #d0e8ff; border-radius: .25em; margin-right: 1.75em; }
261+
#source p .ctxs { display: block; max-height: 0; overflow-y: hidden; transition: all .2s; padding: 0 .5em; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; white-space: nowrap; background: #d0e8ff; border-radius: .25em; margin-right: 1.75em; text-align: right; }
262262

263263
@media (prefers-color-scheme: dark) { #source p .ctxs { background: #056; } }
264264

265-
#source p .ctxs span { display: block; text-align: right; }
266-
267265
#index { font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; font-size: 0.875em; }
268266

269267
#index table.index { margin-left: -.5em; }

0 commit comments

Comments
 (0)