com.gargoylesoftware.htmlunit.html.BaseFrameElement Java Examples

The following examples show how to use com.gargoylesoftware.htmlunit.html.BaseFrameElement. 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: Window.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
@Override
protected Object getWithPreemption(final String name) {
    final List<DomNode> elements = getElements();

    for (final Object next : elements) {
        final BaseFrameElement frameElt = (BaseFrameElement) next;
        final WebWindow window = frameElt.getEnclosedWindow();
        if (name.equals(window.getName())) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Property \"" + name + "\" evaluated (by name) to " + window);
            }
            return getScriptableForElement(window);
        }
        if (getBrowserVersion().hasFeature(JS_WINDOW_FRAMES_ACCESSIBLE_BY_ID) && frameElt.getId().equals(name)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Property \"" + name + "\" evaluated (by id) to " + window);
            }
            return getScriptableForElement(window);
        }
    }

    return NOT_FOUND;
}
 
Example #2
Source File: HTMLDocument.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the specified element as the document's active element.
 * @see HTMLElement#setActive()
 * @param element the new active element for this document
 */
public void setActiveElement(final HTMLElement element) {
    // TODO update page focus element also

    activeElement_ = element;

    if (element != null) {
        // if this is part of an iFrame, make the iFrame tag the
        // active element of his doc
        final WebWindow window = element.getDomNodeOrDie().getPage().getEnclosingWindow();
        if (window instanceof FrameWindow) {
            final BaseFrameElement frame = ((FrameWindow) window).getFrameElement();
            if (frame instanceof HtmlInlineFrame) {
                final Window winWithFrame = (Window) frame.getPage().getEnclosingWindow().getScriptableObject();
                ((HTMLDocument) winWithFrame.getDocument()).setActiveElement(
                            (HTMLElement) frame.getScriptableObject());
            }
        }
    }
}
 
Example #3
Source File: Window.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
@Override
protected Object getWithPreemption(final String name) {
    final List<DomNode> elements = getElements();

    for (final Object next : elements) {
        final BaseFrameElement frameElt = (BaseFrameElement) next;
        final WebWindow window = frameElt.getEnclosedWindow();
        if (name.equals(window.getName())) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Property \"" + name + "\" evaluated (by name) to " + window);
            }
            return getScriptableForElement(window);
        }
        if (getBrowserVersion().hasFeature(JS_WINDOW_FRAMES_ACCESSIBLE_BY_ID) && frameElt.getId().equals(name)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Property \"" + name + "\" evaluated (by id) to " + window);
            }
            return getScriptableForElement(window);
        }
    }

    return NOT_FOUND;
}
 
Example #4
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 #5
Source File: HTMLDocument.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the specified element as the document's active element.
 * @see HTMLElement#setActive()
 * @param element the new active element for this document
 */
public void setActiveElement(final HTMLElement element) {
    // TODO update page focus element also

    activeElement_ = element;

    if (element != null) {
        // if this is part of an iFrame, make the iFrame tag the
        // active element of his doc
        final WebWindow window = element.getDomNodeOrDie().getPage().getEnclosingWindow();
        if (window instanceof FrameWindow) {
            final BaseFrameElement frame = ((FrameWindow) window).getFrameElement();
            if (frame instanceof HtmlInlineFrame) {
                final Window winWithFrame = frame.getPage().getEnclosingWindow().getScriptableObject();
                ((HTMLDocument) winWithFrame.getDocument()).setActiveElement(
                            (HTMLElement) frame.getScriptableObject());
            }
        }
    }
}
 
Example #6
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 #7
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
@Override
protected void addElementIds(final List<String> idList, final List<DomNode> elements) {
    for (final DomNode next : elements) {
        final BaseFrameElement frameElt = (BaseFrameElement) next;
        final WebWindow window = frameElt.getEnclosedWindow();
        final String windowName = window.getName();
        if (windowName != null) {
            idList.add(windowName);
        }
    }
}
 
