style.go 567 B

1234567891011121314151617181920212223242526272829303132
  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) UnmarshalXMLAttr(attr xml.Attr) error {
  20. style, err := ParseStyle(attr.Value)
  21. if err != nil {
  22. return err
  23. }
  24. *s = style
  25. return nil
  26. }
  27. var _ xml.UnmarshalerAttr = &Style{}