43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package atom
|
|
|
|
import (
|
|
"errors"
|
|
"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 and an error.
|
|
func newPlainText(textType, content string) (*PlainText, error) {
|
|
if content == "" {
|
|
return nil, errors.New("error creating new plain text: content string empty")
|
|
}
|
|
|
|
return &PlainText{Type: textType, Text: content}, nil
|
|
}
|
|
|
|
// 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
|
|
}
|