diff options
| author | Shulhan <ms@kilabit.info> | 2024-03-06 03:42:00 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2024-03-09 01:10:17 +0700 |
| commit | e730418289d80985c5d48946353961a357a4b532 (patch) | |
| tree | 9e573bf4fca975775eb25f32448f755a325880a8 /lib/html/node.go | |
| parent | 1e7cb99f42bcd41e98326bd9406d3cecfb2a4542 (diff) | |
| download | pakakeh.go-e730418289d80985c5d48946353961a357a4b532.tar.xz | |
lib: move package "net/html" to "lib/html"
Putting "html" under "net" package make no sense.
Another reason is to make the package flat under "lib/" directory.
Diffstat (limited to 'lib/html/node.go')
| -rw-r--r-- | lib/html/node.go | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/lib/html/node.go b/lib/html/node.go new file mode 100644 index 00000000..ac00ac48 --- /dev/null +++ b/lib/html/node.go @@ -0,0 +1,75 @@ +// Copyright 2020, Shulhan <ms@kilabit.info>. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package html + +import ( + "strings" + + "golang.org/x/net/html" +) + +// Node extends the html.Node. +type Node struct { + *html.Node +} + +// NewNode create new node by embedding html.Node "el". +func NewNode(el *html.Node) *Node { + return &Node{Node: el} +} + +// GetAttrValue get the value of node's attribute with specific key or empty +// if key not found. +func (node *Node) GetAttrValue(key string) string { + for _, attr := range node.Attr { + if key == attr.Key { + return attr.Val + } + } + return "" +} + +// GetFirstChild get the first non-empty child of node or nil if no child +// left. +func (node *Node) GetFirstChild() *Node { + el := node.FirstChild + for el != nil { + if el.Type == html.TextNode { + if len(strings.TrimSpace(el.Data)) == 0 { + el = el.NextSibling + continue + } + } + break + } + if el == nil { + return nil + } + return NewNode(el) +} + +// GetNextSibling get the next non-empty sibling of node or nil if no more +// sibling left. +func (node *Node) GetNextSibling() *Node { + el := node.NextSibling + for el != nil { + if el.Type == html.TextNode { + if len(strings.TrimSpace(el.Data)) == 0 { + el = el.NextSibling + continue + } + } + break + } + if el == nil { + return nil + } + return NewNode(el) +} + +// IsElement will return true if node type is html.ElementNode. +func (node *Node) IsElement() bool { + return node.Type == html.ElementNode +} |
