Search in sources :

Example 6 with HtmlUnitScriptable

use of com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable in project htmlunit by HtmlUnit.

the class RowContainer method insertRow.

/**
 * Inserts a new row at the given position.
 * @param index the index where the row should be inserted (0 <= index <= nbRows)
 * @return the inserted row
 */
public Object insertRow(final int index) {
    final HTMLCollection rows = (HTMLCollection) getRows();
    final int rowCount = rows.getLength();
    final DomElement newRow = ((HtmlPage) getDomNodeOrDie().getPage()).createElement("tr");
    if (rowCount == 0) {
        getDomNodeOrDie().appendChild(newRow);
    } else if (index == rowCount) {
        final HtmlUnitScriptable row = (HtmlUnitScriptable) rows.item(Integer.valueOf(index - 1));
        row.getDomNodeOrDie().getParentNode().appendChild(newRow);
    } else {
        final HtmlUnitScriptable row = (HtmlUnitScriptable) rows.item(Integer.valueOf(index));
        // if at the end, then in the same "sub-container" as the last existing row
        if (index > rowCount - 1) {
            row.getDomNodeOrDie().getParentNode().appendChild(newRow);
        } else {
            row.getDomNodeOrDie().insertBefore(newRow);
        }
    }
    return getScriptableFor(newRow);
}
Also used : HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage)

Example 7 with HtmlUnitScriptable

use of com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable in project htmlunit by HtmlUnit.

the class Console method dir.

/**
 * Implementation of console {@code dir} function. This method does not enter recursively
 * in the passed object, nor prints the details of objects or functions.
 * @param o the object to be printed
 */
@JsxFunction
public void dir(final Object o) {
    if (o instanceof ScriptableObject) {
        final ScriptableObject obj = (ScriptableObject) o;
        final Object[] ids = obj.getIds();
        if (ids != null && ids.length > 0) {
            final StringBuilder sb = new StringBuilder();
            for (final Object id : ids) {
                final Object value = obj.get(id);
                if (value instanceof Delegator) {
                    sb.append(id).append(": ").append(((Delegator) value).getClassName()).append('\n');
                } else if (value instanceof HtmlUnitScriptable) {
                    sb.append(id).append(": ").append(((HtmlUnitScriptable) value).getClassName()).append('\n');
                } else if (value instanceof BaseFunction) {
                    sb.append(id).append(": function ").append(((BaseFunction) value).getFunctionName()).append("()\n");
                } else {
                    sb.append(id).append(": ").append(value).append('\n');
                }
            }
            getWebConsole().info(sb.toString());
        }
    }
}
Also used : HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) BaseFunction(net.sourceforge.htmlunit.corejs.javascript.BaseFunction) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) Delegator(net.sourceforge.htmlunit.corejs.javascript.Delegator) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) NativeObject(net.sourceforge.htmlunit.corejs.javascript.NativeObject) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 8 with HtmlUnitScriptable

use of com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable in project htmlunit by HtmlUnit.

the class XMLDocument method makeScriptableFor.

/**
 * {@inheritDoc}
 */
@Override
public HtmlUnitScriptable makeScriptableFor(final DomNode domNode) {
    final HtmlUnitScriptable 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 = 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;
}
Also used : HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) SvgElement(com.gargoylesoftware.htmlunit.svg.SvgElement) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) SvgElement(com.gargoylesoftware.htmlunit.svg.SvgElement) Element(com.gargoylesoftware.htmlunit.javascript.host.Element) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) DOMException(com.gargoylesoftware.htmlunit.javascript.host.dom.DOMException) IOException(java.io.IOException) Attr(com.gargoylesoftware.htmlunit.javascript.host.dom.Attr) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)

Aggregations

HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)8 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)3 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)2 IOException (java.io.IOException)2 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)2 CSSException (com.gargoylesoftware.css.parser.CSSException)1 CSSParseException (com.gargoylesoftware.css.parser.CSSParseException)1 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)1 DomAttr (com.gargoylesoftware.htmlunit.html.DomAttr)1 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)1 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)1 JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)1 RecursiveFunctionObject (com.gargoylesoftware.htmlunit.javascript.RecursiveFunctionObject)1 ClassConfiguration (com.gargoylesoftware.htmlunit.javascript.configuration.ClassConfiguration)1 Element (com.gargoylesoftware.htmlunit.javascript.host.Element)1 Attr (com.gargoylesoftware.htmlunit.javascript.host.dom.Attr)1 DOMException (com.gargoylesoftware.htmlunit.javascript.host.dom.DOMException)1 SvgElement (com.gargoylesoftware.htmlunit.svg.SvgElement)1 NoSuchElementException (java.util.NoSuchElementException)1 BaseFunction (net.sourceforge.htmlunit.corejs.javascript.BaseFunction)1