shithub: opossum

Download patch

ref: 14f3fdd014c2a01a1d3d19d34d28fa22256260c3
parent: 6f582efbd405ad095635ff0f6991d857e665fc35
author: Philip Silva <philip.silva@protonmail.com>
date: Fri Dec 25 10:39:59 EST 2020

QueryRef

--- a/nodes/nodes.go
+++ b/nodes/nodes.go
@@ -89,6 +89,17 @@
 	return nil
 }
 
+func (n *Node) QueryRef() string {
+	path := make([]string, 0, 5)
+	path = append(path, n.Data())
+	for p := n.Parent; p != nil; p = p.Parent {
+		if p.Data() != "html" && p.Data() != "body" {
+			path = append([]string{p.Data()}, path...)
+		} 
+	}
+	return strings.TrimSpace(strings.Join(path, " "))
+}
+
 func IsPureTextContent(n Node) bool {
 	if n.Text != "" {
 		return true
--- a/nodes/nodes_test.go
+++ b/nodes/nodes_test.go
@@ -1,6 +1,9 @@
 package nodes
 
 import (
+	"golang.org/x/net/html"
+	"opossum/style"
+	"strings"
 	"testing"
 )
 
@@ -10,4 +13,23 @@
 	if out := filterText(in); out != exp {
 		t.Fatalf("%+v", out)
 	}
+}
+
+func TestQueryRef(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[1]
+	a := p.Children[5]
+	if q := a.QueryRef(); q != "p a" { t.Fatalf("%v", q) }
 }
\ No newline at end of file