Diff
diff --git a/selector/doc.md b/selector/doc.md
index a5baa23..e0ce53c 100644
--- a/selector/doc.md
+++ b/selector/doc.md
@@ -13,2 +13,2 @@ flowchart LR;
- False-->Children;
- True-->QuerySelector;
+ False-->QuerySelector;
+ True-->EOL;
@@ -19,2 +18,0 @@ flowchart LR;
- Children-->TestSelector;
- Children-->EOL;
@@ -39 +37,9 @@ flowchart LR;
-## QuerySelector?(go)
+> func (n *Node) QuerySelector(selectString string) *Node {
+
+## QuerySelector
+
+| Arguments | Description |
+| --------------------- | ------------------------------- |
+| n \*element.Node | Target \*element.Node |
+| selectString string | CSS querySelector string |
+| return \*element.Node | element.Node matching the query |
@@ -45 +51 @@ flowchart LR;
-To start out, we check the current element to see if the `selectString` matches the element.Node (n) we called the method on using the [`TestSelector`](./#testselectorgo) function. If it does we can end the function there and return itself. If it does not we can continue and check its children. We do this process recursively to simplify the code.
+To start out, we check the current element to see if the `selectString` matches the `element.Node` we called the method on using the [`TestSelector`](./#testselector) function. If it does we can end the function there and return itself. If it does not we can continue and check its children. We do this process recursively to simplify the code.
@@ -51 +57,9 @@ We also do a check to see if the `element.Node.Properties.Id` has been assigned.
-## QuerySelectorAll?(go)
+> func (n \*Node) QuerySelectorAll(selectString string) \*[]\*Node {
+
+## QuerySelectorAll
+
+| Arguments | Description |
+| --------------------- | ------------------------------- |
+| n \*element.Node | Target \*element.Node |
+| selectString string | CSS querySelector string |
+| return \*element.Node | element.Node matching the query |
@@ -53 +67 @@ We also do a check to see if the `element.Node.Properties.Id` has been assigned.
-See [QuerySelector](./#queryselectorgo). `QuerySelectorAll` works the exact same as `QuerySelector` with an added collector (`results`) to collect all elements that match the selector throughout the recusive execution.
+See [QuerySelector](./#queryselector). `QuerySelectorAll` works the exact same as `QuerySelector` with an added collector (`results`) to collect all elements that match the selector throughout the recusive execution.
@@ -55 +69 @@ See [QuerySelector](./#queryselectorgo). `QuerySelectorAll` works the exact same
-## TestSelector?(go)
+> func TestSelector(selectString string, n \*Node) bool {
@@ -57 +71,9 @@ See [QuerySelector](./#queryselectorgo). `QuerySelectorAll` works the exact same
-`TestSelector` is the foundation of the [`QuerySelector`](./#queryselectorgo) and [`QuerySelectorAll`](./#queryselectorallgo) as seen above.
+## TestSelector
+
+| Arguments | Description |
+| ------------------- | ----------------------------------------------- |
+| selectString string | CSS querySelector string |
+| node \*element.Node | Target \*element.Node |
+| return bool | returns true if the selector matches the string |
+
+`TestSelector` is the foundation of the [`QuerySelector`](./#queryselector) and [`QuerySelectorAll`](./#queryselectorall) as seen above.
@@ -78 +100 @@ Then we need to build the selectors, so we start by creating an array to store t
-Next we use the [`GetCSSSelectors`](./#getcssselectorsgo) method in this package to generate any selectors assigned to the `net/html` Node.
+Next we use the [`GetCSSSelectors`](./#getcssselectors) method in this package to generate any selectors assigned to the `net/html` Node.
@@ -89 +111 @@ Then we add the id to the array to complete the current Nodes selectors.
-After we have the current Nodes selectors we can use the [SplitSelector](./#splitselectorgo) and [Contains](./#containsgo) methods to process the passed query (selectString) and compare the two arrays.
+After we have the current Nodes selectors we can use the [SplitSelector](./#splitselector) and [Contains](./#contains) methods to process the passed query (selectString) and compare the two arrays.
@@ -103 +125,11 @@ If we are not on the last element and the selector matches for this Node then we
-## GetCSSSelectors?(go)
+> func GetCSSSelectors(node \*html.Node, selectors []string) []string {
+
+## GetCSSSelectors
+
+| Arguments | Description |
+| ------------------ | -------------------- |
+| node \*html.Node | Target net/html Node |
+| selectors []string | Previous Selctors |
+| return []string | Output of selectors |
+
+`GetCSSSelectors` purpose is to generate all possible selectors for a `net/html` Node. It is used inside of the element package interally to the [`TestSelector`](./#testselector) function. It does this buy taking the classes, id's, and attributes and creating an array of their string equalivents (.class, #id, and [value="somevalue"]).
@@ -105 +137 @@ If we are not on the last element and the selector matches for this Node then we
-`GetCSSSelectors` purpose is to generate all possible selectors for a `net/html` Node. It is used inside of the element package interally to the [`TestSelector`](./#testselectorgo) function. It does this buy taking the classes, id's, and attributes and creating an array of their string equalivents (.class, #id, and [value="somevalue"]).
+> func SplitSelector(s string) []string {
@@ -107 +139,6 @@ If we are not on the last element and the selector matches for this Node then we
-## SplitSelector?(go)
+## SplitSelector
+
+| Arguments | Description |
+| --------------- | ------------------- |
+| s string | Selector string |
+| return []string | Output of selectors |
@@ -123 +160,9 @@ Result
-## Contains?(go)
+> func Contains(selector []string, node []string) bool {
+
+## Contains
+
+| Arguments | Description |
+| ----------------- | ------------------------------------------- |
+| selector []string | Array of selectors from the target selector |
+| node []string | Array of selectors from the target element |
+| return bool | boolean value |
@@ -127 +172,136 @@ Result
-<{./main.go}>
+```go
+package selector
+
+import (
+ "slices"
+ "strings"
+
+ "golang.org/x/net/html"
+)
+
+func GetCSSSelectors(node *html.Node, selectors []string) []string {
+ if node.Type == html.ElementNode {
+ selectors = append(selectors, node.Data)
+ for _, attr := range node.Attr {
+ if attr.Key == "class" {
+ classes := strings.Split(attr.Val, " ")
+ for _, class := range classes {
+ selectors = append(selectors, "."+class)
+ }
+ } else if attr.Key == "id" {
+ selectors = append(selectors, "#"+attr.Val)
+ } else {
+ selectors = append(selectors, "["+attr.Key+"=\""+attr.Val+"\"]")
+ }
+ }
+ }
+
+ return selectors
+}
+
+func SplitSelector(s string) []string {
+ var result []string
+ var current string
+
+ for _, char := range s {
+ switch char {
+ case '.', '#', '[', ']', ':':
+ if current != "" {
+ if string(char) == "]" {
+ current += string(char)
+ }
+ result = append(result, current)
+ }
+ current = ""
+ if string(char) != "]" {
+ current += string(char)
+ }
+ default:
+ current += string(char)
+ }
+ }
+
+ if current != "" && current != "]" {
+ result = append(result, current)
+ }
+
+ return result
+}
+
+func Contains(selector []string, node []string) bool {
+ has := true
+ for _, s := range selector {
+ if !slices.Contains(node, s) {
+ has = false
+ }
+ }
+ return has
+}
+
+```
+
+```go
+func (n *Node) QuerySelectorAll(selectString string) *[]*Node {
+ results := []*Node{}
+ if TestSelector(selectString, n) {
+ results = append(results, n)
+ }
+
+ for i := range n.Children {
+ el := &n.Children[i]
+ cr := el.QuerySelectorAll(selectString)
+ if len(*cr) > 0 {
+ results = append(results, *cr...)
+ }
+ }
+ return &results
+}
+
+func (n *Node) QuerySelector(selectString string) *Node {
+ if TestSelector(selectString, n) {
+ return n
+ }
+
+ for i := range n.Children {
+ el := &n.Children[i]
+ cr := el.QuerySelector(selectString)
+ if cr.Properties.Id != "" {
+ return cr
+ }
+ }
+
+ return &Node{}
+}
+
+func TestSelector(selectString string, n *Node) bool {
+ parts := strings.Split(selectString, ">")
+
+ s := []string{}
+ if n.Properties.Focusable {
+ if n.Properties.Focused {
+ s = append(s, ":focus")
+ }
+ }
+
+ classes := n.ClassList.Classes
+
+ for _, v := range classes {
+ s = append(s, "."+v)
+ }
+ // fmt.Println(n.Properties.Node)
+ selectors := selector.GetCSSSelectors(n.Properties.Node, s)
+ if n.Id != "" {
+ selectors = append(selectors, "#"+n.Id)
+ }
+
+ part := selector.SplitSelector(strings.TrimSpace(parts[len(parts)-1]))
+
+ has := selector.Contains(part, selectors)
+
+ if len(parts) == 1 || !has {
+ return has
+ } else {
+ return TestSelector(strings.Join(parts[0:len(parts)-1], ">"), n.Parent)
+ }
+}
+```
@@ -129 +309 @@ Result
-<{../element/main.go}>
+