improve detection of buttons in modals

This commit is contained in:
Kyle Spearrin 2018-11-14 08:36:29 -05:00
parent 2709846b46
commit 70d377ab2d
1 changed files with 19 additions and 8 deletions

View File

@ -402,33 +402,44 @@ document.addEventListener('DOMContentLoaded', (event) => {
if (wrappingEl == null) { if (wrappingEl == null) {
return null; return null;
} }
const wrappingElIsForm = wrappingEl.tagName.toLowerCase() === 'form';
let submitButton = wrappingEl.querySelector('input[type="submit"], input[type="image"], ' + let submitButton = wrappingEl.querySelector('input[type="submit"], input[type="image"], ' +
'button[type="submit"], button:not([type])') as HTMLElement; 'button[type="submit"]') as HTMLElement;
if (submitButton != null && submitButton.getAttribute('type') == null) { if (submitButton == null && wrappingElIsForm) {
submitButton = wrappingEl.querySelector('button:not([type])');
if (submitButton != null) {
const buttonText = getButtonText(submitButton); const buttonText = getButtonText(submitButton);
if (buttonText != null && cancelButtonNames.has(buttonText.trim().toLowerCase())) { if (buttonText != null && cancelButtonNames.has(buttonText.trim().toLowerCase())) {
submitButton = null; submitButton = null;
} }
} }
}
if (submitButton == null) { if (submitButton == null) {
const possibleSubmitButtons = Array.from(wrappingEl.querySelectorAll('a, span, button[type="button"], ' + const possibleSubmitButtons = Array.from(wrappingEl.querySelectorAll('a, span, button[type="button"], ' +
'input[type="button"], button:not([type])')) as HTMLElement[]; 'input[type="button"], button:not([type])')) as HTMLElement[];
let typelessButton: HTMLElement = null;
possibleSubmitButtons.forEach((button) => { possibleSubmitButtons.forEach((button) => {
if (submitButton != null || button == null || button.tagName == null) { if (submitButton != null || button == null || button.tagName == null) {
return; return;
} }
const buttonText = getButtonText(button); const buttonText = getButtonText(button);
if (buttonText != null) { if (buttonText != null) {
if (button.tagName.toLowerCase() === 'button' && button.getAttribute('type') == null && if (typelessButton != null && button.tagName.toLowerCase() === 'button' &&
button.getAttribute('type') == null &&
!cancelButtonNames.has(buttonText.trim().toLowerCase())) { !cancelButtonNames.has(buttonText.trim().toLowerCase())) {
submitButton = button; typelessButton = button;
} else if (buttonNames.has(buttonText.trim().toLowerCase())) { } else if (buttonNames.has(buttonText.trim().toLowerCase())) {
submitButton = button; submitButton = button;
} }
} }
}); });
if (submitButton == null && typelessButton != null) {
submitButton = typelessButton;
} }
if (submitButton == null && wrappingEl.tagName.toLowerCase() === 'form') { }
if (submitButton == null && wrappingElIsForm) {
// Maybe it's in a modal? // Maybe it's in a modal?
const parentModal = wrappingEl.closest('div.modal') as HTMLElement; const parentModal = wrappingEl.closest('div.modal') as HTMLElement;
if (parentModal != null) { if (parentModal != null) {