From 258100b83303ba82f3986c7a342b1a7590ea533a Mon Sep 17 00:00:00 2001 From: Tony Grosinger Date: Sun, 30 Apr 2023 19:58:57 -0700 Subject: [PATCH] Add XMLEl function --- gomponents.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/gomponents.go b/gomponents.go index 7b73a3b..4ccdae8 100644 --- a/gomponents.go +++ b/gomponents.go @@ -92,6 +92,30 @@ func El(name string, children ...Node) Node { }) } +// XMLEl behaves identically to El with the exception that checking for void +// elements is disabled. This allows use of elements such as "link" which need +// to have children in RSS feeds, for example. +func XMLEl(name string, children ...Node) Node { + return NodeFunc(func(w2 io.Writer) error { + w := &statefulWriter{w: w2} + + w.Write([]byte("<" + name)) + + for _, c := range children { + renderChild(w, c, AttributeType) + } + + w.Write([]byte(">")) + + for _, c := range children { + renderChild(w, c, ElementType) + } + + w.Write([]byte("")) + return w.err + }) +} + // renderChild c to the given writer w if the node type is t. func renderChild(w *statefulWriter, c Node, t NodeType) { if w.err != nil || c == nil {