use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.
the class HTMLOptionsCollection method remove.
/**
* Removes the option at the specified index.
* @param index the option index
*/
@JsxFunction
public void remove(final int index) {
int idx = index;
final BrowserVersion browser = getBrowserVersion();
if (idx < 0) {
if (browser.hasFeature(JS_SELECT_OPTIONS_REMOVE_IGNORE_IF_INDEX_NEGATIVE)) {
return;
}
if (index < 0 && getBrowserVersion().hasFeature(JS_SELECT_OPTIONS_REMOVE_THROWS_IF_NEGATIV)) {
throw Context.reportRuntimeError("Invalid index for option collection: " + index);
}
}
idx = Math.max(idx, 0);
if (idx >= getLength()) {
return;
}
htmlSelect_.removeOption(idx);
}
use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.
the class HTMLTableRowElement method insertCell.
/**
* Inserts a new cell at the specified index in the element's cells collection. If the index
* is -1 or there is no index specified, then the cell is appended at the end of the
* element's cells collection.
* @see <a href="http://msdn.microsoft.com/en-us/library/ms536455.aspx">MSDN Documentation</a>
* @param index specifies where to insert the cell in the tr.
* The default value is -1, which appends the new cell to the end of the cells collection
* @return the newly-created cell
*/
@JsxFunction
public Object insertCell(final Object index) {
int position = -1;
if (!Undefined.isUndefined(index)) {
position = (int) Context.toNumber(index);
}
final HtmlTableRow htmlRow = (HtmlTableRow) getDomNodeOrDie();
final boolean indexValid = position >= -1 && position <= htmlRow.getCells().size();
if (indexValid) {
final DomElement newCell = ((HtmlPage) htmlRow.getPage()).createElement("td");
if (position == -1 || position == htmlRow.getCells().size()) {
htmlRow.appendChild(newCell);
} else {
htmlRow.getCell(position).insertBefore(newCell);
}
return getScriptableFor(newCell);
}
throw Context.reportRuntimeError("Index or size is negative or greater than the allowed amount");
}
use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.
the class XMLDOMNode method appendChild.
/**
* Appends a new child as the last child of the node.
* @param newChild the new child node to be appended at the end of the list of children belonging to this node
* @return the new child node successfully appended to the list
*/
@JsxFunction
public Object appendChild(final Object newChild) {
if (newChild == null || "null".equals(newChild)) {
throw Context.reportRuntimeError("Type mismatch.");
}
Object appendedChild = null;
if (newChild instanceof XMLDOMNode) {
final XMLDOMNode childNode = (XMLDOMNode) newChild;
// Get XML node for the DOM node passed in
final DomNode childDomNode = childNode.getDomNodeOrDie();
// Get the parent XML node that the child should be added to.
final DomNode parentNode = getDomNodeOrDie();
// Append the child to the parent node
parentNode.appendChild(childDomNode);
appendedChild = newChild;
// create a DocumentFragment to be the parentNode's parentNode.
if (!(parentNode instanceof SgmlPage) && !(this instanceof XMLDOMDocumentFragment) && parentNode.getParentNode() == null) {
final DomDocumentFragment fragment = parentNode.getPage().createDocumentFragment();
fragment.appendChild(parentNode);
}
}
return appendedChild;
}
use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.
the class XMLDOMNode method selectNodes.
/**
* Applies the specified pattern-matching operation to this node's context and returns the list of matching nodes.
* @param expression a string specifying an XPath expression
* @return the collection of nodes selected by applying the given pattern-matching operation;
* if no nodes are selected, returns an empty collection
*/
@JsxFunction
public XMLDOMSelection selectNodes(final String expression) {
final DomNode domNode = getDomNodeOrDie();
final boolean attributeChangeSensitive = expression.contains("@");
return new XMLDOMSelection(domNode, attributeChangeSensitive, "XMLDOMNode.selectNodes('" + expression + "')") {
@Override
protected List<DomNode> computeElements() {
return new ArrayList<>(domNode.getByXPath(expression));
}
};
}
use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.
the class XMLDOMNode method replaceChild.
/**
* Replaces the specified old child node with the supplied new child node.
* @param newChild the new child that is to replace the old child; if {@code null},
* <code>oldChild</code> is removed without a replacement
* @param oldChild the old child that is to be replaced by the new child
* @return the old child that is replaced
*/
@JsxFunction
public Object replaceChild(final Object newChild, final Object oldChild) {
Object removedChild = null;
if (newChild instanceof XMLDOMDocumentFragment) {
final XMLDOMDocumentFragment fragment = (XMLDOMDocumentFragment) newChild;
XMLDOMNode firstNode = null;
final XMLDOMNode refChildObject = ((XMLDOMNode) oldChild).getNextSibling();
for (final DomNode node : fragment.getDomNodeOrDie().getChildren()) {
if (firstNode == null) {
replaceChild(node.getScriptableObject(), oldChild);
firstNode = node.getScriptableObject();
} else {
insertBeforeImpl(new Object[] { node.getScriptableObject(), refChildObject });
}
}
if (firstNode == null) {
removeChild(oldChild);
}
removedChild = oldChild;
} else if (newChild instanceof XMLDOMNode && oldChild instanceof XMLDOMNode) {
final XMLDOMNode newChildNode = (XMLDOMNode) newChild;
// Get XML nodes for the DOM nodes passed in
final DomNode newChildDomNode = newChildNode.getDomNodeOrDie();
final DomNode oldChildDomNode = ((XMLDOMNode) oldChild).getDomNodeOrDie();
// Replace the old child with the new child.
oldChildDomNode.replace(newChildDomNode);
removedChild = oldChild;
}
return removedChild;
}
Aggregations