Skip to content

Commit 48aab26

Browse files
authored
Fix EasyMDE validation (#18161)
1 parent 8eec403 commit 48aab26

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

web_src/js/features/comp/CommentEasyMDE.js

+30-18
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,27 @@ export function createCommentEasyMDE(textarea) {
7575
}
7676
});
7777
attachTribute(inputField, {mentions: true, emoji: true});
78+
attachEasyMDEToElements(easyMDE);
79+
return easyMDE;
80+
}
7881

82+
/**
83+
* attach the EasyMDE object to its input elements (InputField, TextArea)
84+
* @param {EasyMDE} easyMDE
85+
*/
86+
export function attachEasyMDEToElements(easyMDE) {
7987
// TODO: that's the only way we can do now to attach the EasyMDE object to a HTMLElement
88+
89+
// InputField is used by CodeMirror to accept user input
90+
const inputField = easyMDE.codemirror.getInputField();
8091
inputField._data_easyMDE = easyMDE;
81-
textarea._data_easyMDE = easyMDE;
82-
return easyMDE;
92+
93+
// TextArea is the real textarea element in the form
94+
const textArea = easyMDE.codemirror.getTextArea();
95+
textArea._data_easyMDE = easyMDE;
8396
}
8497

98+
8599
/**
86100
* get the attached EasyMDE editor created by createCommentEasyMDE
87101
* @param el jQuery or HTMLElement
@@ -98,29 +112,27 @@ export function getAttachedEasyMDE(el) {
98112
}
99113

100114
/**
101-
* validate if the given textarea from a form, is non-empty.
102-
* @param {jQuery | HTMLElement} form
103-
* @param {jQuery | HTMLElement} textarea
115+
* validate if the given EasyMDE textarea is is non-empty.
116+
* @param {jQuery} $textarea
104117
* @returns {boolean} returns true if validation succeeded.
105118
*/
106-
export function validateTextareaNonEmpty(form, textarea) {
107-
if (form instanceof jQuery) {
108-
form = form[0];
109-
}
110-
if (textarea instanceof jQuery) {
111-
textarea = textarea[0];
112-
}
113-
114-
const $markdownEditorTextArea = $(getAttachedEasyMDE(textarea).codemirror.getInputField());
119+
export function validateTextareaNonEmpty($textarea) {
120+
const $mdeInputField = $(getAttachedEasyMDE($textarea).codemirror.getInputField());
115121
// The original edit area HTML element is hidden and replaced by the
116122
// SimpleMDE/EasyMDE editor, breaking HTML5 input validation if the text area is empty.
117123
// This is a workaround for this upstream bug.
118124
// See https://github.com/sparksuite/simplemde-markdown-editor/issues/324
119-
if (textarea.value.length) {
120-
$markdownEditorTextArea.prop('required', true);
121-
form.reportValidity();
125+
if (!$textarea.val()) {
126+
$mdeInputField.prop('required', true);
127+
const $form = $textarea.parents('form');
128+
if (!$form.length) {
129+
// this should never happen. we put a alert here in case the textarea would be forgotten to be put in a form
130+
alert('Require non-empty content');
131+
} else {
132+
$form[0].reportValidity();
133+
}
122134
return false;
123135
}
124-
$markdownEditorTextArea.prop('required', false);
136+
$mdeInputField.prop('required', false);
125137
return true;
126138
}

web_src/js/features/repo-diff.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function initRepoDiffConversationForm() {
2727

2828
const form = $(e.target);
2929
const $textArea = form.find('textarea');
30-
if (!validateTextareaNonEmpty(form, $textArea)) {
30+
if (!validateTextareaNonEmpty($textArea)) {
3131
return;
3232
}
3333

web_src/js/features/repo-wiki.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {initMarkupContent} from '../markup/content.js';
2-
import {validateTextareaNonEmpty} from './comp/CommentEasyMDE.js';
2+
import {attachEasyMDEToElements, validateTextareaNonEmpty} from './comp/CommentEasyMDE.js';
33
import {initCompMarkupContentPreviewTab} from './comp/MarkupContentPreview.js';
44

55
const {csrfToken} = window.config;
@@ -119,11 +119,15 @@ export function initRepoWikiForm() {
119119
]
120120
});
121121

122-
const $markdownEditorTextArea = $(easyMDE.codemirror.getInputField());
123-
$markdownEditorTextArea.addClass('js-quick-submit');
122+
attachEasyMDEToElements(easyMDE);
124123

125-
$form.on('submit', function () {
126-
validateTextareaNonEmpty(this, $editArea);
124+
const $mdeInputField = $(easyMDE.codemirror.getInputField());
125+
$mdeInputField.addClass('js-quick-submit');
126+
127+
$form.on('submit', () => {
128+
if (!validateTextareaNonEmpty($editArea)) {
129+
return false;
130+
}
127131
});
128132

129133
setTimeout(() => {

0 commit comments

Comments
 (0)