Skip to content

Commit 6af698f

Browse files
GiteaBotwxiaoguang
andauthored
Polyfill SubmitEvent for PaleMoon (#28441) (#28478)
Backport #28441 by wxiaoguang Fix #28319 It only polyfills if there is no "SubmitEvent" class, so it has no side effect for most users. Co-authored-by: wxiaoguang <[email protected]>
1 parent 94a05a4 commit 6af698f

File tree

6 files changed

+31
-7
lines changed

6 files changed

+31
-7
lines changed

web_src/js/features/common-global.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {initCompColorPicker} from './comp/ColorPicker.js';
66
import {showGlobalErrorMessage} from '../bootstrap.js';
77
import {handleGlobalEnterQuickSubmit} from './comp/QuickSubmit.js';
88
import {svg} from '../svg.js';
9-
import {hideElem, showElem, toggleElem} from '../utils/dom.js';
9+
import {hideElem, showElem, toggleElem, initSubmitEventPolyfill, submitEventSubmitter} from '../utils/dom.js';
1010
import {htmlEscape} from 'escape-goat';
1111
import {showTemporaryTooltip} from '../modules/tippy.js';
1212
import {confirmModal} from './comp/ConfirmModal.js';
@@ -122,7 +122,8 @@ async function formFetchAction(e) {
122122
const formMethod = formEl.getAttribute('method') || 'get';
123123
const formActionUrl = formEl.getAttribute('action');
124124
const formData = new FormData(formEl);
125-
const [submitterName, submitterValue] = [e.submitter?.getAttribute('name'), e.submitter?.getAttribute('value')];
125+
const formSubmitter = submitEventSubmitter(e);
126+
const [submitterName, submitterValue] = [formSubmitter?.getAttribute('name'), formSubmitter?.getAttribute('value')];
126127
if (submitterName) {
127128
formData.append(submitterName, submitterValue || '');
128129
}
@@ -193,6 +194,7 @@ export function initGlobalCommon() {
193194

194195
$('.tabular.menu .item').tab();
195196

197+
initSubmitEventPolyfill();
196198
document.addEventListener('submit', formFetchAction);
197199
document.addEventListener('click', linkAction);
198200
}

web_src/js/features/common-issue-list.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import $ from 'jquery';
2-
import {isElemHidden, onInputDebounce, toggleElem} from '../utils/dom.js';
2+
import {isElemHidden, onInputDebounce, submitEventSubmitter, toggleElem} from '../utils/dom.js';
33
import {GET} from '../modules/fetch.js';
44

55
const {appSubUrl} = window.config;
@@ -40,7 +40,7 @@ export function initCommonIssueListQuickGoto() {
4040
$form.on('submit', (e) => {
4141
// if there is no goto button, or the form is submitted by non-quick-goto elements, submit the form directly
4242
let doQuickGoto = !isElemHidden($goto);
43-
const submitter = e.originalEvent.submitter;
43+
const submitter = submitEventSubmitter(e.originalEvent);
4444
if (submitter !== $form[0] && submitter !== $input[0] && submitter !== $goto[0]) doQuickGoto = false;
4545
if (!doQuickGoto) return;
4646

web_src/js/features/repo-diff.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {validateTextareaNonEmpty} from './comp/ComboMarkdownEditor.js';
77
import {initViewedCheckboxListenerFor, countAndUpdateViewedFiles, initExpandAndCollapseFilesButton} from './pull-view-file.js';
88
import {initImageDiff} from './imagediff.js';
99
import {showErrorToast} from '../modules/toast.js';
10+
import {submitEventSubmitter} from '../utils/dom.js';
1011

1112
const {csrfToken, pageData, i18n} = window.config;
1213

@@ -57,7 +58,7 @@ function initRepoDiffConversationForm() {
5758
const formData = new FormData($form[0]);
5859

5960
// if the form is submitted by a button, append the button's name and value to the form data
60-
const submitter = e.originalEvent?.submitter;
61+
const submitter = submitEventSubmitter(e.originalEvent);
6162
const isSubmittedByButton = (submitter?.nodeName === 'BUTTON') || (submitter?.nodeName === 'INPUT' && submitter.type === 'submit');
6263
if (isSubmittedByButton && submitter.name) {
6364
formData.append(submitter.name, submitter.value);

web_src/js/modules/tippy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function switchTitleToTooltip(target) {
106106
/**
107107
* Creating tooltip tippy instance is expensive, so we only create it when the user hovers over the element
108108
* According to https://www.w3.org/TR/DOM-Level-3-Events/#events-mouseevent-event-order , mouseover event is fired before mouseenter event
109-
* Some old browsers like Pale Moon doesn't support "mouseenter(capture)"
109+
* Some browsers like PaleMoon don't support "addEventListener('mouseenter', capture)"
110110
* The tippy by default uses "mouseenter" event to show, so we use "mouseover" event to switch to tippy
111111
* @param e {Event}
112112
*/

web_src/js/utils/dom.js

+21
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,24 @@ export function loadElem(el, src) {
194194
el.src = src;
195195
});
196196
}
197+
198+
// some browsers like PaleMoon don't have "SubmitEvent" support, so polyfill it by a tricky method: use the last clicked button as submitter
199+
// it can't use other transparent polyfill patches because PaleMoon also doesn't support "addEventListener(capture)"
200+
const needSubmitEventPolyfill = typeof SubmitEvent === 'undefined';
201+
202+
export function submitEventSubmitter(e) {
203+
return needSubmitEventPolyfill ? (e.target._submitter || null) : e.submitter;
204+
}
205+
206+
function submitEventPolyfillListener(e) {
207+
const form = e.target.closest('form');
208+
if (!form) return;
209+
form._submitter = e.target.closest('button:not([type]), button[type="submit"], input[type="submit"]');
210+
}
211+
212+
export function initSubmitEventPolyfill() {
213+
if (!needSubmitEventPolyfill) return;
214+
console.warn(`This browser doesn't have "SubmitEvent" support, use a tricky method to polyfill`);
215+
document.body.addEventListener('click', submitEventPolyfillListener);
216+
document.body.addEventListener('focus', submitEventPolyfillListener);
217+
}

web_src/js/webcomponents/webcomponents.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import '@webcomponents/custom-elements'; // polyfill for some browsers like Pale Moon
1+
import '@webcomponents/custom-elements'; // polyfill for some browsers like PaleMoon
22
import './polyfill.js';
33

44
import '@github/relative-time-element';

0 commit comments

Comments
 (0)