Java Code Examples for com.gargoylesoftware.htmlunit.html.HtmlPage#getElementsByName()

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlPage#getElementsByName() . 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: HTMLDocument.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
@Override
@JsxFunction({FF, FF68, FF60})
public HTMLCollection getElementsByName(final String elementName) {
    implicitCloseIfNecessary();
    if ("null".equals(elementName)
            || (elementName.isEmpty()
                && getBrowserVersion().hasFeature(HTMLDOCUMENT_ELEMENTS_BY_NAME_EMPTY))) {
        return HTMLCollection.emptyCollection(getWindow().getDomNodeOrDie());
    }

    final HtmlPage page = getPage();
    return new HTMLCollection(page, true) {
        @Override
        protected List<DomNode> computeElements() {
            return new ArrayList<>(page.getElementsByName(elementName));
        }

        @Override
        protected EffectOnCache getEffectOnCache(final HtmlAttributeChangeEvent event) {
            if ("name".equals(event.getName())) {
                return EffectOnCache.RESET;
            }
            return EffectOnCache.NONE;
        }
    };
}
 
Example 2
Source File: HTMLDocument.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
private static List<DomNode> getItComputeElements(final HtmlPage page, final String name,
        final boolean forIDAndOrName, final boolean alsoFrames) {
    final List<DomElement> elements;
    if (forIDAndOrName) {
        elements = page.getElementsByIdAndOrName(name);
    }
    else {
        elements = page.getElementsByName(name);
    }
    final List<DomNode> matchingElements = new ArrayList<>();
    for (final DomElement elt : elements) {
        if (elt instanceof HtmlForm || elt instanceof HtmlImage || elt instanceof HtmlApplet
                || (alsoFrames && elt instanceof BaseFrameElement)) {
            matchingElements.add(elt);
        }
    }
    return matchingElements;
}
 
Example 3
Source File: HTMLDocument.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
private List<DomNode> getItComputeElements(final HtmlPage page, final String name,
        final boolean forIDAndOrName, final boolean alsoFrames) {
    final List<DomElement> elements;
    if (forIDAndOrName) {
        elements = page.getElementsByIdAndOrName(name);
    }
    else {
        elements = page.getElementsByName(name);
    }
    final List<DomNode> matchingElements = new ArrayList<>();
    for (final DomElement elt : elements) {
        if (elt instanceof HtmlForm || elt instanceof HtmlImage
                || (alsoFrames && elt instanceof BaseFrameElement)) {
            matchingElements.add(elt);
        }
    }
    return matchingElements;
}
 
Example 4
Source File: HTMLDocument.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@JsxFunction(FF)
public HTMLCollection getElementsByName(final String elementName) {
    implicitCloseIfNecessary();
    if ("null".equals(elementName)) {
        return HTMLCollection.emptyCollection(getWindow().getDomNodeOrDie());
    }
    // Null must me changed to '' for proper collection initialization.
    final String expElementName = "null".equals(elementName) ? "" : elementName;

    final HtmlPage page = getPage();
    final HTMLCollection collection = new HTMLCollection(page, true) {
        @Override
        protected List<DomNode> computeElements() {
            return new ArrayList<DomNode>(page.getElementsByName(expElementName));
        }

        @Override
        protected EffectOnCache getEffectOnCache(final HtmlAttributeChangeEvent event) {
            if ("name".equals(event.getName())) {
                return EffectOnCache.RESET;
            }
            return EffectOnCache.NONE;
        }
    };

    return collection;
}
 
Example 5
Source File: UiIntegrationTest.java    From ec2-spot-jenkins-plugin with Apache License 2.0 5 votes vote down vote up
private static List<DomElement> getElementsByNameWithoutJdk(HtmlPage page, String name) {
    String jdkCheckUrl = "/jenkins/descriptorByName/hudson.model.JDK/checkName";
    List<DomElement> r = new ArrayList<>();
    for (DomElement domElement : page.getElementsByName(name)) {
        if (!jdkCheckUrl.equals(domElement.getAttribute("checkurl"))) {
            r.add(domElement);
        }
    }
    return r;
}
 
