Search in sources :

Example 1 with IStyleSelectorAdapter

use of org.eclipse.wst.css.core.internal.provisional.adapters.IStyleSelectorAdapter in project webtools.sourceediting by eclipse.

the class CSSSelector method matchExactly.

/**
 * @return boolean
 */
private boolean matchExactly(ICSSSimpleSelector selector, Element element, String pseudoName) {
    IStyleSelectorAdapter adapter = (IStyleSelectorAdapter) ((INodeNotifier) element).getAdapterFor(IStyleSelectorAdapter.class);
    if (adapter != null) {
        return adapter.match(selector, element, pseudoName);
    }
    if (element == null)
        return false;
    int i;
    String key;
    // check tag name
    if (!selector.isUniversal() && !element.getNodeName().equals(selector.getName()))
        return false;
    // check id
    i = selector.getNumOfIDs();
    if (i > 0) {
        if (i > 1)
            return false;
        if (// $NON-NLS-1$ //$NON-NLS-2$
        !element.hasAttribute("id") || (key = element.getAttribute("id")).length() == 0)
            return false;
        if (!selector.getID(0).equals(key))
            return false;
    }
    // check class
    i = selector.getNumOfClasses();
    if (i > 0) {
        if (// $NON-NLS-1$  //$NON-NLS-2$
        !element.hasAttribute("class") || (key = element.getAttribute("class")).length() == 0)
            return false;
        StringTokenizer tokenizer = new StringTokenizer(key);
        for (i = i - 1; i >= 0; i--) {
            boolean ok = false;
            while (tokenizer.hasMoreTokens()) {
                if (selector.getClass(i).equals(tokenizer.nextToken())) {
                    ok = true;
                    break;
                }
            }
            if (!ok)
                return false;
        }
    }
    // check attributes
    for (i = selector.getNumOfAttributes() - 1; i >= 0; i--) {
        // $NON-NLS-1$
        StringTokenizer tokenizer = new StringTokenizer(selector.getAttribute(i), "=~| \t\r\n\f");
        int countTokens = tokenizer.countTokens();
        if (countTokens > 0) {
            String attrName = tokenizer.nextToken();
            String attrValue = null;
            if (!element.hasAttribute(attrName) || (attrValue = element.getAttribute(attrName)).length() == 0)
                return false;
            if (countTokens > 1) {
                // $NON-NLS-1$
                String token = tokenizer.nextToken("= \t\r\n\f");
                StringTokenizer attrValueTokenizer = null;
                if (token.equals("~")) {
                    // $NON-NLS-1$
                    attrValueTokenizer = new StringTokenizer(attrValue);
                } else if (token.equals("|")) {
                    // $NON-NLS-1$
                    // $NON-NLS-1$
                    attrValueTokenizer = new StringTokenizer(attrValue, "-");
                }
                if (attrValueTokenizer != null) {
                    if (tokenizer.hasMoreTokens()) {
                        token = tokenizer.nextToken();
                        boolean ok = false;
                        while (attrValueTokenizer.hasMoreTokens()) {
                            if (token.equals(attrValueTokenizer.nextToken())) {
                                ok = true;
                                break;
                            }
                        }
                        if (!ok)
                            return false;
                    }
                } else {
                    if (!attrValue.equals(token))
                        return false;
                }
            }
        }
    }
    return true;
}
Also used : StringTokenizer(com.ibm.icu.util.StringTokenizer) IStyleSelectorAdapter(org.eclipse.wst.css.core.internal.provisional.adapters.IStyleSelectorAdapter)

Aggregations

StringTokenizer (com.ibm.icu.util.StringTokenizer)1 IStyleSelectorAdapter (org.eclipse.wst.css.core.internal.provisional.adapters.IStyleSelectorAdapter)1