aboutsummaryrefslogtreecommitdiff
path: root/backend/internal/ibd/html_helpers.go
diff options
context:
space:
mode:
authorGravatar Anshul Gupta <ansg191@anshulg.com> 2024-08-11 13:15:50 -0700
committerGravatar Anshul Gupta <ansg191@anshulg.com> 2024-08-11 13:15:50 -0700
commit6a3c21fb0b1c126849f2bbff494403bbe901448e (patch)
tree5d7805524357c2c8a9819c39d2051a4e3633a1d5 /backend/internal/ibd/html_helpers.go
parent29c6040a51616e9e4cf6c70ee16391b2a3b238c9 (diff)
parentf34b92ded11b07f78575ac62c260a380c468e5ea (diff)
downloadibd-trader-6a3c21fb0b1c126849f2bbff494403bbe901448e.tar.gz
ibd-trader-6a3c21fb0b1c126849f2bbff494403bbe901448e.tar.zst
ibd-trader-6a3c21fb0b1c126849f2bbff494403bbe901448e.zip
Merge remote-tracking branch 'backend/main'
Diffstat (limited to 'backend/internal/ibd/html_helpers.go')
-rw-r--r--backend/internal/ibd/html_helpers.go99
1 files changed, 99 insertions, 0 deletions
diff --git a/backend/internal/ibd/html_helpers.go b/backend/internal/ibd/html_helpers.go
new file mode 100644
index 0000000..0176bc5
--- /dev/null
+++ b/backend/internal/ibd/html_helpers.go
@@ -0,0 +1,99 @@
+package ibd
+
+import (
+ "strings"
+
+ "golang.org/x/net/html"
+)
+
+func findChildren(node *html.Node, f func(node *html.Node) bool) (found []*html.Node) {
+ for c := node.FirstChild; c != nil; c = c.NextSibling {
+ if f(c) {
+ found = append(found, c)
+ }
+ }
+ return
+}
+
+func findChildrenRecursive(node *html.Node, f func(node *html.Node) bool) (found []*html.Node) {
+ if f(node) {
+ found = append(found, node)
+ }
+
+ for c := node.FirstChild; c != nil; c = c.NextSibling {
+ found = append(found, findChildrenRecursive(c, f)...)
+ }
+
+ return
+}
+
+func findClass(node *html.Node, className string) (found *html.Node) {
+ if isClass(node, className) {
+ return node
+ }
+
+ for c := node.FirstChild; c != nil; c = c.NextSibling {
+ if found = findClass(c, className); found != nil {
+ return
+ }
+ }
+
+ return
+}
+
+func isClass(node *html.Node, className string) bool {
+ if node.Type == html.ElementNode {
+ for _, attr := range node.Attr {
+ if attr.Key != "class" {
+ continue
+ }
+ classes := strings.Fields(attr.Val)
+ for _, class := range classes {
+ if class == className {
+ return true
+ }
+ }
+ }
+ }
+ return false
+}
+
+func extractText(node *html.Node) string {
+ var result strings.Builder
+ extractTextInner(node, &result)
+ return result.String()
+}
+
+func extractTextInner(node *html.Node, result *strings.Builder) {
+ if node.Type == html.TextNode {
+ result.WriteString(node.Data)
+ }
+ for c := node.FirstChild; c != nil; c = c.NextSibling {
+ extractTextInner(c, result)
+ }
+}
+
+func findId(node *html.Node, id string) (found *html.Node) {
+ if isId(node, id) {
+ return node
+ }
+
+ for c := node.FirstChild; c != nil; c = c.NextSibling {
+ if found = findId(c, id); found != nil {
+ return
+ }
+ }
+
+ return
+}
+
+func isId(node *html.Node, id string) bool {
+ if node.Type == html.ElementNode {
+ for _, attr := range node.Attr {
+ if attr.Key == "id" && attr.Val == id {
+ return true
+ }
+ }
+ }
+ return false
+}