aboutsummaryrefslogtreecommitdiffstats
path: root/web_src
diff options
context:
space:
mode:
authorcharles <30816317+charles7668@users.noreply.github.com>2024-09-25 03:06:52 +0800
committerGitHub <noreply@github.com>2024-09-24 19:06:52 +0000
commit3269b04d61ffe6a7ce462cd05ee150e4491124e8 (patch)
tree1e64fb5be29b1df410b5421d465c96bb4093507a /web_src
parent6fa962f409c84477a7a4cf35b4a38a4a93fc3224 (diff)
downloadgitea-3269b04d61ffe6a7ce462cd05ee150e4491124e8.tar.gz
gitea-3269b04d61ffe6a7ce462cd05ee150e4491124e8.zip
Notify the user when the file path contains leading or trailing spaces and fix the error message for invalid file names. (#31507)
close #31478
Diffstat (limited to 'web_src')
-rw-r--r--web_src/js/features/repo-editor.ts49
1 files changed, 44 insertions, 5 deletions
diff --git a/web_src/js/features/repo-editor.ts b/web_src/js/features/repo-editor.ts
index dadbd802bc..e4d179d3ae 100644
--- a/web_src/js/features/repo-editor.ts
+++ b/web_src/js/features/repo-editor.ts
@@ -75,23 +75,62 @@ export function initRepoEditor() {
}
filenameInput.addEventListener('input', function () {
const parts = filenameInput.value.split('/');
+ const links = Array.from(document.querySelectorAll('.breadcrumb span.section'));
+ const dividers = Array.from(document.querySelectorAll('.breadcrumb .breadcrumb-divider'));
+ let warningDiv = document.querySelector('.ui.warning.message.flash-message.flash-warning.space-related');
+ let containSpace = false;
if (parts.length > 1) {
for (let i = 0; i < parts.length; ++i) {
const value = parts[i];
+ const trimValue = value.trim();
+ if (trimValue === '..') {
+ // remove previous tree path
+ if (links.length > 0) {
+ const link = links.pop();
+ const divider = dividers.pop();
+ link.remove();
+ divider.remove();
+ }
+ continue;
+ }
if (i < parts.length - 1) {
- if (value.length) {
- filenameInput.before(createElementFromHTML(
+ if (trimValue.length) {
+ const linkElement = createElementFromHTML(
`<span class="section"><a href="#">${htmlEscape(value)}</a></span>`,
- ));
- filenameInput.before(createElementFromHTML(
+ );
+ const dividerElement = createElementFromHTML(
`<div class="breadcrumb-divider">/</div>`,
- ));
+ );
+ links.push(linkElement);
+ dividers.push(dividerElement);
+ filenameInput.before(linkElement);
+ filenameInput.before(dividerElement);
}
} else {
filenameInput.value = value;
}
this.setSelectionRange(0, 0);
+ containSpace |= (trimValue !== value && trimValue !== '');
+ }
+ }
+ containSpace |= Array.from(links).some((link) => {
+ const value = link.querySelector('a').textContent;
+ return value.trim() !== value;
+ });
+ containSpace |= parts[parts.length - 1].trim() !== parts[parts.length - 1];
+ if (containSpace) {
+ if (!warningDiv) {
+ warningDiv = document.createElement('div');
+ warningDiv.classList.add('ui', 'warning', 'message', 'flash-message', 'flash-warning', 'space-related');
+ warningDiv.innerHTML = '<p>File path contains leading or trailing whitespace.</p>';
+ // Add display 'block' because display is set to 'none' in formantic\build\semantic.css
+ warningDiv.style.display = 'block';
+ const inputContainer = document.querySelector('.repo-editor-header');
+ inputContainer.insertAdjacentElement('beforebegin', warningDiv);
}
+ showElem(warningDiv);
+ } else if (warningDiv) {
+ hideElem(warningDiv);
}
joinTreePath();
});