Skip to content

Commit 33737dd

Browse files
abo-abonedbat
authored andcommitted
Put contexts dict in <script> and reveal them using JavaScript
Fixes #1584
1 parent 402858f commit 33737dd

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

coverage/html.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import os
1111
import re
1212
import shutil
13+
import json
14+
from collections import Counter
1315

1416
from dataclasses import dataclass
1517
from typing import Any, Dict, Iterable, List, Optional, Tuple, TYPE_CHECKING, cast
@@ -367,6 +369,11 @@ def write_html_file(self, ftr: FileToReport, prev_html: str, next_html: str) ->
367369

368370
# Write the HTML page for this file.
369371
file_data = self.datagen.data_for_file(ftr.fr, ftr.analysis)
372+
373+
contexts=Counter(c for cline in file_data.lines for c in cline.contexts)
374+
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)
376+
370377
for ldata in file_data.lines:
371378
# Build the HTML for the line.
372379
html_parts = []
@@ -380,6 +387,8 @@ def write_html_file(self, ftr: FileToReport, prev_html: str, next_html: str) ->
380387
)
381388
ldata.html = ''.join(html_parts)
382389

390+
ldata.context_str = ",".join([str(context_codes[c_context]) for c_context in ldata.context_list])
391+
383392
if ldata.short_annotations:
384393
# 202F is NARROW NO-BREAK SPACE.
385394
# 219B is RIGHTWARDS ARROW WITH STROKE.
@@ -412,9 +421,14 @@ def write_html_file(self, ftr: FileToReport, prev_html: str, next_html: str) ->
412421
)
413422
ldata.css_class = ' '.join(css_classes) or "pln"
414423

424+
di = file_data.__dict__
425+
if contexts_json == "{}":
426+
di["contexts_json"] = None
427+
else:
428+
di["contexts_json"] = contexts_json
415429
html_path = os.path.join(self.directory, ftr.html_filename)
416430
html = self.source_tmpl.render({
417-
**file_data.__dict__,
431+
**di,
418432
'prev_html': prev_html,
419433
'next_html': next_html,
420434
})

coverage/htmlfiles/coverage_html.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -595,10 +595,31 @@ coverage.wire_up_sticky_header = function () {
595595
updateHeader();
596596
};
597597

598+
coverage.showContexts = function (e) {
599+
span = e.target.nextElementSibling.nextElementSibling;
600+
span_text = span.textContent;
601+
602+
if (/^[0-9,]+$/.test(span_text))
603+
{
604+
span.textContent = "";
605+
span_text.split(",").forEach(function(s) {
606+
ctx = contexts[s];
607+
span.appendChild(document.createTextNode(ctx));
608+
span.appendChild(document.createElement("br"));
609+
})
610+
}
611+
};
612+
598613
document.addEventListener("DOMContentLoaded", () => {
599-
if (document.body.classList.contains("indexfile")) {
600-
coverage.index_ready();
601-
} else {
602-
coverage.pyfile_ready();
603-
}
614+
cboxes = document.querySelectorAll('[id^=ctxs]')
615+
cboxes.forEach(function(cbox) {
616+
cbox.addEventListener("click", coverage.showContexts)
617+
});
618+
619+
if (document.body.classList.contains("indexfile")) {
620+
coverage.index_ready();
621+
} else {
622+
coverage.pyfile_ready();
623+
}
624+
604625
});

coverage/htmlfiles/pyfile.html

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
{% if extra_css %}
1212
<link rel="stylesheet" href="{{ extra_css }}" type="text/css">
1313
{% endif %}
14+
15+
{% if contexts_json %}
16+
<script type="text/javascript">
17+
contexts = {{ contexts_json }}
18+
</script>
19+
{% endif %}
20+
1421
<script type="text/javascript" src="coverage_html.js" defer></script>
1522
</head>
1623
<body class="pyfile">
@@ -117,11 +124,9 @@ <h2>
117124
{% endif %}
118125
</span>
119126
{# Things that should appear below the line. #}
120-
{% if line.context_list %}
127+
{% if line.context_str %}
121128
<span class="ctxs">
122-
{% for context in line.context_list %}
123-
<span>{{context}}</span>
124-
{% endfor %}
129+
{{ line.context_str }}
125130
</span>
126131
{% endif %}
127132
</p>

0 commit comments

Comments
 (0)