Java Code Examples for com.gargoylesoftware.htmlunit.html.DomElement#ATTRIBUTE_NOT_DEFINED

The following examples show how to use com.gargoylesoftware.htmlunit.html.DomElement#ATTRIBUTE_NOT_DEFINED . 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: XmlUtils.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Search for the namespace URI of the given prefix, starting from the specified element.
 * The default namespace can be searched for by specifying "" as the prefix.
 * @param element the element to start searching from
 * @param prefix the namespace prefix
 * @return the namespace URI bound to the prefix; or null if there is no such namespace
 */
public static String lookupNamespaceURI(final DomElement element, final String prefix) {
    String uri = DomElement.ATTRIBUTE_NOT_DEFINED;
    if (prefix.isEmpty()) {
        uri = element.getAttributeDirect("xmlns");
    }
    else {
        uri = element.getAttribute("xmlns:" + prefix);
    }
    if (uri == DomElement.ATTRIBUTE_NOT_DEFINED) {
        final DomNode parentNode = element.getParentNode();
        if (parentNode instanceof DomElement) {
            uri = lookupNamespaceURI((DomElement) parentNode, prefix);
        }
    }
    return uri;
}
 
Example 2
Source File: HTMLAnchorElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the value of this link's {@code href} property.
 * @return the value of this link's {@code href} property
 */
@JsxGetter
public String getHref() {
    final HtmlAnchor anchor = (HtmlAnchor) getDomNodeOrDie();
    final String hrefAttr = anchor.getHrefAttribute();

    if (hrefAttr == DomElement.ATTRIBUTE_NOT_DEFINED) {
        return "";
    }

    try {
        return getUrl().toString();
    }
    catch (final MalformedURLException e) {
        return hrefAttr;
    }
}
 
Example 3
Source File: HTMLFormElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the value of the property {@code action}.
 * @return the value of this property
 */
@JsxGetter
public String getAction() {
    final String action = getHtmlForm().getActionAttribute();

    if (action == DomElement.ATTRIBUTE_NOT_DEFINED
            && !getBrowserVersion().hasFeature(JS_FORM_ACTION_EXPANDURL_NOT_DEFINED)) {
        return action;
    }

    try {
        return ((HtmlPage) getHtmlForm().getPage()).getFullyQualifiedUrl(action).toExternalForm();
    }
    catch (final MalformedURLException e) {
        // nothing, return action attribute
    }
    return action;
}
 
Example 4
Source File: XMLDOMNodeList.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Adds the ids of the collection's elements to the idList.
 * @param idList the list to add the ids to
 * @param elements the collection's elements
 */
protected void addElementIds(final List<String> idList, final List<DomNode> elements) {
    int index = 0;
    for (final DomNode next : elements) {
        final HtmlElement element = (HtmlElement) next;
        final String name = element.getAttributeDirect("name");
        if (name != DomElement.ATTRIBUTE_NOT_DEFINED) {
            idList.add(name);
        }
        else {
            final String id = element.getId();
            if (id != DomElement.ATTRIBUTE_NOT_DEFINED) {
                idList.add(id);
            }
            else {
                idList.add(Integer.toString(index));
            }
        }
        index++;
    }
}
 
Example 5
Source File: HTMLAnchorElement.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
static String getDefaultValue(final HtmlElement element) {
    String href = element.getAttributeDirect("href");

    if (DomElement.ATTRIBUTE_NOT_DEFINED == href) {
        return ""; // for example for named anchors
    }

    href = href.trim();

    final SgmlPage page = element.getPage();
    if (page == null || !page.isHtmlPage()) {
        return href;
    }

    try {
        return HtmlAnchor.getTargetUrl(href, (HtmlPage) page).toExternalForm();
    }
    catch (final MalformedURLException e) {
        return href;
    }
}
 
