Skip to content

Commit 5a7376c

Browse files
authored
Refactor markup code (#31399)
1. use clearer names 2. remove deadcode 3. avoid name shadowing 4. eliminate some lint warnings
1 parent 363c123 commit 5a7376c

File tree

2 files changed

+10
-37
lines changed

2 files changed

+10
-37
lines changed

modules/markup/html.go

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,6 @@ func CustomLinkURLSchemes(schemes []string) {
144144
common.LinkRegex, _ = xurls.StrictMatchingScheme(strings.Join(withAuth, "|"))
145145
}
146146

147-
// IsSameDomain checks if given url string has the same hostname as current Gitea instance
148-
func IsSameDomain(s string) bool {
149-
if strings.HasPrefix(s, "/") {
150-
return true
151-
}
152-
if uapp, err := url.Parse(setting.AppURL); err == nil {
153-
if u, err := url.Parse(s); err == nil {
154-
return u.Host == uapp.Host
155-
}
156-
return false
157-
}
158-
return false
159-
}
160-
161147
type postProcessError struct {
162148
context string
163149
err error
@@ -429,7 +415,7 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node) *html.Nod
429415
// We ignore code and pre.
430416
switch node.Type {
431417
case html.TextNode:
432-
textNode(ctx, procs, node)
418+
processTextNodes(ctx, procs, node)
433419
case html.ElementNode:
434420
if node.Data == "img" {
435421
next := node.NextSibling
@@ -465,15 +451,16 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node) *html.Nod
465451
for n := node.FirstChild; n != nil; {
466452
n = visitNode(ctx, procs, n)
467453
}
454+
default:
468455
}
469456
return node.NextSibling
470457
}
471458

472-
// textNode runs the passed node through various processors, in order to handle
459+
// processTextNodes runs the passed node through various processors, in order to handle
473460
// all kinds of special links handled by the post-processing.
474-
func textNode(ctx *RenderContext, procs []processor, node *html.Node) {
475-
for _, processor := range procs {
476-
processor(ctx, node)
461+
func processTextNodes(ctx *RenderContext, procs []processor, node *html.Node) {
462+
for _, p := range procs {
463+
p(ctx, node)
477464
}
478465
}
479466

@@ -939,14 +926,11 @@ func issueIndexPatternProcessor(ctx *RenderContext, node *html.Node) {
939926
// Path determines the type of link that will be rendered. It's unknown at this point whether
940927
// the linked item is actually a PR or an issue. Luckily it's of no real consequence because
941928
// Gitea will redirect on click as appropriate.
942-
path := "issues"
943-
if ref.IsPull {
944-
path = "pulls"
945-
}
929+
issuePath := util.Iif(ref.IsPull, "pulls", "issues")
946930
if ref.Owner == "" {
947-
link = createLink(util.URLJoin(ctx.Links.Prefix(), ctx.Metas["user"], ctx.Metas["repo"], path, ref.Issue), reftext, "ref-issue")
931+
link = createLink(util.URLJoin(ctx.Links.Prefix(), ctx.Metas["user"], ctx.Metas["repo"], issuePath, ref.Issue), reftext, "ref-issue")
948932
} else {
949-
link = createLink(util.URLJoin(ctx.Links.Prefix(), ref.Owner, ref.Name, path, ref.Issue), reftext, "ref-issue")
933+
link = createLink(util.URLJoin(ctx.Links.Prefix(), ref.Owner, ref.Name, issuePath, ref.Issue), reftext, "ref-issue")
950934
}
951935
}
952936

@@ -1207,7 +1191,7 @@ func hashCurrentPatternProcessor(ctx *RenderContext, node *html.Node) {
12071191
return
12081192
}
12091193
ctx.AddCancel(func() {
1210-
closer.Close()
1194+
_ = closer.Close()
12111195
ctx.GitRepo = nil
12121196
})
12131197
}

modules/markup/html_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,6 @@ func TestRender_CrossReferences(t *testing.T) {
144144
`<p><a href="`+inputURL+`" rel="nofollow"><code>0123456789/foo.txt (L2-L3)</code></a></p>`)
145145
}
146146

147-
func TestMisc_IsSameDomain(t *testing.T) {
148-
setting.AppURL = markup.TestAppURL
149-
150-
sha := "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
151-
commit := util.URLJoin(markup.TestRepoURL, "commit", sha)
152-
153-
assert.True(t, markup.IsSameDomain(commit))
154-
assert.False(t, markup.IsSameDomain("http://google.com/ncr"))
155-
assert.False(t, markup.IsSameDomain("favicon.ico"))
156-
}
157-
158147
func TestRender_links(t *testing.T) {
159148
setting.AppURL = markup.TestAppURL
160149

0 commit comments

Comments
 (0)