Easy, yeah?
Made by front-ender for front-enders.
Package provide method to parse HTML and get browser-like DOM and DOM API.
It also has jQuery-like API.
go get github.com/bringmetheaugust/goDOM
package motherfckrs
import "github.com/bringmetheaugust/goDOM"
func main() {
// First, we'll get document
bytes := // HTML markup as bytes (from HTTP request, files, etc.)
document, _, err := goDom.Create(bytes) // create document (DOM with DOM API, like in browser)
if err != nil {return} // also check if markup is valid
// Want to find some element by `id`?
el, err := document.GetElementById("lol") // <a id="lol" class="pipi" href="http://lol.com">
if err != nil {return} // check if element exists
print(el.ClassList) // ["pipi"]
print(el.Attributes) // {"id": "lol", class: "pipi", "href": "http://lol.com"}
attr, _ := el.GetAttribute("href") // "http://lol.com"
// Or get a lot of elements by query selector?
elements, err := document.QuerySelectorAll(".weee") // all elements in DOM which have class "weee"
if err != nil {return} // check if elements are existed
for _, el := range elements { // loop slice with existed elements
// your best code here
}
package motherfckrs
import "github.com/bringmetheaugust/goDOM"
func main() {
// First, we'll get document
bytes := // HTML markup as bytes (from HTTP request, files, etc.)
_, jQ, err := goDom.Create(bytes) // create jQ (jQuery with jQUery-like API)
if err != nil {return} // also check if markup is valid
// Want to find some element by `id` ?
attr, err := jQ("#lol").Attr("href") // "http://lol.com" from <a id="lol" class="pipi" href="http://lol.com">
// Or get `data-lol` attributes from elements with class `.wee` which have inside itself links with class `.piu`?
jQ(".wee").Has("a.piu").Each(func(q) {
a, _ := q.Attr("data-lol")
})
More real examples here.
-
methods
- GetElementById
- GetElementsByClassName
- GetElementsByTagName
- QuerySelector (doesn't support
>
,+
,~
, pseudo-elements, pseudo-classes) - QuerySelectorAll (doesn't support
>
,+
,~
, pseudo-elements, pseudo-classes)
-
fields
-
methods
- Contains
- GetAttribute
- GetElementById
- GetElementsByClassName
- GetElementsByTagName
- HasAttribute
- QuerySelector (doesn't support
>
,+
,~
, pseudo-elements, pseudo-classes) - QuerySelectorAll (doesn't support
>
,+
,~
, pseudo-elements, pseudo-classes)
-
fields
- Attr
- Children
- Each
- Filter
- Find
- First
- Has
- HasClass
- Last
- Next
- NextAll
- Not
- Parent
- Parents
Text
get current text element text content
This package uses jQuery API as the origin jQuery library (using JavaScript).
For example in Golang with best practice, we should return data and/or errors almost from each function. How it should look like:
a, err := jQ(".li") // get elements with `li` classes
if err != nil { return } // check if elements exist
b, err := a.Has("a.my-link") // filter if they have links with class `my-link` inside itself
if err != nil { return } // check if elements exist
c, err := b.Find("div[data-lol=lala]") // find `div` elements with attribute `data-lol=lala` inside
if err != nil {return} // check if elements exist
c.Each(func (q) { print(q) })
As You know, this package provides the original jQuery API, so every method (except Attr
) always returns another jQuery element, even if elements are not found and a slice of elements is empty. So we can use jQuery as in origin jQuery library like this:
jQ(".li").Has("a.my-link").Find("div[data-lol=lala]").Each(func (q) { print(q) })
via Makefile
make install
without Makefile
sh ./scripts/install