Example #8
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
@Override
protected Scriptable getScriptableForElement(final Object obj) {
    final WebWindow window;
    if (obj instanceof BaseFrameElement) {
        window = ((BaseFrameElement) obj).getEnclosedWindow();
    }
    else {
        window = ((FrameWindow) obj).getFrameElement().getEnclosedWindow();
    }

    return window.getScriptableObject();
}
 
Example #9
Source File: WebClient.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void webWindowContentChanged(final WebWindowEvent event) {
    final WebWindow window = event.getWebWindow();
    boolean use = false;
    if (window instanceof DialogWindow) {
        use = true;
    }
    else if (window instanceof TopLevelWindow) {
        use = event.getOldPage() == null;
    }
    else if (window instanceof FrameWindow) {
        final FrameWindow fw = (FrameWindow) window;
        final String enclosingPageState = fw.getEnclosingPage().getDocumentElement().getReadyState();
        final URL frameUrl = fw.getEnclosedPage().getUrl();
        if (!DomNode.READY_STATE_COMPLETE.equals(enclosingPageState) || frameUrl == URL_ABOUT_BLANK) {
            return;
        }

        // now looks at the visibility of the frame window
        final BaseFrameElement frameElement = fw.getFrameElement();
        if (frameElement.isDisplayed()) {
            final Object element = frameElement.getScriptableObject();
            final HTMLElement htmlElement = (HTMLElement) element;
            final ComputedCSSStyleDeclaration style =
                    htmlElement.getWindow().getComputedStyle(htmlElement, null);
            use = style.getCalculatedWidth(false, false) != 0
                    && style.getCalculatedHeight(false, false) != 0;
        }
    }
    if (use) {
        webClient_.setCurrentWindow(window);
    }
}
 
Example #10
Source File: WebClient.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void webWindowContentChanged(final WebWindowEvent event) {
    final WebWindow window = event.getWebWindow();
    boolean use = false;
    if (window instanceof DialogWindow) {
        use = true;
    }
    else if (window instanceof TopLevelWindow) {
        use = event.getOldPage() == null;
    }
    else if (window instanceof FrameWindow) {
        final FrameWindow fw = (FrameWindow) window;
        final String enclosingPageState = fw.getEnclosingPage().getDocumentElement().getReadyState();
        final URL frameUrl = fw.getEnclosedPage().getUrl();
        if (!DomNode.READY_STATE_COMPLETE.equals(enclosingPageState) || frameUrl == URL_ABOUT_BLANK) {
            return;
        }

        // now looks at the visibility of the frame window
        final BaseFrameElement frameElement = fw.getFrameElement();
        if (webClient_.isJavaScriptEnabled() && frameElement.isDisplayed()) {
            final Object element = frameElement.getScriptableObject();
            final HTMLElement htmlElement = (HTMLElement) element;
            final ComputedCSSStyleDeclaration style =
                    htmlElement.getWindow().getComputedStyle(htmlElement, null);
            use = style.getCalculatedWidth(false, false) != 0
                    && style.getCalculatedHeight(false, false) != 0;
        }
    }
    if (use) {
        webClient_.setCurrentWindow(window);
    }
}
 
Example #11
Source File: HTMLDocument.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
@Override
@JsxFunction({FF, FF68, FF60})
public void close() throws IOException {
    if (writeInCurrentDocument_) {
        LOG.warn("close() called when document is not open.");
    }
    else {
        final HtmlPage page = getPage();
        final URL url = page.getUrl();
        final StringWebResponse webResponse = new StringWebResponse(writeBuilder_.toString(), url);
        webResponse.setFromJavascript(true);
        writeInCurrentDocument_ = true;
        writeBuilder_.setLength(0);

        final WebClient webClient = page.getWebClient();
        final WebWindow window = page.getEnclosingWindow();
        // reset isAttachedToPageDuringOnload_ to trigger the onload event for chrome also
        if (window instanceof FrameWindow) {
            final BaseFrameElement frame = ((FrameWindow) window).getFrameElement();
            final ScriptableObject scriptable = frame.getScriptableObject();
            if (scriptable instanceof HTMLIFrameElement) {
                ((HTMLIFrameElement) scriptable).onRefresh();
            }
        }
        webClient.loadWebResponseInto(webResponse, window);
    }
}
 
