Search in sources :

Example 21 with ScriptResult

use of com.gargoylesoftware.htmlunit.ScriptResult in project htmlunit by HtmlUnit.

the class HtmlElement method type.

private Page type(final int keyCode, final boolean fireKeyDown, final boolean fireKeyPress, final boolean fireKeyUp, final boolean lastType) {
    if (isDisabledElementAndDisabled()) {
        return getPage();
    }
    final HtmlPage page = (HtmlPage) getPage();
    if (page.getFocusedElement() != this) {
        focus();
    }
    final Event keyDown;
    final ScriptResult keyDownResult;
    if (fireKeyDown) {
        keyDown = new KeyboardEvent(this, Event.TYPE_KEY_DOWN, keyCode, shiftPressed_, ctrlPressed_, altPressed_);
        keyDownResult = fireEvent(keyDown);
    } else {
        keyDown = null;
        keyDownResult = null;
    }
    final BrowserVersion browserVersion = page.getWebClient().getBrowserVersion();
    final Event keyPress;
    final ScriptResult keyPressResult;
    if (fireKeyPress && browserVersion.hasFeature(KEYBOARD_EVENT_SPECIAL_KEYPRESS)) {
        keyPress = new KeyboardEvent(this, Event.TYPE_KEY_PRESS, keyCode, shiftPressed_, ctrlPressed_, altPressed_);
        keyPressResult = fireEvent(keyPress);
    } else {
        keyPress = null;
        keyPressResult = null;
    }
    if (keyDown != null && !keyDown.isAborted(keyDownResult) && (keyPress == null || !keyPress.isAborted(keyPressResult))) {
        doType(keyCode, lastType);
    }
    if (this instanceof HtmlTextInput || this instanceof HtmlTextArea || this instanceof HtmlTelInput || this instanceof HtmlNumberInput || this instanceof HtmlSearchInput || this instanceof HtmlPasswordInput) {
        final Event input = new KeyboardEvent(this, Event.TYPE_INPUT, keyCode, shiftPressed_, ctrlPressed_, altPressed_);
        fireEvent(input);
    }
    if (fireKeyUp) {
        final Event keyUp = new KeyboardEvent(this, Event.TYPE_KEY_UP, keyCode, shiftPressed_, ctrlPressed_, altPressed_);
        fireEvent(keyUp);
    }
    // }
    return page.getWebClient().getCurrentWindow().getEnclosedPage();
}
Also used : ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) KeyboardEvent(com.gargoylesoftware.htmlunit.javascript.host.event.KeyboardEvent) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) KeyboardEvent(com.gargoylesoftware.htmlunit.javascript.host.event.KeyboardEvent) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion)

Example 22 with ScriptResult

use of com.gargoylesoftware.htmlunit.ScriptResult in project htmlunit by HtmlUnit.

the class HtmlInput method executeOnChangeHandlerIfAppropriate.

/**
 * 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 WebClient webClient = page.getWebClient();
    if (!webClient.isJavaScriptEngineEnabled()) {
        return page;
    }
    final AbstractJavaScriptEngine<?> engine = webClient.getJavaScriptEngine();
    if (engine.isScriptRunning()) {
        return page;
    }
    final ScriptResult scriptResult = htmlElement.fireEvent(Event.TYPE_CHANGE);
    if (webClient.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 webClient.getCurrentWindow().getEnclosedPage();
    }
    return page;
}
Also used : ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) WebClient(com.gargoylesoftware.htmlunit.WebClient)

Example 23 with ScriptResult

use of com.gargoylesoftware.htmlunit.ScriptResult in project htmlunit by HtmlUnit.

the class ApplicationCache method dispatchEvent.

/**
 * Dispatches an event into the event system (standards-conformant browsers only). See
 * <a href="https://developer.mozilla.org/en-US/docs/DOM/element.dispatchEvent">the Gecko
 * DOM reference</a> for more information.
 *
 * @param event the event to be dispatched
 * @return {@code false} if at least one of the event handlers which handled the event
 *         called <tt>preventDefault</tt>; {@code true} otherwise
 */
@Override
@JsxFunction
public boolean dispatchEvent(final Event event) {
    event.setTarget(this);
    final ScriptResult result = fireEvent(event);
    return !event.isAborted(result);
}
Also used : ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 24 with ScriptResult

use of com.gargoylesoftware.htmlunit.ScriptResult in project htmlunit by HtmlUnit.

the class EventTarget method fireEvent.

/**
 * Fires the event on the node with capturing and bubbling phase.
 * @param event the event
 * @return the result
 */
