if (count == 1) {
return (Selector)selectors.get(0);
}
int lastDescendantOrChildAxis = Selector.DESCENDANT_AXIS;
Selector result = null;
for (int i = 0; i < count - 1; i++) {
Selector first = (Selector)selectors.get(i);
Selector second = (Selector)selectors.get(i+1);
Token combinator = (Token)combinators.get(i);
if (first.getPseudoElement() != null) {
throw new CSSParseException(
"A simple selector with a pseudo element cannot be " +
"combined with another simple selector", getCurrentLine());
}
boolean sibling = false;
if (combinator == Token.TK_S) {
second.setAxis(Selector.DESCENDANT_AXIS);
lastDescendantOrChildAxis = Selector.DESCENDANT_AXIS;
} else if (combinator == Token.TK_GREATER) {
second.setAxis(Selector.CHILD_AXIS);
lastDescendantOrChildAxis = Selector.CHILD_AXIS;
} else if (combinator == Token.TK_PLUS) {
first.setAxis(Selector.IMMEDIATE_SIBLING_AXIS);
sibling = true;
}
second.setSpecificityB(second.getSpecificityB() + first.getSpecificityB());
second.setSpecificityC(second.getSpecificityC() + first.getSpecificityC());
second.setSpecificityD(second.getSpecificityD() + first.getSpecificityD());
if (! sibling) {
if (result == null) {
result = first;
}
first.setChainedSelector(second);
} else {
second.setSiblingSelector(first);
if (result == null || result == first) {
result = second;
}
if (i > 0) {
for (int j = i-1; j >= 0; j--) {
Selector selector = (Selector)selectors.get(j);
if (selector.getChainedSelector() == first) {
selector.setChainedSelector(second);
second.setAxis(lastDescendantOrChildAxis);
break;
}
}
}