Skip to content

Commit

Permalink
Put contexts dict in <script> and reveal them using JavaScript
Browse files Browse the repository at this point in the history
  • Loading branch information
abo-abo authored and nedbat committed Mar 17, 2023
1 parent 402858f commit 33737dd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
16 changes: 15 additions & 1 deletion coverage/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import os
import re
import shutil
import json
from collections import Counter

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

# Write the HTML page for this file.
file_data = self.datagen.data_for_file(ftr.fr, ftr.analysis)

contexts=Counter(c for cline in file_data.lines for c in cline.contexts)
context_codes = {y: i for (i, y) in enumerate(x[0] for x in contexts.most_common())}
contexts_json = json.dumps({v: k for (k, v) in context_codes.items()}, indent=2)

for ldata in file_data.lines:
# Build the HTML for the line.
html_parts = []
Expand All @@ -380,6 +387,8 @@ def write_html_file(self, ftr: FileToReport, prev_html: str, next_html: str) ->
)
ldata.html = ''.join(html_parts)

ldata.context_str = ",".join([str(context_codes[c_context]) for c_context in ldata.context_list])

if ldata.short_annotations:
# 202F is NARROW NO-BREAK SPACE.
# 219B is RIGHTWARDS ARROW WITH STROKE.
Expand Down Expand Up @@ -412,9 +421,14 @@ def write_html_file(self, ftr: FileToReport, prev_html: str, next_html: str) ->
)
ldata.css_class = ' '.join(css_classes) or "pln"

di = file_data.__dict__
if contexts_json == "{}":
di["contexts_json"] = None
else:
di["contexts_json"] = contexts_json
html_path = os.path.join(self.directory, ftr.html_filename)
html = self.source_tmpl.render({
**file_data.__dict__,
**di,
'prev_html': prev_html,
'next_html': next_html,
})
Expand Down
31 changes: 26 additions & 5 deletions coverage/htmlfiles/coverage_html.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,10 +595,31 @@ coverage.wire_up_sticky_header = function () {
updateHeader();
};

coverage.showContexts = function (e) {
span = e.target.nextElementSibling.nextElementSibling;
span_text = span.textContent;

if (/^[0-9,]+$/.test(span_text))
{
span.textContent = "";
span_text.split(",").forEach(function(s) {
ctx = contexts[s];
span.appendChild(document.createTextNode(ctx));
span.appendChild(document.createElement("br"));
})
}
};

document.addEventListener("DOMContentLoaded", () => {
if (document.body.classList.contains("indexfile")) {
coverage.index_ready();
} else {
coverage.pyfile_ready();
}
cboxes = document.querySelectorAll('[id^=ctxs]')
cboxes.forEach(function(cbox) {
cbox.addEventListener("click", coverage.showContexts)
});

if (document.body.classList.contains("indexfile")) {
coverage.index_ready();
} else {
coverage.pyfile_ready();
}

});
13 changes: 9 additions & 4 deletions coverage/htmlfiles/pyfile.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
{% if extra_css %}
<link rel="stylesheet" href="{{ extra_css }}" type="text/css">
{% endif %}

{% if contexts_json %}
<script type="text/javascript">
contexts = {{ contexts_json }}
</script>
{% endif %}

<script type="text/javascript" src="coverage_html.js" defer></script>
</head>
<body class="pyfile">
Expand Down Expand Up @@ -117,11 +124,9 @@ <h2>
{% endif %}
</span>
{# Things that should appear below the line. #}
{% if line.context_list %}
{% if line.context_str %}
<span class="ctxs">
{% for context in line.context_list %}
<span>{{context}}</span>
{% endfor %}
{{ line.context_str }}
</span>
{% endif %}
</p>
Expand Down

0 comments on commit 33737dd

Please sign in to comment.