Add check for summary element of entry

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

View File

@ -1,6 +1,8 @@
package atomfeed package atomfeed
type Content interface { type Content interface {
isContent() bool
hasSRC() bool
getType() string
Check() error Check() error
IsContent() bool
} }

View File

@ -4,6 +4,8 @@ import (
"encoding/xml" "encoding/xml"
"errors" "errors"
"fmt" "fmt"
"mime"
"strings"
) )
// It is advisable that each atom:entry element contain a non-empty atom:title // It is advisable that each atom:entry element contain a non-empty atom:title
@ -14,7 +16,7 @@ type Entry struct {
*CommonAttributes *CommonAttributes
Authors []*Person `xml:"author,omitempty"` Authors []*Person `xml:"author,omitempty"`
Categories []*Category `xml:"category,omitempty"` Categories []*Category `xml:"category,omitempty"`
Content *Content `xml:"content,omitempty"` Content Content `xml:"content,omitempty"`
Contributors []*Person `xml:"contributors,omitempty"` Contributors []*Person `xml:"contributors,omitempty"`
ID *ID `xml:"id"` ID *ID `xml:"id"`
Links []*Link `xml:"link,omitempty"` Links []*Link `xml:"link,omitempty"`
@ -57,6 +59,10 @@ func alternateRelExists(l []*Link) bool {
return false return false
} }
func isXMLMediaType(mediaType string) bool {
return strings.HasSuffix(mediaType, "/xml") || strings.HasSuffix(mediaType, "+xml")
}
func (e *Entry) AddExtension(name string, value any) { func (e *Entry) AddExtension(name string, value any) {
e.Extensions = append(e.Extensions, &ExtensionElement{XMLName: xml.Name{Local: name}, Value: value}) 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 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) return fmt.Errorf("content element of entry %v: %v", e.ID.URI, err)
} }
} else { } else {
@ -130,6 +136,25 @@ func (e *Entry) Check() error {
if err := e.Summary.Check(); err != nil { if err := e.Summary.Check(); err != nil {
return fmt.Errorf("summary element of entry %v: %v", e.ID.URI, err) 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 { if e.Title == nil {

View File

@ -6,6 +6,10 @@ type InlineOtherContent struct {
AnyElement []*any `xml:"anyelement,omitempty"` AnyElement []*any `xml:"anyelement,omitempty"`
} }
func (i *InlineOtherContent) IsContent() bool { return true } func (i *InlineOtherContent) isContent() bool { return true }
func (i *InlineOtherContent) hasSRC() bool { return false }
func (i *InlineOtherContent) getType() string { return string(i.Type) }
func (i *InlineOtherContent) Check() error { return nil } func (i *InlineOtherContent) Check() error { return nil }

View File

@ -8,7 +8,11 @@ type InlineTextContent struct {
Texts []string `xml:"texts,omitempty"` Texts []string `xml:"texts,omitempty"`
} }
func (i *InlineTextContent) IsContent() bool { return true } func (i *InlineTextContent) isContent() bool { return true }
func (i *InlineTextContent) hasSRC() bool { return false }
func (i *InlineTextContent) getType() string { return i.Type }
func (i *InlineTextContent) Check() error { func (i *InlineTextContent) Check() error {
if i.Type != "" && i.Type != "text" && i.Type != "html" { if i.Type != "" && i.Type != "text" && i.Type != "html" {

View File

@ -8,7 +8,11 @@ type InlineXHTMLContent struct {
XHTMLDiv string `xml:"xhtmldiv"` XHTMLDiv string `xml:"xhtmldiv"`
} }
func (i *InlineXHTMLContent) IsContent() bool { return true } func (i *InlineXHTMLContent) isContent() bool { return true }
func (i *InlineXHTMLContent) hasSRC() bool { return false }
func (i *InlineXHTMLContent) getType() string { return i.Type }
func (i *InlineXHTMLContent) Check() error { func (i *InlineXHTMLContent) Check() error {
if i.Type != "xhtml" { if i.Type != "xhtml" {

View File

@ -8,7 +8,11 @@ type OutOfLineContent struct {
SRC URI `xml:"src,attr"` SRC URI `xml:"src,attr"`
} }
func (o *OutOfLineContent) IsContent() bool { return true } func (o *OutOfLineContent) isContent() bool { return true }
func (o *OutOfLineContent) hasSRC() bool { return true }
func (o *OutOfLineContent) getType() string { return string(o.Type) }
func (o *OutOfLineContent) Check() error { func (o *OutOfLineContent) Check() error {
if o.SRC == "" { if o.SRC == "" {