Skip to content

Commit aac0e1d

Browse files
authored
Merge pull request #10 from wxiaoguang/fix-17514-add-warning-bidi-characters
refactoring fix-17514-add-warning-bidi-characters
2 parents 1dc8a21 + 4e1b449 commit aac0e1d

File tree

10 files changed

+72
-123
lines changed

10 files changed

+72
-123
lines changed

options/locale/locale_en-US.ini

+5-5
Original file line numberDiff line numberDiff line change
@@ -1005,12 +1005,12 @@ file_view_rendered = View Rendered
10051005
file_view_raw = View Raw
10061006
file_permalink = Permalink
10071007
file_too_large = The file is too large to be shown.
1008-
bidi_bad_header = `This file contains unexpected Bidrectional Unicode characters!`
1009-
bidi_bad_description = `This file contains unexpected Bidrectional Unicode characters that may be processed differently from what appears below.<br>If your use case is intentional and legitimate, you can safely ignore this warning.<br>Use the Escape button to reveal hidden characters.`
1010-
bidi_bad_description_escaped = `This file contains unexpected Bidrectional Unicode characters.<br>Hidden unicode characters are escaped below.<br>Use the Unescape button to show how they render.`
1008+
bidi_bad_header = `This file contains unexpected Bidirectional Unicode characters!`
1009+
bidi_bad_description = `This file contains unexpected Bidirectional Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.`
1010+
bidi_bad_description_escaped = `This file contains unexpected Bidirectional Unicode characters. Hidden unicode characters are escaped below. Use the Unescape button to show how they render.`
10111011
unicode_header = `This file contains hidden Unicode characters!`
1012-
unicode_description = `This file contains hidden Unicode characters that may be processed differently from what appears below.<br>If your use case is intentional and legitimate, you can safely ignore this warning.<br>Use the Escape button to reveal hidden characters.`
1013-
unicode_description_escaped = `This file contains hidden Unicode characters.<br>Hidden unicode characters are escaped below.<br>Use the Unescape button to show how they render.`
1012+
unicode_description = `This file contains hidden Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.`
1013+
unicode_description_escaped = `This file contains hidden Unicode characters. Hidden unicode characters are escaped below. Use the Unescape button to show how they render.`
10141014
line_unicode = `This line has hidden unicode characters`
10151015

10161016
escape_control_characters = Escape

services/gitdiff/gitdiff.go

+18-15
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,7 @@ func getDiffLineSectionInfo(treePath, line string, lastLeftIdx, lastRightIdx int
171171
// escape a line's content or return <br> needed for copy/paste purposes
172172
func getLineContent(content string) DiffInline {
173173
if len(content) > 0 {
174-
status, content := charset.EscapeControlString(content)
175-
return DiffInline{
176-
EscapeStatus: status,
177-
Content: template.HTML(html.EscapeString(content)),
178-
}
174+
return DiffInlineWithUnicodeEscape(template.HTML(html.EscapeString(content)))
179175
}
180176
return DiffInline{Content: "<br>"}
181177
}
@@ -487,8 +483,7 @@ func diffToHTML(fileName string, diffs []diffmatchpatch.Diff, lineType DiffLineT
487483
buf.Write(codeTagSuffix)
488484
}
489485
}
490-
status, content := charset.EscapeControlString(buf.String())
491-
return DiffInline{EscapeStatus: status, Content: template.HTML(content)}
486+
return DiffInlineWithUnicodeEscape(template.HTML(buf.String()))
492487
}
493488

494489
// GetLine gets a specific line by type (add or del) and file line number
@@ -546,6 +541,18 @@ type DiffInline struct {
546541
Content template.HTML
547542
}
548543

