Skip to content

Commit 599645e

Browse files
committed
Updated the documentation and add example codes
1 parent f764a27 commit 599645e

File tree

11 files changed

+166
-22
lines changed

11 files changed

+166
-22
lines changed

FUTURE-CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
## v0.0.3
22
- Write test for (closest, QuerySelector, QuerySelectorAll)
33
- Serializer bug fix
4+
5+
## TODO
6+
* Document Combinator and CombinatorEl struct.
7+
* Document TokenizeSelectorsAndCombinators and show a example for it.
8+
* Document Tokenizers and Selectors
9+
* Update the the readme example and change the CHANGELOG
10+

classList.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (classList ClassList) Encode() string {
6868
return classes
6969
}
7070

71-
// EncodeTo encode className for the node.
71+
// EncodeTo encodes classNames for the node.
7272
// If node is nil EncodeTo does nothing.
7373
func (classList ClassList) EncodeTo(node *Node){
7474
if node == nil {

classList_test.go

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,63 @@
11
package GoHtml_test
22

3-
import(
3+
import (
4+
"fmt"
45
"testing"
5-
"github.com/udan-jayanith/GoHTML"
6+
7+
GoHtml "github.com/udan-jayanith/GoHTML"
68
)
79

8-
func TestClasses(t *testing.T){
10+
func TestClasses(t *testing.T) {
911
node := GoHtml.CreateNode("div")
1012
node.SetAttribute("class", "div-container main")
1113

1214
classList := GoHtml.NewClassList()
1315
classList.DecodeFrom(node)
14-
if !classList.Contains("main"){
16+
if !classList.Contains("main") {
1517
t.Fatal("")
1618
return
1719
}
1820
classList.DeleteClass("main")
19-
if classList.Contains("main"){
21+
if classList.Contains("main") {
2022
t.Fatal("")
2123
return
2224
}
2325

2426
classList.AppendClass("main-div")
25-
if !classList.Contains("main-div"){
27+
if !classList.Contains("main-div") {
2628
t.Fatal("")
2729
return
2830
}
2931

3032
classList.EncodeTo(node)
33+
}
34+
35+
func ExampleClassList_Contains() {
36+
//Creates a div that has classes video-container and main-contents
37+
div := GoHtml.CreateNode("div")
38+
div.SetAttribute("class", "video-container main-contents")
39+
40+
classList := GoHtml.NewClassList()
41+
//Add the classes in the div to the class list
42+
classList.DecodeFrom(div)
43+
44+
//Checks wether the following classes exists in the classList
45+
fmt.Println(classList.Contains("container"))
46+
fmt.Println(classList.Contains("video-container"))
47+
48+
//Output:
49+
//false
50+
//true
51+
}
52+
53+
func ExampleClassList_Encode(){
54+
classList := GoHtml.NewClassList()
55+
56+
//Add classes to the class list
57+
classList.AppendClass("container")
58+
classList.AppendClass("warper")
59+
classList.AppendClass("main-content")
60+
61+
//This would output something like this "warper container main-content". Order of the output is not guaranteed.
62+
fmt.Println(classList.Encode())
3163
}

node-list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"iter"
66
)
77

8-
//NodeList can store nodes by appended order.
8+
//NodeList can store nodes by appended order and can iterate over the node list by invoking IterNodeList method.
99
type NodeList struct {
1010
list *list.List
1111
currentEl *list.Element

node-list_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package GoHtml_test
22

33
import (
4+
"fmt"
45
"os"
56
"testing"
67

@@ -40,4 +41,20 @@ func TestIterNodeList2(t *testing.T){
4041
for node := range iter{
4142
t.Log(node)
4243
}
44+
}
45+
46+
func ExampleNodeList(){
47+
nodeList := GoHtml.NewNodeList()
48+
nodeList.Append(GoHtml.CreateNode("br"))
49+
nodeList.Append(GoHtml.CreateNode("hr"))
50+
nodeList.Append(GoHtml.CreateNode("div"))
51+
52+
iter := nodeList.IterNodeList()
53+
for node := range iter{
54+
fmt.Println(node.GetTagName())
55+
}
56+
//Output:
57+
//br
58+
//hr
59+
//div
4360
}

node-tree.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (node *Node) SetPreviousNode(previousNode *Node) {
3939
node.previousNode = previousNode
4040
}
4141

42-
// GetChildNode returns the first child elements of this node.
42+
// GetChildNode returns the first child node of this node.
4343
func (node *Node) GetChildNode() *Node {
4444
return node.childNode
4545
}

parser.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ package GoHtml
33
import (
44
"io"
55
"strings"
6+
67
"golang.org/x/net/html"
78
)
89

9-
1010
// Decode reads from rd and create a node-tree. Then returns the root node and nil.
1111
func Decode(r io.Reader) (*Node, error) {
1212
t := NewTokenizer(r)
1313
nodeTreeBuilder := NewNodeTreeBuilder()
1414
for {
1515
tt := t.Advanced()
16-
if tt == html.ErrorToken{
16+
if tt == html.ErrorToken {
1717
break
1818
}
1919

@@ -30,4 +30,3 @@ func HTMLToNodeTree(html string) (*Node, error) {
3030
return node, err
3131
}
3232

33-

parser_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package GoHtml_test
22

33
import (
4+
"fmt"
45
"os"
56
"strings"
67
"testing"
@@ -25,3 +26,31 @@ func TestDecode(t *testing.T) {
2526
var builder strings.Builder
2627
GoHtml.Encode(&builder, node)
2728
}
29+
30+
func ExampleDecode() {
31+
r := strings.NewReader(`
32+
<!DOCTYPE html>
33+
<html lang="en">
34+
<head>
35+
<meta charset="UTF-8">
36+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
37+
<title>User Profile</title>
38+
</head>
39+
<body>
40+
<h1 class="username">Udan</h1>
41+
<p class="email">udanjayanith@gmail.com</p>
42+
<p>Joined: 01/08/2024</p>
43+
</body>
44+
</html>
45+
`)
46+
47+
rootNode, _ := GoHtml.Decode(r)
48+
49+
titleNode := rootNode.QuerySelector("title")
50+
title := ""
51+
if titleNode != nil {
52+
title = titleNode.GetInnerText()
53+
}
54+
fmt.Println(title)
55+
//Output: User Profile
56+
}

querying_test.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package GoHtml_test
22

33
import (
4+
"fmt"
5+
"net/http"
46
"os"
57
"testing"
68

@@ -165,14 +167,13 @@ func TestQuerySelector(t *testing.T) {
165167
}
166168
//TODO: write test for testcases below.
167169
/*
168-
t.Log(rootNode.QuerySelector("body p"))
169-
t.Log(rootNode.QuerySelector("html head > title"))
170-
t.Log(rootNode.QuerySelector("section+ul"))
171-
t.Log(rootNode.QuerySelector(".item~.last-item"))
170+
t.Log(rootNode.QuerySelector("body p"))
171+
t.Log(rootNode.QuerySelector("html head > title"))
172+
t.Log(rootNode.QuerySelector("section+ul"))
173+
t.Log(rootNode.QuerySelector(".item~.last-item"))
172174
*/
173175
}
174176

175-
176177
func TestQuerySelectorAll(t *testing.T) {
177178
rootNode, err := testFile5NodeTree()
178179
if err != nil {
@@ -200,3 +201,38 @@ func TestQuerySelectorAll(t *testing.T) {
200201
}
201202

202203
}
204+
205+
func ExampleNode_QuerySelector() {
206+
res, err := http.Get("https://example.com/")
207+
if err != nil || res.StatusCode != http.StatusOK {
208+
return
209+
}
210+
defer res.Body.Close()
211+
212+
rootNode, _ := GoHtml.Decode(res.Body)
213+
res.Body.Close()
214+
215+
title := rootNode.QuerySelector("title")
216+
if title != nil {
217+
fmt.Println(title.GetInnerText())
218+
//Example Domain
219+
}
220+
}
221+
222+
func ExampleQuerySearch() {
223+
//Request the html
224+
res, err := http.Get("https://example.com/")
225+
if err != nil || res.StatusCode != http.StatusOK {
226+
return
227+
}
228+
defer res.Body.Close()
229+
230+
//Decode the html
231+
rootNode, _ := GoHtml.Decode(res.Body)
232+
233+
//Iterate over every node that matches the query.
234+
for node := range GoHtml.QuerySearch(rootNode, ".event-columns .column .event-block h4 a") {
235+
//Convert the node and it's children nodes to text html and print it.
236+
fmt.Println(GoHtml.NodeTreeToHTML(node))
237+
}
238+
}

tokenizer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (t *Tokenizer) Advanced() html.TokenType {
2626
}
2727

2828
// CurrentNode returns the current node.
29-
// Returned value can be nil regardless of tt.
29+
// Returned value can be nil regardless of token type.
3030
func (t *Tokenizer) GetCurrentNode() *Node {
3131
currentToken := t.z.Token()
3232
if strings.TrimSpace(currentToken.Data) == "" {

0 commit comments

Comments
 (0)