0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2025-02-15 02:28:27 -05:00

fix inline file preview for files with encoded URL, fix #5069 (#6525)

### Screenshot
#### Before
See #5069.
#### After
![image](/attachments/d57c2ff8-d29e-41b8-9280-c884ac43f12e)
### Tests

- I added test coverage for Go changes...
  - [x] in their respective `*_test.go` for unit tests.
  - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I added test coverage for JavaScript changes...
  - [ ] in `web_src/js/*.test.js` if it can be unit tested.
  - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)).
- Manual testing: see https://codeberg.org/forgejo/forgejo/pulls/6525#issuecomment-2578814. Thanks @earl-warren.

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [ ] I do not want this change to show in the release notes.
- [ ] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6525
Reviewed-by: Otto <otto@codeberg.org>
Co-authored-by: Robert Wolff <mahlzahn@posteo.de>
Co-committed-by: Robert Wolff <mahlzahn@posteo.de>
This commit is contained in:
Robert Wolff 2025-01-30 08:20:05 +00:00 committed by Earl Warren
parent 1698210ead
commit c0a2108c25
16 changed files with 38 additions and 1 deletions

View file

@ -8,6 +8,7 @@ import (
"bytes"
"html/template"
"io"
"net/url"
"regexp"
"slices"
"strconv"
@ -83,6 +84,10 @@ func newFilePreview(ctx *RenderContext, node *html.Node, locale translation.Loca
} else if Type(filePath) != "" {
urlFullSource = node.Data[m[0]:m[6]] + filePath + "?display=source#" + node.Data[m[8]:m[1]]
}
filePath, err := url.QueryUnescape(filePath)
if err != nil {
return nil
}
hash := node.Data[m[8]:m[9]]
preview.start = m[0]

View file

@ -1129,4 +1129,35 @@ func TestRender_FilePreview(t *testing.T) {
localMetas,
)
})
commitFileURL = util.URLJoin(markup.TestRepoURL, "src", "commit", "eeb243c3395e1921c5d90e73bd739827251fc99d", "path", "to", "file%20%23.txt")
t.Run("file with strange characters in name", func(t *testing.T) {
testRender(
commitFileURL+"#L1",
`<p></p>`+
`<div class="file-preview-box">`+
`<div class="header">`+
`<div>`+
`<a href="http://localhost:3000/gogits/gogs/src/commit/eeb243c3395e1921c5d90e73bd739827251fc99d/path/to/file%20%23.txt#L1" class="muted" rel="nofollow">path/to/file #.txt</a>`+
`</div>`+
`<span class="text small grey">`+
`Line 1 in <a href="http://localhost:3000/gogits/gogs/src/commit/eeb243c3395e1921c5d90e73bd739827251fc99d" class="text black" rel="nofollow">eeb243c</a>`+
`</span>`+
`</div>`+
`<div class="ui table">`+
`<table class="file-preview">`+
`<tbody>`+
`<tr>`+
`<td class="lines-num"><span data-line-number="1"></span></td>`+
`<td class="lines-code chroma"><code class="code-inner">A`+"\n"+`</code></td>`+
`</tr>`+
`</tbody>`+
`</table>`+
`</div>`+
`</div>`+
`<p></p>`,
localMetas,
)
})
}

View file

@ -0,0 +1 @@
x•ŽANÃ0EYû³GB;a U=D9€=þ&–ÚÙÓr} 7èê­ÞÓÓëåÒŒBœ^¬´¤˜yY8Ï:AŠ† X}<7D>R×XkÎs­"î;uìFº®9x” Œ ÊEdÐ’%Í~**Zß3\ºÙvíô9Й>nÿ8Žfxkû=<3D>[9K”%L>®ôêÙ{§<>7Ãs;aÕvý4ÛhXOûH·Ô“þÕ†ûð`KÑ

View file

@ -1 +1 @@
c9913120ed2c1e27c1d7752ecdb7a504dc7cf6be
eeb243c3395e1921c5d90e73bd739827251fc99d