Java Code Examples for net.sourceforge.htmlunit.corejs.javascript.Scriptable#NOT_FOUND

The following examples show how to use net.sourceforge.htmlunit.corejs.javascript.Scriptable#NOT_FOUND . 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: CSSStyleDeclaration.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Object get(final String name, final Scriptable start) {
    if (this != start) {
        return super.get(name, start);
    }

    Scriptable prototype = getPrototype();
    while (prototype != null) {
        final Object value = prototype.get(name, start);
        if (value != Scriptable.NOT_FOUND) {
            return value;
        }
        prototype = prototype.getPrototype();
    }

    final Definition style = StyleAttributes.getDefinition(name, getBrowserVersion());
    if (style != null) {
        return getStyleAttribute(style);
    }

    return super.get(name, start);
}
 
Example 2
Source File: HtmlPage.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
private static String getAttributeValue(final DomElement element, final String attribute) {
    // first try real attributes
    String value = element.getAttribute(attribute);

    if (DomElement.ATTRIBUTE_NOT_DEFINED == value
            && !(element instanceof HtmlObject)) {
        // second try are JavaScript attributes
        // ...but applets/objects are a bit special so ignore them
        final Object o = element.getScriptableObject();
        if (o instanceof ScriptableObject) {
            final ScriptableObject scriptObject = (ScriptableObject) o;
            // we have to make sure the scriptObject has a slot for the given attribute.
            // just using get() may use e.g. getWithPreemption().
            if (scriptObject.has(attribute, scriptObject)) {
                final Object jsValue = scriptObject.get(attribute, scriptObject);
                if (jsValue != Scriptable.NOT_FOUND && jsValue instanceof String) {
                    value = (String) jsValue;
                }
            }
        }
    }
    return value;
}
 
Example 3
Source File: HTMLObjectElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Object get(final String name, final Scriptable start) {
    // for java mocks do a bit more, we have handle unknown properties
    // ourself
    if (wrappedActiveX_ instanceof NativeJavaObject) {
        final NativeJavaObject obj = (NativeJavaObject) wrappedActiveX_;
        final Object result = obj.get(name, start);
        if (Scriptable.NOT_FOUND != result) {
            return result;
        }
        return super.get(name, start);
    }

    if (wrappedActiveX_ != null) {
        return wrappedActiveX_.get(name, start);
    }
    return super.get(name, start);
}
 
Example 4
Source File: CSSStyleDeclaration.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
@Override
public void put(final String name, final Scriptable start, final Object value) {
    if (this != start) {
        super.put(name, start, value);
        return;
    }

    final Scriptable prototype = getPrototype();
    if (prototype != null && !"constructor".equals(name) && prototype.get(name, start) != Scriptable.NOT_FOUND) {
        prototype.put(name, start, value);
        return;
    }

    if (getDomNodeOrNull() != null) { // check if prototype or not
        final Definition style = StyleAttributes.getDefinition(name, getBrowserVersion());
        if (style != null) {
            final String stringValue = Context.toString(value);
            setStyleAttribute(style.getAttributeName(), stringValue);
            return;
        }
    }

    super.put(name, start, value);
}
 
Example 5
Source File: HtmlPage.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
private String getAttributeValue(final DomElement element, final String attribute) {
    // first try real attributes
    String value = element.getAttribute(attribute);

    if (DomElement.ATTRIBUTE_NOT_DEFINED == value
            && getWebClient().isJavaScriptEngineEnabled()
            && !(element instanceof HtmlApplet)
            && !(element instanceof HtmlObject)) {
        // second try are JavaScript attributes
        // ...but applets/objects are a bit special so ignore them
        final Object o = element.getScriptableObject();
        if (o instanceof ScriptableObject) {
            final ScriptableObject scriptObject = (ScriptableObject) o;
            // we have to make sure the scriptObject has a slot for the given attribute.
            // just using get() may use e.g. getWithPreemption().
            if (scriptObject.has(attribute, scriptObject)) {
                final Object jsValue = scriptObject.get(attribute, scriptObject);
                if (jsValue != Scriptable.NOT_FOUND && jsValue instanceof String) {
                    value = (String) jsValue;
                }
            }
        }
    }
    return value;
}
 
Example 6
Source File: CSSStyleDeclaration.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Object get(final String name, final Scriptable start) {
    if (this != start) {
        return super.get(name, start);
    }

    Scriptable prototype = getPrototype();
    while (prototype != null) {
        final Object value = prototype.get(name, start);
        if (value != Scriptable.NOT_FOUND) {
            return value;
        }
        prototype = prototype.getPrototype();
    }

    final Definition style = StyleAttributes.getDefinition(name, getBrowserVersion());
    if (style != null) {
        return getStyleAttribute(style);
    }

    return super.get(name, start);
}
 
Example 7
Source File: HTMLObjectElement.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Object get(final String name, final Scriptable start) {
    // for java mocks do a bit more, we have handle unknown properties
    // ourself
    if (wrappedActiveX_ instanceof NativeJavaObject) {
        final NativeJavaObject obj = (NativeJavaObject) wrappedActiveX_;
        final Object result = obj.get(name, start);
        if (Scriptable.NOT_FOUND != result) {
            return result;
        }
        return super.get(name, start);
    }

    if (wrappedActiveX_ != null) {
        return wrappedActiveX_.get(name, start);
    }
    return super.get(name, start);
}
 
