Search in sources :

Example 1 with HTMLElement

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

the class HtmlPage method setFocusedElement.

/**
 * Moves the focus to the specified element. This will trigger any relevant JavaScript
 * event handlers.
 *
 * @param newElement the element that will receive the focus, use {@code null} to remove focus from any element
 * @param windowActivated - whether the enclosing window got focus resulting in specified element getting focus
 * @return true if the specified element now has the focus
 * @see #getFocusedElement()
 */
public boolean setFocusedElement(final DomElement newElement, final boolean windowActivated) {
    if (elementWithFocus_ == newElement && !windowActivated) {
        // nothing to do
        return true;
    }
    final DomElement oldFocusedElement = elementWithFocus_;
    elementWithFocus_ = null;
    if (getWebClient().isJavaScriptEnabled()) {
        final Object o = getScriptableObject();
        if (o instanceof HTMLDocument) {
            ((HTMLDocument) o).setActiveElement(null);
        }
    }
    if (!windowActivated) {
        if (hasFeature(EVENT_FOCUS_IN_FOCUS_OUT_BLUR)) {
            if (oldFocusedElement != null) {
                oldFocusedElement.fireEvent(Event.TYPE_FOCUS_OUT);
            }
            if (newElement != null) {
                newElement.fireEvent(Event.TYPE_FOCUS_IN);
            }
        }
        if (oldFocusedElement != null) {
            oldFocusedElement.removeFocus();
            oldFocusedElement.fireEvent(Event.TYPE_BLUR);
            if (hasFeature(EVENT_FOCUS_FOCUS_IN_BLUR_OUT)) {
                oldFocusedElement.fireEvent(Event.TYPE_FOCUS_OUT);
            }
        }
    }
    elementWithFocus_ = newElement;
    // might be changed by another thread
    if (newElement instanceof SelectableTextInput && hasFeature(PAGE_SELECTION_RANGE_FROM_SELECTABLE_TEXT_INPUT)) {
        final SelectableTextInput sti = (SelectableTextInput) newElement;
        setSelectionRange(new SimpleRange(sti, sti.getSelectionStart(), sti, sti.getSelectionEnd()));
    }
    if (newElement != null) {
        if (getWebClient().isJavaScriptEnabled()) {
            final Object o = getScriptableObject();
            if (o instanceof HTMLDocument) {
                final Object e = newElement.getScriptableObject();
                if (e instanceof HTMLElement) {
                    ((HTMLDocument) o).setActiveElement((HTMLElement) e);
                }
            }
        }
        newElement.focus();
        newElement.fireEvent(Event.TYPE_FOCUS);
        if (hasFeature(EVENT_FOCUS_FOCUS_IN_BLUR_OUT)) {
            newElement.fireEvent(Event.TYPE_FOCUS_IN);
        }
    }
    // element will not have the focus because its page has gone away.
    return this == getEnclosingWindow().getEnclosedPage();
}
Also used : HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) SimpleRange(com.gargoylesoftware.htmlunit.html.impl.SimpleRange) SelectableTextInput(com.gargoylesoftware.htmlunit.html.impl.SelectableTextInput)

Example 2 with HTMLElement

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

the class HtmlImage method fireEvent.

/**
 * {@inheritDoc}
 */
@Override
public ScriptResult fireEvent(final Event event) {
    if (event instanceof MouseEvent) {
        final MouseEvent mouseEvent = (MouseEvent) event;
        final HTMLElement scriptableObject = (HTMLElement) getScriptableObject();
        if (lastClickX_ >= 0) {
            mouseEvent.setClientX(scriptableObject.getPosX() + lastClickX_);
        }
        if (lastClickY_ >= 0) {
            mouseEvent.setClientY(scriptableObject.getPosX() + lastClickY_);
        }
    }
    return super.fireEvent(event);
}
Also used : MouseEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)

Example 3 with HTMLElement

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

the class HtmlElement method getDoTypeNode.