Example #12
Source File: HTMLDocument.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@JsxFunction(FF)
public void close() throws IOException {
    if (writeInCurrentDocument_) {
        LOG.warn("close() called when document is not open.");
    }
    else {
        final HtmlPage page = getPage();
        final URL url = page.getUrl();
        final StringWebResponse webResponse = new StringWebResponse(writeBuilder_.toString(), url);
        webResponse.setFromJavascript(true);
        writeInCurrentDocument_ = true;
        writeBuilder_.setLength(0);

        final WebClient webClient = page.getWebClient();
        final WebWindow window = page.getEnclosingWindow();
        // reset isAttachedToPageDuringOnload_ to trigger the onload event for chrome also
        if (window instanceof FrameWindow) {
            final BaseFrameElement frame = ((FrameWindow) window).getFrameElement();
            final ScriptableObject scriptable = frame.getScriptableObject();
            if (scriptable instanceof HTMLIFrameElement) {
                ((HTMLIFrameElement) scriptable).onRefresh();
            }
        }
        webClient.loadWebResponseInto(webResponse, window);
    }
}
 
Example #13
Source File: Window.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
@Override
protected void addElementIds(final List<String> idList, final List<DomNode> elements) {
    for (final DomNode next : elements) {
        final BaseFrameElement frameElt = (BaseFrameElement) next;
        final WebWindow window = frameElt.getEnclosedWindow();
        final String windowName = window.getName();
        if (windowName != null) {
            idList.add(windowName);
        }
    }
}
 
Example #14
Source File: Window.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
@Override
protected Scriptable getScriptableForElement(final Object obj) {
    final WebWindow window;
    if (obj instanceof BaseFrameElement) {
        window = ((BaseFrameElement) obj).getEnclosedWindow();
    }
    else {
        window = ((FrameWindow) obj).getFrameElement().getEnclosedWindow();
    }

    return window.getScriptableObject();
}
 
Example #15
Source File: HTMLFrameElement.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
private BaseFrameElement getFrame() {
    return (BaseFrameElement) getDomNodeOrDie();
}
 
Example #16
Source File: Document.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new element with the given tag name.
 *
 * @param tagName the tag name
 * @return the new HTML element, or NOT_FOUND if the tag is not supported
 */
@JsxFunction
public Object createElement(String tagName) {
    Object result = NOT_FOUND;
    try {
        final BrowserVersion browserVersion = getBrowserVersion();

        if (browserVersion.hasFeature(JS_DOCUMENT_CREATE_ELEMENT_STRICT)
              && (tagName.contains("<") || tagName.contains(">"))) {
            LOG.info("createElement: Provided string '"
                        + tagName + "' contains an invalid character; '<' and '>' are not allowed");
            throw Context.reportRuntimeError("String contains an invalid character");
        }
        else if (tagName.startsWith("<") && tagName.endsWith(">")) {
            tagName = tagName.substring(1, tagName.length() - 1);

            final Matcher matcher = TAG_NAME_PATTERN.matcher(tagName);
            if (!matcher.matches()) {
                LOG.info("createElement: Provided string '" + tagName + "' contains an invalid character");
                throw Context.reportRuntimeError("String contains an invalid character");
            }
        }

        final SgmlPage page = getPage();
        org.w3c.dom.Node element = page.createElement(tagName);

        if (element instanceof BaseFrameElement) {
            ((BaseFrameElement) element).markAsCreatedByJavascript();
        }
        else if (element instanceof HtmlInput) {
            ((HtmlInput) element).markAsCreatedByJavascript();
        }
        else if (element instanceof HtmlImage) {
            ((HtmlImage) element).markAsCreatedByJavascript();
        }
        else if (element instanceof HtmlRp) {
            ((HtmlRp) element).markAsCreatedByJavascript();
        }
        else if (element instanceof HtmlRt) {
            ((HtmlRt) element).markAsCreatedByJavascript();
        }
        else if (element instanceof HtmlScript) {
            ((HtmlScript) element).markAsCreatedByJavascript();
        }
        else if (element instanceof HtmlUnknownElement) {
            ((HtmlUnknownElement) element).markAsCreatedByJavascript();
        }
        else if (element instanceof HtmlSvg) {
            element = UnknownElementFactory.instance.createElementNS(page, "", "svg", null);
            ((HtmlUnknownElement) element).markAsCreatedByJavascript();
        }
        final Object jsElement = getScriptableFor(element);

        if (jsElement == NOT_FOUND) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("createElement(" + tagName
                    + ") cannot return a result as there isn't a JavaScript object for the element "
                    + element.getClass().getName());
            }
        }
        else {
            result = jsElement;
        }
    }
    catch (final ElementNotFoundException e) {
        // Just fall through - result is already set to NOT_FOUND
    }
    return result;
}
 
