htmlutil

package module
v0.0.0-...-2226738 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2020 License: MIT Imports: 4 Imported by: 1

README

go-htmlutil Build Status PkgGoDev

Package htmlutil

htmlutil is a lightweight, simple package that provides utility working with package golang.org/x/net/html.

Package dom

dom implements the Document and Element interfaces of Document Object Model(DOM).

Installation

$ go get -u github.com/Hunsin/go-htmlutil

Documents and examples can be found in pkg.go.dev.

Documentation

Overview

Package htmlutil provides some html utility functions.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Attr

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

Attr returns the value of keyed attribute under n. If no attribute matches the key, an empty string is returned.

Example
frag := `<html lang="en"></html>`
n, _ := html.Parse(strings.NewReader(frag)) // ignore error

lang := htmlutil.Attr(n.FirstChild, "lang")
fmt.Println(lang)
Output:

en

func First

func First(n *html.Node, fn MatchFunc) *html.Node

First works like Walk(n, fn) but returns the node once fn(node) returns true.

To compare First(), Last() and Walk(), consider a MatchFunc, fn looks like this:

func(n *html.Node) bool {
    return htmlutil.IsElement(n, "li")
}

Base on following HTML fragment, they work as:

<ul>             | First(n, fn) | Last(n, fn) | Walk(n, fn) |
  <li>...</li>   | return node  | not walked  | walked      |
  <li>...</li>   | not walked   | not walked  | walked      |
  <li>...</li>   | not walked   | return node | walked      |
</ul>

func HasAttr

func HasAttr(n *html.Node, key, val string) bool

HasAttr returns whether n has any attribute which matches given key and val.

func HasText

func HasText(n *html.Node, sub string) bool

HasText returns wether n is a html.TextNode and n.Data contains given sub string.

func Int

func Int(n *html.Node) (int, error)

Int returns the first integer it found in n and n's children. Floating-point numbers are ignored. If no integer was found, it returns error.

Example
frag := "<p>2018</p>"
n, _ := html.Parse(strings.NewReader(frag)) // ignore error

i, _ := htmlutil.Int(n)
fmt.Println(i)
Output:

2018

func Int64

func Int64(n *html.Node) (int64, error)

Int64 is like Int(n) but returns an interger of type int64.

func IsElement

func IsElement(n *html.Node, data string) bool

IsElement returns whether n is a html.ElementNode and n.Data matches given data.

Example
frag := "<html></html>"
n, _ := html.Parse(strings.NewReader(frag)) // ignore error

b := htmlutil.IsElement(n.FirstChild, "html")
fmt.Println(b)
Output:

true

func Last

func Last(n *html.Node, fn MatchFunc) *html.Node

Last works like First(n, fn) but searches nodes in different direction. It returns the last matched node in the node tree.

func Real

func Real(n *html.Node) (float64, error)

Real returns the first number it found in n and n's children. If no number was found, it returns error.

func Text

func Text(n *html.Node) string

Text returns the content of n's first text child node. If no html.TextNode was found, it returns empty string.

Example
frag := "<p>Hello <strong>World</strong></p>"
n, _ := html.Parse(strings.NewReader(frag)) // ignore error

txt := htmlutil.Text(n)
fmt.Println(txt)
Output:

Hello

func Uint64

func Uint64(n *html.Node) (uint64, error)

Uint64 is like Int(n) but for unsigned numbers. Negative integers are ignored.

func Walk

func Walk(n *html.Node, fn MatchFunc)

Walk calls fn with n and all the children under n. If fn(n) returns true, it keeps searching the node's siblings but children.

Example
frag := `
	  <div id="first">
	    <div id="child"></div>
	  </div>
	  <div id="sibling"></div>`
n, err := html.Parse(strings.NewReader(frag))
if err != nil {
	fmt.Println(err)
}

fn := func(n *html.Node) bool {
	found := htmlutil.IsElement(n, "div")
	if found {
		fmt.Println(htmlutil.Attr(n, "id"))
	}
	return found
}

htmlutil.Walk(n, fn)
Output:

first
sibling

Types

type MatchFunc

type MatchFunc func(*html.Node) bool

A MatchFunc is a function which returns if a html node matches certain condition.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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