Search in sources :

Example 1 with SgmlPage

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

the class HtmlForm method reset.

/**
 * Resets this form to its initial values, returning the page contained by this form's window after the
 * reset. Note that the returned page may or may not be the same as the original page, based on JavaScript
 * event handlers, etc.
 *
 * @return the page contained by this form's window after the reset
 */
public Page reset() {
    final SgmlPage htmlPage = getPage();
    final ScriptResult scriptResult = fireEvent(Event.TYPE_RESET);
    if (ScriptResult.isFalse(scriptResult)) {
        return htmlPage.getWebClient().getCurrentWindow().getEnclosedPage();
    }
    for (final HtmlElement next : getHtmlElementDescendants()) {
        if (next instanceof SubmittableElement) {
            ((SubmittableElement) next).reset();
        }
    }
    return htmlPage;
}
Also used : ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage)

Example 2 with SgmlPage

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

the class HtmlNumberInput 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 3 with SgmlPage

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

the class Range method createContextualFragment.

/**
 * Parses an HTML snippet.
 * @param valueAsString text that contains text and tags to be converted to a document fragment
 * @return a document fragment
 * @see <a href="https://developer.mozilla.org/en-US/docs/DOM/range.createContextualFragment">Mozilla
 * documentation</a>
 */
@JsxFunction
public Object createContextualFragment(final String valueAsString) {
    final SgmlPage page = startContainer_.getDomNodeOrDie().getPage();
    final DomDocumentFragment fragment = new DomDocumentFragment(page);
    try {
        page.getWebClient().getPageCreator().getHtmlParser().parseFragment(fragment, startContainer_.getDomNodeOrDie(), valueAsString, false);
    } catch (final Exception e) {
        LogFactory.getLog(Range.class).error("Unexpected exception occurred in createContextualFragment", e);
        throw Context.reportRuntimeError("Unexpected exception occurred in createContextualFragment: " + e.getMessage());
    }
    return fragment.getScriptableObject();
}
Also used : DomDocumentFragment(com.gargoylesoftware.htmlunit.html.DomDocumentFragment) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 4 with SgmlPage

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

the class HTMLAnchorElement method getDefaultValue.

static String getDefaultValue(final HtmlElement element) {
    String href = element.getAttributeDirect("href");
    if (ATTRIBUTE_NOT_DEFINED == href) {
        // for example for named anchors
        return "";
    }
    href = href.trim();
    final SgmlPage page = element.getPage();
    if (page == null || !page.isHtmlPage()) {
        return href;
    }
    try {
        return HtmlAnchor.getTargetUrl(href, (HtmlPage) page).toExternalForm();
    } catch (final MalformedURLException e) {
        return href;
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage)

Example 5 with SgmlPage

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

the class HTMLInputElement method setType.

/**
 * Sets the value of the attribute {@code type}.
 * Note: this replace the DOM node with a new one.
 * @param newType the new type to set
 * @param setThroughAttribute set type value through setAttribute()
 */
private void setType(String newType, final boolean setThroughAttribute) {
    HtmlInput input = getDomNodeOrDie();
    final String currentType = input.getAttributeDirect("type");
    final BrowserVersion browser = getBrowserVersion();
    if (!currentType.equalsIgnoreCase(newType)) {
        if (newType != null && browser.hasFeature(JS_INPUT_SET_TYPE_LOWERCASE)) {
            newType = newType.toLowerCase(Locale.ROOT);
        }
        if (!isSupported(newType.toLowerCase(Locale.ROOT), browser)) {
            if (setThroughAttribute) {
                newType = "text";
            } else if (browser.hasFeature(JS_INPUT_SET_UNSUPORTED_TYPE_EXCEPTION)) {
                throw Context.reportRuntimeError("Invalid argument '" + newType + "' for setting property type.");
            }
        }
        final AttributesImpl attributes = readAttributes(input);
        final int index = attributes.getIndex("type");
        if (index > -1) {
            attributes.setValue(index, newType);
        } else {
            attributes.addAttribute(null, "type", "type", null, newType);
        }
        // create a new one only if we have a new type
        if (ATTRIBUTE_NOT_DEFINED != currentType || !"text".equalsIgnoreCase(newType)) {
            final SgmlPage page = input.getPage();
            final HtmlInput newInput = (HtmlInput) page.getWebClient().getPageCreator().getHtmlParser().getFactory(HtmlInput.TAG_NAME).createElement(page, HtmlInput.TAG_NAME, attributes);
            if (input.wasCreatedByJavascript()) {
                newInput.markAsCreatedByJavascript();
            }
            if (input.getParentNode() == null) {
                // the input hasn't yet been inserted into the DOM tree (likely has been
                // created via document.createElement()), so simply replace it with the
                // new Input instance created in the code above
                input = newInput;
            } else {
                input.getParentNode().replaceChild(newInput, input);
            }
            input.setScriptableObject(null);
            setDomNode(newInput, true);
        } else {
            super.setAttribute("type", newType);
        }
    }
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) HtmlInput(com.gargoylesoftware.htmlunit.html.HtmlInput) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion)

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