Move the parsing of the media type to isXMLMediaType

This commit is contained in:
Jason Streifling 2024-10-15 19:46:26 +02:00
parent e3b9ff0225
commit 209059f2b4
2 changed files with 10 additions and 6 deletions

10
atom.go
View File

@ -1,6 +1,9 @@
package atomfeed package atomfeed
import "strings" import (
"mime"
"strings"
)
type ( type (
EmailAddress string EmailAddress string
@ -10,5 +13,10 @@ type (
) )
func isXMLMediaType(mediaType string) bool { func isXMLMediaType(mediaType string) bool {
mediaType, _, err := mime.ParseMediaType(mediaType)
if err != nil {
return false
}
return strings.HasSuffix(mediaType, "/xml") || strings.HasSuffix(mediaType, "+xml") return strings.HasSuffix(mediaType, "/xml") || strings.HasSuffix(mediaType, "+xml")
} }

View File

@ -4,7 +4,6 @@ import (
"encoding/xml" "encoding/xml"
"errors" "errors"
"fmt" "fmt"
"mime"
"strings" "strings"
) )
@ -144,10 +143,7 @@ func (e *Entry) Check() error {
// "type" attribute of atom:content is a MIME media type [MIMEREG], but // "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 // is not an XML media type [RFC3023], does not begin with "text/", and
// does not end with "/xml" or "+xml". // does not end with "/xml" or "+xml".
mediaType, _, err := mime.ParseMediaType(e.Content.getType()) mediaType := 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/") { if !isXMLMediaType(mediaType) && !strings.HasPrefix(mediaType, "text/") {
return fmt.Errorf("no summary element of entry %v but media type not xml", e.ID.URI) return fmt.Errorf("no summary element of entry %v but media type not xml", e.ID.URI)
} }