com.gargoylesoftware.htmlunit.javascript.AbstractJavaScriptEngine Java Examples

The following examples show how to use com.gargoylesoftware.htmlunit.javascript.AbstractJavaScriptEngine. 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: HtmlScript.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * If setting the <tt>src</tt> attribute, this method executes the new JavaScript if necessary
 * (behavior varies by browser version). {@inheritDoc}
 */
@Override
protected void setAttributeNS(final String namespaceURI, final String qualifiedName, final String attributeValue,
        final boolean notifyAttributeChangeListeners, final boolean notifyMutationObservers) {
    // special additional processing for the 'src'
    if (namespaceURI != null || !SRC_ATTRIBUTE.equals(qualifiedName)) {
        super.setAttributeNS(namespaceURI, qualifiedName, attributeValue, notifyAttributeChangeListeners,
                notifyMutationObservers);
        return;
    }

    final String oldValue = getAttributeNS(namespaceURI, qualifiedName);
    super.setAttributeNS(namespaceURI, qualifiedName, attributeValue, notifyAttributeChangeListeners,
            notifyMutationObservers);

    if (isAttachedToPage() && oldValue.isEmpty() && getFirstChild() == null) {
        final PostponedAction action = new PostponedAction(getPage()) {
            @Override
            public void execute() {
                executeScriptIfNeeded();
            }
        };
        final AbstractJavaScriptEngine<?> engine = getPage().getWebClient().getJavaScriptEngine();
        engine.addPostponedAction(action);
    }
}
 
Example #2
Source File: HtmlInput.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Executes the onchange script code for this element if this is appropriate.
 * This means that the element must have an onchange script, script must be enabled
 * and the change in the element must not have been triggered by a script.
 *
 * @param htmlElement the element that contains the onchange attribute
 * @return the page that occupies this window after this method completes (may or
 *         may not be the same as the original page)
 */
static Page executeOnChangeHandlerIfAppropriate(final HtmlElement htmlElement) {
    final SgmlPage page = htmlElement.getPage();

    final AbstractJavaScriptEngine<?> engine = htmlElement.getPage().getWebClient().getJavaScriptEngine();
    if (engine.isScriptRunning()) {
        return page;
    }
    final ScriptResult scriptResult = htmlElement.fireEvent(Event.TYPE_CHANGE);

    if (page.getWebClient().containsWebWindow(page.getEnclosingWindow())) {
        // may be itself or a newly loaded one
        return page.getEnclosingWindow().getEnclosedPage();
    }

    if (scriptResult != null) {
        // current window doesn't exist anymore
        return page.getWebClient().getCurrentWindow().getEnclosedPage();
    }

    return page;
}
 
Example #3
Source File: HtmlScript.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * If setting the <tt>src</tt> attribute, this method executes the new JavaScript if necessary
 * (behavior varies by browser version). {@inheritDoc}
 */
@Override
protected void setAttributeNS(final String namespaceURI, final String qualifiedName, final String attributeValue,
        final boolean notifyAttributeChangeListeners, final boolean notifyMutationObservers) {
    // special additional processing for the 'src'
    if (namespaceURI != null || !"src".equals(qualifiedName)) {
        super.setAttributeNS(namespaceURI, qualifiedName, attributeValue, notifyAttributeChangeListeners,
                notifyMutationObservers);
        return;
    }

    final String oldValue = getAttributeNS(namespaceURI, qualifiedName);
    super.setAttributeNS(namespaceURI, qualifiedName, attributeValue, notifyAttributeChangeListeners,
            notifyMutationObservers);

    if (isAttachedToPage() && oldValue.isEmpty() && getFirstChild() == null) {
        final PostponedAction action = new PostponedAction(getPage()) {
            @Override
            public void execute() {
                executeScriptIfNeeded();
            }
        };
        final AbstractJavaScriptEngine<?> engine = getPage().getWebClient().getJavaScriptEngine();
        engine.addPostponedAction(action);
    }
}
 
