shithub: mycel

Download patch

ref: 1c3a0cb13ff08a5a125eb675c3cf942f2a4afa7d
parent: 49e401ef4b569e9c6b305648af6f7e652d575d2f
author: Philip Silva <philip.silva@protonmail.com>
date: Thu Apr 8 13:27:17 EDT 2021

test json cycles

--- a/nodes/nodes.go
+++ b/nodes/nodes.go
@@ -18,13 +18,13 @@
 // Node represents a node at the render stage. It
 // represents a subTree or just a single html node.
 type Node struct {
-	DomSubtree *html.Node
+	DomSubtree *html.Node `json:"-"`
 	Text string
 	Wrappable bool
 	Attrs []html.Attribute
 	style.Map
 	Children []*Node
-	Parent *Node
+	Parent *Node `json:"-"`
 }
 
 // NewNodeTree propagates the cascading styles to the leaves
--- a/nodes/nodes_test.go
+++ b/nodes/nodes_test.go
@@ -1,6 +1,8 @@
 package nodes
 
 import (
+	"bytes"
+	"encoding/json"
 	"golang.org/x/net/html"
 	"github.com/psilva261/opossum/style"
 	"strings"
@@ -80,3 +82,26 @@
 		t.Fatalf("%+v", text)
 	}
 }
+
+func TestJsonCycles(t *testing.T) {
+	buf := strings.NewReader(`
+	<html>
+		<body style="width: 900px; height: 700px; font-size: 12px;">
+			<p>
+				<b style="height: 100px;">bold stuff</b>
+			</p>
+		</body>
+	</html>`)
+	doc, err := html.Parse(buf)
+	if err != nil { t.Fatalf(err.Error()) }
+	n := NewNodeTree(doc, style.Map{}, make(map[*html.Node]style.Map), nil)
+	body := n.Find("body")
+	_=body
+	
+	b := bytes.NewBufferString("")
+	enc := json.NewEncoder(b)
+	if err := enc.Encode(n); err != nil {
+		t.Fatalf("%+v", err)
+	}
+}
+