Search in sources :

Example 1 with HtmlAttributeChangeEvent

use of com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent in project htmlunit by HtmlUnit.

the class HTMLDocument method getIt.

private Object getIt(final String name) {
    final HtmlPage page = (HtmlPage) getDomNodeOrNull();
    if (page == null) {
        return NOT_FOUND;
    }
    final boolean forIDAndOrName = getBrowserVersion().hasFeature(HTMLDOCUMENT_GET_FOR_ID_AND_OR_NAME);
    final boolean alsoFrames = getBrowserVersion().hasFeature(HTMLDOCUMENT_GET_ALSO_FRAMES);
    // for performance
    // we will calculate the elements to decide if we really have
    // to really create a HTMLCollection or not
    final List<DomNode> matchingElements = getItComputeElements(page, name, forIDAndOrName, alsoFrames);
    final int size = matchingElements.size();
    if (size == 0) {
        return NOT_FOUND;
    }
    if (size == 1) {
        final DomNode object = matchingElements.get(0);
        if (alsoFrames && object instanceof BaseFrameElement) {
            return ((BaseFrameElement) object).getEnclosedWindow().getScriptableObject();
        }
        return super.getScriptableFor(object);
    }
    return new HTMLCollection(page, matchingElements) {

        @Override
        protected List<DomNode> computeElements() {
            return getItComputeElements(page, name, forIDAndOrName, alsoFrames);
        }

        @Override
        protected EffectOnCache getEffectOnCache(final HtmlAttributeChangeEvent event) {
            final String attributeName = event.getName();
            if ("name".equals(attributeName) || (forIDAndOrName && "id".equals(attributeName))) {
                return EffectOnCache.RESET;
            }
            return EffectOnCache.NONE;
        }

        @Override
        protected HtmlUnitScriptable getScriptableFor(final Object object) {
            if (alsoFrames && object instanceof BaseFrameElement) {
                return ((BaseFrameElement) object).getEnclosedWindow().getScriptableObject();
            }
            return super.getScriptableFor(object);
        }
    };
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) BaseFrameElement(com.gargoylesoftware.htmlunit.html.BaseFrameElement) HtmlAttributeChangeEvent(com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)

Example 2 with HtmlAttributeChangeEvent

use of com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent in project htmlunit by HtmlUnit.

the class HTMLDocument method getElementsByName.

/**
 * {@inheritDoc}
 */
@Override
@JsxFunction({ FF, FF_ESR })
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;
        }
    };
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) ArrayList(java.util.ArrayList) HtmlAttributeChangeEvent(com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 3 with HtmlAttributeChangeEvent

use of com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent in project htmlunit by HtmlUnit.

the class HTMLFormElement method getElements.

/**
 * Returns the value of the property {@code elements}.
 * @return the value of this property
 */
@JsxGetter
public HTMLCollection getElements() {
    final HtmlForm htmlForm = getHtmlForm();
    return new HTMLCollection(htmlForm, false) {

        private boolean filterChildrenOfNestedForms_;

        @Override
        protected List<DomNode> computeElements() {
            final List<DomNode> response = super.computeElements();
            // of the cases
            if (filterChildrenOfNestedForms_) {
                for (final Iterator<DomNode> iter = response.iterator(); iter.hasNext(); ) {
                    final HtmlElement field = (HtmlElement) iter.next();
                    if (field.getEnclosingForm() != htmlForm) {
                        iter.remove();
                    }
                }
            }
            response.addAll(htmlForm.getLostChildren());
            return response;
        }

        @Override
        protected Object getWithPreemption(final String name) {
            return HTMLFormElement.this.getWithPreemption(name);
        }

        @Override
        public EffectOnCache getEffectOnCache(final HtmlAttributeChangeEvent event) {
            return EffectOnCache.NONE;
        }

        @Override
        protected boolean isMatching(final DomNode node) {
            if (node instanceof HtmlForm) {
                filterChildrenOfNestedForms_ = true;
                return false;
            }
            return node instanceof HtmlInput || node instanceof HtmlButton || node instanceof HtmlTextArea || node instanceof HtmlSelect;
        }
    };
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HtmlForm(com.gargoylesoftware.htmlunit.html.HtmlForm) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HtmlSelect(com.gargoylesoftware.htmlunit.html.HtmlSelect) HtmlAttributeChangeEvent(com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent) HtmlInput(com.gargoylesoftware.htmlunit.html.HtmlInput) HtmlTextArea(com.gargoylesoftware.htmlunit.html.HtmlTextArea) HtmlButton(com.gargoylesoftware.htmlunit.html.HtmlButton) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)

Example 4 with HtmlAttributeChangeEvent

use of com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent in project htmlunit by HtmlUnit.

the class HTMLCollectionFrames method getElementsByName.

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);
    elements.removeIf(domElement -> !filter.matches(domElement));
    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 (final 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;
        }
    };
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HTMLCollection(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCollection) ArrayList(java.util.ArrayList) HtmlAttributeChangeEvent(com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent)

Aggregations

DomNode (com.gargoylesoftware.htmlunit.html.DomNode)4 HtmlAttributeChangeEvent (com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent)4 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)2 ArrayList (java.util.ArrayList)2 BaseFrameElement (com.gargoylesoftware.htmlunit.html.BaseFrameElement)1 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)1 HtmlButton (com.gargoylesoftware.htmlunit.html.HtmlButton)1 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)1 HtmlForm (com.gargoylesoftware.htmlunit.html.HtmlForm)1 HtmlInput (com.gargoylesoftware.htmlunit.html.HtmlInput)1 HtmlSelect (com.gargoylesoftware.htmlunit.html.HtmlSelect)1 HtmlTextArea (com.gargoylesoftware.htmlunit.html.HtmlTextArea)1 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)1 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)1 HTMLCollection (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCollection)1 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)1