atom/plainText.go

42 lines
1.0 KiB
Go
Raw Permalink Normal View History

2024-10-16 21:28:04 +02:00
package atom
2024-10-13 17:19:40 +02:00
import (
"fmt"
)
2024-10-13 17:19:40 +02:00
type PlainText struct {
*CommonAttributes
Type string `xml:"type,attr,omitempty"` // Must be text or html
Text string `xml:",chardata"`
2024-10-13 17:19:40 +02:00
}
2024-10-16 19:59:28 +02:00
// isText checks whether the PlainText is a Text. It returns a bool.
2024-10-16 18:31:24 +02:00
func (p *PlainText) isText() bool { return true }
2024-10-13 17:19:40 +02:00
// newPlainText creates a new PlainText. It returns a *PlainText.
func newPlainText(textType, content string) *PlainText {
return &PlainText{
CommonAttributes: newCommonAttributes(),
Type: textType,
Text: content,
}
}
2024-10-16 19:59:28 +02:00
// Check checks the PlainText for incompatibilities with RFC4287. It returns an
// error.
2024-10-13 17:19:40 +02:00
func (p *PlainText) Check() error {
if p.Type != "" && p.Type != "text" && p.Type != "html" {
2024-10-18 19:04:08 +02:00
return fmt.Errorf("type attribute of plain text %v must be text or html if not omitted", p)
2024-10-13 17:19:40 +02:00
}
if p.Type == "html" && !isCorrectlyEscaped(p.Text) {
2024-10-18 19:04:08 +02:00
return fmt.Errorf("text element of plain text %v not correctly escaped", p)
}
2024-10-13 17:19:40 +02:00
if p.Text == "" {
2024-10-18 19:04:08 +02:00
return fmt.Errorf("text element of plain text %v empty", p)
2024-10-13 17:19:40 +02:00
}
return nil
}