Search in sources :

Example 11 with JsxConstructor

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor in project htmlunit by HtmlUnit.

the class ProgressEvent method jsConstructor.

/**
 * {@inheritDoc}
 */
@Override
@JsxConstructor({ CHROME, EDGE, FF, FF_ESR })
public void jsConstructor(final String type, final ScriptableObject details) {
    super.jsConstructor(type, details);
    if (details != null && !Undefined.isUndefined(details)) {
        final Object lengthComputable = details.get("lengthComputable");
        if (lengthComputable instanceof Boolean) {
            lengthComputable_ = (Boolean) lengthComputable;
        } else {
            lengthComputable_ = Boolean.parseBoolean(lengthComputable.toString());
        }
        final Object loaded = details.get("loaded");
        if (loaded instanceof Long) {
            loaded_ = loaded;
        } else if (loaded instanceof Double) {
            loaded_ = ((Double) loaded).longValue();
        } else {
            try {
                loaded_ = Long.parseLong(loaded.toString());
            } catch (final NumberFormatException e) {
            // ignore
            }
        }
        final Object total = details.get("total");
        if (total instanceof Long) {
            total_ = (Long) total;
        } else if (total instanceof Double) {
            total_ = ((Double) total).longValue();
        } else {
            try {
                total_ = Long.parseLong(details.get("total").toString());
            } catch (final NumberFormatException e) {
            // ignore
            }
        }
    }
}
Also used : ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)

Example 12 with JsxConstructor

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor in project htmlunit by HtmlUnit.

the class ImageData method jsConstructor.

/**
 * JavaScript constructor.
 * @param cx the current context
 * @param args the arguments to the WebSocket constructor
 * @param ctorObj the function object
 * @param inNewExpr Is new or not
 * @return the java object to allow JavaScript to access
 */
@JsxConstructor({ CHROME, EDGE, FF, FF_ESR })
public static Scriptable jsConstructor(final Context cx, final Object[] args, final Function ctorObj, final boolean inNewExpr) {
    if (args.length < 2) {
        throw Context.reportRuntimeError("ImageData ctor - too less arguments");
    }
    NativeUint8ClampedArray data = null;
    final int width;
    final int height;
    if (args[0] instanceof NativeUint8ClampedArray) {
        data = (NativeUint8ClampedArray) args[0];
        if (data.getArrayLength() % 4 != 0) {
            throw Context.reportRuntimeError("ImageData ctor - data length mod 4 not zero");
        }
        width = (int) ScriptRuntime.toInteger(args[1]);
        if (args.length < 3) {
            height = data.getArrayLength() / 4 / width;
            if (data.getArrayLength() != 4 * width * height) {
                throw Context.reportRuntimeError("ImageData ctor - width not correct");
            }
        } else {
            height = (int) ScriptRuntime.toInteger(args[2]);
        }
        if (data.getArrayLength() != 4 * width * height) {
            throw Context.reportRuntimeError("ImageData ctor - width/height not correct");
        }
    } else {
        width = (int) ScriptRuntime.toInteger(args[0]);
        height = (int) ScriptRuntime.toInteger(args[1]);
    }
    if (width < 0) {
        throw Context.reportRuntimeError("ImageData ctor - width negative");
    }
    if (height < 0) {
        throw Context.reportRuntimeError("ImageData ctor - height negative");
    }
    final ImageData result = new ImageData(null, 0, 0, width, height);
    if (data != null) {
        final byte[] bytes = data.getBuffer().getBuffer();
        System.arraycopy(bytes, 0, result.bytes_, 0, Math.min(bytes.length, result.bytes_.length));
    }
    return result;
}
Also used : NativeUint8ClampedArray(net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeUint8ClampedArray) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)

Aggregations

JsxConstructor (com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)12 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)6 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)3 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)2 WebClient (com.gargoylesoftware.htmlunit.WebClient)1 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)1 MSXMLActiveXObjectFactory (com.gargoylesoftware.htmlunit.activex.javascript.msxml.MSXMLActiveXObjectFactory)1 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)1 DomText (com.gargoylesoftware.htmlunit.html.DomText)1 HtmlOption (com.gargoylesoftware.htmlunit.html.HtmlOption)1 HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)1 WindowProxy (com.gargoylesoftware.htmlunit.javascript.host.WindowProxy)1 NativeArray (net.sourceforge.htmlunit.corejs.javascript.NativeArray)1 NativeObject (net.sourceforge.htmlunit.corejs.javascript.NativeObject)1 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)1 NativeUint8ClampedArray (net.sourceforge.htmlunit.corejs.javascript.typedarrays.NativeUint8ClampedArray)1 AttributesImpl (org.xml.sax.helpers.AttributesImpl)1