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