Documentation
¶
Overview ¶
Example ¶
package main
import (
"fmt"
"log"
"strings"
"github.com/saihon/gohtml"
"github.com/saihon/gohtml/utils"
)
func main() {
text := `
<html>
<head></head>
<body>
<div id="id">hello</div>
</body>
</html>`
doc, err := gohtml.Parse(strings.NewReader(text))
if err != nil {
log.Fatal(err)
}
v := doc.GetElementById("id")
if v == nil {
return
}
fmt.Println(v.TextContent()) // hello
// Attribute
// set
v.SetAttribute("class", "class-1")
// get
classname := v.GetAttribute("class")
fmt.Println(classname) // class-1
// get body
body := doc.Body()
// remove
body.RemoveChild(v)
// create element
div := gohtml.CreateElement("div")
// create text node
textnode := gohtml.CreateTextNode("hello world")
div.AppendChild(textnode)
body.AppendChild(div)
// remove itself
div.Remove()
for _, v := range []string{"foo", "bar", "baz"} {
p := gohtml.CreateElement("p")
t := gohtml.CreateTextNode(v)
p.AppendChild(t)
body.AppendChild(p)
}
collection := body.GetElementsByTagName("p")
for i := 0; i < collection.Length(); i++ {
element := collection.Get(i)
fmt.Println(element.InnerText())
}
utils.Empty(body.Node)
}
Index ¶
- Constants
- type Collection
- type DOMTokenList
- type Document
- func (d Document) All() Collection
- func (d Document) Anchors() Collection
- func (d Document) AppendChild(c *Element)
- func (d Document) Body() *Element
- func (d Document) ChildElementCount() int
- func (d Document) ChildNodes() []*html.Node
- func (d Document) Children() Collection
- func (d Document) CloneNode() *Document
- func (_ Document) CreateElement(tagname string) *Element
- func (_ Document) CreateTextNode(text string) *Element
- func (d Document) DocumentElement() *Element
- func (d Document) FirstChild() *html.Node
- func (d Document) FirstElementChild() *Element
- func (d Document) Form() Collection
- func (d Document) GetByClass(classname string) Collectiondeprecated
- func (d Document) GetById(id string) *Elementdeprecated
- func (d Document) GetByName(name string) Collectiondeprecated
- func (d Document) GetByTag(tagname string) Collectiondeprecated
- func (d Document) GetElementById(id string) *Element
- func (d Document) GetElementsByClassName(classname string) Collection
- func (d Document) GetElementsByName(name string) Collection
- func (d Document) GetElementsByTagName(tagname string) Collection
- func (d Document) HasChildNodes() bool
- func (d Document) Head() *Element
- func (d Document) Images() Collection
- func (d Document) InsertBefore(newChild, oldChild *Element)
- func (d Document) LastChild() *html.Node
- func (d Document) LastElementChild() *Element
- func (d Document) Links() Collection
- func (d Document) NextElementSibling() *Element
- func (d Document) NextSibling() *html.Node
- func (d Document) ParentElement() *Element
- func (d Document) ParentNode() *html.Node
- func (d Document) PreviousElementSibling() *Element
- func (d Document) PreviousSibling() *html.Node
- func (d Document) Query(selector string) *Elementdeprecated
- func (d Document) QueryAll(selector string) Collectiondeprecated
- func (d Document) QuerySelector(s string) *Element
- func (d Document) QuerySelectorAll(s string) Collection
- func (d Document) RemoveChild(c *Element)
- func (d Document) ReplaceChild(newElement, oldElement *Element) *Element
- func (d Document) TextContent(text ...string) string
- func (d Document) Title() string
- type Element
- func (e Element) AppendChild(c *Element)
- func (e Element) Attributes() []html.Attribute
- func (e Element) ChildElementCount() int
- func (e Element) ChildNodes() []*html.Node
- func (e Element) Children() Collection
- func (e Element) ClassList() DOMTokenList
- func (e Element) ClassName() string
- func (e Element) CloneNode() *Element
- func (e Element) FirstChild() *html.Node
- func (e Element) FirstElementChild() *Element
- func (e Element) GetAttribute(key string) string
- func (e Element) GetAttributeNS(namespace, key string) string
- func (e Element) GetAttributeNode(key string) (html.Attribute, bool)
- func (e Element) GetAttributeNodeNS(namespace, key string) (html.Attribute, bool)
- func (e Element) GetByClass(classname string) Collectiondeprecated
- func (e Element) GetById(id string) *Elementdeprecated
- func (e Element) GetByName(name string) Collectiondeprecated
- func (e Element) GetByTag(tagname string) Collectiondeprecated
- func (e Element) GetElementById(id string) *Element
- func (e Element) GetElementsByClassName(classname string) Collection
- func (e Element) GetElementsByName(name string) Collection
- func (e Element) GetElementsByTagName(tagname string) Collection
- func (e Element) HasAttribute(key string) bool
- func (e Element) HasAttributeNS(namespace, key string) bool
- func (e Element) HasAttributes() bool
- func (e Element) HasChildNodes() bool
- func (e Element) Id() string
- func (e Element) InnerHTML(text ...string) string
- func (e Element) InnerText(text ...string) string
- func (e Element) InsertAdjacentElement(p Position, newElement *Element) error
- func (e Element) InsertAdjacentHTML(p Position, texthtml string) error
- func (e Element) InsertAdjacentText(p Position, text string) error
- func (e Element) InsertBefore(newChild, oldChild *Element)
- func (e Element) LastChild() *html.Node
- func (e Element) LastElementChild() *Element
- func (e Element) LocalName() string
- func (e Element) NextElementSibling() *Element
- func (e Element) NextSibling() *html.Node
- func (e Element) OuterHTML() string
- func (e Element) ParentElement() *Element
- func (e Element) ParentNode() *html.Node
- func (e Element) PreviousElementSibling() *Element
- func (e Element) PreviousSibling() *html.Node
- func (e Element) Query(selector string) *Elementdeprecated
- func (e Element) QueryAll(selector string) Collectiondeprecated
- func (e Element) QuerySelector(s string) *Element
- func (e Element) QuerySelectorAll(s string) Collection
- func (e Element) Remove()
- func (e Element) RemoveAttribute(key string)
- func (e Element) RemoveAttributeNS(namespace, key string)
- func (e Element) RemoveAttributeNode(a html.Attribute)
- func (e Element) RemoveChild(c *Element)
- func (e Element) ReplaceChild(newElement, oldElement *Element) *Element
- func (e Element) SetAttribute(key string, value string)
- func (e Element) SetAttributeNS(namespace, key, value string)
- func (e Element) SetAttributeNode(a html.Attribute)
- func (e Element) SetAttributeNodeNS(a html.Attribute)
- func (e Element) TagName() string
- func (e Element) TextContent(text ...string) string
- type ForEachFunc
- type Position
Examples ¶
Constants ¶
const ( Beforebegin = Position(utils.Beforebegin) Afterbegin = Position(utils.Afterbegin) Beforeend = Position(utils.Beforeend) Afterend = Position(utils.Afterend) )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Collection ¶
Collection
func (Collection) Enumerator ¶
func (e Collection) Enumerator() chan *Element
Enumerator can calls with for..range for element := range elements.Enumerator()...
func (Collection) ForEach ¶
func (e Collection) ForEach(fn ForEachFunc)
func (Collection) Get ¶
func (e Collection) Get(index int) *Element
Get returns the "*Element" given index
type DOMTokenList ¶
DOMTokenList
type Document ¶
type Document Element
func Parse ¶
Parse form io.Reader
Example (Bytes) ¶
This example shows how to use parse from bytes
package main
import (
"bytes"
"fmt"
"log"
"github.com/saihon/gohtml"
)
func main() {
text := []byte(`<html><head></head><body></body></html>`)
document, err := gohtml.Parse(bytes.NewReader(text))
if err != nil {
log.Fatal(err)
}
fmt.Println(document.Title())
}
Example (File) ¶
This example shows how to use parse from file
package main
import (
"fmt"
"log"
"os"
"github.com/saihon/gohtml"
)
func main() {
fp, err := os.Open("index.html")
if err != nil {
log.Fatal(err)
}
defer fp.Close()
document, err := gohtml.Parse(fp)
if err != nil {
log.Fatal(err)
}
fmt.Println(document.Title())
}
Example (Httpresponse) ¶
This example shows how to use parse from http response
package main
import (
"fmt"
"log"
"net/http"
"github.com/saihon/gohtml"
)
func main() {
resp, err := http.Get("https://example.com")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
document, err := gohtml.Parse(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(document.Title())
}
Example (String) ¶
This example shows how to use parse from string
package main
import (
"fmt"
"log"
"strings"
"github.com/saihon/gohtml"
)
func main() {
text := `<html><head></head><body></body></html>`
document, err := gohtml.Parse(strings.NewReader(text))
if err != nil {
log.Fatal(err)
}
fmt.Println(document.Title())
}
func (Document) All ¶
func (d Document) All() Collection
All returns all elements of node type ElementNode
func (Document) Anchors ¶
func (d Document) Anchors() Collection
Anchors returns all of <a> element these have "name" attribute
func (Document) AppendChild ¶
AppendChild append "*Element" as a last child
func (Document) ChildElementCount ¶
ChildElementCount returns the number of html.ElementNode
func (Document) ChildNodes ¶
ChildNodes returns all of child nodes
func (Document) Children ¶
func (d Document) Children() Collection
Children returns all of the child html.ElementNode as the "Collection"
func (Document) CreateElement ¶
CreateElement can be called from the "Document" has no meaning and is the same the above
func (Document) CreateTextNode ¶
CreateTextNode same the above
func (Document) DocumentElement ¶
DocumentElement returns <html> element
func (Document) FirstChild ¶
FirstChild returns first child node
func (Document) FirstElementChild ¶
FirstElementChild returns first html.ElementNode as the "*Element"
func (Document) GetByClass
deprecated
func (d Document) GetByClass(classname string) Collection
Deprecated: GetByClass alias `GetElementsByClassName'
func (Document) GetByName
deprecated
func (d Document) GetByName(name string) Collection
Deprecated: GetByName alias `GetElementsByName'
func (Document) GetByTag
deprecated
func (d Document) GetByTag(tagname string) Collection
Deprecated: GetByTag alias `GetElementsByTagName'
func (Document) GetElementById ¶
GetElementById find the element have specified id
func (Document) GetElementsByClassName ¶
func (d Document) GetElementsByClassName(classname string) Collection
GetElementsByClassName find the all elements have specified classname
func (Document) GetElementsByName ¶
func (d Document) GetElementsByName(name string) Collection
GetElementsByName find the all elements have specified name
func (Document) GetElementsByTagName ¶
func (d Document) GetElementsByTagName(tagname string) Collection
GetElementsByTagName find the all elements have specified tagname
func (Document) HasChildNodes ¶
HasChildNodes returns true if "Document" has node
func (Document) InsertBefore ¶
InsertBefore inserts a newElement before the oldElement as a child of a "Document".
func (Document) LastElementChild ¶
LastElementChild returns the last child html.ElementNode as the "*Element"
func (Document) Links ¶
func (d Document) Links() Collection
Links returns all of <a> and <area> element these have "href" attribute
func (Document) NextElementSibling ¶
NextElementSibling - returns nil!!
func (Document) NextSibling ¶
NextSibling - returns nil!!
func (Document) ParentElement ¶
ParentElement - returns nil!!
func (Document) PreviousElementSibling ¶
PreviousElementSibling - returns nil!!
func (Document) PreviousSibling ¶
PreviousSibling - returns nil!!
func (Document) QueryAll
deprecated
func (d Document) QueryAll(selector string) Collection
Deprecated: QueryAll alias `QuerySelectorAll'
func (Document) QuerySelector ¶
QuerySelector find the first element have specified css selector
func (Document) QuerySelectorAll ¶
func (d Document) QuerySelectorAll(s string) Collection
QuerySelectorAll find the all elements have specified css selector
func (Document) RemoveChild ¶
RemoveChild remove a given the "*Element" specified "*Element" is must be the child of "Document"
func (Document) ReplaceChild ¶
ReplaceChild replace oldElement to newElement given "*Element" is both the must be "Document" child, and same node type
func (Document) TextContent ¶
TextContent - returns nil!!
type Element ¶
Element
func CreateElement ¶
CreateElement create the html.ElementNode with specified tag name and then return as the "*Element"
func CreateTextNode ¶
CreateTextNode create the html.TextNode with specified text and returns the "*Element"
func (Element) AppendChild ¶
AppendChild append "*Element" as last child
func (Element) Attributes ¶
Attributes returns all attributes on the element
func (Element) ChildElementCount ¶
ChildElementCount returns the number of html.ElementNode
func (Element) ChildNodes ¶
ChildNodes returns all of child nodes
func (Element) Children ¶
func (e Element) Children() Collection
Children returns all of child html.ElementNode as "Collection"
func (Element) ClassName ¶
ClassName returns class value of attribute or if element not has class empty string
func (Element) FirstChild ¶
FirstChild returns first child node
func (Element) FirstElementChild ¶
FirstElementChild returns first html.ElementNode as "*Element"
func (Element) GetAttribute ¶
GetAttribute returns the value of a specified attribute
func (Element) GetAttributeNS ¶
GetAttributeNS returns the value of the attribute with the specified namespace and key
func (Element) GetAttributeNode ¶
GetAttributeNode returns the attribute, and the bool value indicating whether it exists with the specified key
func (Element) GetAttributeNodeNS ¶
GetAttributeNodeNS returns the attribute, and the bool value indicating whether it exists with the specified namespace and key
func (Element) GetByClass
deprecated
func (e Element) GetByClass(classname string) Collection
Deprecated: GetByClass alias `GetElementsByClassName'
func (Element) GetByName
deprecated
func (e Element) GetByName(name string) Collection
Deprecated: GetByName alias `GetElementsByName'
func (Element) GetByTag
deprecated
func (e Element) GetByTag(tagname string) Collection
Deprecated: GetByTag alias `GetElementsByTagName'
func (Element) GetElementById ¶
GetElementById returns find an element has given id
func (Element) GetElementsByClassName ¶
func (e Element) GetElementsByClassName(classname string) Collection
GetElementsByClassName returns find all elements has given classname
func (Element) GetElementsByName ¶
func (e Element) GetElementsByName(name string) Collection
GetElementsByName returns find all elements has given name
func (Element) GetElementsByTagName ¶
func (e Element) GetElementsByTagName(tagname string) Collection
GetElementsByTagName returns find all elements have given tagname
func (Element) HasAttribute ¶
HasAttribute returns the bool value indicating whether element has an attribute with specified key
func (Element) HasAttributeNS ¶
HasAttributeNS
func (Element) HasAttributes ¶
HasAttributes returns the bool value indicating whether element has attributes
func (Element) HasChildNodes ¶
HasChildNodes returns true if "Document" has node
func (Element) InsertAdjacentElement ¶
InsertAdjacentElement inserts element to specified position
func (Element) InsertAdjacentHTML ¶
InsertAdjacentHTML inserts text HTML as the html.ElementNode to specified position
func (Element) InsertAdjacentText ¶
InsertAdjacentText inserts text as the html.TextNode to specified position
func (Element) InsertBefore ¶
InsertBefore inserts a newChild before the oldChild as child
func (Element) LastElementChild ¶
LastElementChild returns last html.ElementNode as "*Element"
func (Element) NextElementSibling ¶
NextElementSibling returns next html.ElementNode as "*Element"
func (Element) NextSibling ¶
NextSibling returns next sibling node
func (Element) ParentElement ¶
ParentElement returns parent node as "*Element"
func (Element) PreviousElementSibling ¶
PreviousElementSibling returns previous html.ElementNode as "*Element"
func (Element) PreviousSibling ¶
PreviousSibling returns previous sibling node
func (Element) QueryAll
deprecated
func (e Element) QueryAll(selector string) Collection
Deprecated: QueryAll alias `QuerySelectorAll'
func (Element) QuerySelector ¶
QuerySelector returns find an element have given css selector
func (Element) QuerySelectorAll ¶
func (e Element) QuerySelectorAll(s string) Collection
QuerySelectorAll returns find all elements has given css selector
func (Element) RemoveAttributeNS ¶
RemoveAttributeNS
func (Element) RemoveAttributeNode ¶
RemoveAttributeNode
func (Element) RemoveChild ¶
RemoveChild remove a given "*Element" specified *Element is must be child
func (Element) ReplaceChild ¶
ReplaceChild returns old element. panic if an error
func (Element) SetAttribute ¶
SetAttribute sets the value of an attribute on the element
func (Element) SetAttributeNS ¶
SetAttributeNS sets the value of an attribute with the specified namespace and name
func (Element) SetAttributeNode ¶
SetAttributeNode sets the attribute. if already exist the key, it attribute overridden
func (Element) SetAttributeNodeNS ¶
SetAttributeNodeNS sets the namespaced attribute node on the element
func (Element) TextContent ¶
TextContent set or get text to an element
type ForEachFunc ¶
type ForEachFunc = func(value *Element, index int, collection Collection)