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);
}
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;
}
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());
}
}
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);
}
}
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();
}
Aggregations