declaration.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package css
  2. import "fmt"
  3. // Declaration represents a parsed style property
  4. type Declaration struct {
  5. Property string
  6. Value string
  7. Important bool
  8. }
  9. // NewDeclaration instanciates a new Declaration
  10. func NewDeclaration() *Declaration {
  11. return &Declaration{}
  12. }
  13. // Returns string representation of the Declaration
  14. func (decl *Declaration) String() string {
  15. return decl.StringWithImportant(true)
  16. }
  17. // StringWithImportant returns string representation with optional !important part
  18. func (decl *Declaration) StringWithImportant(option bool) string {
  19. result := fmt.Sprintf("%s: %s", decl.Property, decl.Value)
  20. if option && decl.Important {
  21. result += " !important"
  22. }
  23. result += ";"
  24. return result
  25. }
  26. // Equal returns true if both Declarations are equals
  27. func (decl *Declaration) Equal(other *Declaration) bool {
  28. return (decl.Property == other.Property) && (decl.Value == other.Value) && (decl.Important == other.Important)
  29. }
  30. //
  31. // DeclarationsByProperty
  32. //
  33. // DeclarationsByProperty represents sortable style declarations
  34. type DeclarationsByProperty []*Declaration
  35. // Implements sort.Interface
  36. func (declarations DeclarationsByProperty) Len() int {
  37. return len(declarations)
  38. }
  39. // Implements sort.Interface
  40. func (declarations DeclarationsByProperty) Swap(i, j int) {
  41. declarations[i], declarations[j] = declarations[j], declarations[i]
  42. }
  43. // Implements sort.Interface
  44. func (declarations DeclarationsByProperty) Less(i, j int) bool {
  45. return declarations[i].Property < declarations[j].Property
  46. }