Search in sources :

Example 1 with ICSSSelectorCombinator

use of org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorCombinator 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)

Aggregations

ICSSSelectorCombinator (org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorCombinator)1 ICSSSelectorItem (org.eclipse.wst.css.core.internal.provisional.document.ICSSSelectorItem)1 ICSSSimpleSelector (org.eclipse.wst.css.core.internal.provisional.document.ICSSSimpleSelector)1 Element (org.w3c.dom.Element)1