Skip to content

Commit 89c79ee

Browse files
author
Aditya
committed
JENKINS-76241: Minimal fix - early Behaviour.apply() only
1 parent fbc6511 commit 89c79ee

File tree

1 file changed

+8
-84
lines changed

1 file changed

+8
-84
lines changed

war/src/main/webapp/scripts/hudson-behavior.js

Lines changed: 8 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2712,7 +2712,7 @@ var layoutUpdateCallback = {
27122712

27132713
/**
27142714
* Fix for JENKINS-76241: Prevent POST race condition
2715-
* Ensure buildFormTree is attached before any form submission
2715+
* Apply Behaviour rules as early as possible
27162716
*/
27172717
(function () {
27182718
"use strict";
@@ -2722,53 +2722,7 @@ var layoutUpdateCallback = {
27222722
}
27232723
window._jenkinsPostHandlerFixApplied = true;
27242724

2725-
// Helper function to ensure buildFormTree is attached to a form
2726-
function ensureBuildFormTreeAttached(form) {
2727-
// Skip if already attached or form is marked no-json
2728-
if (form._buildFormTreeAttached || form.classList.contains("no-json")) {
2729-
return true;
2730-
}
2731-
2732-
// Check if buildFormTree is already in onsubmit
2733-
if (form.onsubmit && form.onsubmit.toString().indexOf('buildFormTree') !== -1) {
2734-
form._buildFormTreeAttached = true;
2735-
return true;
2736-
}
2737-
2738-
// Attach buildFormTree now
2739-
var oldOnsubmit = form.onsubmit;
2740-
form.onsubmit = function () {
2741-
var result = buildFormTree(this);
2742-
if (oldOnsubmit && typeof oldOnsubmit === "function") {
2743-
return result && oldOnsubmit.call(this);
2744-
}
2745-
return result;
2746-
};
2747-
form._buildFormTreeAttached = true;
2748-
2749-
// Ensure json field exists
2750-
var hasJsonField = false;
2751-
for (var i = 0; i < form.elements.length; i++) {
2752-
if (form.elements[i].name === "json") {
2753-
hasJsonField = true;
2754-
break;
2755-
}
2756-
}
2757-
2758-
if (!hasJsonField) {
2759-
var div = document.createElement("div");
2760-
div.classList.add("jenkins-!-display-contents");
2761-
div.innerHTML = "<input type=hidden name=json value=init>";
2762-
form.appendChild(div);
2763-
}
2764-
2765-
// Ensure crumb is present
2766-
crumb.appendToForm(form);
2767-
2768-
return true;
2769-
}
2770-
2771-
// Apply Behaviour rules early
2725+
// Simply apply Behaviour rules earlier than normal
27722726
function applyBehavioursEarly() {
27732727
if (typeof Behaviour !== "undefined" && Behaviour.apply) {
27742728
try {
@@ -2779,47 +2733,17 @@ var layoutUpdateCallback = {
27792733
}
27802734
}
27812735

2782-
// Override form.submit() to ensure buildFormTree runs
2783-
// This catches forms submitted via JavaScript (form.submit())
2784-
var originalSubmit = HTMLFormElement.prototype.submit;
2785-
HTMLFormElement.prototype.submit = function () {
2786-
ensureBuildFormTreeAttached(this);
2787-
2788-
// Call onsubmit before actual submission
2789-
if (this.onsubmit && typeof this.onsubmit === "function") {
2790-
var result = this.onsubmit.call(this);
2791-
if (result === false) {
2792-
return; // Don't submit if onsubmit returns false
2793-
}
2794-
}
2795-
2796-
// Call original submit
2797-
originalSubmit.call(this);
2798-
};
2799-
2800-
// Intercept submit events (for button clicks)
2801-
document.addEventListener(
2802-
"submit",
2803-
function (event) {
2804-
var form = event.target;
2805-
if (form.tagName === "FORM") {
2806-
ensureBuildFormTreeAttached(form);
2807-
}
2808-
},
2809-
true
2810-
); // Capture phase
2811-
2812-
// Apply Behaviour.apply() as early as possible
2736+
// Apply immediately if possible, otherwise on DOMContentLoaded
28132737
if (document.readyState !== "loading") {
28142738
applyBehavioursEarly();
28152739
} else {
28162740
document.addEventListener("DOMContentLoaded", applyBehavioursEarly);
28172741
}
28182742

2819-
// Watch for dynamically added forms
2743+
// Watch for dynamically added content
28202744
if (typeof MutationObserver !== "undefined") {
28212745
var observer = new MutationObserver(function (mutations) {
2822-
var hasNewForms = false;
2746+
var needsReapply = false;
28232747

28242748
mutations.forEach(function (mutation) {
28252749
mutation.addedNodes.forEach(function (node) {
@@ -2828,13 +2752,13 @@ var layoutUpdateCallback = {
28282752
node.tagName === "FORM" ||
28292753
(node.querySelector && node.querySelector("FORM"))
28302754
) {
2831-
hasNewForms = true;
2755+
needsReapply = true;
28322756
}
28332757
}
28342758
});
28352759
});
28362760

2837-
if (hasNewForms) {
2761+
if (needsReapply) {
28382762
applyBehavioursEarly();
28392763
}
28402764
});
@@ -2851,4 +2775,4 @@ var layoutUpdateCallback = {
28512775
document.addEventListener("DOMContentLoaded", startObserving);
28522776
}
28532777
}
2854-
})();
2778+
})();

0 commit comments

Comments
 (0)