Example #17
Source File: HTMLIFrameElement.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
private BaseFrameElement getFrame() {
    return (BaseFrameElement) getDomNodeOrDie();
}
 
Example #18
Source File: WebClient.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * <p>Creates a page based on the specified response and inserts it into the specified window. All page
 * initialization and event notification is handled here.</p>
 *
 * <p>Note that if the page created is an attachment page, and an {@link AttachmentHandler} has been
 * registered with this client, the page is <b>not</b> loaded into the specified window; in this case,
 * the page is loaded into a new window, and attachment handling is delegated to the registered
 * <tt>AttachmentHandler</tt>.</p>
 *
 * @param webResponse the response that will be used to create the new page
 * @param webWindow the window that the new page will be placed within
 * @throws IOException if an IO error occurs
 * @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
 *         {@link WebClientOptions#setThrowExceptionOnFailingStatusCode(boolean)} is set to true
 * @return the newly created page
 * @see #setAttachmentHandler(AttachmentHandler)
 */
public Page loadWebResponseInto(final WebResponse webResponse, final WebWindow webWindow)
    throws IOException, FailingHttpStatusCodeException {

    WebAssert.notNull("webResponse", webResponse);
    WebAssert.notNull("webWindow", webWindow);

    if (webResponse.getStatusCode() == HttpStatus.SC_NO_CONTENT) {
        return webWindow.getEnclosedPage();
    }

    if (webStartHandler_ != null && "application/x-java-jnlp-file".equals(webResponse.getContentType())) {
        webStartHandler_.handleJnlpResponse(webResponse);
        return webWindow.getEnclosedPage();
    }

    if (attachmentHandler_ != null && Attachment.isAttachment(webResponse)) {
        final WebWindow w = openWindow(null, null, webWindow);
        final Page page = pageCreator_.createPage(webResponse, w);
        attachmentHandler_.handleAttachment(page);
        return page;
    }

    final Page oldPage = webWindow.getEnclosedPage();
    if (oldPage != null) {
        // Remove the old windows before create new ones.
        oldPage.cleanUp();
    }
    Page newPage = null;
    if (windows_.contains(webWindow) || getBrowserVersion().hasFeature(WINDOW_EXECUTE_EVENTS)) {
        newPage = pageCreator_.createPage(webResponse, webWindow);

        if (windows_.contains(webWindow)) {
            fireWindowContentChanged(new WebWindowEvent(webWindow, WebWindowEvent.CHANGE, oldPage, newPage));

            // The page being loaded may already have been replaced by another page via JavaScript code.
            if (webWindow.getEnclosedPage() == newPage) {
                newPage.initialize();
                // hack: onload should be fired the same way for all type of pages
                // here is a hack to handle non HTML pages
                if (webWindow instanceof FrameWindow && !newPage.isHtmlPage()) {
                    final FrameWindow fw = (FrameWindow) webWindow;
                    final BaseFrameElement frame = fw.getFrameElement();
                    if (frame.hasEventHandlers("onload")) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Executing onload handler for " + frame);
                        }
                        final Event event = new Event(frame, Event.TYPE_LOAD);
                        ((Node) frame.getScriptableObject()).executeEventLocally(event);
                    }
                }
            }
        }
    }
    return newPage;
}
 
