html

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2023 License: MIT Imports: 1 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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

func Combine(options ...Node) Node

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

func Document(options ...Node) Node

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

func ForEach[T any](items []T, view func(T) Node) Node

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

func InnerText(content string) Node

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

func NewAttribute(name string, value string) Node

NewAttribute creates an attribute with a value. Like id="some-id" or class="class-a class-b".

func NewBoolAttribute

func NewBoolAttribute(name string) Node

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 NewTag

func NewTag(name string, options ...Node) Node

NewTag creates an element that has a closing tag. Like <div> or </button>.

func NewVoidTag

func NewVoidTag(name string, options ...Node) Node

NewVoidTag creates an element that has no closing tag. Like <img> or <input>.

func RawInnerText

func RawInnerText(content string) Node

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

func (n Node) Render() []byte

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) String

func (n Node) String() string

func (*Node) Visit added in v0.0.2

func (n *Node) Visit(visitor Visitor)

Visit walks the Node tree. It is used by Node.Render to convert the Node tree to HTML.

func (*Node) VisitAttributes added in v0.0.2

func (n *Node) VisitAttributes(visitor Visitor)

VisitAttributes visits the node's attributes. This should be used by the visitor.Tag and visitor.VoidTag implementations to visit the tag's attributes.

func (*Node) VisitChildren added in v0.0.2

func (n *Node) VisitChildren(visitor Visitor)

VisitChildren visits the node's children. This should be used by visitor.Tag implementation to visit tags and inner HTML contained within the tag.

type Visitor added in v0.0.2

type Visitor interface {
	Tag(name string, node *Node)
	VoidTag(name string, node *Node)
	Content(content string)
	Attribute(name string, value *string)
}

Visitor makes it possible to walk the Node tree. See `render.go` for an example visitor implementation. Visitor is used to implement node.Render.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL