Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-25 11:16:11 +01:00

Fix panic where now a third link breaks everything

This commit is contained in:
Mai-Lapyst 2024-04-19 23:54:46 +02:00
parent e9eacdecd2
commit acfae43253
No known key found for this signature in database
GPG key ID: F88D929C09E239F8
2 changed files with 84 additions and 2 deletions

View file

@ -1086,7 +1086,8 @@ func filePreviewPatternProcessor(ctx *RenderContext, node *html.Node) {
// Specialized version of replaceContent, so the parent paragraph element is not destroyed from our div // Specialized version of replaceContent, so the parent paragraph element is not destroyed from our div
before := node.Data[:(preview.start - offset)] before := node.Data[:(preview.start - offset)]
after := node.Data[(preview.end - offset):] after := node.Data[(preview.end - offset):]
offset += preview.end - 3 afterPrefix := "<p>"
offset = preview.end - len(afterPrefix)
node.Data = before node.Data = before
nextSibling := node.NextSibling nextSibling := node.NextSibling
node.Parent.InsertBefore(&html.Node{ node.Parent.InsertBefore(&html.Node{
@ -1096,7 +1097,7 @@ func filePreviewPatternProcessor(ctx *RenderContext, node *html.Node) {
node.Parent.InsertBefore(previewNode, nextSibling) node.Parent.InsertBefore(previewNode, nextSibling)
afterNode := &html.Node{ afterNode := &html.Node{
Type: html.RawNode, Type: html.RawNode,
Data: "<p>" + after, Data: afterPrefix + after,
} }
node.Parent.InsertBefore(afterNode, nextSibling) node.Parent.InsertBefore(afterNode, nextSibling)
node = afterNode node = afterNode

View file

@ -917,5 +917,86 @@ func TestRender_FilePreview(t *testing.T) {
`<p></p>`, `<p></p>`,
localMetas, localMetas,
) )
testRender(
"first "+commitFilePreview+" second "+commitFilePreview+" third "+commitFilePreview,
`<p>first </p>`+
`<div class="file-preview-box">`+
`<div class="header">`+
`<div>`+
`<a href="http://localhost:3000/gogits/gogs/src/commit/190d9492934af498c3f669d6a2431dc5459e5b20/path/to/file.go#L2-L3" class="muted" rel="nofollow">path/to/file.go</a>`+
`</div>`+
`<span class="text small grey">`+
`Lines 2 to 3 in <a href="http://localhost:3000/gogits/gogs/src/commit/190d9492934af498c3f669d6a2431dc5459e5b20" class="text black" rel="nofollow">190d949</a>`+
`</span>`+
`</div>`+
`<div class="ui table">`+
`<table class="file-preview">`+
`<tbody>`+
`<tr>`+
`<td class="lines-num"><span data-line-number="2"></span></td>`+
`<td class="lines-code chroma"><code class="code-inner"><span class="nx">B</span>`+"\n"+`</code></td>`+
`</tr>`+
`<tr>`+
`<td class="lines-num"><span data-line-number="3"></span></td>`+
`<td class="lines-code chroma"><code class="code-inner"><span class="nx">C</span>`+"\n"+`</code></td>`+
`</tr>`+
`</tbody>`+
`</table>`+
`</div>`+
`</div>`+
`<p> second </p>`+
`<div class="file-preview-box">`+
`<div class="header">`+
`<div>`+
`<a href="http://localhost:3000/gogits/gogs/src/commit/190d9492934af498c3f669d6a2431dc5459e5b20/path/to/file.go#L2-L3" class="muted" rel="nofollow">path/to/file.go</a>`+
`</div>`+
`<span class="text small grey">`+
`Lines 2 to 3 in <a href="http://localhost:3000/gogits/gogs/src/commit/190d9492934af498c3f669d6a2431dc5459e5b20" class="text black" rel="nofollow">190d949</a>`+
`</span>`+
`</div>`+
`<div class="ui table">`+
`<table class="file-preview">`+
`<tbody>`+
`<tr>`+
`<td class="lines-num"><span data-line-number="2"></span></td>`+
`<td class="lines-code chroma"><code class="code-inner"><span class="nx">B</span>`+"\n"+`</code></td>`+
`</tr>`+
`<tr>`+
`<td class="lines-num"><span data-line-number="3"></span></td>`+
`<td class="lines-code chroma"><code class="code-inner"><span class="nx">C</span>`+"\n"+`</code></td>`+
`</tr>`+
`</tbody>`+
`</table>`+
`</div>`+
`</div>`+
`<p> third </p>`+
`<div class="file-preview-box">`+
`<div class="header">`+
`<div>`+
`<a href="http://localhost:3000/gogits/gogs/src/commit/190d9492934af498c3f669d6a2431dc5459e5b20/path/to/file.go#L2-L3" class="muted" rel="nofollow">path/to/file.go</a>`+
`</div>`+
`<span class="text small grey">`+
`Lines 2 to 3 in <a href="http://localhost:3000/gogits/gogs/src/commit/190d9492934af498c3f669d6a2431dc5459e5b20" class="text black" rel="nofollow">190d949</a>`+
`</span>`+
`</div>`+
`<div class="ui table">`+
`<table class="file-preview">`+
`<tbody>`+
`<tr>`+
`<td class="lines-num"><span data-line-number="2"></span></td>`+
`<td class="lines-code chroma"><code class="code-inner"><span class="nx">B</span>`+"\n"+`</code></td>`+
`</tr>`+
`<tr>`+
`<td class="lines-num"><span data-line-number="3"></span></td>`+
`<td class="lines-code chroma"><code class="code-inner"><span class="nx">C</span>`+"\n"+`</code></td>`+
`</tr>`+
`</tbody>`+
`</table>`+
`</div>`+
`</div>`+
`<p></p>`,
localMetas,
)
}) })
} }