diff --git a/property.go b/property.go index 0c41755..af3a9ea 100644 --- a/property.go +++ b/property.go @@ -23,7 +23,7 @@ func (s *Selection) Attr(attrName string) (val string, exists bool) { // AttrOr works like Attr but returns default value if attribute is not present. func (s *Selection) AttrOr(attrName, defaultValue string) string { if len(s.Nodes) == 0 { - return "" + return defaultValue } val, exists := getAttributeValue(attrName, s.Nodes[0]) diff --git a/property_test.go b/property_test.go index ee6f821..1095dcc 100644 --- a/property_test.go +++ b/property_test.go @@ -20,6 +20,11 @@ func TestAttrOr(t *testing.T) { } else { t.Logf("Value returned for not existing attribute: %v.", val) } + if val := Doc().Find("zz").AttrOr("fake-attribute", "alternative"); val != "alternative" { + t.Error("Expected an alternative value for 'fake-attribute' on an empty selection.") + } else { + t.Logf("Value returned for empty selection: %v.", val) + } } func TestAttrNotExist(t *testing.T) {