1717import java .io .Serializable ;
1818
1919import com .gargoylesoftware .css .parser .condition .Condition ;
20+ import com .gargoylesoftware .css .parser .condition .NotPseudoClassCondition ;
2021
2122/**
2223 * Calculates a selector's specificity.
@@ -30,16 +31,16 @@ public class SelectorSpecificity implements Comparable<SelectorSpecificity>, Ser
3031 /**
3132 * The specificity for declarations made in the style attributes of an element.
3233 */
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 );
3435 /**
3536 * The specificity for browser defaults.
3637 */
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 );
3839
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_ ;
4344
4445 /**
4546 * Ctor.
@@ -49,11 +50,11 @@ public SelectorSpecificity(final Selector selector) {
4950 readSelectorSpecificity (selector );
5051 }
5152
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 ;
5758 }
5859
5960 private void readSelectorSpecificity (final Selector selector ) {
@@ -71,7 +72,7 @@ private void readSelectorSpecificity(final Selector selector) {
7172 case ELEMENT_NODE_SELECTOR :
7273 final ElementSelector es = (ElementSelector ) selector ;
7374 if (es .getLocalName () != null ) {
74- fieldD_ ++;
75+ typeCount_ ++;
7576 }
7677 if (es .getConditions () != null ) {
7778 for (final Condition condition : es .getConditions ()) {
@@ -83,7 +84,7 @@ private void readSelectorSpecificity(final Selector selector) {
8384 final PseudoElementSelector pes = (PseudoElementSelector ) selector ;
8485 final String pesName = pes .getLocalName ();
8586 if (pesName != null ) {
86- fieldD_ ++;
87+ typeCount_ ++;
8788 }
8889 return ;
8990 case DIRECT_ADJACENT_SELECTOR :
@@ -105,34 +106,41 @@ private void readSelectorSpecificity(final Selector selector) {
105106 private void readSelectorSpecificity (final Condition condition ) {
106107 switch (condition .getConditionType ()) {
107108 case ID_CONDITION :
108- fieldB_ ++;
109+ idCount_ ++;
109110 return ;
110111 case CLASS_CONDITION :
111- fieldC_ ++;
112+ classCount_ ++;
112113 return ;
113114 case ATTRIBUTE_CONDITION :
114- fieldC_ ++;
115+ classCount_ ++;
115116 return ;
116117 case SUBSTRING_ATTRIBUTE_CONDITION :
117- fieldC_ ++;
118+ classCount_ ++;
118119 return ;
119120 case SUFFIX_ATTRIBUTE_CONDITION :
120- fieldC_ ++;
121+ classCount_ ++;
121122 return ;
122123 case PREFIX_ATTRIBUTE_CONDITION :
123- fieldC_ ++;
124+ classCount_ ++;
124125 return ;
125126 case BEGIN_HYPHEN_ATTRIBUTE_CONDITION :
126- fieldC_ ++;
127+ classCount_ ++;
127128 return ;
128129 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+ }
130138 return ;
131139 case PSEUDO_CLASS_CONDITION :
132- fieldC_ ++;
140+ classCount_ ++;
133141 return ;
134142 case LANG_CONDITION :
135- fieldC_ ++;
143+ classCount_ ++;
136144 return ;
137145 default :
138146 throw new RuntimeException ("Unhandled CSS condition type for specifity computation: '"
@@ -145,25 +153,26 @@ private void readSelectorSpecificity(final Condition condition) {
145153 */
146154 @ Override
147155 public String toString () {
148- return fieldA_ + "," + fieldB_ + "," + fieldC_ + "," + fieldD_ ;
156+ // return (fromStyle_ > 0 ? "!" : "") + idCount_ + "," + classCount_ + "," + typeCount_;
157+ return fromStyle_ + "," + idCount_ + "," + classCount_ + "," + typeCount_ ;
149158 }
150159
151160 /**
152161 * {@inheritDoc}
153162 */
154163 @ Override
155164 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_ ;
158167 }
159- else if (fieldB_ != other .fieldB_ ) {
160- return fieldB_ - other .fieldB_ ;
168+ else if (idCount_ != other .idCount_ ) {
169+ return idCount_ - other .idCount_ ;
161170 }
162- else if (fieldC_ != other .fieldC_ ) {
163- return fieldC_ - other .fieldC_ ;
171+ else if (classCount_ != other .classCount_ ) {
172+ return classCount_ - other .classCount_ ;
164173 }
165- else if (fieldD_ != other .fieldD_ ) {
166- return fieldD_ - other .fieldD_ ;
174+ else if (typeCount_ != other .typeCount_ ) {
175+ return typeCount_ - other .typeCount_ ;
167176 }
168177 return 0 ;
169178 }
@@ -172,10 +181,10 @@ else if (fieldD_ != other.fieldD_) {
172181 public int hashCode () {
173182 final int prime = 31 ;
174183 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_ ;
179188 return result ;
180189 }
181190
@@ -191,16 +200,16 @@ public boolean equals(final Object obj) {
191200 return false ;
192201 }
193202 final SelectorSpecificity other = (SelectorSpecificity ) obj ;
194- if (fieldA_ != other .fieldA_ ) {
203+ if (fromStyle_ != other .fromStyle_ ) {
195204 return false ;
196205 }
197- if (fieldB_ != other .fieldB_ ) {
206+ if (idCount_ != other .idCount_ ) {
198207 return false ;
199208 }
200- if (fieldC_ != other .fieldC_ ) {
209+ if (classCount_ != other .classCount_ ) {
201210 return false ;
202211 }
203- if (fieldD_ != other .fieldD_ ) {
212+ if (typeCount_ != other .typeCount_ ) {
204213 return false ;
205214 }
206215 return true ;
0 commit comments