Skip to content

Commit c75c22e

Browse files
Merge pull request #14 from udan-jayanith/patches
Some changes
2 parents c404042 + a561eca commit c75c22e

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

benchmarks/benchmark_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,15 @@ func TestFetchPostCovers(t *testing.T){
3030
t.Log(node.GetInnerText())
3131
}
3232
t.Log(time.Since(tim).Seconds())
33+
}
34+
35+
func toNodeTree(url string) *GoHtml.Node{
36+
res, err := http.Get(url)
37+
if err != nil || res.StatusCode != http.StatusOK{
38+
return nil
39+
}
40+
defer res.Body.Close()
41+
42+
rootNode, _ := GoHtml.Decode(res.Body)
43+
return rootNode
3344
}

parser_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,6 @@ func ExampleDecode() {
5252
title = titleNode.GetInnerText()
5353
}
5454
fmt.Println(title)
55-
//Output: User Profile
55+
//Output:
56+
//User Profile
5657
}

querying.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,13 @@ func (node *Node) GetElementsById(idName string) NodeList {
101101
}
102102

103103
/*
104-
QuerySearch search returns a iterator that traverse through the node tree from given node and passes nodes that matches the given selector.
105-
*/
104+
QuerySearch search returns a iterator that traverse through the node tree from given node and passes nodes that matches the given selector.
105+
*/
106106
func QuerySearch(node *Node, selector string) iter.Seq[*Node] {
107107
traverser := NewTraverser(node)
108108
return func(yield func(node *Node) bool) {
109109
selectorTokens := TokenizeSelectorsAndCombinators(selector)
110-
iter := traverser.Walkthrough
111-
for node := range iter {
110+
for node := range traverser.Walkthrough {
112111
if matchFromRightMostSelectors(node, selectorTokens) && !yield(node) {
113112
return
114113
}
@@ -128,7 +127,6 @@ func matchFromRightMostSelectors(node *Node, selectorTokens []CombinatorEl) bool
128127
return node != nil
129128
}
130129

131-
132130
// QuerySelector returns the first node that matches with the selector from the node.
133131
func (node *Node) QuerySelector(selector string) *Node {
134132
iter := QuerySearch(node, selector)
@@ -148,4 +146,3 @@ func (node *Node) QuerySelectorAll(selector string) NodeList {
148146
}
149147
return nodeList
150148
}
151-

selectors.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package GoHtml
22

33
import (
44
"strings"
5+
56
"golang.org/x/net/html"
67
)
78

@@ -13,8 +14,8 @@ const (
1314
Tag
1415
)
1516

16-
//Selector struct represents a single css selector
17-
//Ex: .my-class, #video, div
17+
// Selector struct represents a single css selector
18+
// Ex: .my-class, #video, div
1819
type Selector struct {
1920
selector string
2021
selectorName string
@@ -42,8 +43,8 @@ func matchNode(node *Node, basicSelectorName string, basicSelectorType BasicSele
4243
return false
4344
}
4445

45-
//NewSelector takes a single css selector and returns a Selector struct.
46-
//Selector string should be only of basic selector.
46+
// NewSelector takes a single css selector and returns a Selector struct.
47+
// Selector string should be only of basic selector.
4748
func NewSelector(selector string) Selector {
4849
selector = strings.TrimSpace(html.EscapeString(selector))
4950
selectorStruct := Selector{}
@@ -60,7 +61,7 @@ func NewSelector(selector string) Selector {
6061
selectorStruct.selectorType = Tag
6162
}
6263

63-
selectorStruct.selector = strings.ToLower(selector)
64+
//selectorStruct.selector = strings.ToLower(selector)
6465
if selectorStruct.selectorType != Tag {
6566
selectorStruct.selectorName = selector[1:]
6667
} else {
@@ -80,26 +81,26 @@ const (
8081
NoneCombinator
8182
)
8283

83-
//CombinatorEl is used to represent selectors that are around a combinator.
84+
// CombinatorEl is used to represent selectors that are around a combinator.
8485
type CombinatorEl struct {
8586
Type Combinator
8687
Selector1 Selector
8788
Selector2 Selector
8889
}
8990

90-
//This takes a selector or combinators and selectors and then returns a slice of CombinatorEl.
91+
// This takes a selector or combinators and selectors and then returns a slice of CombinatorEl.
9192
func TokenizeSelectorsAndCombinators(selector string) []CombinatorEl {
9293
iter := func(yield func(string) bool) {
9394
currentStr := ""
9495
for _, char := range selector {
9596
switch char {
9697
case ' ', '>', '+', '~':
97-
if !yield(currentStr) || !yield(string(char)){
98+
if !yield(currentStr) || !yield(string(char)) {
9899
return
99100
}
100101
currentStr = ""
101102
default:
102-
currentStr+=string(char)
103+
currentStr += string(char)
103104
}
104105
}
105106
yield(currentStr)
@@ -167,6 +168,7 @@ func (ce *CombinatorEl) getDescended(node *Node) *Node {
167168
if matchNode(parentNode, ce.Selector1.selectorName, ce.Selector1.selectorType) {
168169
return parentNode
169170
}
171+
170172
parentNode = parentNode.GetParent()
171173
}
172174
return nil

0 commit comments

Comments
 (0)