Skip to content

Commit a1029cb

Browse files
GiteaBotsilverwind
andauthored
Fix EOL handling in web editor (#27141) (#27234)
Backport #27141 by @silverwind Fixes #27136. This does the following for Monaco's EOL setting: 1. Use editorconfig setting if present 2. Use the file's dominant line ending as detected by monaco, which uses LF for empty file Co-authored-by: silverwind <[email protected]>
1 parent 134c763 commit a1029cb

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

routers/web/repo/editor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b
287287
Operation: operation,
288288
FromTreePath: ctx.Repo.TreePath,
289289
TreePath: form.TreePath,
290-
ContentReader: strings.NewReader(strings.ReplaceAll(form.Content, "\r", "")),
290+
ContentReader: strings.NewReader(form.Content),
291291
},
292292
},
293293
Signoff: form.Signoff,

templates/repo/editor/edit.tmpl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@
3434
{{end}}
3535
</div>
3636
<div class="ui bottom attached active tab segment" data-tab="write">
37-
<textarea id="edit_area" name="content" class="gt-hidden" data-id="repo-{{.Repository.Name}}-{{.TreePath}}"
37+
<textarea id="edit_area" name="content" class="gt-hidden"
38+
data-id="repo-{{.Repository.Name}}-{{.TreePath}}"
3839
data-url="{{.Repository.Link}}/markup"
3940
data-context="{{.RepoLink}}"
4041
data-previewable-extensions="{{.PreviewableExtensions}}"
41-
data-line-wrap-extensions="{{.LineWrapExtensions}}">
42-
{{.FileContent}}</textarea>
42+
data-line-wrap-extensions="{{.LineWrapExtensions}}"
43+
data-initial-value="{{JsonUtils.EncodeToString .FileContent}}"></textarea>
4344
<div class="editor-loading is-loading"></div>
4445
</div>
4546
<div class="ui bottom attached tab segment markup" data-tab="preview">

web_src/js/features/codeeditor.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export async function createMonaco(textarea, filename, editorOpts) {
6262
const monaco = await import(/* webpackChunkName: "monaco" */'monaco-editor');
6363

6464
initLanguages(monaco);
65-
let {language, ...other} = editorOpts;
65+
let {language, eol, ...other} = editorOpts;
6666
if (!language) language = getLanguage(filename);
6767

6868
const container = document.createElement('div');
@@ -105,14 +105,28 @@ export async function createMonaco(textarea, filename, editorOpts) {
105105
monaco.languages.register({id: 'vs.editor.nullLanguage'});
106106
monaco.languages.setLanguageConfiguration('vs.editor.nullLanguage', {});
107107

108+
// We encode the initial value in JSON on the backend to prevent browsers from
109+
// discarding the \r during HTML parsing:
110+
// https://html.spec.whatwg.org/multipage/parsing.html#preprocessing-the-input-stream
111+
const value = JSON.parse(textarea.getAttribute('data-initial-value') || '""');
112+
textarea.value = value;
113+
textarea.removeAttribute('data-initial-value');
114+
108115
const editor = monaco.editor.create(container, {
109-
value: textarea.value,
116+
value,
110117
theme: 'gitea',
111118
language,
112119
...other,
113120
});
114121

115122
const model = editor.getModel();
123+
124+
// Monaco performs auto-detection of dominant EOL in the file, biased towards LF for
125+
// empty files. If there is an editorconfig value, override this detected value.
126+
if (eol in monaco.editor.EndOfLineSequence) {
127+
model.setEOL(monaco.editor.EndOfLineSequence[eol]);
128+
}
129+
116130
model.onDidChangeContent(() => {
117131
textarea.value = editor.getValue();
118132
textarea.dispatchEvent(new Event('change')); // seems to be needed for jquery-are-you-sure
@@ -187,5 +201,6 @@ function getEditorConfigOptions(ec) {
187201
opts.trimAutoWhitespace = ec.trim_trailing_whitespace === true;
188202
opts.insertSpaces = ec.indent_style === 'space';
189203
opts.useTabStops = ec.indent_style === 'tab';
204+
opts.eol = ec.end_of_line?.toUpperCase();
190205
return opts;
191206
}

0 commit comments

Comments
 (0)