Skip to content

Commit ee66653

Browse files
committed
Add QuerySelectorAll and redocumented the library. QuerySearch, QuerySelector and QuerySelectorAll still not tested and have tests to write.
1 parent 771f70c commit ee66653

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"cSpell.words": [
33
"autoplay",
4+
"Combinators",
45
"DOCTYPEDTD",
56
"emirpasic",
67
"gohtml",

node-tree.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,16 @@ func (node *Node) IsTextNode() bool {
205205
return node.GetTagName() == ""
206206
}
207207

208-
// Closest traverses the node tree and its parents (heading toward the root node) until it finds a node that matches the specified query.
208+
// Closest traverses the node tree and its parents (heading toward the root node) until it finds a node that matches the full query.
209+
/*
210+
Ex:
211+
query = "div .video-container #div"
212+
213+
<div class="video-container id="div"></div>
214+
215+
As shown elements with every described specifiers will only match.
216+
But this is not the case for QuerySearch, QuerySelector and QuerySelectorAll.
217+
*/
209218
// Adapted from [https://developer.mozilla.org/en-US/docs/Web/API/Element/closest](MDN Element: closest() method)
210219
func (node *Node) Closest(query string) *Node {
211220
queryTokens := TokenizeQuery(query)

querying.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ func (node *Node) QueryAll(query string) NodeList {
132132
return nodeList
133133
}
134134

135+
/*
136+
QuerySearch tokenizes the query string and search for nodes that matches with the right most query token. After matching right most query it proceeds to match nodes parents nodes for left over tokens and then passed that node to (yield/range). QuerySearch search the whole node tree for matches unless yield get canceled or range iterator get cancel.
137+
*/
135138
func QuerySearch(node *Node, query string) iter.Seq[*Node] {
136139
return func(yield func(node *Node) bool) {
137140
queryTokens := TokenizeQuery(query)
@@ -232,10 +235,22 @@ outer:
232235
return i
233236
}
234237

238+
//QuerySelector only returns the first node that matches with the QuerySearch.
235239
func (node *Node) QuerySelector(query string) *Node {
236240
iter := QuerySearch(node, query)
237241
for node := range iter{
238242
return node
239243
}
240244
return nil
241245
}
246+
247+
//QuerySelectorAll stores nodes passed down by QuerySearch in a nodeList and returns the nodeList.
248+
func (node *Node) QuerySelectorAll(query string) NodeList{
249+
iter := QuerySearch(node, query)
250+
nodeList := NewNodeList()
251+
252+
for node := range iter{
253+
nodeList.Append(node)
254+
}
255+
return nodeList
256+
}

tokenizer.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,12 @@ type QueryToken struct {
136136
Selector string
137137
}
138138

139-
// TokenizeQuery tokenizes the query and returns a list of QueryToken.
139+
/*
140+
TokenizeQuery tokenizes the query and returns a list of QueryToken.
141+
142+
query should be of only consists of class, tag and/or id. This applies to every function that accepts a parameter name query.
143+
query should not consists of css selectors, Combinators and separators.
144+
*/
140145
func TokenizeQuery(query string) []QueryToken {
141146
slice := make([]QueryToken, 0, 1)
142147
if strings.TrimSpace(query) == "" {

0 commit comments

Comments
 (0)