forked from ukamnya/microdata_mirror
Fix #3: fatal stack overflow
parent
2dfc019ddc
commit
c529673a50
|
@ -167,10 +167,12 @@ func (p *Parser) readItem(item *Item, node *html.Node) {
|
|||
itemref = strings.TrimSpace(itemref)
|
||||
|
||||
if refnode, exists := p.identifiedNodes[itemref]; exists {
|
||||
if refnode != node {
|
||||
p.readItem(subitem, refnode)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for child := node.FirstChild; child != nil; {
|
||||
p.readItem(subitem, child)
|
||||
|
|
|
@ -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 := `<body itemscope itemtype="http://schema.org/WebPage">
|
||||
<span id="1" itemscope itemtype="http://data-vocabulary.org/Breadcrumb" itemprop="child" itemref="1">
|
||||
<a title="Foo" itemprop="url" href="/foo/bar"><span itemprop="title">Foo</span></a>
|
||||
</span>
|
||||
</body>`
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue