Search in sources :

Example 1 with EvaluatorException

use of net.sourceforge.htmlunit.corejs.javascript.EvaluatorException in project htmlunit by HtmlUnit.

the class HTMLOptionsCollection method add.

/**
 * Adds a new item to the option collection.
 *
 * <p><b><i>Implementation Note:</i></b> The specification for the JavaScript add() method
 * actually calls for the optional newIndex parameter to be an integer. However, the
 * newIndex parameter is specified as an Object here rather than an int because of the
 * way Rhino and HtmlUnit process optional parameters for the JavaScript method calls.
 * If the newIndex parameter were specified as an int, then the Undefined value for an
 * integer is specified as NaN (Not A Number, which is a Double value), but Rhino
 * translates this value into 0 (perhaps correctly?) when converting NaN into an int.
 * As a result, when the newIndex parameter is not specified, it is impossible to make
 * a distinction between a caller of the form add(someObject) and add (someObject, 0).
 * Since the behavior of these two call forms is different, the newIndex parameter is
 * specified as an Object. If the newIndex parameter is not specified by the actual
 * JavaScript code being run, then newIndex is of type net.sourceforge.htmlunit.corejs.javascript.Undefined.
 * If the newIndex parameter is specified, then it should be of type java.lang.Number and
 * can be converted into an integer value.</p>
 *
 * <p>This method will call the {@link #put(int, Scriptable, Object)} method for actually
 * adding the element to the collection.</p>
 *
 * <p>According to <a href="http://msdn.microsoft.com/en-us/library/ms535921.aspx">the
 * Microsoft DHTML reference page for the JavaScript add() method of the options collection</a>,
 * the index parameter is specified as follows:
 * <p>
 * <i>Optional. Integer that specifies the index position in the collection where the element is
 * placed. If no value is given, the method places the element at the end of the collection.</i>
 *
 * @param newOptionObject the DomNode to insert in the collection
 * @param beforeOptionObject An optional parameter which specifies the index position in the
 * collection where the element is placed. If no value is given, the method places
 * the element at the end of the collection.
 *
 * @see #put(int, Scriptable, Object)
 */
@JsxFunction
public void add(final Object newOptionObject, final Object beforeOptionObject) {
    final HtmlOption htmlOption = (HtmlOption) ((HTMLOptionElement) newOptionObject).getDomNodeOrNull();
    HtmlOption beforeOption = null;
    // If newIndex was specified, then use it
    if (beforeOptionObject instanceof Number) {
        final int index = ((Integer) Context.jsToJava(beforeOptionObject, Integer.class)).intValue();
        if (index < 0 || index >= getLength()) {
            // Add a new option at the end.
            htmlSelect_.appendOption(htmlOption);
            return;
        }
        beforeOption = (HtmlOption) ((HTMLOptionElement) item(index)).getDomNodeOrDie();
    } else if (beforeOptionObject instanceof HTMLOptionElement) {
        beforeOption = (HtmlOption) ((HTMLOptionElement) beforeOptionObject).getDomNodeOrDie();
        if (beforeOption.getParentNode() != htmlSelect_) {
            throw new EvaluatorException("Unknown option.");
        }
    }
    if (null == beforeOption) {
        htmlSelect_.appendOption(htmlOption);
        return;
    }
    beforeOption.insertBefore(htmlOption);
}
Also used : EvaluatorException(net.sourceforge.htmlunit.corejs.javascript.EvaluatorException) HtmlOption(com.gargoylesoftware.htmlunit.html.HtmlOption) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

HtmlOption (com.gargoylesoftware.htmlunit.html.HtmlOption)1 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)1 EvaluatorException (net.sourceforge.htmlunit.corejs.javascript.EvaluatorException)1