Search in sources :

Example 6 with SgmlPage

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

the class HTMLOptionElement method jsConstructor.

/**
 * JavaScript constructor.
 * @param newText the text
 * @param newValue the value
 * @param defaultSelected Whether the option is initially selected
 * @param selected the current selection state of the option
 */
@JsxConstructor({ CHROME, EDGE, FF, FF_ESR })
public void jsConstructor(final Object newText, final String newValue, final boolean defaultSelected, final boolean selected) {
    final SgmlPage page = (SgmlPage) getWindow().getWebWindow().getEnclosedPage();
    AttributesImpl attributes = null;
    if (defaultSelected) {
        attributes = new AttributesImpl();
        attributes.addAttribute(null, "selected", "selected", null, "selected");
    }
    final HtmlOption htmlOption = (HtmlOption) page.getWebClient().getPageCreator().getHtmlParser().getFactory(HtmlOption.TAG_NAME).createElement(page, HtmlOption.TAG_NAME, attributes);
    htmlOption.setSelected(selected);
    setDomNode(htmlOption);
    if (!Undefined.isUndefined(newText)) {
        final String newTextString = Context.toString(newText);
        htmlOption.appendChild(new DomText(page, newTextString));
        htmlOption.setLabelAttribute(newTextString);
    }
    if (!"undefined".equals(newValue)) {
        htmlOption.setValueAttribute(newValue);
    }
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) DomText(com.gargoylesoftware.htmlunit.html.DomText) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) HtmlOption(com.gargoylesoftware.htmlunit.html.HtmlOption) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)

Example 7 with SgmlPage

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

the class HTMLOptionsCollection method setLength.

/**
 * Changes the number of options: removes options if the new length
 * is less than the current one else add new empty options to reach the
 * new length.
 * @param newLength the new length property value
 */
@JsxSetter
public void setLength(final int newLength) {
    if (newLength < 0) {
        if (getBrowserVersion().hasFeature(JS_SELECT_OPTIONS_IGNORE_NEGATIVE_LENGTH)) {
            return;
        }
        throw Context.reportRuntimeError("Length is negative");
    }
    final int currentLength = htmlSelect_.getOptionSize();
    if (currentLength > newLength) {
        htmlSelect_.setOptionSize(newLength);
    } else {
        final SgmlPage page = htmlSelect_.getPage();
        final ElementFactory factory = page.getWebClient().getPageCreator().getHtmlParser().getFactory(HtmlOption.TAG_NAME);
        for (int i = currentLength; i < newLength; i++) {
            final HtmlOption option = (HtmlOption) factory.createElement(page, HtmlOption.TAG_NAME, null);
            htmlSelect_.appendOption(option);
        }
    }
}
Also used : SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) HtmlOption(com.gargoylesoftware.htmlunit.html.HtmlOption) ElementFactory(com.gargoylesoftware.htmlunit.html.ElementFactory) JsxSetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)

