net.sourceforge.htmlunit.corejs.javascript.Context Java Examples

The following examples show how to use net.sourceforge.htmlunit.corejs.javascript.Context. 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: CanvasRenderingContext2D.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Calculate TextMetrics for the given text.
 * @param text the text to measure
 * @return the text metrics
 */
@JsxFunction
public TextMetrics measureText(final Object text) {
    if (text == null || Undefined.instance == text) {
        throw Context.throwAsScriptRuntimeEx(
                new RuntimeException("Missing argument for CanvasRenderingContext2D.measureText()."));
    }

    final String textValue = Context.toString(text);

    // TODO take font into account
    final int width = textValue.length() * getBrowserVersion().getPixesPerChar();

    final TextMetrics metrics = new TextMetrics(width);
    metrics.setParentScope(getParentScope());
    metrics.setPrototype(getPrototype(metrics.getClass()));
    return metrics;
}
 
Example #2
Source File: Node.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Encapsulates the given {@link DOMException} into a Rhino-compatible exception.
 *
 * @param exception the exception to encapsulate
 * @return the created exception
 */
protected RhinoException asJavaScriptException(final DOMException exception) {
    final Window w = getWindow();
    exception.setPrototype(w.getPrototype(exception.getClass()));
    exception.setParentScope(w);

    // get current line and file name
    // this method can only be used in interpreted mode. If one day we choose to use compiled mode,
    // then we'll have to find an other way here.
    final String fileName;
    final int lineNumber;
    if (Context.getCurrentContext().getOptimizationLevel() == -1) {
        final int[] linep = new int[1];
        final String sourceName = new Interpreter().getSourcePositionFromStack(Context.getCurrentContext(), linep);
        fileName = sourceName.replaceFirst("script in (.*) from .*", "$1");
        lineNumber = linep[0];
    }
    else {
        throw new Error("HtmlUnit not ready to run in compiled mode");
    }

    exception.setLocation(fileName, lineNumber);

    return new JavaScriptException(exception, fileName, lineNumber);
}
 
Example #3
Source File: XMLDOMCharacterData.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Deletes specified data.
 * @param offset the offset, in characters, at which to start deleting string data
 * @param count the number of characters to delete
 */
@JsxFunction
public void deleteData(final int offset, final int count) {
    if (offset < 0) {
        throw Context.reportRuntimeError("The offset must be 0 or a positive number that is not greater than the "
                + "number of characters in the data.");
    }
    if (count < 0) {
        throw Context.reportRuntimeError("The offset must be 0 or a positive number that is not greater than the "
                + "number of characters in the data.");
    }
    if (count == 0) {
        return;
    }

    final DomCharacterData domCharacterData = getDomNodeOrDie();
    if (offset > domCharacterData.getLength()) {
        throw Context.reportRuntimeError("The offset must be 0 or a positive number that is not greater than the "
                + "number of characters in the data.");
    }

    domCharacterData.deleteData(offset, count);
}
 
Example #4
Source File: HTMLTextAreaElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the number of rows in this text area.
 * @param rows the number of rows in this text area
 */
@JsxSetter
public void setRows(final String rows) {
    try {
        final int i = new Float(rows).intValue();
        if (i < 0) {
            if (getBrowserVersion().hasFeature(JS_TEXT_AREA_SET_ROWS_NEGATIVE_THROWS_EXCEPTION)) {
                throw new NumberFormatException("New value for rows '" + rows + "' is smaller than zero.");
            }
            getDomNodeOrDie().setAttribute("rows", null);
            return;
        }
        getDomNodeOrDie().setAttribute("rows", Integer.toString(i));
    }
    catch (final NumberFormatException e) {
        if (getBrowserVersion().hasFeature(JS_TEXT_AREA_SET_ROWS_THROWS_EXCEPTION)) {
            throw Context.throwAsScriptRuntimeEx(e);
        }

        getDomNodeOrDie().setAttribute("rows", "2");
    }
}
 
Example #5
Source File: Document.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the value of the {@code forms} property.
 * @return the value of the {@code forms} property
 */
@JsxGetter({CHROME, IE})
public Object getForms() {
    return new HTMLCollection(getDomNodeOrDie(), false) {
        @Override
        protected boolean isMatching(final DomNode node) {
            return node instanceof HtmlForm && node.getPrefix() == null;
        }

        @Override
        public Object call(final Context cx, final Scriptable scope,
                final Scriptable thisObj, final Object[] args) {
            if (getBrowserVersion().hasFeature(JS_DOCUMENT_FORMS_FUNCTION_SUPPORTED)) {
                return super.call(cx, scope, thisObj, args);
            }
            throw Context.reportRuntimeError("TypeError: document.forms is not a function");
        }
    };
}
 
