mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-28 07:45:27 +00:00
fix
This commit is contained in:
@@ -303,29 +303,10 @@ func TestPackageNpm(t *testing.T) {
|
||||
AddBasicAuth(user.Name)
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
doc := NewHTMLParser(t, resp.Body)
|
||||
markup := doc.Find(".markup.markdown")
|
||||
paragraph := markup.Find("p")
|
||||
require.Len(t, paragraph.Nodes, 1)
|
||||
assert.Equal(t, "auto", paragraph.AttrOr("dir", ""))
|
||||
|
||||
links := paragraph.Find("a")
|
||||
require.Len(t, links.Nodes, 2)
|
||||
|
||||
docLink := links.Eq(0)
|
||||
assert.Equal(t, "docs", strings.TrimSpace(docLink.Text()))
|
||||
assert.Equal(t, "/user2/repo1/src/branch/master/package-subdir/docs/usage.md", docLink.AttrOr("href", ""))
|
||||
assert.Equal(t, "nofollow", docLink.AttrOr("rel", ""))
|
||||
|
||||
imageLink := links.Eq(1)
|
||||
assert.Equal(t, "/user2/repo1/src/branch/master/package-subdir/logo.png", imageLink.AttrOr("href", ""))
|
||||
assert.Equal(t, "nofollow noopener", imageLink.AttrOr("rel", ""))
|
||||
assert.Equal(t, "_blank", imageLink.AttrOr("target", ""))
|
||||
|
||||
image := imageLink.Find("img")
|
||||
require.Len(t, image.Nodes, 1)
|
||||
assert.Equal(t, "/user2/repo1/media/branch/master/package-subdir/logo.png", image.AttrOr("src", ""))
|
||||
assert.Equal(t, "logo", image.AttrOr("alt", ""))
|
||||
assert.Equal(t, "lazy", image.AttrOr("loading", ""))
|
||||
rendered, _ := doc.Find(".markup.markdown").Html()
|
||||
assertHTMLEq(t, `<p dir="auto"><a href="/user2/repo1/src/branch/master/package-subdir/docs/usage.md" rel="nofollow">docs</a>
|
||||
<a href="/user2/repo1/src/branch/master/package-subdir/logo.png" rel="nofollow noopener" target="_blank"><img src="/user2/repo1/media/branch/master/package-subdir/logo.png" alt="logo" loading="lazy"/></a></p>
|
||||
`, rendered)
|
||||
})
|
||||
|
||||
t.Run("Delete", func(t *testing.T) {
|
||||
|
||||
@@ -5,10 +5,13 @@ package integration
|
||||
|
||||
import (
|
||||
"io"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
// HTMLDoc struct
|
||||
@@ -47,3 +50,36 @@ func AssertHTMLElement[T int | bool](t testing.TB, doc *HTMLDoc, selector string
|
||||
assert.Equal(t, v, sel.Length())
|
||||
}
|
||||
}
|
||||
|
||||
func assertHTMLEq(t testing.TB, expected, actual string) {
|
||||
t.Helper()
|
||||
exp, err := html.Parse(strings.NewReader(expected))
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
act, err := html.Parse(strings.NewReader(actual))
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
var normalize func(n *html.Node)
|
||||
normalize = func(n *html.Node) {
|
||||
slices.SortFunc(n.Attr, func(a, b html.Attribute) int {
|
||||
if cmp := strings.Compare(a.Namespace, b.Namespace); cmp != 0 {
|
||||
return cmp
|
||||
}
|
||||
if cmp := strings.Compare(a.Key, b.Key); cmp != 0 {
|
||||
return cmp
|
||||
}
|
||||
return strings.Compare(a.Val, b.Val)
|
||||
})
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
normalize(c)
|
||||
}
|
||||
}
|
||||
normalize(exp)
|
||||
normalize(act)
|
||||
var expNormalized, actNormalized strings.Builder
|
||||
assert.NoError(t, html.Render(&expNormalized, exp))
|
||||
assert.NoError(t, html.Render(&actNormalized, act))
|
||||
assert.Equal(t, expNormalized.String(), actNormalized.String())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user