Search in sources :

Example 1 with Window

use of com.gargoylesoftware.htmlunit.javascript.host.Window in project htmlunit by HtmlUnit.

the class Range method getClientRects.

/**
 * Retrieves a collection of rectangles that describes the layout of the contents of an object
 * or range within the client. Each rectangle describes a single line.
 * @return a collection of rectangles that describes the layout of the contents
 */
@JsxFunction
public ClientRectList getClientRects() {
    final Window w = getWindow();
    final ClientRectList rectList = new ClientRectList();
    rectList.setParentScope(w);
    rectList.setPrototype(getPrototype(rectList.getClass()));
    // simple impl for now
    for (final DomNode node : toW3C().containedNodes()) {
        final ScriptableObject scriptable = node.getScriptableObject();
        if (scriptable instanceof HTMLElement) {
            final ClientRect rect = new ClientRect(0, 0, 1, 1);
            rect.setParentScope(w);
            rect.setPrototype(getPrototype(rect.getClass()));
            rectList.add(rect);
        }
    }
    return rectList;
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) ClientRect(com.gargoylesoftware.htmlunit.javascript.host.ClientRect) ClientRectList(com.gargoylesoftware.htmlunit.javascript.host.ClientRectList) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 2 with Window

use of com.gargoylesoftware.htmlunit.javascript.host.Window in project htmlunit by HtmlUnit.

the class TextRange method parentElement.

/**
 * Retrieves the parent element for the given text range.
 * The parent element is the element that completely encloses the text in the range.
 * If the text range spans text in more than one element, this method returns the smallest element that encloses
 * all the elements. When you insert text into a range that spans multiple elements, the text is placed in the
 * parent element rather than in any of the contained elements.
 *
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536654.aspx">MSDN doc</a>
 * @return the parent element object if successful, or null otherwise.
 */
@JsxFunction
public Node parentElement() {
    final org.w3c.dom.Node parent = range_.getCommonAncestorContainer();
    if (null == parent) {
        if (null == range_.getStartContainer() || null == range_.getEndContainer()) {
            try {
                final Window window = (Window) getParentScope();
                final HtmlPage page = (HtmlPage) window.getDomNodeOrDie();
                return (Node) getScriptableFor(page.getBody());
            } catch (final Exception e) {
            // ok bad luck
            }
        }
        return null;
    }
    return (Node) getScriptableFor(parent);
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 3 with Window

use of com.gargoylesoftware.htmlunit.javascript.host.Window in project htmlunit by HtmlUnit.

the class MessageEvent method jsConstructor.

/**
 * JavaScript constructor.
 *
 * @param type the event type
 * @param details the event details (optional)
 */
@Override
@JsxConstructor({ CHROME, EDGE, FF, FF_ESR })
public void jsConstructor(final String type, final ScriptableObject details) {
    super.jsConstructor(type, details);
    if (getBrowserVersion().hasFeature(EVENT_ONMESSAGE_DEFAULT_DATA_NULL)) {
        data_ = null;
    }
    String origin = "";
    String lastEventId = "";
    if (details != null && !Undefined.isUndefined(details)) {
        data_ = details.get("data");
        final String detailOrigin = (String) details.get(HttpHeader.ORIGIN_LC);
        if (detailOrigin != null) {
            origin = detailOrigin;
        }
        final Object detailLastEventId = details.get("lastEventId");
        if (detailLastEventId != null) {
            lastEventId = Context.toString(detailLastEventId);
        }
        source_ = null;
        final Object detailSource = details.get("source");
        if (detailSource instanceof Window) {
            source_ = (Window) detailSource;
        } else if (detailSource instanceof WindowProxy) {
            source_ = ((WindowProxy) detailSource).getDelegee();
        }
        ports_ = details.get("ports");
    }
    origin_ = origin;
    lastEventId_ = lastEventId;
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) WindowProxy(com.gargoylesoftware.htmlunit.javascript.host.WindowProxy) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)

Example 4 with Window

use of com.gargoylesoftware.htmlunit.javascript.host.Window in project htmlunit by HtmlUnit.

the class UIEvent method jsConstructor.

