Search in sources :

Example 1 with HtmlUnitScriptable

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

the class HtmlPage method cloneNode.

/**
 * {@inheritDoc}
 */
@Override
public HtmlPage cloneNode(final boolean deep) {
    // we need the ScriptObject clone before cloning the kids.
    final HtmlPage result = (HtmlPage) super.cloneNode(false);
    if (getWebClient().isJavaScriptEnabled()) {
        final HtmlUnitScriptable jsObjClone = ((HtmlUnitScriptable) getScriptableObject()).clone();
        jsObjClone.setDomNode(result);
    }
    // if deep, clone the kids too, and re initialize parts of the clone
    if (deep) {
        synchronized (lock_) {
            result.attributeListeners_ = null;
        }
        result.selectionRanges_ = new ArrayList<>(3);
        result.afterLoadActions_ = new ArrayList<>();
        result.frameElements_ = new TreeSet<>(documentPositionComparator);
        for (DomNode child = getFirstChild(); child != null; child = child.getNextSibling()) {
            result.appendChild(child.cloneNode(true));
        }
    }
    return result;
}
Also used : HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)

Example 2 with HtmlUnitScriptable

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

the class DomNode method getScriptableObject.

/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Returns the JavaScript object that corresponds to this node, lazily initializing a new one if necessary.
 *
 * The logic of when and where the JavaScript object is created needs a clean up: functions using
 * a DOM node's JavaScript object should not have to check if they should create it first.
 *
 * @param <T> the object type
 * @return the JavaScript object that corresponds to this node
 */
@SuppressWarnings("unchecked")
public <T> T getScriptableObject() {
    if (scriptObject_ == null) {
        final SgmlPage page = getPage();
        if (this == page) {
            final StringBuilder msg = new StringBuilder("No script object associated with the Page.");
            // because this is a strange case we like to provide as much info as possible
            msg.append(" class: '").append(page.getClass().getName()).append('\'');
            try {
                msg.append(" url: '").append(page.getUrl()).append("' content: ").append(page.getWebResponse().getContentAsString());
            } catch (final Exception e) {
                // ok bad luck with detail
                msg.append(" no details: '").append(e).append('\'');
            }
            throw new IllegalStateException(msg.toString());
        }
        final Object o = page.getScriptableObject();
        if (o instanceof HtmlUnitScriptable) {
            scriptObject_ = ((HtmlUnitScriptable) o).makeScriptableFor(this);
        }
    }
    return (T) scriptObject_;
}
Also used : HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) CSSException(com.gargoylesoftware.css.parser.CSSException) DOMException(org.w3c.dom.DOMException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException)

Example 3 with HtmlUnitScriptable

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

the class RowContainer method moveRow.

/**
 * Moves the row at the specified source index to the specified target index, returning
 * the row that was moved.
 * @param sourceIndex the index of the row to move
 * @param targetIndex the index to move the row to
 * @return the row that was moved
 */
@JsxFunction(IE)
public Object moveRow(final int sourceIndex, final int targetIndex) {
    final HTMLCollection rows = (HTMLCollection) getRows();
    final int rowCount = rows.getLength();
    final boolean sourceIndexValid = sourceIndex >= 0 && sourceIndex < rowCount;
    final boolean targetIndexValid = targetIndex >= 0 && targetIndex < rowCount;
    if (sourceIndexValid && targetIndexValid) {
        final HtmlUnitScriptable sourceRow = (HtmlUnitScriptable) rows.item(Integer.valueOf(sourceIndex));
        final HtmlUnitScriptable targetRow = (HtmlUnitScriptable) rows.item(Integer.valueOf(targetIndex));
        targetRow.getDomNodeOrDie().insertBefore(sourceRow.getDomNodeOrDie());
        return sourceRow;
    }
    return null;
}
Also used : HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 4 with HtmlUnitScriptable

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

the class Intl method define.

private void define(final Class<? extends HtmlUnitScriptable> c, final BrowserVersion browserVersion) {
    try {
        final ClassConfiguration config = AbstractJavaScriptConfiguration.getClassConfiguration(c, browserVersion);
        final HtmlUnitScriptable prototype = JavaScriptEngine.configureClass(config, this, browserVersion);
        final FunctionObject functionObject = new RecursiveFunctionObject(c.getSimpleName(), config.getJsConstructor(), this);
        if (c == V8BreakIterator.class) {
            prototype.setClassName("v8BreakIterator");
        }
        functionObject.addAsConstructor(this, prototype);
    } catch (final Exception e) {
        throw Context.throwAsScriptRuntimeEx(e);
    }
}
Also used : HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) RecursiveFunctionObject(com.gargoylesoftware.htmlunit.javascript.RecursiveFunctionObject) FunctionObject(net.sourceforge.htmlunit.corejs.javascript.FunctionObject) RecursiveFunctionObject(com.gargoylesoftware.htmlunit.javascript.RecursiveFunctionObject) ClassConfiguration(com.gargoylesoftware.htmlunit.javascript.configuration.ClassConfiguration)

Example 5 with HtmlUnitScriptable

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

the class RowContainer method deleteRow.

/**
 * Deletes the row at the specified index.
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536408.aspx">MSDN Documentation</a>
 * @param rowIndex the zero-based index of the row to delete
 */
@JsxFunction
public void deleteRow(int rowIndex) {
    final HTMLCollection rows = (HTMLCollection) getRows();
    final int rowCount = rows.getLength();
    if (rowIndex == -1) {
        rowIndex = rowCount - 1;
    }
    final boolean rowIndexValid = rowIndex >= 0 && rowIndex < rowCount;
    if (rowIndexValid) {
        final HtmlUnitScriptable row = (HtmlUnitScriptable) rows.item(Integer.valueOf(rowIndex));
        row.getDomNodeOrDie().remove();
    }
}
Also used : HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

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