17
17
import java .io .Serializable ;
18
18
19
19
import com .gargoylesoftware .css .parser .condition .Condition ;
20
+ import com .gargoylesoftware .css .parser .condition .NotPseudoClassCondition ;
20
21
21
22
/**
22
23
* Calculates a selector's specificity.
@@ -30,16 +31,16 @@ public class SelectorSpecificity implements Comparable<SelectorSpecificity>, Ser
30
31
/**
31
32
* The specificity for declarations made in the style attributes of an element.
32
33
*/
33
- public static final SelectorSpecificity FROM_STYLE_ATTRIBUTE = new SelectorSpecificity (1 , 0 , 0 , 0 );
34
+ public static final SelectorSpecificity FROM_STYLE_ATTRIBUTE = new SelectorSpecificity (true , 0 , 0 , 0 );
34
35
/**
35
36
* The specificity for browser defaults.
36
37
*/
37
- public static final SelectorSpecificity DEFAULT_STYLE_ATTRIBUTE = new SelectorSpecificity (0 , 0 , 0 , 0 );
38
+ public static final SelectorSpecificity DEFAULT_STYLE_ATTRIBUTE = new SelectorSpecificity (false , 0 , 0 , 0 );
38
39
39
- private int fieldA_ ;
40
- private int fieldB_ ;
41
- private int fieldC_ ;
42
- private int fieldD_ ;
40
+ private int fromStyle_ ;
41
+ private int idCount_ ;
42
+ private int classCount_ ;
43
+ private int typeCount_ ;
43
44
44
45
/**
45
46
* Ctor.
@@ -49,11 +50,11 @@ public SelectorSpecificity(final Selector selector) {
49
50
readSelectorSpecificity (selector );
50
51
}
51
52
52
- private SelectorSpecificity (final int a , final int b , final int c , final int d ) {
53
- fieldA_ = a ;
54
- fieldB_ = b ;
55
- fieldC_ = c ;
56
- fieldD_ = d ;
53
+ private SelectorSpecificity (final boolean fromStyle , final int idCount , final int classCount , final int typeCount ) {
54
+ fromStyle_ = fromStyle ? 1 : 0 ;
55
+ idCount_ = idCount ;
56
+ classCount_ = classCount ;
57
+ typeCount_ = typeCount ;
57
58
}
58
59
59
60
private void readSelectorSpecificity (final Selector selector ) {
@@ -71,7 +72,7 @@ private void readSelectorSpecificity(final Selector selector) {
71
72
case ELEMENT_NODE_SELECTOR :
72
73
final ElementSelector es = (ElementSelector ) selector ;
73
74
if (es .getLocalName () != null ) {
74
- fieldD_ ++;
75
+ typeCount_ ++;
75
76
}
76
77
if (es .getConditions () != null ) {
77
78
for (final Condition condition : es .getConditions ()) {
@@ -83,7 +84,7 @@ private void readSelectorSpecificity(final Selector selector) {
83
84
final PseudoElementSelector pes = (PseudoElementSelector ) selector ;
84
85
final String pesName = pes .getLocalName ();
85
86
if (pesName != null ) {
86
- fieldD_ ++;
87
+ typeCount_ ++;
87
88
}
88
89
return ;
89
90
case DIRECT_ADJACENT_SELECTOR :
@@ -105,34 +106,41 @@ private void readSelectorSpecificity(final Selector selector) {
105
106
private void readSelectorSpecificity (final Condition condition ) {
106
107
switch (condition .getConditionType ()) {
107
108
case ID_CONDITION :
108
- fieldB_ ++;
109
+ idCount_ ++;
109
110
return ;
110
111
case CLASS_CONDITION :
111
- fieldC_ ++;
112
+ classCount_ ++;
112
113
return ;
113
114
case ATTRIBUTE_CONDITION :
114
- fieldC_ ++;
115
+ classCount_ ++;
115
116
return ;
116
117
case SUBSTRING_ATTRIBUTE_CONDITION :
117
- fieldC_ ++;
118
+ classCount_ ++;
118
119
return ;
119
120
case SUFFIX_ATTRIBUTE_CONDITION :
120
- fieldC_ ++;
121
+ classCount_ ++;
121
122
return ;
122
123
case PREFIX_ATTRIBUTE_CONDITION :
123
- fieldC_ ++;
124
+ classCount_ ++;
124
125
return ;
125
126
case BEGIN_HYPHEN_ATTRIBUTE_CONDITION :
126
- fieldC_ ++;
127
+ classCount_ ++;
127
128
return ;
128
129
case ONE_OF_ATTRIBUTE_CONDITION :
129
- fieldC_ ++;
130
+ classCount_ ++;
131
+ return ;
132
+ case NOT_PSEUDO_CLASS_CONDITION :
133
+ final NotPseudoClassCondition notPseudoCondition = (NotPseudoClassCondition ) condition ;
134
+ final SelectorList selectorList = notPseudoCondition .getSelectors ();
135
+ for (final Selector selector : selectorList ) {
136
+ readSelectorSpecificity (selector );
137
+ }
130
138
return ;
131
139
case PSEUDO_CLASS_CONDITION :
132
- fieldC_ ++;
140
+ classCount_ ++;
133
141
return ;
134
142
case LANG_CONDITION :
135
- fieldC_ ++;
143
+ classCount_ ++;
136
144
return ;
137
145
default :
138
146
throw new RuntimeException ("Unhandled CSS condition type for specifity computation: '"
@@ -145,25 +153,26 @@ private void readSelectorSpecificity(final Condition condition) {
145
153
*/
146
154
@ Override
147
155
public String toString () {
148
- return fieldA_ + "," + fieldB_ + "," + fieldC_ + "," + fieldD_ ;
156
+ // return (fromStyle_ > 0 ? "!" : "") + idCount_ + "," + classCount_ + "," + typeCount_;
157
+ return fromStyle_ + "," + idCount_ + "," + classCount_ + "," + typeCount_ ;
149
158
}
150
159
151
160
/**
152
161
* {@inheritDoc}
153
162
*/
154
163
@ Override
155
164
public int compareTo (final SelectorSpecificity other ) {
156
- if (fieldA_ != other .fieldA_ ) {
157
- return fieldA_ - other .fieldA_ ;
165
+ if (fromStyle_ != other .fromStyle_ ) {
166
+ return fromStyle_ - other .fromStyle_ ;
158
167
}
159
- else if (fieldB_ != other .fieldB_ ) {
160
- return fieldB_ - other .fieldB_ ;
168
+ else if (idCount_ != other .idCount_ ) {
169
+ return idCount_ - other .idCount_ ;
161
170
}
162
- else if (fieldC_ != other .fieldC_ ) {
163
- return fieldC_ - other .fieldC_ ;
171
+ else if (classCount_ != other .classCount_ ) {
172
+ return classCount_ - other .classCount_ ;
164
173
}
165
- else if (fieldD_ != other .fieldD_ ) {
166
- return fieldD_ - other .fieldD_ ;
174
+ else if (typeCount_ != other .typeCount_ ) {
175
+ return typeCount_ - other .typeCount_ ;
167
176
}
168
177
return 0 ;
169
178
}
@@ -172,10 +181,10 @@ else if (fieldD_ != other.fieldD_) {
172
181
public int hashCode () {
173
182
final int prime = 31 ;
174
183
int result = 1 ;
175
- result = prime * result + fieldA_ ;
176
- result = prime * result + fieldB_ ;
177
- result = prime * result + fieldC_ ;
178
- result = prime * result + fieldD_ ;
184
+ result = prime * result + fromStyle_ ;
185
+ result = prime * result + idCount_ ;
186
+ result = prime * result + classCount_ ;
187
+ result = prime * result + typeCount_ ;
179
188
return result ;
180
189
}
181
190
@@ -191,16 +200,16 @@ public boolean equals(final Object obj) {
191
200
return false ;
192
201
}
193
202
final SelectorSpecificity other = (SelectorSpecificity ) obj ;
194
- if (fieldA_ != other .fieldA_ ) {
203
+ if (fromStyle_ != other .fromStyle_ ) {
195
204
return false ;
196
205
}
197
- if (fieldB_ != other .fieldB_ ) {
206
+ if (idCount_ != other .idCount_ ) {
198
207
return false ;
199
208
}
200
- if (fieldC_ != other .fieldC_ ) {
209
+ if (classCount_ != other .classCount_ ) {
201
210
return false ;
202
211
}
203
- if (fieldD_ != other .fieldD_ ) {
212
+ if (typeCount_ != other .typeCount_ ) {
204
213
return false ;
205
214
}
206
215
return true ;
0 commit comments