/css/selectors/selectors.go

https://code.google.com/p/gosvg/ · Go · 85 lines · 64 code · 21 blank · 0 comment · 4 complexity · 78bbefeb3bc43d29b60c9959c33082c0 MD5 · raw file

  1. package selectors
  2. import ()
  3. type selector interface {
  4. String() string
  5. }
  6. type SimpleSelector struct {
  7. type_val string
  8. extra_val string
  9. sealed bool
  10. }
  11. func (s *SimpleSelector) String() string {
  12. if s.extra_val == "" {
  13. return s.type_val
  14. }
  15. if s.type_val == "*" {
  16. return s.extra_val
  17. }
  18. return s.type_val + s.extra_val
  19. }
  20. func (s *SimpleSelector) Sealed() bool {
  21. return s.sealed
  22. }
  23. func Universal() *SimpleSelector {
  24. return &SimpleSelector{type_val:"*"}
  25. }
  26. func Type(s string) *SimpleSelector {
  27. return &SimpleSelector{type_val:s}
  28. }
  29. func Attribute(s string) *SimpleSelector {
  30. return &SimpleSelector{type_val:"*", extra_val:"[" + s + "]"}
  31. }
  32. func AttributeSpaceList(s, b string) *SimpleSelector {
  33. return Attribute(s + "~=\"" + b + "\"")
  34. }
  35. func AttributeEquals(s, b string) *SimpleSelector {
  36. return Attribute(s + "=\"" + b + "\"")
  37. }
  38. func AttributeBegins(s, b string) *SimpleSelector {
  39. return Attribute(s + "^=\"" + b + "\"")
  40. }
  41. func AttributeEnds(s, b string) *SimpleSelector {
  42. return Attribute(s + "$=\"" + b + "\"")
  43. }
  44. func AttributeSubstring(s, b string) *SimpleSelector {
  45. return Attribute(s + "*=\"" + b + "\"")
  46. }
  47. func AttributeHyphenList(s, b string) *SimpleSelector {
  48. return Attribute(s + "|=\"" + b + "\"")
  49. }
  50. func PseudoClass(s string) *SimpleSelector {
  51. return &SimpleSelector{type_val:"*", extra_val: ":" + s}
  52. }
  53. func PseudoElement(s string) *SimpleSelector {
  54. return &SimpleSelector{type_val:"*", extra_val: "::" + s}
  55. }
  56. func Id(id string) *SimpleSelector {
  57. return &SimpleSelector{type_val:"", extra_val: "#" + id}
  58. }
  59. func Not(s *SimpleSelector) *SimpleSelector {
  60. s.sealed = true
  61. return &SimpleSelector{}
  62. }
  63. type GroupSelector struct {
  64. }