Search in sources :

Example 61 with JsxFunction

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

the class CharacterData method deleteData.

/**
 * Delete characters from character data.
 * @param offset the position of the first character to be deleted
 * @param count the number of characters to be deleted
 */
@JsxFunction
public void deleteData(final int offset, final int count) {
    if (offset < 0) {
        throw Context.reportRuntimeError("Provided offset: " + offset + " is less than zero.");
    }
    if (getBrowserVersion().hasFeature(JS_DOM_CDATA_DELETE_THROWS_NEGATIVE_COUNT)) {
        if (count < 0) {
            throw Context.reportRuntimeError("Provided count: " + count + " is less than zero.");
        }
        if (count == 0) {
            return;
        }
    }
    final DomCharacterData domCharacterData = getDomCharacterDataOrDie();
    if (offset > domCharacterData.getLength()) {
        throw Context.reportRuntimeError("Provided offset: " + offset + " is greater than length.");
    }
    domCharacterData.deleteData(offset, count);
}
Also used : DomCharacterData(com.gargoylesoftware.htmlunit.html.DomCharacterData) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 62 with JsxFunction

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

the class DOMImplementation method createDocument.

/**
 * Creates an {@link XMLDocument}.
 *
 * @param namespaceURI the URI that identifies an XML namespace
 * @param qualifiedName the qualified name of the document to instantiate
 * @param doctype the document types of the document
 * @return the newly created {@link XMLDocument}
 */
@JsxFunction
public XMLDocument createDocument(final String namespaceURI, final String qualifiedName, final DocumentType doctype) {
    final XMLDocument document = new XMLDocument(getWindow().getWebWindow());
    document.setParentScope(getParentScope());
    document.setPrototype(getPrototype(document.getClass()));
    if (qualifiedName != null && !qualifiedName.isEmpty()) {
        final XmlPage page = (XmlPage) document.getDomNodeOrDie();
        page.appendChild(page.createElementNS("".equals(namespaceURI) ? null : namespaceURI, qualifiedName));
    }
    return document;
}
Also used : XmlPage(com.gargoylesoftware.htmlunit.xml.XmlPage) XMLDocument(com.gargoylesoftware.htmlunit.javascript.host.xml.XMLDocument) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 63 with JsxFunction

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

the class DOMImplementation method createHTMLDocument.

/**
 * Creates an {@link HTMLDocument}.
 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createHTMLDocument">
 *   createHTMLDocument (MDN)</a>
 *
 * @param titleObj the document title
 * @return the newly created {@link HTMLDocument}
 */
@JsxFunction
public HTMLDocument createHTMLDocument(final Object titleObj) {
    if (Undefined.isUndefined(titleObj) && getBrowserVersion().hasFeature(JS_DOMIMPLEMENTATION_CREATE_HTMLDOCOMENT_REQUIRES_TITLE)) {
        throw Context.reportRuntimeError("Title is required");
    }
    // com.gargoylesoftware.htmlunit.javascript.host.dom.DOMParser.parseFromString(String, Object)
    try {
        final WebWindow webWindow = getWindow().getWebWindow();
        final String html;
        if (Undefined.isUndefined(titleObj)) {
            html = "<html><head></head><body></body></html>";
        } else {
            html = "<html><head><title>" + Context.toString(titleObj) + "</title></head><body></body></html>";
        }
        final WebResponse webResponse = new StringWebResponse(html, UrlUtils.URL_ABOUT_BLANK);
        final HtmlPage page = new HtmlPage(webResponse, webWindow);
        // According to spec and behavior of function in browsers new document
        // has no location object and is not connected with any window
        page.setEnclosingWindow(null);
        // document knows the window but is not the windows document
        final HTMLDocument document = new HTMLDocument();
        document.setParentScope(getWindow());
        document.setPrototype(getPrototype(document.getClass()));
        // document.setWindow(getWindow());
        document.setDomNode(page);
        final HTMLParser htmlParser = webWindow.getWebClient().getPageCreator().getHtmlParser();
        htmlParser.parse(webResponse, page, false, false);
        return page.getScriptableObject();
    } catch (final IOException e) {
        throw Context.reportRuntimeError("Parsing failed" + e.getMessage());
    }
}
Also used : StringWebResponse(com.gargoylesoftware.htmlunit.StringWebResponse) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) StringWebResponse(com.gargoylesoftware.htmlunit.StringWebResponse) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) HTMLParser(com.gargoylesoftware.htmlunit.html.parser.HTMLParser) IOException(java.io.IOException) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 64 with JsxFunction

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

the class WorkerJob method importScripts.

/**
 * Import external script(s).
 * @param cx the current context
 * @param thisObj this object
 * @param args the script(s) to import
 * @param funObj the JS function called
 * @throws IOException in case of problem loading/executing the scripts
 */
@JsxFunction
public static void importScripts(final Context cx, final Scriptable thisObj, final Object[] args, final Function funObj) throws IOException {
    final DedicatedWorkerGlobalScope scope = (DedicatedWorkerGlobalScope) thisObj;
    final WebClient webClient = scope.owningWindow_.getWebWindow().getWebClient();
    final boolean checkContentType = !webClient.getBrowserVersion().hasFeature(JS_WORKER_IMPORT_SCRIPTS_ACCEPTS_ALL);
    for (final Object arg : args) {
        final String url = Context.toString(arg);
        scope.loadAndExecute(webClient, url, cx, checkContentType);
    }
}
Also used : WebClient(com.gargoylesoftware.htmlunit.WebClient) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 65 with JsxFunction

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

the class HTMLTableRowElement method deleteCell.

/**
 * Deletes the cell at the specified index in the element's cells collection. If the index
 * is -1 (or while simulating IE, when there is no index specified), then the last cell is deleted.
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536406.aspx">MSDN Documentation</a>
 * @see <a href="http://www.w3.org/TR/2003/REC-DOM-Level-2-HTML-20030109/html.html#ID-11738598">W3C DOM Level2</a>
 * @param index specifies the cell to delete.
 */
@JsxFunction
public void deleteCell(final Object index) {
    int position = -1;
    if (!Undefined.isUndefined(index)) {
        position = (int) Context.toNumber(index);
    } else if (getBrowserVersion().hasFeature(JS_TABLE_ROW_DELETE_CELL_REQUIRES_INDEX)) {
        throw Context.reportRuntimeError("No enough arguments");
    }
    final HtmlTableRow htmlRow = (HtmlTableRow) getDomNodeOrDie();
    if (position == -1) {
        position = htmlRow.getCells().size() - 1;
    }
    final boolean indexValid = position >= -1 && position <= htmlRow.getCells().size();
    if (!indexValid) {
        throw Context.reportRuntimeError("Index or size is negative or greater than the allowed amount");
    }
    htmlRow.getCell(position).remove();
}
Also used : HtmlTableRow(com.gargoylesoftware.htmlunit.html.HtmlTableRow) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)133 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)30 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)20 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)19 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)16 URL (java.net.URL)14 IOException (java.io.IOException)11 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)11 WebClient (com.gargoylesoftware.htmlunit.WebClient)10 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)10 SimpleRange (com.gargoylesoftware.htmlunit.html.impl.SimpleRange)10 Event (com.gargoylesoftware.htmlunit.javascript.host.event.Event)10 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)8 HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)8 MessageEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent)8 HTMLElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)8 NameValuePair (com.gargoylesoftware.htmlunit.util.NameValuePair)8 Range (org.w3c.dom.ranges.Range)8 HtmlAttributeChangeEvent (com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent)7 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)7