Skip to content

Commit 7771c53

Browse files
committed
test: update support files for gold output
1 parent de8e905 commit 7771c53

File tree

2 files changed

+67
-48
lines changed

2 files changed

+67
-48
lines changed

tests/gold/html/support/coverage_html.js

+63-46
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ function on_click(sel, fn) {
3636
function getCellValue(row, column = 0) {
3737
const cell = row.cells[column] // nosemgrep: eslint.detect-object-injection
3838
if (cell.childElementCount == 1) {
39-
const child = cell.firstElementChild
40-
if (child instanceof HTMLTimeElement && child.dateTime) {
41-
return child.dateTime
42-
} else if (child instanceof HTMLDataElement && child.value) {
43-
console.log("child.value", child.value);
44-
return child.value
39+
var child = cell.firstElementChild;
40+
if (child.tagName === "A") {
41+
child = child.firstElementChild;
42+
}
43+
if (child instanceof HTMLDataElement && child.value) {
44+
return child.value;
4545
}
4646
}
4747
return cell.innerText || cell.textContent;
@@ -51,28 +51,37 @@ function rowComparator(rowA, rowB, column = 0) {
5151
let valueA = getCellValue(rowA, column);
5252
let valueB = getCellValue(rowB, column);
5353
if (!isNaN(valueA) && !isNaN(valueB)) {
54-
return valueA - valueB
54+
return valueA - valueB;
5555
}
5656
return valueA.localeCompare(valueB, undefined, {numeric: true});
5757
}
5858

5959
function sortColumn(th) {
6060
// Get the current sorting direction of the selected header,
61-
// clear state on other headers and then set the new sorting direction
61+
// clear state on other headers and then set the new sorting direction.
6262
const currentSortOrder = th.getAttribute("aria-sort");
6363
[...th.parentElement.cells].forEach(header => header.setAttribute("aria-sort", "none"));
64+
var direction;
6465
if (currentSortOrder === "none") {
65-
th.setAttribute("aria-sort", th.dataset.defaultSortOrder || "ascending");
66-
} else {
67-
th.setAttribute("aria-sort", currentSortOrder === "ascending" ? "descending" : "ascending");
66+
direction = th.dataset.defaultSortOrder || "ascending";
67+
}
68+
else if (currentSortOrder === "ascending") {
69+
direction = "descending";
6870
}
71+
else {
72+
direction = "ascending";
73+
}
74+
th.setAttribute("aria-sort", direction);
6975

7076
const column = [...th.parentElement.cells].indexOf(th)
7177

72-
// Sort all rows and afterwards append them in order to move them in the DOM
78+
// Sort all rows and afterwards append them in order to move them in the DOM.
7379
Array.from(th.closest("table").querySelectorAll("tbody tr"))
74-
.sort((rowA, rowB) => rowComparator(rowA, rowB, column) * (th.getAttribute("aria-sort") === "ascending" ? 1 : -1))
75-
.forEach(tr => tr.parentElement.appendChild(tr) );
80+
.sort((rowA, rowB) => rowComparator(rowA, rowB, column) * (direction === "ascending" ? 1 : -1))
81+
.forEach(tr => tr.parentElement.appendChild(tr));
82+
83+
// Save the sort order for next time.
84+
localStorage.setItem(coverage.INDEX_SORT_STORAGE, JSON.stringify({column, direction}));
7685
}
7786

7887
// Find all the elements with data-shortcut attribute, and use them to assign a shortcut key.
@@ -105,11 +114,17 @@ coverage.wire_up_filter = function () {
105114

106115
// Hide / show elements.
107116
table_body_rows.forEach(row => {
108-
show = false;
117+
var show = false;
118+
var target = event.target.value;
119+
var casefold = (target === target.toLowerCase());
109120
for (let column = 0; column < totals.length; column++) {
110121
cell = row.cells[column];
111122
if (cell.classList.contains("name")) {
112-
if (cell.textContent.includes(event.target.value)) {
123+
var celltext = cell.textContent;
124+
if (casefold) {
125+
celltext = celltext.toLowerCase();
126+
}
127+
if (celltext.includes(target)) {
113128
show = true;
114129
}
115130
}
@@ -136,7 +151,8 @@ coverage.wire_up_filter = function () {
136151
const [numer, denom] = cell.dataset.ratio.split(" ");
137152
totals[column]["numer"] += parseInt(numer, 10); // nosemgrep: eslint.detect-object-injection
138153
totals[column]["denom"] += parseInt(denom, 10); // nosemgrep: eslint.detect-object-injection
139-
} else {
154+
}
155+
else {
140156
totals[column] += parseInt(cell.textContent, 10); // nosemgrep: eslint.detect-object-injection
141157
}
142158
}
@@ -175,7 +191,8 @@ coverage.wire_up_filter = function () {
175191
cell.textContent = denom
176192
? `${(numer * 100 / denom).toFixed(places)}%`
177193
: `${(100).toFixed(places)}%`;
178-
} else {
194+
}
195+
else {
179196
cell.textContent = totals[column]; // nosemgrep: eslint.detect-object-injection
180197
}
181198
}
@@ -186,37 +203,31 @@ coverage.wire_up_filter = function () {
186203
document.getElementById("filter").dispatchEvent(new Event("input"));
187204
};
188205

189-
coverage.INDEX_SORT_STORAGE = "COVERAGE_INDEX_SORT_2";
190-
191-
// Loaded on index.html
192-
coverage.index_ready = function () {
193-
coverage.assign_shortkeys();
194-
coverage.wire_up_filter();
206+
// Set up the click-to-sort columns.
207+
coverage.wire_up_sorting = function () {
195208
document.querySelectorAll("[data-sortable] th[aria-sort]").forEach(
196209
th => th.addEventListener("click", e => sortColumn(e.target))
197210
);
198211

199212
// Look for a localStorage item containing previous sort settings:
213+
var column = 0, direction = "ascending";
200214
const stored_list = localStorage.getItem(coverage.INDEX_SORT_STORAGE);
201-
202215
if (stored_list) {
203-
const {column, direction} = JSON.parse(stored_list);
204-
const th = document.querySelector("[data-sortable]").tHead.rows[0].cells[column]; // nosemgrep: eslint.detect-object-injection
205-
th.setAttribute("aria-sort", direction === "ascending" ? "descending" : "ascending");
206-
th.click()
216+
({column, direction} = JSON.parse(stored_list));
207217
}
208218

209-
// Watch for page unload events so we can save the final sort settings:
210-
window.addEventListener("unload", function () {
211-
const th = document.querySelector('[data-sortable] th[aria-sort="ascending"], [data-sortable] [aria-sort="descending"]');
212-
if (!th) {
213-
return;
214-
}
215-
localStorage.setItem(coverage.INDEX_SORT_STORAGE, JSON.stringify({
216-
column: [...th.parentElement.cells].indexOf(th),
217-
direction: th.getAttribute("aria-sort"),
218-
}));
219-
});
219+
const th = document.querySelector("[data-sortable]").tHead.rows[0].cells[column]; // nosemgrep: eslint.detect-object-injection
220+
th.setAttribute("aria-sort", direction === "ascending" ? "descending" : "ascending");
221+
th.click()
222+
};
223+
224+
coverage.INDEX_SORT_STORAGE = "COVERAGE_INDEX_SORT_2";
225+
226+
// Loaded on index.html
227+
coverage.index_ready = function () {
228+
coverage.assign_shortkeys();
229+
coverage.wire_up_filter();
230+
coverage.wire_up_sorting();
220231

221232
on_click(".button_prev_file", coverage.to_prev_file);
222233
on_click(".button_next_file", coverage.to_next_file);
@@ -234,7 +245,8 @@ coverage.pyfile_ready = function () {
234245
if (frag.length > 2 && frag[1] === "t") {
235246
document.querySelector(frag).closest(".n").classList.add("highlight");
236247
coverage.set_sel(parseInt(frag.substr(2), 10));
237-
} else {
248+
}
249+
else {
238250
coverage.set_sel(0);
239251
}
240252

@@ -458,7 +470,8 @@ coverage.to_next_chunk_nicely = function () {
458470
if (line.parentElement !== document.getElementById("source")) {
459471
// The element is not a source line but the header or similar
460472
coverage.select_line_or_chunk(1);
461-
} else {
473+
}
474+
else {
462475
// We extract the line number from the id
463476
coverage.select_line_or_chunk(parseInt(line.id.substring(1), 10));
464477
}
@@ -477,7 +490,8 @@ coverage.to_prev_chunk_nicely = function () {
477490
if (line.parentElement !== document.getElementById("source")) {
478491
// The element is not a source line but the header or similar
479492
coverage.select_line_or_chunk(coverage.lines_len);
480-
} else {
493+
}
494+
else {
481495
// We extract the line number from the id
482496
coverage.select_line_or_chunk(parseInt(line.id.substring(1), 10));
483497
}
@@ -579,7 +593,8 @@ coverage.build_scroll_markers = function () {
579593
if (line_number === previous_line + 1) {
580594
// If this solid missed block just make previous mark higher.
581595
last_mark.style.height = `${line_top + line_height - last_top}px`;
582-
} else {
596+
}
597+
else {
583598
// Add colored line in scroll_marker block.
584599
last_mark = document.createElement("div");
585600
last_mark.id = `m${line_number}`;
@@ -607,7 +622,8 @@ coverage.wire_up_sticky_header = function () {
607622
function updateHeader() {
608623
if (window.scrollY > header_bottom) {
609624
header.classList.add("sticky");
610-
} else {
625+
}
626+
else {
611627
header.classList.remove("sticky");
612628
}
613629
}
@@ -635,7 +651,8 @@ coverage.expand_contexts = function (e) {
635651
document.addEventListener("DOMContentLoaded", () => {
636652
if (document.body.classList.contains("indexfile")) {
637653
coverage.index_ready();
638-
} else {
654+
}
655+
else {
639656
coverage.pyfile_ready();
640657
}
641658
});

tests/gold/html/support/style.css

+4-2
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,15 @@ kbd { border: 1px solid black; border-color: #888 #333 #333 #888; padding: .1em
280280

281281
@media (prefers-color-scheme: dark) { #index th:hover { background: #333; } }
282282

283+
#index th .arrows { color: #666; font-size: 85%; font-family: sans-serif; font-style: normal; pointer-events: none; }
284+
283285
#index th[aria-sort="ascending"], #index th[aria-sort="descending"] { white-space: nowrap; background: #eee; padding-left: .5em; }
284286

285287
@media (prefers-color-scheme: dark) { #index th[aria-sort="ascending"], #index th[aria-sort="descending"] { background: #333; } }
286288

287-
#index th[aria-sort="ascending"]::after { font-family: sans-serif; content: " "; }
289+
#index th[aria-sort="ascending"] .arrows::after { content: " "; }
288290

289-
#index th[aria-sort="descending"]::after { font-family: sans-serif; content: " "; }
291+
#index th[aria-sort="descending"] .arrows::after { content: " "; }
290292

291293
#index td.name { font-size: 1.15em; }
292294

0 commit comments

Comments
 (0)