Skip to content

Commit b573512

Browse files
Correctly query the primary button in a form (#32438)
The "primary button" is used at many places, but sometimes they might conflict (due to button switch, hidden panel, dropdown menu, etc). Sometimes we could add a special CSS class for the buttons, but sometimes not (see the comment of QuickSubmit) This PR introduces `querySingleVisibleElem` to help to get the correct primary button (the only visible one), and prevent from querying the wrong buttons. Fix #32437 --------- Co-authored-by: silverwind <me@silverwind.io>
1 parent 41b4ef8 commit b573512

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

web_src/js/features/comp/QuickSubmit.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {querySingleVisibleElem} from '../../utils/dom.ts';
2+
13
export function handleGlobalEnterQuickSubmit(target) {
24
let form = target.closest('form');
35
if (form) {
@@ -12,7 +14,11 @@ export function handleGlobalEnterQuickSubmit(target) {
1214
}
1315
form = target.closest('.ui.form');
1416
if (form) {
15-
form.querySelector('.ui.primary.button')?.click();
17+
// A form should only have at most one "primary" button to do quick-submit.
18+
// Here we don't use a special class to mark the primary button,
19+
// because there could be a lot of forms with a primary button, the quick submit should work out-of-box,
20+
// but not keeps asking developers to add that special class again and again (it could be forgotten easily)
21+
querySingleVisibleElem<HTMLButtonElement>(form, '.ui.primary.button')?.click();
1622
return true;
1723
}
1824
return false;

web_src/js/features/repo-issue-edit.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {handleReply} from './repo-issue.ts';
33
import {getComboMarkdownEditor, initComboMarkdownEditor, ComboMarkdownEditor} from './comp/ComboMarkdownEditor.ts';
44
import {POST} from '../modules/fetch.ts';
55
import {showErrorToast} from '../modules/toast.ts';
6-
import {hideElem, showElem} from '../utils/dom.ts';
6+
import {hideElem, querySingleVisibleElem, showElem} from '../utils/dom.ts';
77
import {attachRefIssueContextPopup} from './contextpopup.ts';
88
import {initCommentContent, initMarkupContent} from '../markup/content.ts';
99
import {triggerUploadStateChanged} from './comp/EditorUpload.ts';
@@ -77,20 +77,22 @@ async function onEditContent(event) {
7777
}
7878
};
7979

80+
// Show write/preview tab and copy raw content as needed
81+
showElem(editContentZone);
82+
hideElem(renderContent);
83+
8084
comboMarkdownEditor = getComboMarkdownEditor(editContentZone.querySelector('.combo-markdown-editor'));
8185
if (!comboMarkdownEditor) {
8286
editContentZone.innerHTML = document.querySelector('#issue-comment-editor-template').innerHTML;
83-
const saveButton = editContentZone.querySelector('.ui.primary.button');
87+
const saveButton = querySingleVisibleElem<HTMLButtonElement>(editContentZone, '.ui.primary.button');
88+
const cancelButton = querySingleVisibleElem<HTMLButtonElement>(editContentZone, '.ui.cancel.button');
8489
comboMarkdownEditor = await initComboMarkdownEditor(editContentZone.querySelector('.combo-markdown-editor'));
8590
const syncUiState = () => saveButton.disabled = comboMarkdownEditor.isUploading();
8691
comboMarkdownEditor.container.addEventListener(ComboMarkdownEditor.EventUploadStateChanged, syncUiState);
87-
editContentZone.querySelector('.ui.cancel.button').addEventListener('click', cancelAndReset);
92+
cancelButton.addEventListener('click', cancelAndReset);
8893
saveButton.addEventListener('click', saveAndRefresh);
8994
}
9095

91-
// Show write/preview tab and copy raw content as needed
92-
showElem(editContentZone);
93-
hideElem(renderContent);
9496
// FIXME: ideally here should reload content and attachment list from backend for existing editor, to avoid losing data
9597
if (!comboMarkdownEditor.value()) {
9698
comboMarkdownEditor.value(rawContent.textContent);

web_src/js/utils/dom.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {createElementFromAttrs, createElementFromHTML} from './dom.ts';
1+
import {createElementFromAttrs, createElementFromHTML, querySingleVisibleElem} from './dom.ts';
22

33
test('createElementFromHTML', () => {
44
expect(createElementFromHTML('<a>foo<span>bar</span></a>').outerHTML).toEqual('<a>foo<span>bar</span></a>');
@@ -16,3 +16,12 @@ test('createElementFromAttrs', () => {
1616
}, 'txt', createElementFromHTML('<span>inner</span>'));
1717
expect(el.outerHTML).toEqual('<button id="the-id" class="cls-1 cls-2" disabled="" tabindex="0" data-foo="the-data">txt<span>inner</span></button>');
1818
});
19+
20+
test('querySingleVisibleElem', () => {
21+
let el = createElementFromHTML('<div><span>foo</span></div>');
22+
expect(querySingleVisibleElem(el, 'span').textContent).toEqual('foo');
23+
el = createElementFromHTML('<div><span style="display: none;">foo</span><span>bar</span></div>');
24+
expect(querySingleVisibleElem(el, 'span').textContent).toEqual('bar');
25+
el = createElementFromHTML('<div><span>foo</span><span>bar</span></div>');
26+
expect(() => querySingleVisibleElem(el, 'span')).toThrowError('Expected exactly one visible element');
27+
});

web_src/js/utils/dom.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ export function initSubmitEventPolyfill() {
269269
*/
270270
export function isElemVisible(element: HTMLElement): boolean {
271271
if (!element) return false;
272-
273-
return Boolean(element.offsetWidth || element.offsetHeight || element.getClientRects().length);
272+
// checking element.style.display is not necessary for browsers, but it is required by some tests with happy-dom because happy-dom doesn't really do layout
273+
return Boolean((element.offsetWidth || element.offsetHeight || element.getClientRects().length) && element.style.display !== 'none');
274274
}
275275

276276
// replace selected text in a textarea while preserving editor history, e.g. CTRL-Z works after this
@@ -330,3 +330,10 @@ export function animateOnce(el: Element, animationClassName: string): Promise<vo
330330
el.classList.add(animationClassName);
331331
});
332332
}
333+
334+
export function querySingleVisibleElem<T extends HTMLElement>(parent: Element, selector: string): T | null {
335+
const elems = parent.querySelectorAll<HTMLElement>(selector);
336+
const candidates = Array.from(elems).filter(isElemVisible);
337+
if (candidates.length > 1) throw new Error(`Expected exactly one visible element matching selector "${selector}", but found ${candidates.length}`);
338+
return candidates.length ? candidates[0] as T : null;
339+
}

0 commit comments

Comments
 (0)