Example #6
Source File: XMLDOMElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Sets or updates the supplied attribute node on this element.
 * @param newAtt the attribute node to be associated with this element
 * @return the replaced attribute node, if any, {@code null} otherwise
 */
@JsxFunction
public XMLDOMAttribute setAttributeNode(final XMLDOMAttribute newAtt) {
    if (newAtt == null) {
        throw Context.reportRuntimeError("Type mismatch.");
    }

    final String name = newAtt.getBaseName();

    final XMLDOMNamedNodeMap nodes = (XMLDOMNamedNodeMap) getAttributes();
    final XMLDOMAttribute replacedAtt = (XMLDOMAttribute) nodes.getNamedItemWithoutSyntheticClassAttr(name);
    if (replacedAtt != null) {
        replacedAtt.detachFromParent();
    }

    final DomAttr newDomAttr = newAtt.getDomNodeOrDie();
    getDomNodeOrDie().setAttributeNode(newDomAttr);
    return replacedAtt;
}
 
Example #7
Source File: Node.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether the given element is contained within this object.
 * @param element element object that specifies the element to check
 * @return true if the element is contained within this object
 */
@JsxFunction({CHROME, FF})
public boolean contains(final Object element) {
    if (!(element instanceof Node)) {
        if (getBrowserVersion().hasFeature(JS_NODE_CONTAINS_RETURNS_FALSE_FOR_INVALID_ARG)) {
            return false;
        }
        throw Context.reportRuntimeError("Could not convert JavaScript argument arg 0");
    }

    if (getBrowserVersion().hasFeature(JS_NODE_CONTAINS_RETURNS_FALSE_FOR_INVALID_ARG)) {
        if (element instanceof CharacterData) {
            return false;
        }
        if (this instanceof CharacterData) {
            throw Context.reportRuntimeError("Function 'contains' not available for text nodes.");
        }
    }

    for (Node parent = (Node) element; parent != null; parent = parent.getParentElement()) {
        if (this == parent) {
            return true;
        }
    }
    return false;
}
 
Example #8
Source File: HTMLTableColElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the value of the {@code span} property.
 * @param span the value of the {@code span} property
 */
@JsxSetter
public void setSpan(final Object span) {
    final double d = Context.toNumber(span);
    int i = (int) d;
    if (i < 1) {
        if (getBrowserVersion().hasFeature(JS_TABLE_SPAN_THROWS_EXCEPTION_IF_INVALID)) {
            final Exception e = new Exception("Cannot set the span property to invalid value: " + span);
            Context.throwAsScriptRuntimeEx(e);
        }
        else {
            i = 1;
        }
    }
    getDomNodeOrDie().setAttribute("span", Integer.toString(i));
}
 
Example #9
Source File: Document.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the value of the {@code embeds} property.
 * @return the value of the {@code embeds} property
 */
@JsxGetter({CHROME, IE})
public Object getImages() {
    return new HTMLCollection(getDomNodeOrDie(), false) {
        @Override
        protected boolean isMatching(final DomNode node) {
            return node instanceof HtmlImage;
        }

        @Override
        public Object call(final Context cx, final Scriptable scope,
                final Scriptable thisObj, final Object[] args) {
            if (getBrowserVersion().hasFeature(JS_DOCUMENT_FORMS_FUNCTION_SUPPORTED)) {
                return super.call(cx, scope, thisObj, args);
            }
            throw Context.reportRuntimeError("TypeError: document.images is not a function");
        }
    };
}
 
Example #10
Source File: HTMLImageElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the value of the {@code src} attribute.
 * @return the value of the {@code src} attribute
 */
@JsxGetter
public String getSrc() {
    final HtmlImage img = (HtmlImage) getDomNodeOrDie();
    final String src = img.getSrcAttribute();
    if ("".equals(src)) {
        return src;
    }
    try {
        final HtmlPage page = (HtmlPage) img.getPage();
        return page.getFullyQualifiedUrl(src).toExternalForm();
    }
    catch (final MalformedURLException e) {
        final String msg = "Unable to create fully qualified URL for src attribute of image " + e.getMessage();
        throw Context.reportRuntimeError(msg);
    }
}
 
Example #11
Source File: HTMLTextAreaElement.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the number of columns in this text area.
 * @param cols the number of columns in this text area
 */
