Check for links with "alternate" ref and duplicate type and hreflang attributes

This commit is contained in:
Jason Streifling 2024-10-15 17:52:38 +02:00
parent e1ba1b8277
commit 05cc967ea8

22
feed.go
View File

@ -26,6 +26,25 @@ type Feed struct {
Entries []*Entry `xml:"entry,omitempty"` Entries []*Entry `xml:"entry,omitempty"`
} }
// atom:feed elements MUST NOT contain more than one atom:link
// element with a rel attribute value of "alternate" that has the
// same combination of type and hreflang attribute values.
func hasAlternateDuplicateLinks(l []*Link) bool {
linkMap := make(map[string]bool)
for _, link := range l {
if link.Rel == "alternate" {
key := fmt.Sprint(link.Type, "|", link.HrefLang)
if linkMap[key] {
return true
}
linkMap[key] = true
}
}
return false
}
// NewFeed creates a new feed. // NewFeed creates a new feed.
func NewFeed(title string) (*Feed, error) { func NewFeed(title string) (*Feed, error) {
text, err := NewText("text", title) text, err := NewText("text", title)
@ -172,6 +191,9 @@ func (f *Feed) Check() error {
} }
} }
} }
if hasAlternateDuplicateLinks(f.Links) {
return errors.New("duplicate links with with a rel attribute value of \"alternate\" found")
}
if f.Logo != nil { if f.Logo != nil {
if err := f.Logo.Check(); err != nil { if err := f.Logo.Check(); err != nil {