Search in sources :

Example 1 with ICSSSelectorItem

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;
}
Also used : Element(org.w3c.dom.Element) ICSSSimpleSelector(org.eclipse.wst.css.core.internal.provisional.document.ICSSSimpleSelector) ICSSSelectorCombinator(org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorCombinator) ICSSSelectorItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorItem)

Example 2 with ICSSSelectorItem

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();
}
Also used : ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ICSSSimpleSelector(org.eclipse.wst.css.core.internal.provisional.document.ICSSSimpleSelector) ICSSSelectorItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorItem)

Example 3 with ICSSSelectorItem

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;
}
Also used : ICSSSimpleSelector(org.eclipse.wst.css.core.internal.provisional.document.ICSSSimpleSelector) Iterator(java.util.Iterator) ICSSSelectorItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorItem)

Example 4 with ICSSSelectorItem

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);
                }
            }
        }
    }
}
Also used : ICSSSelectorList(org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorList) Iterator(java.util.Iterator) ICSSSelector(org.eclipse.wst.css.core.internal.provisional.document.ICSSSelector) ICSSSimpleSelector(org.eclipse.wst.css.core.internal.provisional.document.ICSSSimpleSelector) ICSSSelectorItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorItem)

Example 5 with ICSSSelectorItem

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;
}
Also used : ICSSSelector(org.eclipse.wst.css.core.internal.provisional.document.ICSSSelector) ICSSSimpleSelector(org.eclipse.wst.css.core.internal.provisional.document.ICSSSimpleSelector) ICSSSelectorItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorItem)

Aggregations

ICSSSelectorItem (org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorItem)47 ICSSSelector (org.eclipse.wst.css.core.internal.provisional.document.ICSSSelector)42 ICSSSelectorList (org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorList)41 ICSSSimpleSelector (org.eclipse.wst.css.core.internal.provisional.document.ICSSSimpleSelector)6 Iterator (java.util.Iterator)4 ICSSSelectorCombinator (org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorCombinator)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Element (org.w3c.dom.Element)1