Files
gitea-MIRROR/modules/markup/html_codepreview_test.go
T Mohan Aditya c6bdd3efd6 fix(markup): continue scanning text nodes after skipped matches (#40506)
Several markup processors stopped scanning a text node after skipping
one match. An external issue or code preview URL hid later local ones,
an SSH remote like `git@host:org/repo.git` hid a later email address, a
short link with a disallowed scheme hid later short links, and a text
node ending in an ellipsis left all its URLs unlinked.

The processors now continue past skipped matches. Only a URL in the last
word of a text node ending in an ellipsis is treated as truncated.

Fixes: https://github.com/go-gitea/gitea/issues/40505

Assisted-by: OpenCode:gemini-4-neon
Assisted-by: Claude Code:claude-opus-5-5

---------

Co-authored-by: silverwind <me@silverwind.io>
2026-11-27 05:42:42 +00:00

37 lines
1.8 KiB
Go

// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package markup_test
import (
"context"
"html/template"
"strings"
"testing"
"gitea.dev/modules/markup"
"gitea.dev/modules/markup/markdown"
"github.com/stretchr/testify/assert"
)
func TestRenderCodePreview(t *testing.T) {
markup.Init(&markup.RenderHelperFuncs{
RenderRepoFileCodePreview: func(ctx context.Context, options markup.RenderCodePreviewOptions) (template.HTML, error) {
if options.FilePath == "missing.md" {
return "", assert.AnError
}
return "<div>code preview</div>", nil
},
})
test := func(input, expected string) {
buffer, err := testRenderString(markup.NewTestRenderContext().WithMarkupType(markdown.MarkupName), input)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
}
test("http://localhost:3000/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20", "<p><div>code preview</div></p>")
test("http://other/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20", `<p><a href="http://other/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20" rel="nofollow">http://other/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20</a></p>`)
test("http://other/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20 http://localhost:3000/owner/repo/src/commit/0123456789/missing.md#L1-L2 http://localhost:3000/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20",
`<p><a href="http://other/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20" rel="nofollow">http://other/owner/repo/src/commit/0123456789/foo/bar.md#L10-L20</a> <a href="http://localhost:3000/owner/repo/src/commit/0123456789/missing.md#L1-L2" rel="nofollow">http://localhost:3000/owner/repo/src/commit/0123456789/missing.md#L1-L2</a> <div>code preview</div></p>`)
}