atom/plainText.go

38 lines
989 B
Go

package atom
import (
"fmt"
)
type PlainText struct {
*CommonAttributes
Type string `xml:"type,attr,omitempty"` // Must be text or html
Text string `xml:",chardata"`
}
// isText checks whether the PlainText is a Text. It returns a bool.
func (p *PlainText) isText() bool { return true }
// newPlainText creates a new PlainText. It returns a *PlainText.
func newPlainText(textType, content string) *PlainText {
return &PlainText{Type: textType, Text: content}
}
// Check checks the PlainText for incompatibilities with RFC4287. It returns an
// error.
func (p *PlainText) Check() error {
if p.Type != "" && p.Type != "text" && p.Type != "html" {
return fmt.Errorf("type attribute of plain text %v must be text or html if not omitted", p)
}
if p.Type == "html" && !isCorrectlyEscaped(p.Text) {
return fmt.Errorf("text element of plain text %v not correctly escaped", p)
}
if p.Text == "" {
return fmt.Errorf("text element of plain text %v empty", p)
}
return nil
}