xhtml

package module
v0.0.0-...-5cb68bd Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2024 License: MIT Imports: 7 Imported by: 0

README

xhtml GoDoc Go Report Card

Utilities for working with Go's x/net/html package.

It requires Go 1.23 and a version of x/net/html with iterators.

Documentation

Overview

Package xhtml makes x/net/html easier

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AdoptChildren

func AdoptChildren(dst, src *html.Node)

func AppendText

func AppendText(n *html.Node, text string)

func Attr

func Attr(n *html.Node, name string) string

func Clone

func Clone(n *html.Node) *html.Node

Clone n and all of its children.

func Closest

func Closest(n *html.Node, match Selector) *html.Node

Closest traverses the node and its parents until it finds a node that matches the Selector.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/earthboundkid/xhtml"
	"golang.org/x/net/html"
)

func main() {
	doc, err := html.Parse(strings.NewReader(`
	<div data-server-id="abc123">
		<button id="theButton"></button
	</div>`))
	if err != nil {
		panic(err)
	}
	// Find #theButton
	bttnEl := xhtml.Select(doc, xhtml.WithID("theButton"))
	// Find its data-server-id
	serverIDEl := xhtml.Closest(bttnEl, xhtml.WithDataset("server-id"))
	serverID := xhtml.Attr(serverIDEl, "data-server-id")
	fmt.Println(serverID)
}
Output:

abc123

func DeepEqual

func DeepEqual(a, b *html.Node) bool

DeepEqual returns true if a and b are ShallowEqual and all of their descendants are ShallowEqual as well.

func DeleteAttr

func DeleteAttr(n *html.Node, key string)

func InnerHTML

func InnerHTML(n *html.Node) string

InnerHTML returns the serialized markup contained within n.

func InnerHTMLBlocks

func InnerHTMLBlocks(n *html.Node) string

InnerHTMLBlocks is the same as InnerHTML, but it separates top level nodes with a line break.

func IsBalanced

func IsBalanced(s string) bool

IsBalanced reports whether every opening tag has a closing pair.

func LastChildOrNew

func LastChildOrNew(p *html.Node, tag string, attrs ...string) *html.Node

LastChildOrNew returns the last child of p if it is ShallowEqual to a new *html.Node with tag and attrs. Otherwise, it appends a new *html.Node with tag and attrs and returns that. For why this is operation useful, see Converting docx to clean HTML.

func New

func New(tag string, attrs ...string) *html.Node

func OuterHTML

func OuterHTML(n *html.Node) string

OuterHTML returns a serialized node.

func RemoveAll

func RemoveAll(nodes []*html.Node)

RemoveAll orphans the nodes it is passed. It ignores a node if the node is nil or already an orphan.

func ReplaceWith

func ReplaceWith(old, new *html.Node)

func Select

func Select(n *html.Node, match Selector) *html.Node

Select returns the first descendant node matched by the Selector or nil.

func SelectAll

func SelectAll(n *html.Node, match Selector) iter.Seq[*html.Node]

SelectAll returns an iterator yielding matching nodes in n.Descendants().

Example
package main

import (
	"fmt"
	"strings"

	"github.com/earthboundkid/xhtml"
	"golang.org/x/net/html"
	"golang.org/x/net/html/atom"
)

func main() {
	doc, err := html.Parse(strings.NewReader(`
	<ul>
		<li><a href="https://example.com/en-us">Hello, World!</a></li>
		<li><a href="https://example.com/ja-jp">こんにちは世界!</a></li>
	</ul>`))
	if err != nil {
		panic(err)
	}
	// Find all links in a document
	// And print the link URL and text
	for link := range xhtml.SelectAll(doc, xhtml.WithAtom(atom.A)) {
		fmt.Println(xhtml.Attr(link, "href"), xhtml.TextContent(link))
	}
}
Output:

https://example.com/en-us Hello, World!
https://example.com/ja-jp こんにちは世界!

func SelectSlice

func SelectSlice(n *html.Node, match Selector) []*html.Node

SelectSlice returns a slice of descendant nodes matched by the Selector.

func SetAttr

func SetAttr(n *html.Node, key, value string)

func SetInnerHTML

func SetInnerHTML(n *html.Node, s string) error

func ShallowEqual

func ShallowEqual(a, b *html.Node) bool

ShallowEqual returns true if a and b have the same Type, DataAtom, Data, Namespace, and Attr. It does not look at parents, children, or siblings. nil *html.Nodes are never equal to each other.

func TextContent

func TextContent(n *html.Node) string

TextContent joins and trims the text node children of n.

func ToBuffer

func ToBuffer(n *html.Node) *bytes.Buffer

ToBuffer returns a *bytes.Buffer containing the outerHTML of n.

func UnnestChildren

func UnnestChildren(n *html.Node)

UnnestChildren has all of the children of node adopted by its parent, and then it removes the node.

Types

type Selector

type Selector = func(n *html.Node) bool

Selector is a function that matches html.Nodes.

func WithAtom

func WithAtom(a atom.Atom) Selector

WithAtom returns a Selector that matches html.Nodes with the given atom.Atom.

func WithDataset

func WithDataset(attr string) Selector

WithDataset returns a Selector that matches html.Nodes with the given data attribute set. The attribute should be in kebab-case, not camelCase, without the "data-" prefix.

func WithID

func WithID(id string) Selector

WithID returns a Selector that matches html.Nodes with the given id= attribute.

Jump to

Keyboard shortcuts

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