Example 6
Source File: Window.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
private Object getElementsByName(final HtmlPage page, final String name) {

        // May be attempting to retrieve element(s) by name. IMPORTANT: We're using map-backed operations
        // like getHtmlElementsByName() and getHtmlElementById() as much as possible, so as to avoid XPath
        // overhead. We only use an XPath-based operation when we have to (where there is more than one
        // matching element). This optimization appears to improve performance in certain situations by ~15%
        // vs using XPath-based operations throughout.
        final List<DomElement> elements = page.getElementsByName(name);

        final boolean includeFormFields = getBrowserVersion().hasFeature(JS_WINDOW_FORMFIELDS_ACCESSIBLE_BY_NAME);
        final Filter filter = new Filter(includeFormFields);

        final Iterator<DomElement> it = elements.iterator();
        while (it.hasNext()) {
            if (!filter.matches(it.next())) {
                it.remove();
            }
        }

        if (elements.isEmpty()) {
            return NOT_FOUND;
        }

        if (elements.size() == 1) {
            return getScriptableFor(elements.get(0));
        }

        // Null must be changed to '' for proper collection initialization.
        final String expElementName = "null".equals(name) ? "" : name;

        return new HTMLCollection(page, true) {
            @Override
            protected List<DomNode> computeElements() {
                final List<DomElement> expElements = page.getElementsByName(expElementName);
                final List<DomNode> result = new ArrayList<>(expElements.size());

                for (DomElement domElement : expElements) {
                    if (filter.matches(domElement)) {
                        result.add(domElement);
                    }
                }
                return result;
            }

            @Override
            protected EffectOnCache getEffectOnCache(final HtmlAttributeChangeEvent event) {
                if ("name".equals(event.getName())) {
                    return EffectOnCache.RESET;
                }
                return EffectOnCache.NONE;
            }
        };
    }
 
Example 7
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
private Object getElementsByName(final HtmlPage page, final String name) {

        // May be attempting to retrieve element(s) by name. IMPORTANT: We're using map-backed operations
        // like getHtmlElementsByName() and getHtmlElementById() as much as possible, so as to avoid XPath
        // overhead. We only use an XPath-based operation when we have to (where there is more than one
        // matching element). This optimization appears to improve performance in certain situations by ~15%
        // vs using XPath-based operations throughout.
        final List<DomElement> elements = page.getElementsByName(name);

        final boolean includeFormFields = getBrowserVersion().hasFeature(JS_WINDOW_FORMFIELDS_ACCESSIBLE_BY_NAME);
        final Filter filter = new Filter(includeFormFields);

        final Iterator<DomElement> it = elements.iterator();
        while (it.hasNext()) {
            if (!filter.matches(it.next())) {
                it.remove();
            }
        }

        if (elements.isEmpty()) {
            return NOT_FOUND;
        }

        if (elements.size() == 1) {
            return getScriptableFor(elements.get(0));
        }

        // Null must be changed to '' for proper collection initialization.
        final String expElementName = "null".equals(name) ? "" : name;

        return new HTMLCollection(page, true) {
            @Override
            protected List<DomNode> computeElements() {
                final List<DomElement> expElements = page.getElementsByName(expElementName);
                final List<DomNode> result = new ArrayList<>(expElements.size());

                for (DomElement domElement : expElements) {
                    if (filter.matches(domElement)) {
                        result.add(domElement);
                    }
                }
                return result;
            }

            @Override
            protected EffectOnCache getEffectOnCache(final HtmlAttributeChangeEvent event) {
                if ("name".equals(event.getName())) {
                    return EffectOnCache.RESET;
                }
                return EffectOnCache.NONE;
            }
        };
    }