Example 6
Source File: LabelsHelper.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * This is overridden instead of {@link #computeElements()} in order to prevent caching at all.
 *
 * {@inheritDoc}
 */
@Override
public List<DomNode> getElements() {
    final List<DomNode> response = new ArrayList<>();
    final DomElement domElement = (DomElement) getDomNodeOrDie();
    for (DomNode parent = domElement.getParentNode(); parent != null; parent = parent.getParentNode()) {
        if (parent instanceof HtmlLabel) {
            response.add(parent);
        }
    }
    final String id = domElement.getId();
    if (id != DomElement.ATTRIBUTE_NOT_DEFINED) {
        for (final DomElement label : domElement.getHtmlPageOrNull().getElementsByTagName("label")) {
            if (id.equals(label.getAttributeDirect("for"))) {
                response.add(label);
            }
        }
    }

    return response;
}
 
Example 7
Source File: HTMLDocument.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
@Override
@JsxGetter({CHROME, FF, FF68, FF60})
public String getVlinkColor() {
    String color = getPage().getBody().getAttribute("vLink");
    if (color == DomElement.ATTRIBUTE_NOT_DEFINED && getBrowserVersion().hasFeature(HTMLDOCUMENT_COLOR)) {
        color = "#800080";
    }
    if (getBrowserVersion().hasFeature(HTML_COLOR_EXPAND_ZERO) && "#0".equals(color)) {
        color = "#000000";
    }
    return color;
}
 
Example 8
Source File: HTMLAnchorElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the focus to this element.
 */
@Override
public void focus() {
    final HtmlAnchor anchor = (HtmlAnchor) getDomNodeOrDie();
    final String hrefAttr = anchor.getHrefAttribute();

    if (hrefAttr != DomElement.ATTRIBUTE_NOT_DEFINED) {
        anchor.focus();
    }
}
 
Example 9
Source File: HTMLElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@code contentEditable} property.
 * @return the {@code contentEditable} property
 */
@JsxGetter
public String getContentEditable() {
    final String attribute = getDomNodeOrDie().getAttribute("contentEditable");
    if (attribute == DomElement.ATTRIBUTE_NOT_DEFINED) {
        return "inherit";
    }
    if (attribute == DomElement.ATTRIBUTE_VALUE_EMPTY) {
        return "true";
    }
    return attribute;
}
 
Example 10
Source File: HTMLOptGroupElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the value of the {@code label} property.
 * @return the value of the {@code label} property
 */
@JsxGetter
public String getLabel() {
    final String label = getDomNodeOrDie().getAttributeDirect("label");
    if (DomElement.ATTRIBUTE_NOT_DEFINED == label) {
        return "";
    }
    return label;
}
 
Example 11
Source File: Element.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns all the descendant elements with the specified class.
 * @param className the name to search for
 * @return all the descendant elements with the specified class name
 */
@JsxFunction({CHROME, FF})
public HTMLCollection getElementsByClassName(final String className) {
    final DomElement elt = getDomNodeOrDie();
    final String[] classNames = CLASS_NAMES_SPLIT_PATTERN.split(className, 0);

    final HTMLCollection collection = new HTMLCollection(elt, true) {
        @Override
        protected boolean isMatching(final DomNode node) {
            if (!(node instanceof HtmlElement)) {
                return false;
            }
            String classAttribute = ((HtmlElement) node).getAttributeDirect("class");
            if (classAttribute == DomElement.ATTRIBUTE_NOT_DEFINED) {
                return false; // probably better performance as most of elements won't have a class attribute
            }

            classAttribute = " " + classAttribute + " ";
            for (final String aClassName : classNames) {
                if (!classAttribute.contains(" " + aClassName + " ")) {
                    return false;
                }
            }
            return true;
        }
    };

    return collection;
}
 
Example 12
Source File: HTMLAreaElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the focus to this element.
 */
@Override
public void focus() {
    // in reality this depends also on the visibility of the area itself
    final HtmlArea area = (HtmlArea) getDomNodeOrDie();
    final String hrefAttr = area.getHrefAttribute();

    if (hrefAttr != DomElement.ATTRIBUTE_NOT_DEFINED
            || getBrowserVersion().hasFeature(JS_AREA_WITHOUT_HREF_FOCUSABLE)) {
        area.focus();
    }
}
 
