detached 5f5b000

plaintext_test.go RawHistoryFolder

1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package web
4
5import (
6	"bytes"
7	"context"
8	"testing"
9
10	"github.com/stretchr/testify/assert"
11	"github.com/stretchr/testify/require"
12)
13
14// Plain text stays text; only http(s) addresses become links, without the
15// punctuation that ends a sentence; invisible characters are marked.
16func TestPlainParts(t *testing.T) {
17	t.Parallel()
18	tests := []struct {
19		name, in string
20		want     []TextPart
21	}{
22		{"plain", "nothing to see", []TextPart{{Text: "nothing to see"}}},
23		{"empty", "", nil},
24		{"link", "see https://example.org/a/b?x=1#y now", []TextPart{{Text: "see "}, {Text: "https://example.org/a/b?x=1#y", Href: "https://example.org/a/b?x=1#y"}, {Text: " now"}}},
25		{"sentence end", "go to http://x.test/a.", []TextPart{{Text: "go to "}, {Text: "http://x.test/a", Href: "http://x.test/a"}, {Text: "."}}},
26		{"closing bracket", "(see https://x.test/a)", []TextPart{{Text: "(see "}, {Text: "https://x.test/a", Href: "https://x.test/a"}, {Text: ")"}}},
27		{"balanced bracket", "https://x.test/a_(b)", []TextPart{{Text: "https://x.test/a_(b)", Href: "https://x.test/a_(b)"}}},
28		{"other schemes stay text", "javascript:alert(1) ftp://x.test data:text/html,x", []TextPart{{Text: "javascript:alert(1) ftp://x.test data:text/html,x"}}},
29		{"no host", "https:// and http:///x", []TextPart{{Text: "https:// and http:///x"}}},
30		{"quote ends the address", `http://x.test/"onmouseover="y`, []TextPart{{Text: "http://x.test/", Href: "http://x.test/"}, {Text: `"onmouseover="y`}}},
31		{"crlf", "a\r\nb", []TextPart{{Text: "a\nb"}}},
32		{"invisible", "a\u202eb", []TextPart{{Text: "a<U+202E>b"}}},
33	}
34	for _, tt := range tests {
35		t.Run(tt.name, func(t *testing.T) {
36			t.Parallel()
37			assert.Equal(t, tt.want, plainParts(tt.in))
38		})
39	}
40}
41
42// userText escapes the text and the address, and marks the link as untrusted.
43func TestUserTextEscapes(t *testing.T) {
44	t.Parallel()
45	var b bytes.Buffer
46	require.NoError(t, userText(plainParts(`<b>x</b> & http://x.test/?a=1&b=2`)).Render(context.Background(), &b))
47	assert.Equal(t, `&lt;b&gt;x&lt;/b&gt; &amp; <a href="http://x.test/?a=1&amp;b=2" rel="noopener nofollow ugc">http://x.test/?a=1&amp;b=2</a>`, b.String())
48}
49
50func TestParseOn(t *testing.T) {
51	t.Parallel()
52	assert.Equal(t, &lineRef{side: "new", line: 12, path: "a/b:c.go"}, parseOn("new:12:a/b:c.go"))
53	assert.Equal(t, &lineRef{side: "old", line: 1, path: "x"}, parseOn("old:1:x"))
54	for _, s := range []string{"", "new", "new:12", "new:12:", "new:0:x", "new:-1:x", "new:x:y", "up:1:x", "new::x"} {
55		assert.Nil(t, parseOn(s), s)
56	}
57}
58
59func TestShellWord(t *testing.T) {
60	t.Parallel()
61	assert.Equal(t, "feat/search", shellWord("feat/search"))
62	assert.Equal(t, `'a b'`, shellWord("a b"))
63	assert.Equal(t, `'it'\''s'`, shellWord("it's"))
64	assert.Equal(t, `'$(x)'`, shellWord("$(x)"))
65}
66
67// The notes of a split row: a context line counts once.
68func TestSplitRowNotes(t *testing.T) {
69	t.Parallel()
70	a, b := CommentView{ID: "1"}, CommentView{ID: "2"}
71	ctx := &DiffLine{Notes: []CommentView{a}}
72	assert.Equal(t, []CommentView{a}, SplitRow{ctx, ctx}.Notes())
73	del, add := &DiffLine{Notes: []CommentView{a}}, &DiffLine{Notes: []CommentView{b}}
74	assert.Equal(t, []CommentView{a, b}, SplitRow{del, add}.Notes())
75	assert.Equal(t, []CommentView{a}, SplitRow{del, nil}.Notes())
76	assert.Equal(t, []CommentView{b}, SplitRow{nil, add}.Notes())
77	assert.Nil(t, SplitRow{}.Notes())
78}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 4

Tags 45

↑↓ moveenter openesc close