Java Code Examples for net.sourceforge.htmlunit.corejs.javascript.Context#throwAsScriptRuntimeEx()

The following examples show how to use net.sourceforge.htmlunit.corejs.javascript.Context#throwAsScriptRuntimeEx() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HTMLTextAreaElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Sets minimum number of characters in this text area.
 * @param minLength minimum number of characters in this text area.
 */
@JsxSetter({CHROME, FF, FF68, FF60})
public void setMinLength(final String minLength) {
    try {
        final int i = Integer.parseInt(minLength);

        if (i < 0) {
            throw Context.throwAsScriptRuntimeEx(
                new NumberFormatException("New value for minLength '" + minLength + "' is smaller than zero."));
        }
        getDomNodeOrDie().setAttribute("minLength", minLength);
    }
    catch (final NumberFormatException e) {
        getDomNodeOrDie().setAttribute("minLength", "0");
    }
}
 
Example 2
Source File: History.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Replaces a state.
 * @param object the state object
 * @param title the title
 * @param url an optional URL
 */
@JsxFunction
public void replaceState(final Object object, final String title, final String url) {
    final WebWindow w = getWindow().getWebWindow();
    try {
        URL newStateUrl = null;
        final HtmlPage page = (HtmlPage) w.getEnclosedPage();
        if (StringUtils.isNotBlank(url)) {
            newStateUrl = page.getFullyQualifiedUrl(url);
        }
        w.getHistory().replaceState(object, newStateUrl);
    }
    catch (final MalformedURLException e) {
        Context.throwAsScriptRuntimeEx(e);
    }
}
 
Example 3
Source File: HTMLTextAreaElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Sets maximum number of characters in this text area.
 * @param maxLength maximum number of characters in this text area.
 */
@JsxSetter
public void setMaxLength(final String maxLength) {
    try {
        final int i = Integer.parseInt(maxLength);

        if (i < 0 && getBrowserVersion().hasFeature(JS_TEXT_AREA_SET_MAXLENGTH_NEGATIVE_THROWS_EXCEPTION)) {
            throw Context.throwAsScriptRuntimeEx(
                new NumberFormatException("New value for maxLength '" + maxLength + "' is smaller than zero."));
        }
        getDomNodeOrDie().setAttribute("maxLength", maxLength);
    }
    catch (final NumberFormatException e) {
        getDomNodeOrDie().setAttribute("maxLength", "0");
    }
}
 
Example 4
Source File: Window.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a modal dialog box that displays the specified HTML document.
 * @param url the URL of the document to load and display
 * @param arguments object to be made available via <tt>window.dialogArguments</tt> in the dialog window
 * @param features string that specifies the window ornaments for the dialog window
 * @return the value of the {@code returnValue} property as set by the modal dialog's window
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536759.aspx">MSDN Documentation</a>
 * @see <a href="https://developer.mozilla.org/en/DOM/window.showModalDialog">Mozilla Documentation</a>
 */
@JsxFunction(IE)
public Object showModalDialog(final String url, final Object arguments, final String features) {
    final WebWindow webWindow = getWebWindow();
    final WebClient client = webWindow.getWebClient();
    try {
        final URL completeUrl = ((HtmlPage) getDomNodeOrDie()).getFullyQualifiedUrl(url);
        final DialogWindow dialog = client.openDialogWindow(completeUrl, webWindow, arguments);
        // TODO: Theoretically, we shouldn't return until the dialog window has been close()'ed...
        // But we have to return so that the window can be close()'ed...
        // Maybe we can use Rhino's continuation support to save state and restart when
        // the dialog window is close()'ed? Would only work in interpreted mode, though.
        final ScriptableObject jsDialog = dialog.getScriptableObject();
        return jsDialog.get("returnValue", jsDialog);
    }
    catch (final IOException e) {
        throw Context.throwAsScriptRuntimeEx(e);
    }
}
 
Example 5
Source File: Element.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Replaces all child elements of this element with the supplied value.
 * @param value the new value for the contents of this element
 */
@JsxSetter({CHROME, FF})
public void setInnerHTML(final Object value) {
    final DomNode domNode;
    try {
        domNode = getDomNodeOrDie();
    }
    catch (final IllegalStateException e) {
        Context.throwAsScriptRuntimeEx(e);
        return;
    }

    domNode.removeAllChildren();

    final boolean addChildForNull = getBrowserVersion().hasFeature(JS_INNER_HTML_ADD_CHILD_FOR_NULL_VALUE);
    if ((value == null && addChildForNull) || (value != null && !"".equals(value))) {

        final String valueAsString = Context.toString(value);
        parseHtmlSnippet(domNode, valueAsString);
    }
}
 
Example 6
Source File: HTMLDocument.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Closes the document implicitly, i.e. flushes the <tt>document.write</tt> buffer (IE only).
 */
private void implicitCloseIfNecessary() {
    if (!writeInCurrentDocument_) {
        try {
            close();
        }
        catch (final IOException e) {
            throw Context.throwAsScriptRuntimeEx(e);
        }
    }
}
 
Example 7
Source File: SimpleScriptable.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean hasInstance(final Scriptable instance) {
    if (getPrototype() == null) {
        // to handle cases like "x instanceof HTMLElement",
        // but HTMLElement is not in the prototype chain of any element
        final Object prototype = get("prototype", this);
        if (!(prototype instanceof ScriptableObject)) {
            Context.throwAsScriptRuntimeEx(new Exception("Null prototype"));
        }
        return ((ScriptableObject) prototype).hasInstance(instance);
    }

    return super.hasInstance(instance);
}
 