Example #4
Source File: HtmlInput.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Executes the onchange script code for this element if this is appropriate.
 * This means that the element must have an onchange script, script must be enabled
 * and the change in the element must not have been triggered by a script.
 *
 * @param htmlElement the element that contains the onchange attribute
 * @return the page that occupies this window after this method completes (may or
 *         may not be the same as the original page)
 */
static Page executeOnChangeHandlerIfAppropriate(final HtmlElement htmlElement) {
    final SgmlPage page = htmlElement.getPage();

    final AbstractJavaScriptEngine<?> engine = htmlElement.getPage().getWebClient().getJavaScriptEngine();
    if (engine.isScriptRunning()) {
        return page;
    }
    final ScriptResult scriptResult = htmlElement.fireEvent(Event.TYPE_CHANGE);

    if (page.getWebClient().containsWebWindow(page.getEnclosingWindow())) {
        // may be itself or a newly loaded one
        return page.getEnclosingWindow().getEnclosedPage();
    }

    if (scriptResult != null) {
        // current window doesn't exist anymore
        return scriptResult.getNewPage();
    }

    return page;
}
 
Example #5
Source File: WebClient.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * This method is intended for testing only - use at your own risk.
 *
 * @param engine the new script engine to use
 */
public void setJavaScriptEngine(final AbstractJavaScriptEngine<?> engine) {
    if (engine == null) {
        throw new IllegalArgumentException("Can't set JavaScriptEngine to null");
    }
    scriptEngine_ = engine;
}
 
