Documentation
¶
Overview ¶
Package htmlx is a library which provides a set of extensions on go's golang.org/x/net/html library.
Example ¶
package main import ( "fmt" "strings" "github.com/mdigger/htmlx" ) func main() { source := `<ul id="test"> <li><a href="test1.html">test1</a></li> <li><a href="test2.html">test2</a></li> <li><a href="test3.html">test3</a></li> </ul>` doc, err := htmlx.Parse(strings.NewReader(source)) if err != nil { panic(err) } ul := doc.Find(htmlx.ID("test")) for _, e := range ul.FindAll(htmlx.TagName("a")) { if href, ok := e.Attr("href"); ok { fmt.Println(href) } } err = ul.SetHTML(`<li>no links</li>`) if err != nil { panic(err) } fmt.Println(ul) }
Output: test1.html test2.html test3.html <ul id="test"><li>no links</li></ul>
Index ¶
- Constants
- Variables
- func AddAttrWord(attrs []html.Attribute, key, word string) []html.Attribute
- func AttrVal(attrs []html.Attribute, key string) (val string, ok bool)
- func Find(n *html.Node, m Matcher) *html.Node
- func FindAll(n *html.Node, m Matcher) (result []*html.Node)
- func FindNext(n *html.Node, m Matcher) *html.Node
- func FindPrev(n *html.Node, m Matcher) *html.Node
- func HTML(n *html.Node, self bool) (string, error)
- func HasAttrWord(attrs []html.Attribute, key, word string) bool
- func Remove(n *html.Node)
- func RemoveAttr(attrs []html.Attribute, key string) []html.Attribute
- func RemoveChilds(n *html.Node)
- func Rename(n *html.Node, name string)
- func SetAttr(attrs []html.Attribute, key, val string) []html.Attribute
- func SetHTML(n *html.Node, data string) (err error)
- func SetText(n *html.Node, text string) error
- func Stats(n *html.Node) (c wstat.Counter)
- func Text(n *html.Node) string
- func WriteText(w io.StringWriter, n *html.Node, ignore map[atom.Atom]bool) error
- type Matcher
- type Node
- func (n Node) AddClass(name string)
- func (n Node) Attr(name string) (val string, ok bool)
- func (n Node) Find(m Matcher) Node
- func (n Node) FindAll(m Matcher) []Node
- func (n Node) FindNext(m Matcher) Node
- func (n Node) FindPrev(m Matcher) Node
- func (n Node) FirstChild() Node
- func (n Node) HasClass(name string) (ok bool)
- func (n Node) ID() string
- func (n Node) InnerHTML() (string, error)
- func (n Node) IsEmpty() bool
- func (n Node) LastChild() Node
- func (n Node) NextSibling() Node
- func (n Node) OuterHTML() (string, error)
- func (n Node) Parent() Node
- func (n Node) PrevSibling() Node
- func (n Node) Remove()
- func (n Node) RemoveAttr(name, value string)
- func (n Node) RemoveChilds()
- func (n Node) Rename(name string)
- func (n Node) SetAttr(name, value string)
- func (n Node) SetHTML(data string) error
- func (n Node) SetText(text string) error
- func (n Node) Stats() (c wstat.Counter)
- func (n Node) String() string
- func (n Node) Text() string
Examples ¶
Constants ¶
const ( AttrID = "id" AttrClass = "class" )
Predefined attribute names.
Variables ¶
var TextIgnoreAtom = map[atom.Atom]bool{ atom.Head: true, atom.Script: true, atom.Style: true, atom.Area: true, atom.Base: true, atom.Br: true, atom.Col: true, atom.Embed: true, atom.Hr: true, atom.Img: true, atom.Input: true, atom.Keygen: true, atom.Link: true, atom.Meta: true, atom.Param: true, atom.Source: true, atom.Track: true, atom.Wbr: true, }
TextIgnoreAtom specifies the list of items whose contents are ignored when working with text nodes.
Functions ¶
func AddAttrWord ¶
AddAttrWord add new word to attribute value.
func AttrVal ¶
AttrVal returns the attribute value with the specified key name. If the attribute is not specified, the false flag is returned by the second value.
func HasAttrWord ¶
HasAttrWord returns true if the attribute value with the specified key name and specified word in value is found.
func RemoveAttr ¶
RemoveAttr removes the attribute with the specified key name.
func RemoveChilds ¶
RemoveChilds removes all child elements if they are.
func SetHTML ¶
SetHTML parses an HTML fragment in the context of the current element and replaces them the child elements.
Types ¶
type Matcher ¶
Matcher used as synonym the functions for searching and selecting HTML elements.
func HasAttrVal ¶
HasAttrVal is used to find an element with a specified attribute value.
type Node ¶
Node expands html.node with additional methods.
func (Node) Attr ¶
Attr returns the attribute value with the specified name and the flag that the attribute was specified for this item.
func (Node) FirstChild ¶
FirstChild returns the first child element.
func (Node) NextSibling ¶
NextSibling returns the previous sibling element.
func (Node) PrevSibling ¶
PrevSibling returns the previous sibling element.
func (Node) Remove ¶
func (n Node) Remove()
Remove removes the specified element from the HTML tree.
func (Node) RemoveAttr ¶
RemoveAttr removes the attribute value with the specified name.
func (Node) RemoveChilds ¶
func (n Node) RemoveChilds()
RemoveChilds removes all child elements if they are exists.
func (Node) SetHTML ¶
SetHTML parses an HTML fragment in the context of the current element and replaces them the child elements.
func (Node) SetText ¶
SetText replaces the text of the element to the new and removes possible child items.