/**
 * JavaScript constructor.
 *
 * @param type the event type
 * @param details the event details (optional)
 */
@JsxConstructor({ CHROME, EDGE, FF, FF_ESR })
@Override
public void jsConstructor(final String type, final ScriptableObject details) {
    super.jsConstructor(type, details);
    view_ = NO_VIEW;
    if (details != null && !Undefined.isUndefined(details)) {
        final Object view = details.get("view", details);
        if (view instanceof Window) {
            view_ = view;
        } else if (view != Scriptable.NOT_FOUND) {
            throw ScriptRuntime.typeError("View must be a window.");
        }
    }
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)

Example 5 with Window

use of com.gargoylesoftware.htmlunit.javascript.host.Window in project htmlunit by HtmlUnit.

the class EventListenersContainer method executeEventListeners.

private void executeEventListeners(final int eventPhase, final Event event, final Object[] args) {
    final DomNode node = jsNode_.getDomNodeOrNull();
    // some event don't apply on all kind of nodes, for instance "blur"
    if (node != null && !node.handles(event)) {
        return;
    }
    final TypeContainer container = getTypeContainer(event.getType());
    final List<Scriptable> listeners = container.getListeners(eventPhase);
    if (!listeners.isEmpty()) {
        event.setCurrentTarget(jsNode_);
        final HtmlPage page;
        if (jsNode_ instanceof Window) {
            page = (HtmlPage) jsNode_.getDomNodeOrDie();
        } else {
            final Scriptable parentScope = jsNode_.getParentScope();
            if (parentScope instanceof Window) {
                page = (HtmlPage) ((Window) parentScope).getDomNodeOrDie();
            } else if (parentScope instanceof HTMLDocument) {
                page = ((HTMLDocument) parentScope).getPage();
            } else {
                page = ((HTMLElement) parentScope).getDomNodeOrDie().getHtmlPageOrNull();
            }
        }
        // no need for a copy, listeners are copy on write
        for (Scriptable listener : listeners) {
            boolean isPropertyHandler = false;
            if (listener == TypeContainer.EVENT_HANDLER_PLACEHOLDER) {
                listener = container.handler_;
                isPropertyHandler = true;
            }
            Function function = null;
            Scriptable thisObject = null;
            if (listener instanceof Function) {
                function = (Function) listener;
                thisObject = jsNode_;
            } else if (listener instanceof NativeObject) {
                final Object handleEvent = ScriptableObject.getProperty(listener, "handleEvent");
                if (handleEvent instanceof Function) {
                    function = (Function) handleEvent;
                    thisObject = listener;
                }
            }
            if (function != null) {
                final ScriptResult result = page.executeJavaScriptFunction(function, thisObject, args, node);
                // Return value is only honored for property handlers (Tested in Chrome/FF/IE11)
                if (isPropertyHandler && !ScriptResult.isUndefined(result)) {
                    event.handlePropertyHandlerReturnValue(result.getJavaScriptResult());
                }
            }
            if (event.isImmediatePropagationStopped()) {
                return;
            }
        }
    }
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) NativeObject(net.sourceforge.htmlunit.corejs.javascript.NativeObject) ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) Function(net.sourceforge.htmlunit.corejs.javascript.Function) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) NativeObject(net.sourceforge.htmlunit.corejs.javascript.NativeObject) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable)

Aggregations

Window (com.gargoylesoftware.htmlunit.javascript.host.Window)33 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)13 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)10 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)9 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)7 JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)6 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)6 WebClient (com.gargoylesoftware.htmlunit.WebClient)5 FrameWindow (com.gargoylesoftware.htmlunit.html.FrameWindow)5 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)5 HTMLDocument (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument)5 IOException (java.io.IOException)5 PostponedAction (com.gargoylesoftware.htmlunit.javascript.PostponedAction)4 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)3 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)3 XHtmlPage (com.gargoylesoftware.htmlunit.html.XHtmlPage)3 JsxConstructor (com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)3 XmlPage (com.gargoylesoftware.htmlunit.xml.XmlPage)3 AjaxController (com.gargoylesoftware.htmlunit.AjaxController)2 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)2