@JsxSetter
public void setCols(final String cols) {
    try {
        final int i = Float.valueOf(cols).intValue();
        if (i < 0) {
            if (getBrowserVersion().hasFeature(JS_TEXT_AREA_SET_COLS_NEGATIVE_THROWS_EXCEPTION)) {
                throw new NumberFormatException("New value for cols '" + cols + "' is smaller than zero.");
            }
            getDomNodeOrDie().setAttribute("cols", null);
            return;
        }
        getDomNodeOrDie().setAttribute("cols", Integer.toString(i));
    }
    catch (final NumberFormatException e) {
        if (getBrowserVersion().hasFeature(JS_TEXT_AREA_SET_COLS_THROWS_EXCEPTION)) {
            throw Context.throwAsScriptRuntimeEx(e);
        }
        getDomNodeOrDie().setAttribute("cols", "20");
    }
}
 
Example #12
Source File: XMLHttpRequest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Invokes the onerror handler if one has been set.
 * @param context the context within which the onerror handler is to be invoked;
 *                if {@code null}, the current thread's context is used.
 */
private void processError(Context context) {
    final Function onError = getOnerror();
    if (onError != null) {
        final Scriptable scope = onError.getParentScope();
        final JavaScriptEngine jsEngine = (JavaScriptEngine) containingPage_.getWebClient().getJavaScriptEngine();

        final Object[] params = {new ProgressEvent(this, Event.TYPE_ERROR)};

        if (LOG.isDebugEnabled()) {
            LOG.debug("Calling onerror handler");
        }
        jsEngine.callFunction(containingPage_, onError, this, scope, params);
        if (LOG.isDebugEnabled()) {
            if (context == null) {
                context = Context.getCurrentContext();
            }
            LOG.debug("onerror handler: " + context.decompileFunction(onError, 4));
            LOG.debug("Calling onerror handler done.");
        }
    }
}
 
Example #13
Source File: XMLDOMImplementation.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Indicates support for the specified feature.
 * @param feature a string that specifies the feature to test
 * @param version a string that specifies the version number to test
 * @return true if the specified feature is implemented; otherwise false
 */
@JsxFunction
public boolean hasFeature(final String feature, final String version) {
    if (feature == null || "null".equals(feature) || version == null || "null".equals(version)) {
        throw Context.reportRuntimeError("Type mismatch.");
    }

    if ("XML".equals(feature) && "1.0".equals(version)) {
        return true;
    }
    else if ("DOM".equals(feature) && "1.0".equals(version)) {
        return true;
    }
    else if ("MS-DOM".equals(feature) && ("1.0".equals(version) || "2.0".equals(version))) {
        return true;
    }
    return false;
}
 
Example #14
Source File: DateCustom.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a date to a string, returning the "time" portion using the current locale's conventions.
 * @param context the JavaScript context
 * @param thisObj the scriptable
 * @param args the arguments passed into the method
 * @param function the function
 * @return converted string
 */
public static String toLocaleTimeString(
        final Context context, final Scriptable thisObj, final Object[] args, final Function function) {
    final String formatString;
    final BrowserVersion browserVersion = ((Window) thisObj.getParentScope()).getBrowserVersion();

    if (browserVersion.hasFeature(JS_DATE_WITH_LEFT_TO_RIGHT_MARK)) {
        // [U+200E] -> Unicode Character 'LEFT-TO-RIGHT MARK'
        formatString = "\u200Eh\u200E:\u200Emm\u200E:\u200Ess\u200E \u200Ea";
    }
    else {
        formatString = "h:mm:ss a";
    }
    final FastDateFormat format =  FastDateFormat.getInstance(formatString, getLocale(browserVersion));
    return format.format(getDateValue(thisObj));
}
 
Example #15
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 #16
Source File: EventHandler.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Object call(final Context cx, final Scriptable scope,
    final Scriptable thisObj, final Object[] args)
    throws JavaScriptException {

    // the js object to which this event is attached has to be the scope
    final SimpleScriptable jsObj = node_.getScriptableObject();
    // compile "just in time"
    if (realFunction_ == null) {
        realFunction_ = cx.compileFunction(jsObj, jsSnippet_, eventName_ + " event for " + node_
            + " in " + node_.getPage().getUrl(), 0, null);
        realFunction_.setParentScope(jsObj);
    }

    return realFunction_.call(cx, scope, thisObj, args);
}
 
Example #17
Source File: XMLHTTPRequest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the HTTP status code returned by a request.
 * @return the HTTP status code returned by a request
 */
