Documentation ¶
Index ¶
- type AttributeVisitor
- type Node
- func Combine(options ...Node) Node
- func Document(options ...Node) Node
- func ForEach[T any](items []T, view func(T) Node) Node
- func InnerText(content string) Node
- func NewAttribute(name string, value string) Node
- func NewBoolAttribute(name string) Node
- func NewTag(name string, options ...Node) Node
- func NewVoidTag(name string, options ...Node) Node
- func RawInnerText(content string) Node
- type TagVisitor
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AttributeVisitor ¶ added in v0.0.3
AttributeVisitor is used to iterate over a tag's attributes. See `pkg/html/render.go` for an example of how to use the visitor interfaces.
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node represents an HTML tag or attribute. It is the core type of the sanity library. Nodes are immutable and may be safely shared across threads.
- The `tags` package contains constructors for all standard HTML5 tags.
- The `attr` package contains constructors for all standard HTML5 attributes.
- The `html` package containing the Node implementation contains generic constructors and utilities.
func Combine ¶
Combine multiple Nodes into a single Node. This can be useful for grouping nodes without adding a wrapping element.
Example Usage:
c := Combine(tag.Div(InnerText("hello")), tag.Div(InnerText("world"))) c.String() == "<div>hello</div><div>world</div>"
func Document ¶
Document renders the preamble required by the HTML standard.
html.Document(attr.Lang("en"), html.InnerText("Hello World!")) would render as: <!DOCTYPE html> <html lang="en">Hello world!</html>
func ForEach ¶
ForEach applies the view function to each item, then returns a node containing the combined results.
Example Usage:
fruits := []string{"apple", "bannana", "orange"} list := tag.Ul(ForEach(fruits, func(fruit string) Node { return tag.Li(InnerText(fruit)) }) list.String() == "<ul><li>apple</li><li>bannana</li><li>orange</li></ul>"
func InnerText ¶
InnerText is rendered as text inside a tag. HTML in the content is escaped.
Example Usage: node := tag.Div(InnerText("hello world!")) node.String() == "<div>hello world!</div>"
func NewAttribute ¶
NewAttribute creates an attribute with a value. Like id="some-id" or class="class-a class-b".
func NewBoolAttribute ¶
NewBoolAttribute creates a bool attribute. A bool attribute is an attribute with no value. An example bool attribute is the `disabled` attribute in <button disabled>Submit</button>.
func NewVoidTag ¶
NewVoidTag creates an element that has no closing tag. Like <img> or <input>.
func RawInnerText ¶
RawInnerText is rendered as text inside a tag. Danger: HTML in the content is not escaped.
Example Usage: node := tag.Div(RawInnerText("<script>alert("hello world")</script>")) node.String() == "<div>"<script>alert("hello world")</script>"</div>"
func (Node) Render ¶
Render converts the node and all of its children into a byte array. Writing the byte array to an io.Writer is slightly more efficient than writing a string.
Example Usage: writer.Write(node.Render())
func (*Node) Visit ¶ added in v0.0.2
func (n *Node) Visit(visitor TagVisitor)
Visit calls the `TagVisitor.Tag`, `TagVisitor.VoidTag`, or `TagVisitor.Content` method depending on the type of thhe Node. The Visit* methods are used by Node.Render to convert the Node tree to HTML.
func (*Node) VisitAttributes ¶ added in v0.0.2
func (n *Node) VisitAttributes(visitor AttributeVisitor)
VisitAttributes visits the Node's attributes. This should be used by the visitor.Tag and visitor.VoidTag implementations to iterate over the tag's attributes.
func (*Node) VisitChildren ¶ added in v0.0.2
func (n *Node) VisitChildren(visitor TagVisitor)
VisitChildren visits the Node's children. This should be used by visitor.Tag implementation to visit the tag's child tags and inner HTML.
type TagVisitor ¶ added in v0.0.3
type TagVisitor interface { Tag(name string, node *Node) VoidTag(name string, node *Node) Content(content string) }
TagVisitor makes it possible to walk the Node tree. See `pkg/html/render.go` for an example visitor implementation. TagVisitor is used to implement node.Render.