style.go 628 B

123456789101112131415161718192021222324252627282930313233
  1. package svg
  2. import "encoding/xml"
  3. import "strings"
  4. type Style map[string]string
  5. func ParseStyle(str string) (Style, error) {
  6. res := make(Style)
  7. rules := strings.Split(";", str)
  8. for _, rule := range rules {
  9. parts := strings.SplitN(rule, ":", 2)
  10. key := parts[0]
  11. value := "true"
  12. if len(parts) > 1 {
  13. value = parts[1]
  14. }
  15. res[key] = value
  16. }
  17. return res, nil
  18. }
  19. func (s *Style) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
  20. var value string
  21. // Read tag content into value
  22. d.DecodeElement(&value, &start)
  23. style, err := ParseStyle(value)
  24. if err != nil {
  25. return err
  26. }
  27. *s = style
  28. return nil
  29. }