Example 8 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 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 triggerMouseEvents if true trigger the mouse events also
 * @param handleFocus if true set the focus (and trigger the event)
 * @param ignoreVisibility whether to ignore visibility or not
 * @param disableProcessLabelAfterBubbling ignore label processing
 * @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 boolean shiftKey, final boolean ctrlKey, final boolean altKey, final boolean triggerMouseEvents, final boolean handleFocus, final boolean ignoreVisibility, final boolean disableProcessLabelAfterBubbling) throws IOException {
    // make enclosing window the current one
    final SgmlPage page = getPage();
    page.getWebClient().setCurrentWindow(page.getEnclosingWindow());
    if (!ignoreVisibility) {
        if (!(page instanceof HtmlPage)) {
            return (P) page;
        }
        if (!isDisplayed()) {
            if (LOG.isWarnEnabled()) {
                LOG.warn("Calling click() ignored because the target element '" + this + "' is not displayed.");
            }
            return (P) page;
        }
        if (isDisabledElementAndDisabled()) {
            if (LOG.isWarnEnabled()) {
                LOG.warn("Calling click() ignored because the target element '" + this + "' is disabled.");
            }
            return (P) page;
        }
    }
    synchronized (page) {
        if (triggerMouseEvents) {
            mouseDown(shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT);
        }
        if (handleFocus) {
            // give focus to current element (if possible) or only remove it from previous one
            DomElement elementToFocus = null;
            if (this instanceof SubmittableElement || this instanceof HtmlAnchor && ATTRIBUTE_NOT_DEFINED != ((HtmlAnchor) this).getHrefAttribute() || this instanceof HtmlArea && (ATTRIBUTE_NOT_DEFINED != ((HtmlArea) this).getHrefAttribute() || getPage().getWebClient().getBrowserVersion().hasFeature(JS_AREA_WITHOUT_HREF_FOCUSABLE)) || this instanceof HtmlElement && ((HtmlElement) this).getTabIndex() != null) {
                elementToFocus = this;
            } else if (this instanceof HtmlOption) {
                elementToFocus = ((HtmlOption) this).getEnclosingSelect();
            }
            if (elementToFocus == null) {
                ((HtmlPage) page).setFocusedElement(null);
            } else {
                elementToFocus.focus();
            }
        }
        if (triggerMouseEvents) {
            mouseUp(shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT);
        }
        MouseEvent event = null;
        if (page.getWebClient().isJavaScriptEnabled()) {
            final BrowserVersion browser = page.getWebClient().getBrowserVersion();
            if (browser.hasFeature(EVENT_ONCLICK_USES_POINTEREVENT)) {
                if (browser.hasFeature(EVENT_ONCLICK_POINTEREVENT_DETAIL_0)) {
                    event = new PointerEvent(getEventTargetElement(), MouseEvent.TYPE_CLICK, shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT, 0);
                } else {
                    event = new PointerEvent(getEventTargetElement(), MouseEvent.TYPE_CLICK, shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT, 1);
                }
            } else {
                event = new MouseEvent(getEventTargetElement(), MouseEvent.TYPE_CLICK, shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT);
            }
            if (disableProcessLabelAfterBubbling) {
                event.disableProcessLabelAfterBubbling();
            }
        }
        return click(event, shiftKey, ctrlKey, altKey, ignoreVisibility);
    }
}
Also used : PointerEvent(com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent) MouseEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion)

Example 9 with SgmlPage

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

the class DomNode method getScriptableObject.

/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Returns the JavaScript object that corresponds to this node, lazily initializing a new one if necessary.
 *
 * The logic of when and where the JavaScript object is created needs a clean up: functions using
 * a DOM node's JavaScript object should not have to check if they should create it first.
 *
 * @param <T> the object type
 * @return the JavaScript object that corresponds to this node
 */
@SuppressWarnings("unchecked")
public <T> T getScriptableObject() {
    if (scriptObject_ == null) {
        final SgmlPage page = getPage();
        if (this == page) {
            final StringBuilder msg = new StringBuilder("No script object associated with the Page.");
            // because this is a strange case we like to provide as much info as possible
            msg.append(" class: '").append(page.getClass().getName()).append('\'');
            try {
                msg.append(" url: '").append(page.getUrl()).append("' content: ").append(page.getWebResponse().getContentAsString());
            } catch (final Exception e) {
                // ok bad luck with detail
                msg.append(" no details: '").append(e).append('\'');
            }
            throw new IllegalStateException(msg.toString());
        }
        final Object o = page.getScriptableObject();
        if (o instanceof HtmlUnitScriptable) {
            scriptObject_ = ((HtmlUnitScriptable) o).makeScriptableFor(this);
        }
    }
    return (T) scriptObject_;
}
Also used : HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) CSSException(com.gargoylesoftware.css.parser.CSSException) DOMException(org.w3c.dom.DOMException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException)

Example 10 with SgmlPage

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

the class XMLDOMNode method appendChild.

/**
 * Appends a new child as the last child of the node.
 * @param newChild the new child node to be appended at the end of the list of children belonging to this node
 * @return the new child node successfully appended to the list
 */
@JsxFunction
public Object appendChild(final Object newChild) {
    if (newChild == null || "null".equals(newChild)) {
        throw Context.reportRuntimeError("Type mismatch.");
    }
    Object appendedChild = null;
    if (newChild instanceof XMLDOMNode) {
        final XMLDOMNode childNode = (XMLDOMNode) newChild;
        // Get XML node for the DOM node passed in
        final DomNode childDomNode = childNode.getDomNodeOrDie();
        // Get the parent XML node that the child should be added to.
        final DomNode parentNode = getDomNodeOrDie();
        // Append the child to the parent node
        parentNode.appendChild(childDomNode);
        appendedChild = newChild;
        // create a DocumentFragment to be the parentNode's parentNode.
        if (!(parentNode instanceof SgmlPage) && !(this instanceof XMLDOMDocumentFragment) && parentNode.getParentNode() == null) {
            final DomDocumentFragment fragment = parentNode.getPage().createDocumentFragment();
            fragment.appendChild(parentNode);
        }
    }
    return appendedChild;
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomDocumentFragment(com.gargoylesoftware.htmlunit.html.DomDocumentFragment) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

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