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"), Attributes(&AttributeMap{ "data-attr-3": "value", "data-attr-4": "value", }), HxBeforeRequest( SetText("before request"), ), HxAfterRequest( SetText("after request"), ), Children( Div(Text("hello, world")), ), Text("hello, child"), ) div.attributes["data-attr-1"] = "value" expectedRaw := `