shithub: mycel

Download patch

ref: 71869fcb56f7cb931e414c11be16165d22174d81
parent: 89d4bc312ff9a8639a4bf912f36e1a48e8a4e113
author: Philip Silva <philip.silva@protonmail.com>
date: Tue Oct 12 13:58:33 EDT 2021

path in inspect and more consistent paths

- ignore empty text nodes

--- a/browser/browser.go
+++ b/browser/browser.go
@@ -653,8 +653,8 @@
 		if el == nil {
 			log.Infof("inspect nil element")
 		} else {
-			log.Infof("%v:", el.n.Data())
-			log.Infof("inspect el %+v %+v %+v", el, el.n, el.UI)
+			p, _ := el.n.Path()
+			log.Infof("%v", p)
 		}
 	}
 
--- a/browser/browser_test.go
+++ b/browser/browser_test.go
@@ -298,16 +298,16 @@
 
 	// 1. nodes are row-like
 	outerSpan := nt.Find("span")
-	if outerSpan.Attr("id") != "outer" || len(outerSpan.Children) != 7 || outerSpan.IsFlex() {
+	if outerSpan.Attr("id") != "outer" || len(outerSpan.Children) != 3 || outerSpan.IsFlex() {
 		t.Errorf("node: %+v", outerSpan)
 	}
 	bracket := outerSpan.Children[0]
-	if /*bracket.Data() != "(" || */ !bracket.IsInline() {
+	if bracket.Data() != "span" || !bracket.IsInline() {
 		t.Errorf("bracket, is inline: %v %+v %+v", bracket.IsInline(), bracket, bracket.Data())
 	}
-	sp1 := outerSpan.Children[1]
-	if sp1.Data() != "span" || !sp1.IsInline() {
-		t.Errorf("sp1, is inline: %v, %+v %+v", sp1.IsInline(), sp1, sp1.Data())
+	a := outerSpan.Children[1]
+	if a.Data() != "a" || !a.IsInline() {
+		t.Errorf("sp1, is inline: %v, %+v %+v", a.IsInline(), a, a.Data())
 	}
 
 	// 2. Elements are row-like
--- a/browser/fs/fs_unix.go
+++ b/browser/fs/fs_unix.go
@@ -1,5 +1,4 @@
 //go:build !plan9
-// +build !plan9
 
 package fs
 
--- a/nodes/experimental.go
+++ b/nodes/experimental.go
@@ -6,6 +6,41 @@
 	"golang.org/x/net/html"
 )
 
+// Path relative to body
+func (n *Node) Path() (p string, ok bool) {
+	p, ok = n.path()
+	if ok {
+		p = PathPrefix+p
+	}
+	return
+}
+
+func (n *Node) path() (p string, ok bool) {
+	var i int
+	var c *Node
+
+	if n.DomSubtree == nil || n.Type() != html.ElementNode {
+		return
+	}
+	if n.parent == nil {
+		return "/", true
+	}
+	for i, c = range n.parent.Children {
+		if c == n {
+			break
+		}
+		if c.Type() == html.ElementNode {
+			i++
+		}
+	}
+	p += fmt.Sprintf("/%v", i)
+	q, ok := n.parent.path()
+	if ok {
+		p = q + p
+	}
+	return p, true
+}
+
 func (n *Node) Query(s string) (ns []*Node, err error) {
 	cs, err := cascadia.Compile(s)
 	if err != nil {
--- /dev/null
+++ b/nodes/experimental_plan9.go
@@ -1,0 +1,3 @@
+package nodes
+
+const PathPrefix = "/mnt/opossum"
--- a/nodes/experimental_test.go
+++ b/nodes/experimental_test.go
@@ -5,7 +5,32 @@
 	"golang.org/x/net/html"
 	"strings"
 	"testing"
+"fmt"
 )
+
+func TestPath(t *testing.T) {
+	buf := strings.NewReader(`
+	<html>
+		<body>
+			<p>
+				<b>bold stuff</b>
+				<i>italic stuff</i>
+				<a>link</a>
+			</p>
+		</body>
+	</html>`)
+	doc, err := html.Parse(buf)
+	if err != nil {
+		t.Fatalf(err.Error())
+	}
+	nt := NewNodeTree(doc, style.Map{}, make(map[*html.Node]style.Map), nil)
+	p := nt.Children[0].Children[1].Children[0]
+	a := p.Children[2]
+	fmt.Printf("%v\n", a.Data())
+	if p, _ := a.Path(); p != PathPrefix+"/0/1/0/2" {
+		t.Fatalf("%v", p)
+	}
+}
 
 func TestQuery(t *testing.T) {
 	buf := strings.NewReader(`
--- /dev/null
+++ b/nodes/experimental_unix.go
@@ -1,0 +1,5 @@
+//go:build !plan9
+
+package nodes
+
+const PathPrefix = "opossum"
--- a/nodes/nodes.go
+++ b/nodes/nodes.go
@@ -59,11 +59,12 @@
 	}
 	i := 0
 	for c := doc.FirstChild; c != nil; c = c.NextSibling {
-		if c.Type != html.CommentNode {
-			cnt := NewNodeTree(c, ncs, nodeMap, n)
-			n.Children = append(n.Children, cnt)
-			i++
+		if c.Type == html.CommentNode || (c.Type == html.TextNode && strings.TrimSpace(c.Data) == "") {
+			continue
 		}
+		cnt := NewNodeTree(c, ncs, nodeMap, n)
+		n.Children = append(n.Children, cnt)
+		i++
 	}
 	n.Map.DomTree = n
 
--- a/nodes/nodes_test.go
+++ b/nodes/nodes_test.go
@@ -33,8 +33,8 @@
 		t.Fatalf(err.Error())
 	}
 	nt := NewNodeTree(doc, style.Map{}, make(map[*html.Node]style.Map), nil)
-	p := nt.Children[0].Children[1].Children[1]
-	a := p.Children[5]
+	p := nt.Children[0].Children[1].Children[0]
+	a := p.Children[2]
 	if q := a.QueryRef(); q != "p:nth-child(1) > a:nth-child(3)" {
 		t.Fatalf("%v", q)
 	}
@@ -172,8 +172,8 @@
 				</body>
 			</html>
 		`: {
-			"body": {"a", "", "div", ""},
-			"div":  {"", ""},
+			"body": {"a", "div"},
+			"div":  {},
 			"a":    {"link"},
 		},
 		`
@@ -185,8 +185,8 @@
 				</body>
 			</html>
 		`: {
-			"body": {"", "div", ""},
-			"div":  {"a", "", ""},
+			"body": {"div"},
+			"div":  {"a"},
 			"a":    {"link"},
 		},
 		`
@@ -200,9 +200,9 @@
 				</body>
 			</html>
 		`: {
-			"body":    {"", "main", ""},
-			"main":    {"a", "", "article", ""},
-			"article": {"", ""},
+			"body":    {"main"},
+			"main":    {"a", "article"},
+			"article": {},
 			"a":       {"link"},
 		},
 	}