Search in sources :

Example 16 with SgmlPage

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

the class HtmlPasswordInput method setAttributeNS.

/**
 * {@inheritDoc}
 */
@Override
protected void setAttributeNS(final String namespaceURI, final String qualifiedName, final String attributeValue, final boolean notifyAttributeChangeListeners, final boolean notifyMutationObservers) {
    super.setAttributeNS(namespaceURI, qualifiedName, attributeValue, notifyAttributeChangeListeners, notifyMutationObservers);
    if ("value".equals(qualifiedName)) {
        final SgmlPage page = getPage();
        if (page != null && page.isHtmlPage()) {
            int pos = 0;
            if (!hasFeature(JS_INPUT_SET_VALUE_MOVE_SELECTION_TO_START)) {
                pos = attributeValue.length();
            }
            setSelectionStart(pos);
            setSelectionEnd(pos);
        }
    }
}
Also used : SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage)

Example 17 with SgmlPage

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

the class HtmlTextInput method setAttributeNS.

/**
 * {@inheritDoc}
 */
@Override
protected void setAttributeNS(final String namespaceURI, final String qualifiedName, final String attributeValue, final boolean notifyAttributeChangeListeners, final boolean notifyMutationObservers) {
    super.setAttributeNS(namespaceURI, qualifiedName, attributeValue, notifyAttributeChangeListeners, notifyMutationObservers);
    if ("value".equals(qualifiedName)) {
        final SgmlPage page = getPage();
        if (page != null && page.isHtmlPage()) {
            int pos = 0;
            if (!hasFeature(JS_INPUT_SET_VALUE_MOVE_SELECTION_TO_START)) {
                pos = attributeValue.length();
            }
            setSelectionStart(pos);
            setSelectionEnd(pos);
        }
    }
}
Also used : SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage)

Example 18 with SgmlPage

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

the class Document method createElement.

/**
 * 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 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 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;
}
Also used : HtmlImage(com.gargoylesoftware.htmlunit.html.HtmlImage) Matcher(java.util.regex.Matcher) ElementNotFoundException(com.gargoylesoftware.htmlunit.ElementNotFoundException) HtmlInput(com.gargoylesoftware.htmlunit.html.HtmlInput) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) FunctionObject(net.sourceforge.htmlunit.corejs.javascript.FunctionObject) HtmlRp(com.gargoylesoftware.htmlunit.html.HtmlRp) HtmlUnknownElement(com.gargoylesoftware.htmlunit.html.HtmlUnknownElement) HtmlSvg(com.gargoylesoftware.htmlunit.html.HtmlSvg) HtmlRt(com.gargoylesoftware.htmlunit.html.HtmlRt) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 19 with SgmlPage

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

the class NamedAttrNodeMapImpl method doMouseEvent.

/**
 * Simulates the specified mouse event, returning the page which this element's window contains after the event.
 * The returned page may or may not be the same as the original page, depending on JavaScript event handlers, etc.
 *
 * @param eventType the mouse event type to simulate
 * @param shiftKey {@code true} if SHIFT is pressed during the mouse event
 * @param ctrlKey {@code true} if CTRL is pressed during the mouse event
 * @param altKey {@code true} if ALT is pressed during the mouse event
 * @param button the button code, must be {@link MouseEvent#BUTTON_LEFT}, {@link MouseEvent#BUTTON_MIDDLE}
 *        or {@link MouseEvent#BUTTON_RIGHT}
 * @return the page which this element's window contains after the event
 */
private Page doMouseEvent(final String eventType, final boolean shiftKey, final boolean ctrlKey, final boolean altKey, final int button) {
    final SgmlPage page = getPage();
    if (!page.getWebClient().isJavaScriptEnabled()) {
        return page;
    }
    final ScriptResult scriptResult;
    final Event event;
    if (MouseEvent.TYPE_CONTEXT_MENU.equals(eventType) && getPage().getWebClient().getBrowserVersion().hasFeature(EVENT_ONCLICK_USES_POINTEREVENT)) {
        event = new PointerEvent(this, eventType, shiftKey, ctrlKey, altKey, button, 0);
    } else {
        event = new MouseEvent(this, eventType, shiftKey, ctrlKey, altKey, button);
    }
    scriptResult = fireEvent(event);
    final Page currentPage;
    if (scriptResult == null) {
        currentPage = page;
    } else {
        currentPage = page.getWebClient().getCurrentWindow().getEnclosedPage();
    }
    final boolean mouseOver = !MouseEvent.TYPE_MOUSE_OUT.equals(eventType);
    if (mouseOver_ != mouseOver) {
        mouseOver_ = mouseOver;
        page.clearComputedStyles();
    }
    return currentPage;
}
Also used : ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) PointerEvent(com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent) MouseEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) MouseEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) PointerEvent(com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent) Page(com.gargoylesoftware.htmlunit.Page) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage)

Example 20 with SgmlPage

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

the class NamedAttrNodeMapImpl method click.

/**
 * <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();
}
Also used : ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) Page(com.gargoylesoftware.htmlunit.Page) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage)

Aggregations

SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)38 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)10 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)7 DomDocumentFragment (com.gargoylesoftware.htmlunit.html.DomDocumentFragment)5 DomText (com.gargoylesoftware.htmlunit.html.DomText)5 Page (com.gargoylesoftware.htmlunit.Page)4 ScriptResult (com.gargoylesoftware.htmlunit.ScriptResult)4 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)3 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)3 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)3 JsxSetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)3 Node (com.gargoylesoftware.htmlunit.javascript.host.dom.Node)3 HTMLDocument (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument)3 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)2 HtmlInput (com.gargoylesoftware.htmlunit.html.HtmlInput)2 HtmlOption (com.gargoylesoftware.htmlunit.html.HtmlOption)2 JsxConstructor (com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)2 DocumentFragment (com.gargoylesoftware.htmlunit.javascript.host.dom.DocumentFragment)2 MouseEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent)2 PointerEvent (com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent)2