diff --git a/microdata.go b/microdata.go index 569b1fe..3130fb3 100644 --- a/microdata.go +++ b/microdata.go @@ -167,7 +167,9 @@ func (p *Parser) readItem(item *Item, node *html.Node) { itemref = strings.TrimSpace(itemref) if refnode, exists := p.identifiedNodes[itemref]; exists { - p.readItem(subitem, refnode) + if refnode != node { + p.readItem(subitem, refnode) + } } } } diff --git a/microdata_test.go b/microdata_test.go index c57ca09..01bdc97 100644 --- a/microdata_test.go +++ b/microdata_test.go @@ -8,6 +8,7 @@ package microdata import ( "bytes" "net/url" + "reflect" "strings" "testing" ) @@ -569,3 +570,30 @@ func TestJsonWithType(t *testing.T) { t.Errorf("Expecting %s but got %s", expected, actual) } } + +// This test checks stack overflow doesn't happen as mentioned in +// https://github.com/iand/microdata/issues/3 +func TestSkipSelfReferencingItemref(t *testing.T) { + html := ` + + + + ` + + actual := ParseData(html, t) + + child := NewItem() + child.AddString("title", "Foo") + child.AddString("url", "http://example.com/foo/bar") + + item := NewItem() + item.AddType("http://schema.org/WebPage") + item.AddItem("child", child) + + expected := NewMicrodata() + expected.AddItem(item) + + if !reflect.DeepEqual(expected, actual) { + t.Errorf("Expecting %s but got %s", expected, actual) + } +}