Java Code Examples for com.gargoylesoftware.htmlunit.html.HtmlElement#getAttributeDirect()

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlElement#getAttributeDirect() . 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: HTMLAnchorElement.java    From htmlunit 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 2
Source File: XMLDOMNodeList.java    From htmlunit 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 3
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 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: WebAssert.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Many HTML elements are "tabbable" and can have a <tt>tabindex</tt> attribute
 * that determines the order in which the components are navigated when
 * pressing the tab key. To ensure good usability for keyboard navigation,
 * all tabbable elements should have the <tt>tabindex</tt> attribute set.</p>
 *
 * <p>This method verifies that all tabbable elements have a valid value set for
 * the <tt>tabindex</tt> attribute.</p>
 *
 * @param page the page to check
 */
public static void assertAllTabIndexAttributesSet(final HtmlPage page) {
    final List<String> tags =
        Arrays.asList(new String[] {"a", "area", "button", "input", "object", "select", "textarea"});

    for (final String tag : tags) {
        for (final HtmlElement element : page.getDocumentElement().getElementsByTagName(tag)) {
            final Short tabIndex = element.getTabIndex();
            if (tabIndex == null || tabIndex == HtmlElement.TAB_INDEX_OUT_OF_BOUNDS) {
                final String s = element.getAttributeDirect("tabindex");
                throw new AssertionError("Illegal value for tab index: '" + s + "'.");
            }
        }
    }
}
 
Example 6
Source File: WebAssert.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Many HTML components can have an <tt>accesskey</tt> attribute which defines a hot key for
 * keyboard navigation. This method verifies that all the <tt>accesskey</tt> attributes on the
 * specified page are unique.
 *
 * @param page the page to check
 */
public static void assertAllAccessKeyAttributesUnique(final HtmlPage page) {
    final List<String> list = new ArrayList<>();
    for (final HtmlElement element : page.getHtmlElementDescendants()) {
        final String key = element.getAttributeDirect("accesskey");
        if (key != null && !key.isEmpty()) {
            if (list.contains(key)) {
                throw new AssertionError("The access key '" + key + "' is not unique.");
            }
            list.add(key);
        }
    }
}
 
Example 7
Source File: WebAssert.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Many HTML elements are "tabbable" and can have a <tt>tabindex</tt> attribute
 * that determines the order in which the components are navigated when
 * pressing the tab key. To ensure good usability for keyboard navigation,
 * all tabbable elements should have the <tt>tabindex</tt> attribute set.</p>
 *
 * <p>This method verifies that all tabbable elements have a valid value set for
 * the <tt>tabindex</tt> attribute.</p>
 *
 * @param page the page to check
 */
public static void assertAllTabIndexAttributesSet(final HtmlPage page) {
    final List<String> tags =
        Arrays.asList(new String[] {"a", "area", "button", "input", "object", "select", "textarea"});

    for (final String tag : tags) {
        for (final HtmlElement element : page.getDocumentElement().getElementsByTagName(tag)) {
            final Short tabIndex = element.getTabIndex();
            if (tabIndex == null || tabIndex == HtmlElement.TAB_INDEX_OUT_OF_BOUNDS) {
                final String s = element.getAttributeDirect("tabindex");
                throw new AssertionError("Illegal value for tab index: '" + s + "'.");
            }
        }
    }
}
 
Example 8
Source File: WebAssert.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Many HTML components can have an <tt>accesskey</tt> attribute which defines a hot key for
 * keyboard navigation. This method verifies that all the <tt>accesskey</tt> attributes on the
 * specified page are unique.
 *
 * @param page the page to check
 */
public static void assertAllAccessKeyAttributesUnique(final HtmlPage page) {
    final List<String> list = new ArrayList<>();
    for (final HtmlElement element : page.getHtmlElementDescendants()) {
        final String key = element.getAttributeDirect("accesskey");
        if (key != null && !key.isEmpty()) {
            if (list.contains(key)) {
                throw new AssertionError("The access key '" + key + "' is not unique.");
            }
            list.add(key);
        }
    }
}
 
Example 9
Source File: HTMLBodyElement.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the value of the {@code background} attribute.
 * @return the value of the {@code background} attribute
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms533498.aspx">MSDN Documentation</a>
 */
@JsxGetter
public String getBackground() {
    final HtmlElement node = getDomNodeOrDie();
    return node.getAttributeDirect("background");
}
 
Example 10
Source File: HTMLBodyElement.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the value of the {@code background} attribute.
 * @return the value of the {@code background} attribute
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms533498.aspx">MSDN Documentation</a>
 */
@JsxGetter
public String getBackground() {
    final HtmlElement node = getDomNodeOrDie();
    return node.getAttributeDirect("background");
}