Refactor markup code (#31399)
1. use clearer names 2. remove deadcode 3. avoid name shadowing 4. eliminate some lint warnings (cherry picked from commit 5a7376c0605415e63cb5b3b8f89ead01e567229b) Conflicts: modules/markup/html.go simple code divergence, trivial logic
This commit is contained in:
parent
9c48511c69
commit
75bbca68ce
|
@ -210,7 +210,6 @@ code.gitea.io/gitea/modules/json
|
||||||
StdJSON.Indent
|
StdJSON.Indent
|
||||||
|
|
||||||
code.gitea.io/gitea/modules/markup
|
code.gitea.io/gitea/modules/markup
|
||||||
IsSameDomain
|
|
||||||
GetRendererByType
|
GetRendererByType
|
||||||
RenderString
|
RenderString
|
||||||
IsMarkupFile
|
IsMarkupFile
|
||||||
|
|
|
@ -143,20 +143,6 @@ func CustomLinkURLSchemes(schemes []string) {
|
||||||
common.LinkRegex, _ = xurls.StrictMatchingScheme(strings.Join(withAuth, "|"))
|
common.LinkRegex, _ = xurls.StrictMatchingScheme(strings.Join(withAuth, "|"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsSameDomain checks if given url string has the same hostname as current Gitea instance
|
|
||||||
func IsSameDomain(s string) bool {
|
|
||||||
if strings.HasPrefix(s, "/") {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if uapp, err := url.Parse(setting.AppURL); err == nil {
|
|
||||||
if u, err := url.Parse(s); err == nil {
|
|
||||||
return u.Host == uapp.Host
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
type postProcessError struct {
|
type postProcessError struct {
|
||||||
context string
|
context string
|
||||||
err error
|
err error
|
||||||
|
@ -393,7 +379,7 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node) {
|
||||||
// We ignore code and pre.
|
// We ignore code and pre.
|
||||||
switch node.Type {
|
switch node.Type {
|
||||||
case html.TextNode:
|
case html.TextNode:
|
||||||
textNode(ctx, procs, node)
|
processTextNodes(ctx, procs, node)
|
||||||
case html.ElementNode:
|
case html.ElementNode:
|
||||||
if node.Data == "img" {
|
if node.Data == "img" {
|
||||||
for i, attr := range node.Attr {
|
for i, attr := range node.Attr {
|
||||||
|
@ -436,15 +422,16 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node) {
|
||||||
for n := node.FirstChild; n != nil; n = n.NextSibling {
|
for n := node.FirstChild; n != nil; n = n.NextSibling {
|
||||||
visitNode(ctx, procs, n)
|
visitNode(ctx, procs, n)
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
}
|
}
|
||||||
// ignore everything else
|
// ignore everything else
|
||||||
}
|
}
|
||||||
|
|
||||||
// textNode runs the passed node through various processors, in order to handle
|
// processTextNodes runs the passed node through various processors, in order to handle
|
||||||
// all kinds of special links handled by the post-processing.
|
// all kinds of special links handled by the post-processing.
|
||||||
func textNode(ctx *RenderContext, procs []processor, node *html.Node) {
|
func processTextNodes(ctx *RenderContext, procs []processor, node *html.Node) {
|
||||||
for _, processor := range procs {
|
for _, p := range procs {
|
||||||
processor(ctx, node)
|
p(ctx, node)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -135,17 +135,6 @@ func TestRender_CrossReferences(t *testing.T) {
|
||||||
`<p><a href="`+urlWithQuery+`" rel="nofollow"><code>`+sha[:10]+`/README.md (L1-L5)</code></a></p>`)
|
`<p><a href="`+urlWithQuery+`" rel="nofollow"><code>`+sha[:10]+`/README.md (L1-L5)</code></a></p>`)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMisc_IsSameDomain(t *testing.T) {
|
|
||||||
setting.AppURL = markup.TestAppURL
|
|
||||||
|
|
||||||
sha := "b6dd6210eaebc915fd5be5579c58cce4da2e2579"
|
|
||||||
commit := util.URLJoin(markup.TestRepoURL, "commit", sha)
|
|
||||||
|
|
||||||
assert.True(t, markup.IsSameDomain(commit))
|
|
||||||
assert.False(t, markup.IsSameDomain("http://google.com/ncr"))
|
|
||||||
assert.False(t, markup.IsSameDomain("favicon.ico"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRender_links(t *testing.T) {
|
func TestRender_links(t *testing.T) {
|
||||||
setting.AppURL = markup.TestAppURL
|
setting.AppURL = markup.TestAppURL
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue