Java Code Examples for com.gargoylesoftware.htmlunit.html.DomNode#hasFeature()

The following examples show how to use com.gargoylesoftware.htmlunit.html.DomNode#hasFeature() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleScriptable.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Builds a new the JavaScript object that corresponds to the specified object.
 * @param domNode the DOM node for which a JS object should be created
 * @return the JavaScript object
 */
@SuppressWarnings("unchecked")
public SimpleScriptable makeScriptableFor(final DomNode domNode) {
    // Get the JS class name for the specified DOM node.
    // Walk up the inheritance chain if necessary.
    Class<? extends SimpleScriptable> javaScriptClass = null;
    if (domNode instanceof HtmlImage && "image".equals(((HtmlImage) domNode).getOriginalQualifiedName())
            && ((HtmlImage) domNode).wasCreatedByJavascript()) {
        if (domNode.hasFeature(HTMLIMAGE_HTMLELEMENT)) {
            javaScriptClass = HTMLElement.class;
        }
        else if (domNode.hasFeature(HTMLIMAGE_HTMLUNKNOWNELEMENT)) {
            javaScriptClass = HTMLUnknownElement.class;
        }
    }
    if (javaScriptClass == null) {
        final JavaScriptEngine javaScriptEngine =
                (JavaScriptEngine) getWindow().getWebWindow().getWebClient().getJavaScriptEngine();
        for (Class<?> c = domNode.getClass(); javaScriptClass == null && c != null; c = c.getSuperclass()) {
            javaScriptClass = (Class<? extends SimpleScriptable>) javaScriptEngine.getJavaScriptClass(c);
        }
    }

    final SimpleScriptable scriptable;
    if (javaScriptClass == null) {
        // We don't have a specific subclass for this element so create something generic.
        scriptable = new HTMLElement();
        if (LOG.isDebugEnabled()) {
            LOG.debug("No JavaScript class found for element <" + domNode.getNodeName() + ">. Using HTMLElement");
        }
    }
    else {
        try {
            scriptable = javaScriptClass.newInstance();
        }
        catch (final Exception e) {
            throw Context.throwAsScriptRuntimeEx(e);
        }
    }
    initParentScope(domNode, scriptable);

    scriptable.setPrototype(getPrototype(javaScriptClass));
    scriptable.setDomNode(domNode);

    return scriptable;
}
 