@JsxGetter
public int getStatus() {
    if (state_ != STATE_DONE) {
        throw Context.reportRuntimeError("Unspecified error (request not sent).");
    }
    if (webResponse_ != null) {
        return webResponse_.getStatusCode();
    }

    if (LOG.isErrorEnabled()) {
        LOG.error("XMLHTTPRequest.status was retrieved without a response available (readyState: "
            + state_ + ").");
    }
    return 0;
}
 
Example #18
Source File: HTMLElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the value of the {@code vAlign} property.
 * @param vAlign the value of the {@code vAlign} property
 * @param valid the valid values; if {@code null}, any value is valid
 */
protected void setVAlign(final Object vAlign, final String[] valid) {
    final String valign = Context.toString(vAlign);
    final String valignLC = valign.toLowerCase(Locale.ROOT);
    if (valid == null || ArrayUtils.contains(valid, valignLC)) {
        if (getBrowserVersion().hasFeature(JS_VALIGN_CONVERTS_TO_LOWERCASE)) {
            getDomNodeOrDie().setAttribute("valign", valignLC);
        }
        else {
            getDomNodeOrDie().setAttribute("valign", valign);
        }
    }
    else {
        throw Context.reportRuntimeError("Cannot set the vAlign property to invalid value: " + vAlign);
    }
}
 
Example #19
Source File: Symbol.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@JsxFunction
public String toString() {
    String name;
    if (name_ == Undefined.instance) {
        name = "";
    }
    else {
        name = Context.toString(name_);
        final ClassConfiguration config = AbstractJavaScriptConfiguration
                .getClassConfiguration(getClass(), getBrowserVersion());

        for (final Entry<String, ClassConfiguration.PropertyInfo> propertyEntry
                : config.getStaticPropertyEntries()) {
            if (propertyEntry.getKey().equals(name)) {
                name = "Symbol." + name;
                break;
            }
        }
    }
    return "Symbol(" + name + ')';
}
 
Example #20
Source File: TextRange.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Changes the start position of the range.
 * @param unit specifies the units to move
 * @param count the number of units to move
 * @return the number of units moved
 */
@JsxFunction
public int moveStart(final String unit, final Object count) {
    if (!"character".equals(unit)) {
        LOG.warn("moveStart('" + unit + "') is not yet supported");
        return 0;
    }
    int c = 1;
    if (count != Undefined.instance) {
        c = (int) Context.toNumber(count);
    }
    if (range_.getStartContainer() == range_.getEndContainer()
            && range_.getStartContainer() instanceof SelectableTextInput) {
        final SelectableTextInput input = (SelectableTextInput) range_.getStartContainer();
        c = constrainMoveBy(c, range_.getStartOffset(), input.getText().length());
        range_.setStart(input, range_.getStartOffset() + c);
    }
    return c;
}
 
Example #21
Source File: XPathResult.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the value of this number result.
 * @return the value of this number result
 */
@JsxGetter
public double getNumberValue() {
    if (resultType_ != NUMBER_TYPE) {
        throw Context.reportRuntimeError("Cannot get numberValue for type: " + resultType_);
    }
    final String asString = asString();
    Double answer;
    try {
        answer = Double.parseDouble(asString);
    }
    catch (final NumberFormatException e) {
        answer = Double.NaN;
    }
    return answer;
}
 
Example #22
Source File: Node.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Replaces this ChildNode in the children list of its parent with a set of Node or DOMString objects.
 * @param context the context
 * @param thisObj this object
 * @param args the arguments
 * @param function the function
 */
protected static void replaceWith(final Context context, final Scriptable thisObj, final Object[] args,
        final Function function) {
    final DomNode thisDomNode = ((Node) thisObj).getDomNodeOrDie();
    final DomNode parentNode = thisDomNode.getParentNode();
    final DomNode nextSibling = thisDomNode.getNextSibling();
    boolean isFirst = true;
    for (Object arg : args) {
        final DomNode newNode = toNodeOrTextNode((Node) thisObj, arg).getDomNodeOrDie();
        if (isFirst) {
            isFirst = false;
            thisDomNode.replace(newNode);
        }
        else {
            if (nextSibling != null) {
                nextSibling.insertBefore(newNode);
            }
            else {
                parentNode.appendChild(newNode);
            }
        }
    }
}
 
Example #23
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a modeless 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 a reference to the new window object created for the modeless dialog
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536761.aspx">MSDN Documentation</a>
 */