544+
// DiffInlineWithUnicodeEscape makes a DiffInline with hidden unicode characters escaped
545+
func DiffInlineWithUnicodeEscape(s template.HTML) DiffInline {
546+
status, content := charset.EscapeControlString(string(s))
547+
return DiffInline{EscapeStatus: status, Content: template.HTML(content)}
548+
}
549+
550+
// DiffInlineWithHighlightCode makes a DiffInline with code highlight and hidden unicode characters escaped
551+
func DiffInlineWithHighlightCode(fileName, language, code string) DiffInline {
552+
status, content := charset.EscapeControlString(highlight.Code(fileName, language, code))
553+
return DiffInline{EscapeStatus: status, Content: template.HTML(content)}
554+
}
555+
549556
// GetComputedInlineDiffFor computes inline diff for the given line.
550557
func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) DiffInline {
551558
if setting.Git.DisableDiffHighlight {
@@ -570,26 +577,22 @@ func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) Dif
570577
case DiffLineAdd:
571578
compareDiffLine = diffSection.GetLine(DiffLineDel, diffLine.RightIdx)
572579
if compareDiffLine == nil {
573-
status, escaped := charset.EscapeControlString(highlight.Code(diffSection.FileName, language, diffLine.Content[1:]))
574-
return DiffInline{EscapeStatus: status, Content: template.HTML(escaped)}
580+
return DiffInlineWithHighlightCode(diffSection.FileName, language, diffLine.Content[1:])
575581
}
576582
diff1 = compareDiffLine.Content
577583
diff2 = diffLine.Content
578584
case DiffLineDel:
579585
compareDiffLine = diffSection.GetLine(DiffLineAdd, diffLine.LeftIdx)
580586
if compareDiffLine == nil {
581-
status, escaped := charset.EscapeControlString(highlight.Code(diffSection.FileName, language, diffLine.Content[1:]))
582-
return DiffInline{EscapeStatus: status, Content: template.HTML(escaped)}
587+
return DiffInlineWithHighlightCode(diffSection.FileName, language, diffLine.Content[1:])
583588
}
584589
diff1 = diffLine.Content
585590
diff2 = compareDiffLine.Content
586591
default:
587592
if strings.IndexByte(" +-", diffLine.Content[0]) > -1 {
588-
status, escaped := charset.EscapeControlString(highlight.Code(diffSection.FileName, language, diffLine.Content[1:]))
589-
return DiffInline{EscapeStatus: status, Content: template.HTML(escaped)}
593+
return DiffInlineWithHighlightCode(diffSection.FileName, language, diffLine.Content[1:])
590594
}
591-
status, escaped := charset.EscapeControlString(highlight.Code(diffSection.FileName, language, diffLine.Content))
592-
return DiffInline{EscapeStatus: status, Content: template.HTML(escaped)}
595+
return DiffInlineWithHighlightCode(diffSection.FileName, language, diffLine.Content)
593596
}
594597

595598
diffRecord := diffMatchPatch.DiffMain(highlight.Code(diffSection.FileName, language, diff1[1:]), highlight.Code(diffSection.FileName, language, diff2[1:]), true)

templates/repo/settings/lfs_file.tmpl

+1-17
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,7 @@
1616
</div>
1717
</h4>
1818
<div class="ui attached table unstackable segment">
19-
{{if .EscapeStatus.BadBIDI}}
20-
<div class="ui error message">
21-
<span class="close icon">{{svg "octicon-x" 16 "close inside"}}</span>
22-
<div class="header">
23-
{{.i18n.Tr "repo.bidi_bad_header"}}
24-
</div>
25-
<p>{{.i18n.Tr "repo.bidi_bad_description" | Str2html}}</p>
26-
</div>
27-
{{else if .EscapeStatus.Escaped}}
28-
<div class="ui warning message">
29-
<span class="close icon">{{svg "octicon-x" 16 "close inside"}}</span>
30-
<div class="header">
31-
{{.i18n.Tr "repo.unicode_header"}}
32-
</div>
33-
<p>{{.i18n.Tr "repo.unicode_description" | Str2html}}</p>
34-
</div>
35-
{{end}}
19+
{{template "repo/unicode_escape_prompt" dict "EscapeStatus" .EscapeStatus "root" $}}
3620
<div class="file-view{{if .IsMarkup}} markup {{.MarkupType}}{{else if .IsRenderedHTML}} plain-text{{else if .IsTextFile}} code-view{{end}}">
3721
{{if .IsMarkup}}
3822
{{if .FileContent}}{{.FileContent | Safe}}{{end}}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{{if .EscapeStatus.BadBIDI}}
2+
<div class="ui error message unicode-escape-prompt">
3+
<span class="close icon hide-panel button" data-panel-closest=".message">{{svg "octicon-x" 16 "close inside"}}</span>
4+
<div class="header">
5+
{{$.root.i18n.Tr "repo.bidi_bad_header"}}
6+
</div>
7+
<p>{{$.root.i18n.Tr "repo.bidi_bad_description" | Str2html}}</p>
8+
</div>
9+
{{else if .EscapeStatus.Escaped}}
10+
<div class="ui warning message unicode-escape-prompt">
11+
<span class="close icon hide-panel button" data-panel-closest=".message">{{svg "octicon-x" 16 "close inside"}}</span>
12+
<div class="header">
13+
{{$.root.i18n.Tr "repo.unicode_header"}}
14+
</div>
15+
<p>{{$.root.i18n.Tr "repo.unicode_description" | Str2html}}</p>
16+
</div>
17+
{{end}}

