Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-69658] - CSP compatibility for summary.jelly #654

Merged
merged 11 commits into from
Oct 7, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ THE SOFTWARE.
<j:set var="id" value="${attrs.id}-${attrs.name}"/>
<j:set var="display" value="${attrs.opened ? '' : 'none'}"/>
<j:set var="idisplay" value="${attrs.opened ? 'none' : ''}"/>
<j:set var="open" value="javascript:showFailureSummary('${id}')"/>
<j:set var="close" value="javascript:hideFailureSummary('${id}')"/>
<st:adjunct includes="lib.hudson.test.js.failureSummary" />
<h4>
<a id="${id}-showlink" href="${open}" title="Show ${title}" style="display: ${idisplay}">
<a id="${id}-showlink" title="Show ${title}" style="display: ${idisplay}">
<l:icon src="symbol-add-outline plugin-ionicons-api" class="icon-sm"/><st:nbsp/>${title}
</a>
<a id="${id}-hidelink" href="${close}" title="Hide ${title}" style="display: ${display}">
<a id="${id}-hidelink" title="Hide ${title}" style="display: ${display}">
<l:icon src="symbol-remove-outline plugin-ionicons-api" class="icon-sm"/><st:nbsp/>${title}
</a>
</h4>
Expand Down
47 changes: 27 additions & 20 deletions src/main/resources/lib/hudson/test/js/failureSummary.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

const PREFIX = "test-";
const SHOWLINK_SUFFIX = "-showlink";
const HIDELINK_SUFFIX = "-hidelink";
Expand All @@ -10,12 +9,16 @@ function showFailureSummary(summaryId, query) {
document.getElementById(summaryId + SHOWLINK_SUFFIX).style.display = "none";
document.getElementById(summaryId + HIDELINK_SUFFIX).style.display = "";

if (typeof query !== 'undefined') {
if (typeof query !== 'undefined' && element.innerHTML.trim() === 'Loading...') {
let rqo = new XMLHttpRequest();
rqo.open('GET', query, true);
rqo.onreadystatechange = function() { element.innerHTML = rqo.responseText; }
rqo.onreadystatechange = function() {
element.innerHTML = rqo.responseText;
initializeShowHideLinks(element);
}
rqo.send(null);
}

}

function hideFailureSummary(summaryId) {
Expand All @@ -24,24 +27,28 @@ function hideFailureSummary(summaryId) {
document.getElementById(summaryId + HIDELINK_SUFFIX).style.display = "none";
}

function initializeShowHideLinks(container) {
container = container || document;

document.addEventListener('DOMContentLoaded', () => {
const testShowlinks = document.querySelectorAll("a[id*=test-][id*=-showlink]");
testShowlinks.forEach((element) => {
element.addEventListener('click', (event) => {
const id = element.id.replace(PREFIX, '').replace(SHOWLINK_SUFFIX, '');
const summaryId = PREFIX + id;
showFailureSummary(summaryId, document.URL + id + "summary");
})
container.querySelectorAll('a[id$="-showlink"], a[id$="-hidelink"]').forEach(link => {
link.addEventListener('click', handleShowHideClick);
link.style.cursor = 'pointer';
});
}

// add the onclick behavior for all the "hidelinks"
const testHidelinks = document.querySelectorAll("a[id*=test-][id*=-hidelink]");
testHidelinks.forEach((element) => {
element.addEventListener('click', (event) => {
const id = element.id.replace(PREFIX, '').replace(HIDELINK_SUFFIX, '');
const summaryId = PREFIX + id;
hideFailureSummary(summaryId);
})
});
function handleShowHideClick(event) {
event.preventDefault();

let link = event.target.closest('a[id$="-showlink"], a[id$="-hidelink"]');
const id = link.id.replace(/-showlink$/, '').replace(/-hidelink$/, '');

if (link.id.endsWith('-showlink')) {
showFailureSummary(id, document.URL + id.replace(PREFIX, '') + "summary");
} else {
hideFailureSummary(id);
}
}

document.addEventListener('DOMContentLoaded', () => {
initializeShowHideLinks();
});