use of org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorItem in project webtools.sourceediting by eclipse.
the class CSSSelector method match.
/**
* @return boolean
* @param element
* org.w3c.dom.Element
*/
public boolean match(org.w3c.dom.Element element, java.lang.String pseudoName) {
Element target = element;
char combinatorType = ICSSSelectorCombinator.UNKNOWN;
// for CHILD and ADJACENT
Element chunkStartElement = null;
// combinator
// for CHILD and ADJACENT combinator
int chunkStartItem = -1;
int numItems = getLength();
for (int iItem = numItems - 1; iItem >= 0; iItem--) {
// Check Selector Items
ICSSSelectorItem item = getItem(iItem);
if (item instanceof ICSSSimpleSelector) {
// Simple Selector
if (target == null)
return false;
if (!matchExactly((ICSSSimpleSelector) item, target, pseudoName)) {
switch(combinatorType) {
case ICSSSelectorCombinator.DESCENDANT:
do {
target = getParentElement(target);
if (target == null)
return false;
} while (!matchExactly((ICSSSimpleSelector) item, target, pseudoName));
break;
case ICSSSelectorCombinator.CHILD:
case ICSSSelectorCombinator.ADJACENT:
if (chunkStartElement != null && chunkStartElement != element) {
// previous conbinator must be DESCENDENT.
// goto parent
target = getParentElement(chunkStartElement);
iItem = chunkStartItem + 1;
chunkStartElement = null;
chunkStartItem = -1;
break;
}
default:
// other combinators are not supported yet.
return false;
}
}
} else if (item instanceof ICSSSelectorCombinator) {
// Combinator ( "+", ">", " ", ...)
if (iItem == numItems - 1)
// last item is combinator
return false;
ICSSSelectorCombinator sc = (ICSSSelectorCombinator) item;
combinatorType = sc.getCombinatorType();
switch(combinatorType) {
case ICSSSelectorCombinator.DESCENDANT:
target = getParentElement(target);
break;
case ICSSSelectorCombinator.CHILD:
case ICSSSelectorCombinator.ADJACENT:
if (chunkStartElement == null) {
chunkStartElement = target;
// safe because this
chunkStartItem = iItem + 1;
// is not a last item.
}
if (combinatorType == ICSSSelectorCombinator.CHILD) {
target = getParentElement(target);
} else {
target = getPreviousElement(target);
}
break;
}
} else {
// what is this item ???
return false;
}
}
// OK this selector maches the element.
return true;
}
use of org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorItem in project webtools.sourceediting by eclipse.
the class CSSSelector method getErrors.
public Iterator getErrors() {
if (fNameErrors == null) {
fNameErrors = new ArrayList();
Iterator iItem = getIterator();
while (iItem.hasNext()) {
ICSSSelectorItem item = (ICSSSelectorItem) iItem.next();
if (item instanceof ICSSSimpleSelector) {
if (!((ICSSSimpleSelector) item).isUniversal()) {
String name = ((ICSSSimpleSelector) item).getName();
if (!NameValidator.isValid(name)) {
fNameErrors.add(item);
}
}
}
}
}
List errors = new ArrayList(fParseErrors);
errors.addAll(fNameErrors);
return errors.iterator();
}
use of org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorItem in project webtools.sourceediting by eclipse.
the class CSSSelector method getSpecificity.
/**
* Calculate a selector's specificity a = the number of ID attributes in
* the selector b = the number of other attributes and pseudo-classes in
* the selector c = the number of element names in the selector (ignore
* pseudo-elements) Concatenating the three numbers a-b-c (in a number
* system with a large base) gives the specificity.
*
* @return int
*/
public int getSpecificity() {
if (fSpecificity < 0) {
int nIDs = 0;
int nAttributes = 0;
int nElements = 0;
Iterator i = getIterator();
while (i.hasNext()) {
ICSSSelectorItem item = (ICSSSelectorItem) i.next();
if (item instanceof CSSSimpleSelector) {
CSSSimpleSelector selector = (CSSSimpleSelector) item;
nIDs += selector.getNumOfIDs();
nAttributes += selector.getNumOfAttributes();
nAttributes += selector.getNumOfClasses();
nAttributes += selector.getNumOfPseudoNames();
if (!selector.isUniversal()) {
nElements++;
}
}
}
fSpecificity = nIDs * 10000 + nAttributes * 100 + nElements;
}
return fSpecificity;
}
use of org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorItem in project webtools.sourceediting by eclipse.
the class CSSClassTraverser method addClassNames.
/**
*/
private void addClassNames(ICSSStyleRule rule) {
ICSSSelectorList selectorList = rule.getSelectors();
Iterator iSelector = selectorList.getIterator();
while (iSelector.hasNext()) {
ICSSSelector selector = (ICSSSelector) iSelector.next();
Iterator iItem = selector.getIterator();
while (iItem.hasNext()) {
ICSSSelectorItem item = (ICSSSelectorItem) iItem.next();
if (item.getItemType() == ICSSSelectorItem.SIMPLE) {
ICSSSimpleSelector sel = (ICSSSimpleSelector) item;
int nClasses = sel.getNumOfClasses();
for (int iClass = 0; iClass < nClasses; iClass++) {
String className = sel.getClass(iClass);
if (!fClassNames.contains(className))
fClassNames.add(className);
}
}
}
}
}
use of org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorItem in project webtools.sourceediting by eclipse.
the class SelectorValidator method getOnlyOneSimpleSelector.
private ICSSSimpleSelector getOnlyOneSimpleSelector() {
parse();
if (fSelectorList != null && fSelectorList.getLength() == 1) {
ICSSSelector selector = fSelectorList.getSelector(0);
int nItem = selector.getLength();
if (nItem == 1) {
ICSSSelectorItem item = selector.getItem(0);
if (item instanceof ICSSSimpleSelector) {
return (ICSSSimpleSelector) item;
}
}
}
return null;
}
Aggregations