templates/repo/view_file.tmpl

+1-17
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,7 @@
7272
</div>
7373
</h4>
7474
<div class="ui attached table unstackable segment">
75-
{{if .EscapeStatus.BadBIDI}}
76-
<div class="ui error message file-message">
77-
<span class="close icon">{{svg "octicon-x" 16 "close inside"}}</span>
78-
<div class="header">
79-
{{.i18n.Tr "repo.bidi_bad_header"}}
80-
</div>
81-
<p>{{.i18n.Tr "repo.bidi_bad_description" | Str2html}}</p>
82-
</div>
83-
{{else if .EscapeStatus.Escaped}}
84-
<div class="ui warning message file-message">
85-
<span class="close icon">{{svg "octicon-x" 16 "close inside"}}</span>
86-
<div class="header">
87-
{{.i18n.Tr "repo.unicode_header"}}
88-
</div>
89-
<p>{{.i18n.Tr "repo.unicode_description" | Str2html}}</p>
90-
</div>
91-
{{end}}
75+
{{template "repo/unicode_escape_prompt" dict "EscapeStatus" .EscapeStatus "root" $}}
9276
<div class="file-view{{if .IsMarkup}} markup {{.MarkupType}}{{else if .IsRenderedHTML}} plain-text{{else if .IsTextSource}} code-view{{end}}">
9377
{{if .IsMarkup}}
9478
{{if .FileContent}}{{.FileContent | Safe}}{{end}}

templates/repo/wiki/view.tmpl

+6-50
Original file line numberDiff line numberDiff line change
@@ -65,72 +65,28 @@
6565
</div>
6666
{{end}}
6767
<div class="ui {{if .sidebarPresent}}grid equal width{{end}}" style="margin-top: 1rem;">
68-
<div class="ui {{if .sidebarPresent}}eleven wide column{{end}} segment markup">
69-
{{if .EscapeStatus.BadBIDI}}
70-
<div class="ui error message wiki-message">
71-
<span class="close icon">{{svg "octicon-x" 16 "close inside"}}</span>
72-
<div class="header">
73-
{{.i18n.Tr "repo.bidi_bad_header"}}
74-
</div>
75-
<p>{{.i18n.Tr "repo.bidi_bad_description" | Str2html}}</p>
76-
</div>
77-
{{else if .EscapeStatus.Escaped}}
78-
<div class="ui warning message wiki-message">
79-
<span class="close icon">{{svg "octicon-x" 16 "close inside"}}</span>
80-
<div class="header">
81-
{{.i18n.Tr "repo.unicode_header"}}
82-
</div>
83-
<p>{{.i18n.Tr "repo.unicode_description" | Str2html}}</p>
84-
</div>
85-
{{end}}
68+
<div class="ui {{if .sidebarPresent}}eleven wide column{{end}} segment markup wiki-content-main">
69+
{{template "repo/unicode_escape_prompt" dict "EscapeStatus" .EscapeStatus "root" $}}
8670
{{.content | Safe}}
8771
</div>
8872
{{if .sidebarPresent}}
8973
<div class="column" style="padding-top: 0;">
90-
<div class="ui segment">
74+
<div class="ui segment wiki-content-sidebar">
9175
{{if and .CanWriteWiki (not .Repository.IsMirror)}}
9276
<a class="ui right floated muted" href="{{.RepoLink}}/wiki/_Sidebar?action=_edit" aria-label="{{.i18n.Tr "repo.wiki.edit_page_button"}}">{{svg "octicon-pencil"}}</a>
9377
{{end}}
94-
{{if .sidebarEscapeStatus.BadBIDI}}
95-
<div class="ui error message">
96-
<span class="close icon">{{svg "octicon-x" 16 "close inside"}}</span>
97-
<div class="header">
98-
{{.i18n.Tr "repo.bidi_bad_header"}}
99-
</div>
100-
</div>
101-
{{else if .sidebarEscapeStatus.Escaped}}
102-
<div class="ui warning message">
103-
<span class="close icon">{{svg "octicon-x" 16 "close inside"}}</span>
104-
<div class="header">
105-
{{.i18n.Tr "repo.unicode_header"}}
106-
</div>
107-
</div>
108-
{{end}}
78+
{{template "repo/unicode_escape_prompt" dict "EscapeStatus" .sidebarEscapeStatus "root" $}}
10979
{{.sidebarContent | Safe}}
11080
</div>
11181
</div>
11282
{{end}}
11383
</div>
11484
{{if .footerPresent}}
115-
<div class="ui segment">
85+
<div class="ui segment wiki-content-footer">
11686
{{if and .CanWriteWiki (not .Repository.IsMirror)}}
11787
<a class="ui right floated muted" href="{{.RepoLink}}/wiki/_Footer?action=_edit" aria-label="{{.i18n.Tr "repo.wiki.edit_page_button"}}">{{svg "octicon-pencil"}}</a>
11888
{{end}}
119-
{{if .footerEscapeStatus.BadBIDI}}
120-
<div class="ui error message">
121-
<span class="close icon">{{svg "octicon-x" 16 "close inside"}}</span>
122-
<div class="header">
123-
{{.i18n.Tr "repo.bidi_bad_header"}}
124-
</div>
125-
</div>
126-
{{else if .footerEscapeStatus.Escaped}}
127-
<div class="ui warning message">
128-
<span class="close icon">{{svg "octicon-x" 16 "close inside"}}</span>
129-
<div class="header">
130-
{{.i18n.Tr "repo.unicode_header"}}
131-
</div>
132-
</div>
133-
{{end}}
89+
{{template "repo/unicode_escape_prompt" dict "footerEscapeStatus" .sidebarEscapeStatus "root" $}}
13490
{{.footerContent | Safe}}
13591
</div>
13692
{{end}}