@JsxFunction(IE)
public Object showModelessDialog(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);
        final Window jsDialog = (Window) dialog.getScriptableObject();
        return jsDialog;
    }
    catch (final IOException e) {
        throw Context.throwAsScriptRuntimeEx(e);
    }
}
 
Example #24
Source File: ScriptableWrapper.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * @see ScriptableObject#get(String,net.sourceforge.htmlunit.corejs.javascript.Scriptable)
 */
@Override
public Object get(final String name, final Scriptable start) {
    final Method propertyGetter = properties_.get(name);
    final Object response;
    if (propertyGetter != null) {
        response = invoke(propertyGetter);
    }
    else {
        final Object fromSuper = super.get(name, start);
        if (fromSuper != Scriptable.NOT_FOUND) {
            response = fromSuper;
        }
        else {
            final Object byName = invoke(getByNameFallback_, new Object[] {name});
            if (byName != null) {
                response = byName;
            }
            else {
                response = Scriptable.NOT_FOUND;
            }
        }
    }

    return Context.javaToJS(response, ScriptableObject
            .getTopLevelScope(start));
}
 
Example #25
Source File: DebugFrameImpl.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void onEnter(final Context cx, final Scriptable activation, final Scriptable thisObj, final Object[] args) {
    if (LOG.isTraceEnabled()) {
        final StringBuilder sb = new StringBuilder();

        final String line = getFirstLine(cx);
        final String source = getSourceName(cx);
        sb.append(source).append(":").append(line).append(" ");

        Scriptable parent = activation.getParentScope();
        while (parent != null) {
            sb.append("   ");
            parent = parent.getParentScope();
        }
        final String functionName = getFunctionName(thisObj);
        sb.append(functionName).append("(");
        final int nbParams = functionOrScript_.getParamCount();
        for (int i = 0; i < nbParams; i++) {
            final String argAsString;
            if (i < args.length) {
                argAsString = stringValue(args[i]);
            }
            else {
                argAsString = "undefined";
            }
            sb.append(getParamName(i)).append(": ").append(argAsString);
            if (i < nbParams - 1) {
                sb.append(", ");
            }
        }
        sb.append(")");

        LOG.trace(sb);
    }
}
 
Example #26
Source File: NativeFunctionToStringFunction.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Object call(final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
    final String s = (String) super.call(cx, scope, thisObj, args);

    if (thisObj instanceof BaseFunction && s.indexOf("[native code]") > -1) {
        final String functionName = ((BaseFunction) thisObj).getFunctionName();
        return "function " + functionName + "() { [native code] }";
    }
    return s;
}
 
Example #27
Source File: XMLDOMNamedNodeMap.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Removes an attribute from the collection.
 * @param name the string specifying the name of the attribute to remove from the collection
 * @return the node removed from the collection or {@code null} if the named node is not an attribute
 */
@JsxFunction
public Object removeNamedItem(final String name) {
    if (name == null || "null".equals(name)) {
        throw Context.reportRuntimeError("Type mismatch.");
    }

    final DomNode attr = (DomNode) attributes_.removeNamedItem(name);
    if (attr != null) {
        return attr.getScriptableObject();
    }
    return null;
}
 
Example #28
Source File: XMLDOMDocument.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the root element of the document.
 * @param element the root element of the document
 */
@JsxSetter
public void setDocumentElement(final XMLDOMElement element) {
    if (element == null) {
        throw Context.reportRuntimeError("Type mismatch.");
    }

    final XMLDOMElement documentElement = getDocumentElement();
    if (documentElement != null) {
        documentElement.getDomNodeOrDie().remove();
    }

    appendChild(element);
}
 
Example #29
Source File: XMLDOMDocument.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance, with associated {@link XmlPage}.
 * @param enclosingWindow the window
 */
public XMLDOMDocument(final WebWindow enclosingWindow) {
    if (enclosingWindow != null) {
        try {
            final XmlPage page = new XmlPage((WebResponse) null, enclosingWindow, true, false);
            setDomNode(page);
        }
        catch (final IOException e) {
            throw Context.reportRuntimeError("IOException: " + e);
        }
    }
}
 
Example #30
Source File: XMLDOMElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Attempting to set the value of elements generates an error.
 * @param newValue the new value to set
 */
@Override
public void setNodeValue(final String newValue) {
    if (newValue == null || "null".equals(newValue)) {
        throw Context.reportRuntimeError("Type mismatch.");
    }
    throw Context.reportRuntimeError("This operation cannot be performed with a node of type ELEMENT.");
}