Compare commits
No commits in common. "f27116930abaa27396d2ffb1ad70003ed63d3296" and "14696371e2ecbe708eb96f3d0ecafbd61cd59bb8" have entirely different histories.
f27116930a
...
14696371e2
26
atom.go
26
atom.go
@ -3,7 +3,6 @@ package atomfeed
|
||||
import (
|
||||
"mime"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -14,32 +13,11 @@ type (
|
||||
URI string
|
||||
)
|
||||
|
||||
func isValidURL(uri URI) bool {
|
||||
_, err := url.ParseRequestURI(string(uri))
|
||||
func isValidURL(testURL URI) bool {
|
||||
_, err := url.ParseRequestURI(string(testURL))
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func isValidURN(uri URI) bool {
|
||||
pattern := `\A(?i:urn:(?!urn:)(?<nid>[a-z0-9][a-z0-9-]{1,31}):(?<nss>(?:[-a-z0-9()+,.:=@;$_!*'&~\/]|%[0-9a-f]{2})+)(?:\?\+(?<rcomponent>.*?))?(?:\?=(?<qcomponent>.*?))?(?:#(?<fcomponent>.*?))?)\z`
|
||||
return regexp.MustCompile(pattern).MatchString(string(uri))
|
||||
}
|
||||
|
||||
func isValidURI(uri URI) bool {
|
||||
return isValidURL(uri) || isValidURN(uri)
|
||||
}
|
||||
|
||||
func isCorrectlyEscaped(text string) bool {
|
||||
relevantEntities := []string{"&", "<", ">", """, "'"}
|
||||
|
||||
for _, entity := range relevantEntities {
|
||||
if strings.Contains(text, entity) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func isCompositeMediaType(mediaType string) bool {
|
||||
mediaType, _, err := mime.ParseMediaType(mediaType)
|
||||
if err != nil {
|
||||
|
15
category.go
15
category.go
@ -3,7 +3,6 @@ package atomfeed
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"html"
|
||||
)
|
||||
|
||||
type Category struct {
|
||||
@ -23,25 +22,11 @@ func NewCategory(term string) (*Category, error) {
|
||||
return &Category{Term: term, Content: content}, nil
|
||||
}
|
||||
|
||||
func (c *Category) SetLabel(label string) {
|
||||
c.Label = html.UnescapeString(label)
|
||||
}
|
||||
|
||||
func (c *Category) Check() error {
|
||||
if c.Term == "" {
|
||||
return errors.New("term attribute of category empty")
|
||||
}
|
||||
|
||||
if c.Scheme != "" {
|
||||
if !isValidURI(c.Scheme) {
|
||||
return fmt.Errorf("scheme attribute of category %v not correctly formatted", c.Scheme)
|
||||
}
|
||||
}
|
||||
|
||||
if !isCorrectlyEscaped(c.Label) {
|
||||
return fmt.Errorf("label attribute of category %v not correctly escaped", c.Label)
|
||||
}
|
||||
|
||||
if c.Content == nil {
|
||||
return errors.New("no content element of category")
|
||||
} else {
|
||||
|
@ -22,7 +22,7 @@ func newOutOfLineContent(mediaType string, content any) (*OutOfLineContent, erro
|
||||
return nil, fmt.Errorf("content type %T incompatible with out of line content", content)
|
||||
}
|
||||
|
||||
if !isValidURI(content.(URI)) {
|
||||
if !isValidURL(content.(URI)) {
|
||||
return nil, errors.New("content not a valid uri")
|
||||
}
|
||||
|
||||
|
@ -29,7 +29,7 @@ func (p *Person) Check() error {
|
||||
}
|
||||
|
||||
if p.URI != "" {
|
||||
if !isValidURI(p.URI) {
|
||||
if !isValidURL(p.URI) {
|
||||
return fmt.Errorf("uri element of person %v not correctly formatted", p.Name)
|
||||
}
|
||||
}
|
||||
|
@ -15,10 +15,6 @@ func (p *PlainText) Check() error {
|
||||
return errors.New("type attribute of plain text must be text or html if not omitted")
|
||||
}
|
||||
|
||||
if p.Type == "html" && !isCorrectlyEscaped(p.Text) {
|
||||
return errors.New("text element of plain text not correctly escaped")
|
||||
}
|
||||
|
||||
if p.Text == "" {
|
||||
return errors.New("text element of plain text empty")
|
||||
}
|
||||
|
2
text.go
2
text.go
@ -15,7 +15,7 @@ func NewText(textType, content string) (Text, error) {
|
||||
case "text", "":
|
||||
return &PlainText{Type: textType, Text: content}, nil
|
||||
case "html":
|
||||
return &PlainText{Type: textType, Text: html.UnescapeString(content)}, nil
|
||||
return &PlainText{Type: textType, Text: html.EscapeString(content)}, nil
|
||||
case "xhtml":
|
||||
return &XHTMLText{
|
||||
Type: textType,
|
||||
|
Loading…
x
Reference in New Issue
Block a user