Example 2
Source File: CSSStyleSheet.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * @param documentMode see {@link HTMLDocument#getDocumentMode()}
 */
private static boolean isValidCondition(final Condition condition, final int documentMode, final DomNode domNode) {
    switch (condition.getConditionType()) {
        case ATTRIBUTE_CONDITION:
        case ID_CONDITION:
        case LANG_CONDITION:
        case ONE_OF_ATTRIBUTE_CONDITION:
        case BEGIN_HYPHEN_ATTRIBUTE_CONDITION:
        case CLASS_CONDITION:
        case PREFIX_ATTRIBUTE_CONDITION:
        case SUBSTRING_ATTRIBUTE_CONDITION:
        case SUFFIX_ATTRIBUTE_CONDITION:
            return true;
        case PSEUDO_CLASS_CONDITION:
            String value = condition.getValue();
            if (value.endsWith(")")) {
                if (value.endsWith("()")) {
                    return false;
                }
                value = value.substring(0, value.indexOf('(') + 1) + ')';
            }
            if (documentMode < 9) {
                return CSS2_PSEUDO_CLASSES.contains(value);
            }

            if (!CSS2_PSEUDO_CLASSES.contains(value)
                    && domNode.hasFeature(QUERYSELECTOR_CSS3_PSEUDO_REQUIRE_ATTACHED_NODE)
                    && !domNode.isAttachedToPage()
                    && !domNode.hasChildNodes()) {
                throw new CSSException("Syntax Error");
            }

            if ("nth-child()".equals(value)) {
                final String arg = StringUtils.substringBetween(condition.getValue(), "(", ")").trim();
                return "even".equalsIgnoreCase(arg) || "odd".equalsIgnoreCase(arg)
                        || NTH_NUMERIC.matcher(arg).matches()
                        || NTH_COMPLEX.matcher(arg).matches();
            }
            return CSS3_PSEUDO_CLASSES.contains(value);
        default:
            if (LOG.isWarnEnabled()) {
                LOG.warn("Unhandled CSS condition type '"
                            + condition.getConditionType() + "'. Accepting it silently.");
            }
            return true;
    }
}
 
Example 3
Source File: SimpleScriptable.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Builds a new the JavaScript object that corresponds to the specified object.
 * @param domNode the DOM node for which a JS object should be created
 * @return the JavaScript object
 */
@SuppressWarnings("unchecked")
public SimpleScriptable makeScriptableFor(final DomNode domNode) {
    // Get the JS class name for the specified DOM node.
    // Walk up the inheritance chain if necessary.
    Class<? extends SimpleScriptable> javaScriptClass = null;
    if (domNode instanceof HtmlImage && "image".equals(((HtmlImage) domNode).getOriginalQualifiedName())
            && ((HtmlImage) domNode).wasCreatedByJavascript()) {
        if (domNode.hasFeature(HTMLIMAGE_HTMLELEMENT)) {
            javaScriptClass = HTMLElement.class;
        }
        else if (domNode.hasFeature(HTMLIMAGE_HTMLUNKNOWNELEMENT)) {
            javaScriptClass = HTMLUnknownElement.class;
        }
    }
    if (javaScriptClass == null) {
        final JavaScriptEngine javaScriptEngine =
                (JavaScriptEngine) getWindow().getWebWindow().getWebClient().getJavaScriptEngine();
        for (Class<?> c = domNode.getClass(); javaScriptClass == null && c != null; c = c.getSuperclass()) {
            javaScriptClass = (Class<? extends SimpleScriptable>) javaScriptEngine.getJavaScriptClass(c);
        }
    }

    final SimpleScriptable scriptable;
    if (javaScriptClass == null) {
        // We don't have a specific subclass for this element so create something generic.
        scriptable = new HTMLElement();
        if (LOG.isDebugEnabled()) {
            LOG.debug("No JavaScript class found for element <" + domNode.getNodeName() + ">. Using HTMLElement");
        }
    }
    else {
        try {
            scriptable = javaScriptClass.newInstance();
        }
        catch (final Exception e) {
            throw Context.throwAsScriptRuntimeEx(e);
        }
    }
    initParentScope(domNode, scriptable);

    scriptable.setPrototype(getPrototype(javaScriptClass));
    scriptable.setDomNode(domNode);

    return scriptable;
}
 
Example 4
Source File: CSSStyleSheet.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * @param documentMode see {@link HTMLDocument#getDocumentMode()}
 */
private static boolean isValidCondition(final Condition condition, final int documentMode, final DomNode domNode) {
    switch (condition.getConditionType()) {
        case ATTRIBUTE_CONDITION:
        case ID_CONDITION:
        case LANG_CONDITION:
        case ONE_OF_ATTRIBUTE_CONDITION:
        case BEGIN_HYPHEN_ATTRIBUTE_CONDITION:
        case CLASS_CONDITION:
            return true;
        case PSEUDO_CLASS_CONDITION:
            final PseudoClassCondition pcc = (PseudoClassCondition) condition;
            String value = pcc.getValue();
            if (value.endsWith(")")) {
                if (value.endsWith("()")) {
                    return false;
                }
                value = value.substring(0, value.indexOf('(') + 1) + ')';
            }
            if (documentMode < 9) {
                return CSS2_PSEUDO_CLASSES.contains(value);
            }

            if (!CSS2_PSEUDO_CLASSES.contains(value)
                    && domNode.hasFeature(QUERYSELECTOR_CSS3_PSEUDO_REQUIRE_ATTACHED_NODE)
                    && !domNode.isAttachedToPage()
                    && !domNode.hasChildNodes()) {
                throw new CSSException("Syntax Error");
            }

            if ("nth-child()".equals(value)) {
                final String arg = StringUtils.substringBetween(pcc.getValue(), "(", ")").trim();
                return "even".equalsIgnoreCase(arg) || "odd".equalsIgnoreCase(arg)
                        || NTH_NUMERIC.matcher(arg).matches()
                        || NTH_COMPLEX.matcher(arg).matches();
            }
            return CSS3_PSEUDO_CLASSES.contains(value);
        default:
            LOG.warn("Unhandled CSS condition type '" + condition.getConditionType() + "'. Accepting it silently.");
            return true;
    }
}