Example #6
Source File: BaseFrameElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Called after the node for the {@code frame} or {@code iframe} has been added to the containing page.
 * The node needs to be added first to allow JavaScript in the frame to see the frame in the parent.
 * @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
 *      {@link com.gargoylesoftware.htmlunit.WebClientOptions#setThrowExceptionOnFailingStatusCode(boolean)} is
 *      set to true
 */

public void loadInnerPage() throws FailingHttpStatusCodeException {
    String source = getSrcAttribute();
    if (source.isEmpty() || StringUtils.startsWithIgnoreCase(source, WebClient.ABOUT_SCHEME)) {
        source = WebClient.ABOUT_BLANK;
    }

    loadInnerPageIfPossible(source);

    final Page enclosedPage = getEnclosedPage();
    if (enclosedPage != null && enclosedPage.isHtmlPage()) {
        final HtmlPage htmlPage = (HtmlPage) enclosedPage;

        final AbstractJavaScriptEngine<?> jsEngine = getPage().getWebClient().getJavaScriptEngine();
        if (jsEngine != null && jsEngine.isScriptRunning()) {
            final PostponedAction action = new PostponedAction(getPage()) {
                @Override
                public void execute() throws Exception {
                    htmlPage.setReadyState(READY_STATE_COMPLETE);
                }
            };
            jsEngine.addPostponedAction(action);
        }
        else {
            htmlPage.setReadyState(READY_STATE_COMPLETE);
        }
    }
}
 
Example #7
Source File: BaseFrameElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private void loadSrc() {
    loadSrcWhenAddedToPage_ = false;
    final String src = getSrcAttribute();

    final AbstractJavaScriptEngine<?> jsEngine = getPage().getWebClient().getJavaScriptEngine();
    // When src is set from a script, loading is postponed until script finishes
    // in fact this implementation is probably wrong: JavaScript URL should be
    // first evaluated and only loading, when any, should be postponed.
    if (jsEngine == null || !jsEngine.isScriptRunning()
            || src.startsWith(JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
        loadInnerPageIfPossible(src);
    }
    else {
        final Page pageInFrame = getEnclosedPage();
        final PostponedAction action = new PostponedAction(getPage()) {
            @Override
            public void execute() throws Exception {
                if (!src.isEmpty() && getSrcAttribute().equals(src)) {
                    loadInnerPage();
                }
            }

            @Override
            public boolean isStillAlive() {
                // skip if page in frame has already been changed
                return super.isStillAlive() && pageInFrame == getEnclosedPage();
            }
        };
        jsEngine.addPostponedAction(action);
    }
}
 
Example #8
Source File: WebClient.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * This method is intended for testing only - use at your own risk.
 *
 * @param engine the new script engine to use
 */
public void setJavaScriptEngine(final AbstractJavaScriptEngine<?> engine) {
    if (engine == null) {
        throw new IllegalArgumentException("Can't set JavaScriptEngine to null");
    }
    scriptEngine_ = engine;
}
 
Example #9
Source File: BaseFrameElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Called after the node for the {@code frame} or {@code iframe} has been added to the containing page.
 * The node needs to be added first to allow JavaScript in the frame to see the frame in the parent.
 * @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
 *      {@link com.gargoylesoftware.htmlunit.WebClientOptions#setThrowExceptionOnFailingStatusCode(boolean)} is
 *      set to true
 */

public void loadInnerPage() throws FailingHttpStatusCodeException {
    String source = getSrcAttribute();
    if (source.isEmpty() || StringUtils.startsWithIgnoreCase(source, WebClient.ABOUT_SCHEME)) {
        source = WebClient.ABOUT_BLANK;
    }

    loadInnerPageIfPossible(source);

    final Page enclosedPage = getEnclosedPage();
    if (enclosedPage != null && enclosedPage.isHtmlPage()) {
        final HtmlPage htmlPage = (HtmlPage) enclosedPage;
        final AbstractJavaScriptEngine<?> jsEngine = getPage().getWebClient().getJavaScriptEngine();
        if (jsEngine.isScriptRunning()) {
            final PostponedAction action = new PostponedAction(getPage()) {
                @Override
                public void execute() throws Exception {
                    htmlPage.setReadyState(READY_STATE_COMPLETE);
                }
            };
            jsEngine.addPostponedAction(action);
        }
        else {
            htmlPage.setReadyState(READY_STATE_COMPLETE);
        }
    }
}
 
Example #10
Source File: BaseFrameElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private void loadSrc() {
    loadSrcWhenAddedToPage_ = false;
    final String src = getSrcAttribute();

    final AbstractJavaScriptEngine<?> jsEngine = getPage().getWebClient().getJavaScriptEngine();
    // When src is set from a script, loading is postponed until script finishes
    // in fact this implementation is probably wrong: JavaScript URL should be
    // first evaluated and only loading, when any, should be postponed.
    if (!jsEngine.isScriptRunning() || src.startsWith(JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
        loadInnerPageIfPossible(src);
    }
    else {
        final Page pageInFrame = getEnclosedPage();
        final PostponedAction action = new PostponedAction(getPage()) {
            @Override
            public void execute() throws Exception {
                if (!src.isEmpty() && getSrcAttribute().equals(src)) {
                    loadInnerPage();
                }
            }

            @Override
            public boolean isStillAlive() {
                // skip if page in frame has already been changed
                return super.isStillAlive() && pageInFrame == getEnclosedPage();
            }
        };
        jsEngine.addPostponedAction(action);
    }
}
 
Example #11
Source File: DomElement.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Simulates clicking on this element, returning the page in the window that has the focus
 * after the element has been clicked. Note that the returned page may or may not be the same
 * as the original page, depending on the type of element being clicked, the presence of JavaScript
 * action listeners, etc.
 *
 * @param event the click event used
 * @param shiftKey {@code true} if SHIFT is pressed during the click
 * @param ctrlKey {@code true} if CTRL is pressed during the click
 * @param altKey {@code true} if ALT is pressed during the click
 * @param ignoreVisibility whether to ignore visibility or not
 * @param <P> the page type
 * @return the page contained in the current window as returned by {@link WebClient#getCurrentWindow()}
 * @exception IOException if an IO error occurs
 */
@SuppressWarnings("unchecked")
public <P extends Page> P click(final Event event,
                    final boolean shiftKey, final boolean ctrlKey, final boolean altKey,
                    final boolean ignoreVisibility) throws IOException {
    final SgmlPage page = getPage();

    if ((!ignoreVisibility && !isDisplayed()) || isDisabledElementAndDisabled()) {
        return (P) page;
    }

    if (!page.getWebClient().isJavaScriptEnabled()) {
        doClickStateUpdate(shiftKey, ctrlKey);
        page.getWebClient().loadDownloadedResponses();
        return (P) getPage().getWebClient().getCurrentWindow().getEnclosedPage();
    }

    // may be different from page when working with "orphaned pages"
    // (ex: clicking a link in a page that is not active anymore)
    final Page contentPage = page.getEnclosingWindow().getEnclosedPage();

    boolean stateUpdated = false;
    boolean changed = false;
    if (isStateUpdateFirst()) {
        changed = doClickStateUpdate(shiftKey, ctrlKey);
        stateUpdated = true;
    }

    final AbstractJavaScriptEngine<?> jsEngine = page.getWebClient().getJavaScriptEngine();
    jsEngine.holdPosponedActions();
    try {
        final ScriptResult scriptResult = doClickFireClickEvent(event);
        final boolean eventIsAborted = event.isAborted(scriptResult);

        final boolean pageAlreadyChanged = contentPage != page.getEnclosingWindow().getEnclosedPage();
        if (!pageAlreadyChanged && !stateUpdated && !eventIsAborted) {
            changed = doClickStateUpdate(shiftKey, ctrlKey);
        }
    }
    finally {
        jsEngine.processPostponedActions();
    }

    if (changed) {
        doClickFireChangeEvent();
    }

    return (P) getPage().getWebClient().getCurrentWindow().getEnclosedPage();
}
 
Example #12
Source File: DomElement.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Simulates clicking on this element, returning the page in the window that has the focus
 * after the element has been clicked. Note that the returned page may or may not be the same
 * as the original page, depending on the type of element being clicked, the presence of JavaScript
 * action listeners, etc.
 *
 * @param event the click event used
 * @param ignoreVisibility whether to ignore visibility or not
 * @param <P> the page type
 * @return the page contained in the current window as returned by {@link WebClient#getCurrentWindow()}
 * @exception IOException if an IO error occurs
 */
@SuppressWarnings("unchecked")
public <P extends Page> P click(final Event event, final boolean ignoreVisibility) throws IOException {
    final SgmlPage page = getPage();

    if ((!ignoreVisibility && !isDisplayed())
            || (this instanceof DisabledElement && ((DisabledElement) this).isDisabled())) {
        return (P) page;
    }

    // may be different from page when working with "orphaned pages"
    // (ex: clicking a link in a page that is not active anymore)
    final Page contentPage = page.getEnclosingWindow().getEnclosedPage();

    boolean stateUpdated = false;
    boolean changed = false;
    if (isStateUpdateFirst()) {
        changed = doClickStateUpdate(event.isShiftKey(), event.isCtrlKey());
        stateUpdated = true;
    }

    final AbstractJavaScriptEngine<?> jsEngine = page.getWebClient().getJavaScriptEngine();
    jsEngine.holdPosponedActions();
    try {
        final ScriptResult scriptResult = doClickFireClickEvent(event);
        final boolean eventIsAborted = event.isAborted(scriptResult);

        final boolean pageAlreadyChanged = contentPage != page.getEnclosingWindow().getEnclosedPage();
        if (!pageAlreadyChanged && !stateUpdated && !eventIsAborted) {
            changed = doClickStateUpdate(event.isShiftKey(), event.isCtrlKey());
        }
    }
    finally {
        jsEngine.processPostponedActions();
    }

    if (changed) {
        doClickFireChangeEvent();
    }

    return (P) getPage().getWebClient().getCurrentWindow().getEnclosedPage();
}
 
Example #13
Source File: WebClient.java    From htmlunit with Apache License 2.0 2 votes vote down vote up
/**
 * This method is intended for testing only - use at your own risk.
 * @return the current JavaScript engine (never {@code null})
 */
public AbstractJavaScriptEngine<?> getJavaScriptEngine() {
    return scriptEngine_;
}
 
Example #14
Source File: WebClient.java    From HtmlUnit-Android with Apache License 2.0 2 votes vote down vote up
/**
 * This method is intended for testing only - use at your own risk.
 * @return the current JavaScript engine (never {@code null})
 */
public AbstractJavaScriptEngine<?> getJavaScriptEngine() {
    return scriptEngine_;
}