Example 13
Source File: HTMLDocument.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
@Override
@JsxGetter({CHROME, FF, FF68, FF60})
public String getFgColor() {
    String color = getPage().getBody().getAttributeDirect("text");
    if (color == DomElement.ATTRIBUTE_NOT_DEFINED && getBrowserVersion().hasFeature(HTMLDOCUMENT_COLOR)) {
        color = "#000000";
    }
    if (getBrowserVersion().hasFeature(HTML_COLOR_EXPAND_ZERO) && "#0".equals(color)) {
        color = "#000000";
    }
    return color;
}
 
Example 14
Source File: Node.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * If we have added a new iframe that
 * had no source attribute, we have to take care the
 * 'onload' handler is triggered.
 *
 * @param childDomNode
 */
private static void initInlineFrameIfNeeded(final DomNode childDomNode) {
    if (childDomNode instanceof HtmlInlineFrame) {
        final HtmlInlineFrame frame = (HtmlInlineFrame) childDomNode;
        if (DomElement.ATTRIBUTE_NOT_DEFINED == frame.getSrcAttribute()) {
            frame.loadInnerPage();
        }
    }
}
 
Example 15
Source File: HTMLOptGroupElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the value of the {@code label} property.
 * @return the value of the {@code label} property
 */
@JsxGetter
public String getLabel() {
    final String label = getDomNodeOrDie().getAttributeDirect("label");
    if (DomElement.ATTRIBUTE_NOT_DEFINED == label) {
        return "";
    }
    return label;
}
 
Example 16
Source File: HTMLDocument.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
@Override
@JsxGetter({CHROME, FF, FF68, FF60})
public String getAlinkColor() {
    String color = getPage().getBody().getAttribute("aLink");
    if (color == DomElement.ATTRIBUTE_NOT_DEFINED && getBrowserVersion().hasFeature(HTMLDOCUMENT_COLOR)) {
        color = "#0000ff";
    }
    if (getBrowserVersion().hasFeature(HTML_COLOR_EXPAND_ZERO) && "#0".equals(color)) {
        color = "#000000";
    }
    return color;
}
 
Example 17
Source File: HTMLDocument.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
@Override
@JsxGetter({CHROME, FF})
public String getLinkColor() {
    String color = getPage().getBody().getAttributeDirect("link");
    if (color == DomElement.ATTRIBUTE_NOT_DEFINED && getBrowserVersion().hasFeature(HTMLDOCUMENT_COLOR)) {
        color = "#0000ff";
    }
    if (getBrowserVersion().hasFeature(HTML_COLOR_EXPAND_ZERO) && "#0".equals(color)) {
        color = "#000000";
    }
    return color;
}
 
Example 18
Source File: HTMLAnchorElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the focus to this element.
 */
@Override
public void focus() {
    final HtmlAnchor anchor = (HtmlAnchor) getDomNodeOrDie();
    final String hrefAttr = anchor.getHrefAttribute();

    if (hrefAttr != DomElement.ATTRIBUTE_NOT_DEFINED) {
        anchor.focus();
    }
}
 
Example 19
Source File: DOMStringMap.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Object get(final String name, final Scriptable start) {
    final HtmlElement e = (HtmlElement) getDomNodeOrNull();
    if (e != null) {
        final String value = e.getAttribute("data-" + decamelize(name));
        if (value != DomElement.ATTRIBUTE_NOT_DEFINED) {
            return value;
        }
    }
    return NOT_FOUND;
}
 
Example 20
Source File: Node.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * If we have added a new iframe that
 * had no source attribute, we have to take care the
 * 'onload' handler is triggered.
 *
 * @param childDomNode
 */
private static void initInlineFrameIfNeeded(final DomNode childDomNode) {
    if (childDomNode instanceof HtmlInlineFrame) {
        final HtmlInlineFrame frame = (HtmlInlineFrame) childDomNode;
        if (DomElement.ATTRIBUTE_NOT_DEFINED == frame.getSrcAttribute()) {
            frame.loadInnerPage();
        }
    }
}