Example #19
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean isMatching(final DomNode node) {
    return node instanceof BaseFrameElement;
}
 
Example #20
Source File: HTMLIFrameElement.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
private BaseFrameElement getFrame() {
    return (BaseFrameElement) getDomNodeOrDie();
}
 
Example #21
Source File: HTMLFrameElement.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
private BaseFrameElement getFrame() {
    return (BaseFrameElement) getDomNodeOrDie();
}
 
Example #22
Source File: Document.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new element with the given tag name.
 *
 * @param tagName the tag name
 * @return the new HTML element, or NOT_FOUND if the tag is not supported
 */
@JsxFunction
public Object createElement(String tagName) {
    Object result = NOT_FOUND;
    try {
        if (tagName.contains("<") || tagName.contains(">")) {
            if (LOG.isInfoEnabled()) {
                LOG.info("createElement: Provided string '"
                            + tagName + "' contains an invalid character; '<' and '>' are not allowed");
            }
            throw Context.reportRuntimeError("String contains an invalid character");
        }
        else if (tagName.length() > 0 && tagName.charAt(0) == '<' && tagName.endsWith(">")) {
            tagName = tagName.substring(1, tagName.length() - 1);

            final Matcher matcher = TAG_NAME_PATTERN.matcher(tagName);
            if (!matcher.matches()) {
                if (LOG.isInfoEnabled()) {
                    LOG.info("createElement: Provided string '" + tagName + "' contains an invalid character");
                }
                throw Context.reportRuntimeError("String contains an invalid character");
            }
        }

        final SgmlPage page = getPage();
        org.w3c.dom.Node element = page.createElement(tagName);

        if (element instanceof BaseFrameElement) {
            ((BaseFrameElement) element).markAsCreatedByJavascript();
        }
        else if (element instanceof HtmlInput) {
            ((HtmlInput) element).markAsCreatedByJavascript();
        }
        else if (element instanceof HtmlImage) {
            ((HtmlImage) element).markAsCreatedByJavascript();
        }
        else if (element instanceof HtmlRp) {
            ((HtmlRp) element).markAsCreatedByJavascript();
        }
        else if (element instanceof HtmlRt) {
            ((HtmlRt) element).markAsCreatedByJavascript();
        }
        else if (element instanceof HtmlScript) {
            ((HtmlScript) element).markAsCreatedByJavascript();
        }
        else if (element instanceof HtmlUnknownElement) {
            ((HtmlUnknownElement) element).markAsCreatedByJavascript();
        }
        else if (element instanceof HtmlSvg) {
            element = UnknownElementFactory.instance.createElementNS(page, "", "svg", null);
            ((HtmlUnknownElement) element).markAsCreatedByJavascript();
        }
        final Object jsElement = getScriptableFor(element);

        if (jsElement == NOT_FOUND) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("createElement(" + tagName
                    + ") cannot return a result as there isn't a JavaScript object for the element "
                    + element.getClass().getName());
            }
        }
        else {
            result = jsElement;
        }
    }
    catch (final ElementNotFoundException e) {
        // Just fall through - result is already set to NOT_FOUND
    }
    return result;
}
 
Example #23
Source File: Window.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean isMatching(final DomNode node) {
    return node instanceof BaseFrameElement;
}
 
Example #24
Source File: FrameContentHandler.java    From htmlunit with Apache License 2.0 2 votes vote down vote up
/**
 * Called to decide if the content (referred from the source attribute)
 * should be loaded or not.
 *
 * @param baseFrameElement the element that likes to load the content
 * @return true if the content should be loaded
 */
boolean loadFrameDocument(BaseFrameElement baseFrameElement);