Example 8
Source File: Int16Array.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected Object fromArray(final byte[] array, final int offset) {
    if (offset < 0 || offset >= array.length) {
        return Scriptable.NOT_FOUND;
    }
    final ByteBuffer buff = ByteBuffer.wrap(array);
    buff.order(ByteOrder.LITTLE_ENDIAN);
    return buff.getShort(offset);
}
 
Example 9
Source File: Uint8ClampedArray.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected Object fromArray(final byte[] array, final int offset) {
    if (offset < 0 || offset >= array.length) {
        return Scriptable.NOT_FOUND;
    }
    return array[offset] & 0xFF;
}
 
Example 10
Source File: Uint32Array.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected Object fromArray(final byte[] array, final int offset) {
    if (offset < 0 || offset >= array.length) {
        return Scriptable.NOT_FOUND;
    }
    final ByteBuffer buff = ByteBuffer.wrap(array);
    buff.order(ByteOrder.LITTLE_ENDIAN);
    return buff.getInt(offset) & 0xFFFFFFFFL;
}
 
Example 11
Source File: ArrayBufferViewBase.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Object get(final int index, final Scriptable start) {
    final int offset = index * getBytesPerElement() + getByteOffset();
    final ArrayBuffer buffer = getBuffer();
    if (buffer == null) {
        return Scriptable.NOT_FOUND;
    }
    return fromArray(buffer.getBytes(), offset);
}
 
Example 12
Source File: Uint8Array.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected Object fromArray(final byte[] array, final int offset) {
    if (offset < 0 || offset >= array.length) {
        return Scriptable.NOT_FOUND;
    }
    return array[offset] & 0xFF;
}
 
Example 13
Source File: Uint16Array.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected Object fromArray(final byte[] array, final int offset) {
    if (offset < 0 || offset >= array.length) {
        return Scriptable.NOT_FOUND;
    }
    final ByteBuffer buff = ByteBuffer.wrap(array);
    buff.order(ByteOrder.LITTLE_ENDIAN);
    return buff.getShort(offset) & 0xFFFF;
}
 
Example 14
Source File: SimpleScriptable.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
@Override
public boolean has(final int index, final Scriptable start) {
    final Object found = get(index, start);
    if (!Undefined.isUndefined(found) && Scriptable.NOT_FOUND != found) {
        return true;
    }
    return super.has(index, start);
}
 
Example 15
Source File: Int8Array.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected Object fromArray(final byte[] array, final int offset) {
    if (offset < 0 || offset >= array.length) {
        return Scriptable.NOT_FOUND;
    }
    return array[offset];
}
 
Example 16
Source File: Int32Array.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected Object fromArray(final byte[] array, final int offset) {
    if (offset < 0 || offset >= array.length) {
        return Scriptable.NOT_FOUND;
    }
    final ByteBuffer buff = ByteBuffer.wrap(array);
    buff.order(ByteOrder.LITTLE_ENDIAN);
    return buff.getInt(offset);
}
 
Example 17
Source File: Float64Array.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected Object fromArray(final byte[] array, final int offset) {
    if (offset < 0 || offset >= array.length) {
        return Scriptable.NOT_FOUND;
    }
    final ByteBuffer buff = ByteBuffer.wrap(array);
    buff.order(ByteOrder.LITTLE_ENDIAN);
    return buff.getDouble(offset);
}
 
Example 18
Source File: SimpleScriptable.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
@Override
public boolean has(final int index, final Scriptable start) {
    final Object found = get(index, start);
    if (Undefined.instance != found && Scriptable.NOT_FOUND != found) {
        return true;
    }
    return super.has(index, start);
}
 
Example 19
Source File: ScriptableWrapper.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * @see ScriptableObject#get(java.lang.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 20
Source File: ArrayCustom.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new Array instance from an array-like or iterable object.
 * @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 Scriptable from(
        final Context context, final Scriptable thisObj, final Object[] args, final Function function) {
    final Object arrayLike = args[0];
    Object[] array = null;
    if (arrayLike instanceof Scriptable) {
        final Scriptable scriptable = (Scriptable) arrayLike;
        final Object length = scriptable.get("length", scriptable);
        if (length != Scriptable.NOT_FOUND) {
            final int size = (int) Context.toNumber(length);
            array = new Object[size];
            for (int i = 0; i < size; i++) {
                array[i] = scriptable.get(i, scriptable);
            }
        }
        else {
            final List<Object> list = new ArrayList<>();
            if (Iterator.iterate(context, thisObj, scriptable,
                    new Consumer<Object>() {
                        @Override
                        public void accept(Object value) {
                            if (value != Undefined.instance) {
                                list.add(value);
                            }
                        }
                    })) {
                array = list.toArray();
            }
        }
    }
    else if (arrayLike instanceof String) {
        final String string = (String) arrayLike;
        array = new Object[string.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = string.charAt(i);
        }
    }
    if (array == null) {
        array = new Object[0];
    }
    return context.newArray(thisObj, array);
}