/**
 * Returns the node to type into.
 * @return the node
 */
private DomText getDoTypeNode() {
    final HTMLElement scriptElement = getScriptableObject();
    if (scriptElement.isIsContentEditable() || "on".equals(((Document) scriptElement.getOwnerDocument()).getDesignMode())) {
        DomNodeList<DomNode> children = getChildNodes();
        while (!children.isEmpty()) {
            final DomNode lastChild = children.get(children.size() - 1);
            if (lastChild instanceof DomText) {
                return (DomText) lastChild;
            }
            children = lastChild.getChildNodes();
        }
        final DomText domText = new DomText(getPage(), "");
        appendChild(domText);
        return domText;
    }
    return null;
}
Also used : HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) Document(com.gargoylesoftware.htmlunit.javascript.host.dom.Document)

Example 4 with HTMLElement

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

the class HtmlUnitScriptable method makeScriptableFor.

/**
 * Builds a new the JavaScript object that corresponds to the specified object.
 * @param domNode the DOM node for which a JS object should be created
 * @return the JavaScript object
 */
public HtmlUnitScriptable makeScriptableFor(final DomNode domNode) {
    // Get the JS class name for the specified DOM node.
    // Walk up the inheritance chain if necessary.
    Class<? extends HtmlUnitScriptable> javaScriptClass = null;
    if (domNode instanceof HtmlImage && "image".equals(((HtmlImage) domNode).getOriginalQualifiedName()) && ((HtmlImage) domNode).wasCreatedByJavascript()) {
        if (domNode.hasFeature(HTMLIMAGE_HTMLELEMENT)) {
            javaScriptClass = HTMLElement.class;
        } else if (domNode.hasFeature(HTMLIMAGE_HTMLUNKNOWNELEMENT)) {
            javaScriptClass = HTMLUnknownElement.class;
        }
    }
    if (javaScriptClass == null) {
        final JavaScriptEngine javaScriptEngine = (JavaScriptEngine) getWindow().getWebWindow().getWebClient().getJavaScriptEngine();
        for (Class<?> c = domNode.getClass(); javaScriptClass == null && c != null; c = c.getSuperclass()) {
            javaScriptClass = javaScriptEngine.getJavaScriptClass(c);
        }
    }
    final HtmlUnitScriptable scriptable;
    if (javaScriptClass == null) {
        // We don't have a specific subclass for this element so create something generic.
        scriptable = new HTMLElement();
        if (LOG.isDebugEnabled()) {
            LOG.debug("No JavaScript class found for element <" + domNode.getNodeName() + ">. Using HTMLElement");
        }
    } else {
        try {
            scriptable = javaScriptClass.newInstance();
        } catch (final Exception e) {
            throw Context.throwAsScriptRuntimeEx(e);
        }
    }
    initParentScope(domNode, scriptable);
    scriptable.setPrototype(getPrototype(javaScriptClass));
    scriptable.setDomNode(domNode);
    return scriptable;
}
Also used : HtmlImage(com.gargoylesoftware.htmlunit.html.HtmlImage) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) HTMLUnknownElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLUnknownElement)

Example 5 with HTMLElement

use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement 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)

Aggregations

HTMLElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)35 AttributedString (java.text.AttributedString)10 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)7 Test (org.junit.Test)7 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)6 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)6 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)5 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)5 MouseEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent)5 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)5 BaseFrameElement (com.gargoylesoftware.htmlunit.html.BaseFrameElement)4 HtmlAttributeChangeEvent (com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent)4 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)4 Event (com.gargoylesoftware.htmlunit.javascript.host.event.Event)4 MessageEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent)4 HTMLUnknownElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLUnknownElement)4 StyleElement (com.gargoylesoftware.htmlunit.css.StyleElement)3 Element (com.gargoylesoftware.htmlunit.javascript.host.Element)3 HTMLBodyElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLBodyElement)3 HTMLCanvasElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCanvasElement)3