public ScriptResult fireEvent(final Event event) {
    final Window window = getWindow();
    event.startFire();
    final Event previousEvent = window.getCurrentEvent();
    window.setCurrentEvent(event);
    try {
        // These can be null if we aren't tied to a DOM node
        final DomNode ourNode = getDomNodeOrNull();
        final DomNode ourParentNode = (ourNode != null) ? ourNode.getParentNode() : null;
        // Determine the propagation path which is fixed here and not affected by
        // DOM tree modification from intermediate listeners (tested in Chrome)
        final List<EventTarget> propagationPath = new ArrayList<>();
        // We're added to the propagation path first
        propagationPath.add(this);
        // and MessagePort, etc. will not have any parents)
        for (DomNode parent = ourParentNode; parent != null; parent = parent.getParentNode()) {
            propagationPath.add(parent.getScriptableObject());
        }
        // (see Note in https://www.w3.org/TR/DOM-Level-3-Events/#event-type-load)
        if (!Event.TYPE_LOAD.equals(event.getType())) {
            // Add Window if the the propagation path reached Document
            if (propagationPath.get(propagationPath.size() - 1) instanceof Document) {
                propagationPath.add(window);
            }
        }
        // capturing phase
        event.setEventPhase(Event.CAPTURING_PHASE);
        for (int i = propagationPath.size() - 1; i >= 1; i--) {
            final EventTarget jsNode = propagationPath.get(i);
            final EventListenersContainer elc = jsNode.eventListenersContainer_;
            if (elc != null) {
                elc.executeCapturingListeners(event, new Object[] { event });
                if (event.isPropagationStopped()) {
                    return new ScriptResult(null);
                }
            }
        }
        // at target phase
        event.setEventPhase(Event.AT_TARGET);
        if (!propagationPath.isEmpty()) {
            // Note: This element is not always the same as event.getTarget():
            // e.g. the 'load' event targets Document but "at target" is on Window.
            final EventTarget jsNode = propagationPath.get(0);
            final EventListenersContainer elc = jsNode.eventListenersContainer_;
            if (elc != null) {
                elc.executeAtTargetListeners(event, new Object[] { event });
                if (event.isPropagationStopped()) {
                    return new ScriptResult(null);
                }
            }
        }
        // bubbling phase
        if (event.isBubbles()) {
            // This belongs here inside the block because events that don't bubble never set
            // eventPhase = 3 (tested in Chrome)
            event.setEventPhase(Event.BUBBLING_PHASE);
            for (int i = 1, size = propagationPath.size(); i < size; i++) {
                final EventTarget jsNode = propagationPath.get(i);
                final EventListenersContainer elc = jsNode.eventListenersContainer_;
                if (elc != null) {
                    elc.executeBubblingListeners(event, new Object[] { event });
                    if (event.isPropagationStopped()) {
                        return new ScriptResult(null);
                    }
                }
            }
        }
        HtmlLabel label = null;
        if (event.processLabelAfterBubbling()) {
            for (DomNode parent = ourParentNode; parent != null; parent = parent.getParentNode()) {
                if (parent instanceof HtmlLabel) {
                    label = (HtmlLabel) parent;
                    break;
                }
            }
        }
        if (label != null) {
            final HtmlElement element = label.getLabeledElement();
            if (element != null && element != getDomNodeOrNull()) {
                try {
                    element.click(event.isShiftKey(), event.isCtrlKey(), event.isAltKey(), false, true, true, true);
                } catch (final IOException e) {
                // ignore for now
                }
            }
        }
    } finally {
        event.endFire();
        // reset event
        window.setCurrentEvent(previousEvent);
    }
    return new ScriptResult(null);
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(com.gargoylesoftware.htmlunit.javascript.host.dom.Document) ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HtmlLabel(com.gargoylesoftware.htmlunit.html.HtmlLabel)

Example 25 with ScriptResult

use of com.gargoylesoftware.htmlunit.ScriptResult in project htmlunit by HtmlUnit.

the class EventTarget method dispatchEvent.

/**
 * Dispatches an event into the event system (standards-conformant browsers only). See
 * <a href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent">the Gecko
 * DOM reference</a> for more information.
 *
 * @param event the event to be dispatched
 * @return {@code false} if at least one of the event handlers which handled the event
 *         called <tt>preventDefault</tt>; {@code true} otherwise
 */
@JsxFunction
public boolean dispatchEvent(final Event event) {
    event.setTarget(this);
    final DomElement element = (DomElement) getDomNodeOrNull();
    ScriptResult result = null;
    if (event.getType().equals(MouseEvent.TYPE_CLICK)) {
        try {
            element.click(event, event.isShiftKey(), event.isCtrlKey(), event.isAltKey(), true);
        } catch (final IOException e) {
            throw Context.reportRuntimeError("Error calling click(): " + e.getMessage());
        }
    } else {
        result = fireEvent(event);
    }
    return !event.isAborted(result);
}
Also used : ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) IOException(java.io.IOException) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

ScriptResult (com.gargoylesoftware.htmlunit.ScriptResult)26 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)8 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)5 WebClient (com.gargoylesoftware.htmlunit.WebClient)5 Page (com.gargoylesoftware.htmlunit.Page)4 Event (com.gargoylesoftware.htmlunit.javascript.host.event.Event)4 Test (org.junit.Test)4 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)3 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)3 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)2 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)2 AbstractJavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.AbstractJavaScriptEngine)2 JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)2 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)2 KeyboardEvent (com.gargoylesoftware.htmlunit.javascript.host.event.KeyboardEvent)2 MouseEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent)2 PointerEvent (com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent)2 IOException (java.io.IOException)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 PageObject (org.jenkinsci.test.acceptance.po.PageObject)2