web_src/js/features/common-global.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@ export function initGlobalCommon() {
8181
}
8282

8383
// Semantic UI modules.
84-
$('.message .close').on('click', function() {
85-
$(this)
86-
.closest('.message')
87-
.hide();
88-
});
89-
9084
$('.dropdown:not(.custom)').dropdown({
9185
fullTextSearch: 'exact'
9286
});
@@ -303,8 +297,20 @@ export function initGlobalButtons() {
303297
});
304298

305299
$('.hide-panel.button').on('click', function (event) {
306-
$($(this).data('panel')).hide();
300+
// a `.hide-panel.button` can hide a panel, by `data-panel="selector"` or `data-panel-closest="selector"`
307301
event.preventDefault();
302+
let sel = $(this).attr('data-panel');
303+
if (sel) {
304+
$(sel).hide();
305+
return;
306+
}
307+
sel = $(this).attr('data-panel-closest');
308+
if (sel) {
309+
$(this).closest(sel).hide();
310+
return;
311+
}
312+
// should never happen, otherwise there is a bug in code
313+
alert('Nothing to hide');
308314
});
309315

310316
$('.show-modal.button').on('click', function () {

web_src/js/features/repo-legacy.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
initRepoIssueWipToggle, initRepoPullRequestMerge, initRepoPullRequestUpdate,
1111
updateIssuesMeta,
1212
} from './repo-issue.js';
13-
import {initEscapeButton} from './repo-unicode-escape.js';
13+
import {initUnicodeEscapeButton} from './repo-unicode-escape.js';
1414
import {svg} from '../svg.js';
1515
import {htmlEscape} from 'escape-goat';
1616
import {initRepoBranchTagDropdown} from '../components/RepoBranchTagDropdown.js';
@@ -535,7 +535,7 @@ export function initRepository() {
535535
});
536536
}
537537

538-
initEscapeButton();
538+
initUnicodeEscapeButton();
539539
}
540540

541541
function initRepoIssueCommentEdit() {

web_src/js/features/repo-unicode-escape.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function initEscapeButton() {
1+
export function initUnicodeEscapeButton() {
22
$(document).on('click', 'a.escape-button', (e) => {
33
e.preventDefault();
44
$(e.target).parents('.file-content, .non-diff-file-content').find('.file-code, .file-view').addClass('unicode-escaped');

web_src/less/_repository.less

+8-9
Original file line numberDiff line numberDiff line change
@@ -3038,18 +3038,17 @@ td.blob-excerpt {
30383038
padding-left: 8px;
30393039
}
30403040

3041-
.file-message {
3042-
margin-bottom: 0 !important;
3043-
}
3044-
3045-
.file-message,
3046-
.wiki-message {
3047-
border-radius: 0 !important;
3041+
.ui.message.unicode-escape-prompt {
3042+
margin-bottom: 0;
3043+
border-radius: 0;
30483044
display: flex;
30493045
flex-direction: column;
3046+
}
3047+
3048+
.wiki-content-sidebar .ui.message.unicode-escape-prompt,
3049+
.wiki-content-footer .ui.message.unicode-escape-prompt {
30503050
p {
3051-
text-align: left;
3052-
align-self: center;
3051+
display: none;
30533052
}
30543053
}
30553054

0 commit comments

Comments
 (0)