Example 8
Source File: CSSStyleSheet.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes an existing rule.
 * @param position the position of the rule to be deleted
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms531195(v=VS.85).aspx">MSDN</a>
 */
@JsxFunction({IE, CHROME})
public void removeRule(final int position) {
    try {
        initCssRules();
        wrapped_.deleteRule(fixIndex(position));
        refreshCssRules();
    }
    catch (final DOMException e) {
        throw Context.throwAsScriptRuntimeEx(e);
    }
}
 
Example 9
Source File: CanvasRenderingContext2D.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the Data URL.
 *
 * @param type an optional type
 * @return the dataURL
 */
public String toDataURL(String type) {
    try {
        if (type == null) {
            type = "image/png";
        }
        return "data:" + type + ";base64," + getRenderingBackend().encodeToString(type);
    }
    catch (final IOException ioe) {
        throw Context.throwAsScriptRuntimeEx(ioe);
    }
}
 
Example 10
Source File: History.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * JavaScript function "back".
 */
@JsxFunction
public void back() {
    try {
        getWindow().getWebWindow().getHistory().back();
    }
    catch (final IOException e) {
        Context.throwAsScriptRuntimeEx(e);
    }
}
 
Example 11
Source File: Iterator.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void setParentScope(final Scriptable scope) {
    super.setParentScope(scope);
    try {
        final FunctionObject functionObject = new FunctionObject("next", getClass().getDeclaredMethod("next"),
                scope);
        defineProperty("next", functionObject, ScriptableObject.DONTENUM);
    }
    catch (final Exception e) {
        Context.throwAsScriptRuntimeEx(e);
    }
}
 
Example 12
Source File: XMLDocument.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public SimpleScriptable makeScriptableFor(final DomNode domNode) {
    final SimpleScriptable scriptable;

    // TODO: cleanup, getScriptObject() should be used!!!
    if (domNode instanceof DomElement && !(domNode instanceof HtmlElement)) {
        if (domNode instanceof SvgElement) {
            final Class<? extends HtmlUnitScriptable> javaScriptClass
                = ((JavaScriptEngine) getWindow().getWebWindow().getWebClient()
                    .getJavaScriptEngine()).getJavaScriptClass(domNode.getClass());
            try {
                scriptable = (SimpleScriptable) javaScriptClass.newInstance();
            }
            catch (final Exception e) {
                throw Context.throwAsScriptRuntimeEx(e);
            }
        }
        else {
            scriptable = new Element();
        }
    }
    else if (domNode instanceof DomAttr) {
        scriptable = new Attr();
    }
    else {
        return super.makeScriptableFor(domNode);
    }

    scriptable.setPrototype(getPrototype(scriptable.getClass()));
    scriptable.setParentScope(getParentScope());
    scriptable.setDomNode(domNode);
    return scriptable;
}
 
Example 13
Source File: HTMLAnchorElement.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the {@code shape} attribute.
 * @param shape {@code shape} attribute
 */
@JsxSetter
public void setShape(final String shape) {
    throw Context.throwAsScriptRuntimeEx(new UnsupportedOperationException());
}
 
Example 14
Source File: HTMLAreaElement.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the {@code relList} attribute.
 * @return the {@code relList} attribute
 */
@JsxGetter(FF)
public DOMTokenList getRelList() {
    throw Context.throwAsScriptRuntimeEx(new UnsupportedOperationException());
}
 
Example 15
Source File: HTMLAnchorElement.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the {@code mimeType} attribute.
 * @return the {@code mimeType} attribute
 */
@JsxGetter(IE)
public String getMimeType() {
    throw Context.throwAsScriptRuntimeEx(new UnsupportedOperationException());
}
 
Example 16
Source File: HTMLAnchorElement.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the {@code Methods} attribute.
 * @return the {@code Methods} attribute
 */
@JsxGetter(propertyName = "Methods", value = IE)
public String getMethods() {
    throw Context.throwAsScriptRuntimeEx(new UnsupportedOperationException());
}
 
Example 17
Source File: HTMLAnchorElement.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the {@code mimeType} attribute.
 * @param mimeType {@code mimeType} attribute
 */
@JsxSetter(IE)
public void setMimeType(final String mimeType) {
    throw Context.throwAsScriptRuntimeEx(new UnsupportedOperationException());
}
 
Example 18
Source File: HTMLAnchorElement.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the {@code Methods} attribute.
 * @param methods {@code Methods} attribute
 */
@JsxSetter(propertyName = "Methods", value = IE)
public void setMethods(final String methods) {
    throw Context.throwAsScriptRuntimeEx(new UnsupportedOperationException());
}
 
Example 19
Source File: HTMLAnchorElement.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the {@code shape} attribute.
 * @return the {@code shape} attribute
 */
@JsxGetter
public String getShape() {
    throw Context.throwAsScriptRuntimeEx(new UnsupportedOperationException());
}
 
Example 20
Source File: BeforeUnloadEvent.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * The JavaScript constructor. It seems it is not possible to do it from JavaScript code.
 */
@JsxConstructor({CHROME, FF, EDGE})
public void jConstructor() {
    Context.throwAsScriptRuntimeEx(new IllegalArgumentException("Illegal Constructor"));
}