fix framework tests (sort attributes)
This commit is contained in:
parent
9d0f397c04
commit
e77ab0bc28
3 changed files with 67 additions and 2 deletions
|
|
@ -6,6 +6,7 @@ require (
|
|||
github.com/go-chi/chi/v5 v5.1.0
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/stretchr/testify v1.9.0
|
||||
golang.org/x/net v0.29.0
|
||||
)
|
||||
|
||||
require (
|
||||
|
|
|
|||
16
framework/go.sum
Normal file
16
framework/go.sum
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
|
||||
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
|
||||
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
@ -1,11 +1,55 @@
|
|||
package h
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"golang.org/x/net/html"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Sort attributes of a node by attribute name
|
||||
func sortAttributes(node *html.Node) {
|
||||
if node.Type == html.ElementNode && len(node.Attr) > 1 {
|
||||
sort.SliceStable(node.Attr, func(i, j int) bool {
|
||||
return node.Attr[i].Key < node.Attr[j].Key
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Traverse and sort attributes in the entire HTML tree
|
||||
func traverseAndSortAttributes(node *html.Node) {
|
||||
sortAttributes(node)
|
||||
for child := node.FirstChild; child != nil; child = child.NextSibling {
|
||||
traverseAndSortAttributes(child)
|
||||
}
|
||||
}
|
||||
|
||||
// Parse HTML, sort attributes, and render back to a string
|
||||
func parseSortAndRenderHTML(input string) string {
|
||||
// Parse the HTML string into a node tree
|
||||
doc, err := html.Parse(strings.NewReader(input))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Traverse and sort attributes for each node
|
||||
traverseAndSortAttributes(doc)
|
||||
|
||||
// Use a buffer to capture the rendered HTML
|
||||
var buf bytes.Buffer
|
||||
err = html.Render(&buf, doc)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Return the rendered HTML as a string
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func TestRender(t *testing.T) {
|
||||
t.Parallel()
|
||||
div := Div(
|
||||
Id("my-div"),
|
||||
Attribute("data-attr-2", "value"),
|
||||
|
|
@ -27,20 +71,24 @@ func TestRender(t *testing.T) {
|
|||
|
||||
div.attributes["data-attr-1"] = "value"
|
||||
|
||||
result := Render(div)
|
||||
expectedRaw := `<div data-attr-1="value" id="my-div" data-attr-2="value" data-attr-3="value" data-attr-4="value" hx-on::before-request="this.innerText = 'before request';" hx-on::after-request="this.innerText = 'after request';"><div >hello, world</div>hello, child</div>`
|
||||
expected := parseSortAndRenderHTML(expectedRaw)
|
||||
result := parseSortAndRenderHTML(Render(div))
|
||||
|
||||
assert.Equal(t,
|
||||
`<div data-attr-1="value" id="my-div" data-attr-2="value" data-attr-3="value" data-attr-4="value" hx-on::before-request="this.innerText = 'before request';" hx-on::after-request="this.innerText = 'after request';"><div >hello, world</div>hello, child</div>`,
|
||||
expected,
|
||||
result)
|
||||
}
|
||||
|
||||
func TestRawContent(t *testing.T) {
|
||||
t.Parallel()
|
||||
str := "<div>hello, world</div>"
|
||||
raw := Raw(str)
|
||||
assert.Equal(t, str, Render(raw))
|
||||
}
|
||||
|
||||
func TestConditional(t *testing.T) {
|
||||
t.Parallel()
|
||||
result := Render(
|
||||
Div(
|
||||
Ternary(true, Text("true"), Text("false")),
|
||||
|
|
|
|||
Loading…
Reference in a new issue