Add check for summary element of entry

This commit is contained in:
2024-10-15 19:32:14 +02:00
parent 9920e077eb
commit b08b62e794
6 changed files with 50 additions and 7 deletions

View File

@@ -4,6 +4,8 @@ import (
"encoding/xml"
"errors"
"fmt"
"mime"
"strings"
)
// It is advisable that each atom:entry element contain a non-empty atom:title
@@ -14,7 +16,7 @@ type Entry struct {
*CommonAttributes
Authors []*Person `xml:"author,omitempty"`
Categories []*Category `xml:"category,omitempty"`
Content *Content `xml:"content,omitempty"`
Content Content `xml:"content,omitempty"`
Contributors []*Person `xml:"contributors,omitempty"`
ID *ID `xml:"id"`
Links []*Link `xml:"link,omitempty"`
@@ -57,6 +59,10 @@ func alternateRelExists(l []*Link) bool {
return false
}
func isXMLMediaType(mediaType string) bool {
return strings.HasSuffix(mediaType, "/xml") || strings.HasSuffix(mediaType, "+xml")
}
func (e *Entry) AddExtension(name string, value any) {
e.Extensions = append(e.Extensions, &ExtensionElement{XMLName: xml.Name{Local: name}, Value: value})
}
@@ -81,7 +87,7 @@ func (e *Entry) Check() error {
}
if e.Content != nil {
if err := (*e.Content).Check(); err != nil {
if err := e.Content.Check(); err != nil {
return fmt.Errorf("content element of entry %v: %v", e.ID.URI, err)
}
} else {
@@ -130,6 +136,25 @@ func (e *Entry) Check() error {
if err := e.Summary.Check(); err != nil {
return fmt.Errorf("summary element of entry %v: %v", e.ID.URI, err)
}
} else {
// atom:entry elements MUST contain an atom:summary element in either
// of the following cases:
// the atom:entry contains an atom:content that has a "src" attribute
// (and is thus empty).
if e.Content.hasSRC() {
return fmt.Errorf("no summary element of entry %v but content of type out of line content", e.ID.URI)
}
// the atom:entry contains content that is encoded in Base64; i.e., the
// "type" attribute of atom:content is a MIME media type [MIMEREG], but
// is not an XML media type [RFC3023], does not begin with "text/", and
// does not end with "/xml" or "+xml".
mediaType, _, err := mime.ParseMediaType(e.Content.getType())
if err != nil {
return fmt.Errorf("type attribute of content element of entry %v: %v", e.ID.URI, err)
}
if !isXMLMediaType(mediaType) && !strings.HasPrefix(mediaType, "text/") {
return fmt.Errorf("no summary element of entry %v but media type not xml", e.ID.URI)
}
}
if e.Title == nil {