shithub: mycel

Download patch

ref: c7094acb596f25738d6c26a0f1b229303dba7832
parent: ba65f8f37fb20c47857bdabab61916255e12ed26
author: Philip Silva <philip.silva@protonmail.com>
date: Tue Feb 2 14:54:05 EST 2021

more robust query ref

--- a/nodes/nodes.go
+++ b/nodes/nodes.go
@@ -2,7 +2,7 @@
 
 import (
 	"bytes"
-	//"fmt"
+	"fmt"
 	"golang.org/x/net/html"
 	"github.com/chris-ramon/douceur/css"
 	"github.com/psilva261/opossum/logger"
@@ -146,6 +146,7 @@
 	return ""
 }
 
+// QueryRef relative to html > body
 func (n *Node) QueryRef() string {
 	nRef, ok := n.queryRef()
 	if ok && strings.Contains(nRef, "#") {
@@ -168,7 +169,7 @@
 			}
 		}
 	}
-	return strings.TrimSpace(strings.Join(path, " "))
+	return strings.TrimSpace(strings.Join(path, " > "))
 }
 
 func (n *Node) queryRef() (ref string, ok bool) {
@@ -185,23 +186,20 @@
 
 	ref = n.Data()
 
-	var sl []string
-	if c := strings.TrimSpace(n.Attr("class")); c != "" {
-		l := strings.Split(c, " ")
-		sl = make([]string, 0, len(l))
+	if n.Parent == nil {
+		return ref, true
+	}
 
-		for _, cl := range l {
-			if cl == "" {
-				continue
-			}
-
-			sl = append(sl, cl)
+	i := 0
+	for _, c := range n.Parent.Children {
+		if c.Type() == html.ElementNode {
+			i++
 		}
-
-		if len(sl) > 0 {
-			ref += "." + strings.Join(sl, ".")
+		if c == n {
+			break
 		}
 	}
+	ref += fmt.Sprintf(":nth-child(%v)", i)
 
 	return ref, true
 }
--- a/nodes/nodes_test.go
+++ b/nodes/nodes_test.go
@@ -31,7 +31,7 @@
 	nt := NewNodeTree(doc, style.Map{}, make(map[*html.Node]style.Map), nil)
 	p := nt.Children[0].Children[1].Children[1]
 	a := p.Children[5]
-	if q := a.QueryRef(); q != "p a" { t.Fatalf("%v", q) }
+	if q := a.QueryRef(); q != "p:nth-child(1) > a:nth-child(3)" { t.Fatalf("%v", q) }
 }
 
 func TestSetText(t *testing.T) {