diff --git a/node.go b/node.go index 2ade2f2..ee72d9a 100644 --- a/node.go +++ b/node.go @@ -71,6 +71,15 @@ func El(tag string, children ...Node) Node { } } +// Attr sets an attribute on an El node. Returns the node for chaining. +// If the node is not an *elNode, returns it unchanged. +func Attr(n Node, key, value string) Node { + if el, ok := n.(*elNode); ok { + el.attrs[key] = value + } + return n +} + func (n *elNode) Render(ctx *Context) string { var b strings.Builder diff --git a/node_test.go b/node_test.go index cfca0c1..c1df25b 100644 --- a/node_test.go +++ b/node_test.go @@ -155,6 +155,34 @@ func TestEachNode_Empty(t *testing.T) { } } +func TestElNode_Attr(t *testing.T) { + ctx := NewContext() + node := Attr(El("div", Raw("content")), "class", "container") + got := node.Render(ctx) + want := `