use of com.gargoylesoftware.htmlunit.html.DomDocumentFragment in project htmlunit by HtmlUnit.
the class Document method createDocumentFragment.
/**
* Creates a new document fragment.
* @return a newly created document fragment
*/
@JsxFunction
public Object createDocumentFragment() {
final DomDocumentFragment fragment = getDomNodeOrDie().getPage().createDocumentFragment();
final DocumentFragment node = new DocumentFragment();
node.setParentScope(getParentScope());
node.setPrototype(getPrototype(node.getClass()));
node.setDomNode(fragment);
return getScriptableFor(fragment);
}
use of com.gargoylesoftware.htmlunit.html.DomDocumentFragment in project htmlunit by HtmlUnit.
the class Node method insertBeforeImpl.
/**
* Add a DOM node as a child to this node before the referenced node.
* If the referenced node is null, append to the end.
* @param args the arguments
* @return the newly added child node
*/
protected Object insertBeforeImpl(final Object[] args) {
if (args.length < 1) {
throw ScriptRuntime.typeError("Failed to execute 'insertBefore' on 'Node': 2 arguments required, but only 0 present.");
}
final Object newChildObject = args[0];
final Object refChildObject;
if (args.length > 1) {
refChildObject = args[1];
} else {
refChildObject = Undefined.instance;
}
Object insertedChild = null;
if (newChildObject instanceof Node) {
final Node newChild = (Node) newChildObject;
// is the node allowed here?
if (!isNodeInsertable(newChild)) {
throw ScriptRuntime.constructError("ReferenceError", "Node cannot be inserted at the specified point in the hierarchy");
}
final DomNode newChildNode = newChild.getDomNodeOrDie();
if (newChildNode instanceof DomDocumentFragment) {
final DomDocumentFragment fragment = (DomDocumentFragment) newChildNode;
for (final DomNode child : fragment.getChildren()) {
if (!isNodeInsertable(child.getScriptableObject())) {
throw ScriptRuntime.constructError("ReferenceError", "Node cannot be inserted at the specified point in the hierarchy");
}
}
}
// extract refChild
final DomNode refChildNode;
if (Undefined.isUndefined(refChildObject)) {
if (args.length == 2 || getBrowserVersion().hasFeature(JS_NODE_INSERT_BEFORE_REF_OPTIONAL)) {
refChildNode = null;
} else {
throw ScriptRuntime.typeError("Failed to execute 'insertBefore' on 'Node': 2 arguments required, but only 1 present.");
}
} else if (refChildObject == null) {
refChildNode = null;
} else {
refChildNode = ((Node) refChildObject).getDomNodeOrDie();
}
final DomNode domNode = getDomNodeOrDie();
try {
domNode.insertBefore(newChildNode, refChildNode);
} catch (final org.w3c.dom.DOMException e) {
throw ScriptRuntime.constructError("ReferenceError", e.getMessage());
}
insertedChild = newChild;
}
return insertedChild;
}
use of com.gargoylesoftware.htmlunit.html.DomDocumentFragment in project htmlunit by HtmlUnit.
the class SimpleRange method cloneContents.
/**
* {@inheritDoc}
*/
@Override
public DomDocumentFragment cloneContents() throws DOMException {
// Clone the common ancestor.
final DomNode ancestor = (DomNode) getCommonAncestorContainer();
if (ancestor == null) {
return new DomDocumentFragment(null);
}
final DomNode ancestorClone = ancestor.cloneNode(true);
// Find the start container and end container clones.
DomNode startClone = null;
DomNode endClone = null;
final DomNode start = (DomNode) startContainer_;
final DomNode end = (DomNode) endContainer_;
if (start == ancestor) {
startClone = ancestorClone;
}
if (end == ancestor) {
endClone = ancestorClone;
}
final Iterable<DomNode> descendants = ancestor.getDescendants();
if (startClone == null || endClone == null) {
final Iterator<DomNode> i = descendants.iterator();
final Iterator<DomNode> ci = ancestorClone.getDescendants().iterator();
while (i.hasNext()) {
final DomNode e = i.next();
final DomNode ce = ci.next();
if (start == e) {
startClone = ce;
} else if (end == e) {
endClone = ce;
break;
}
}
}
// Remove everything following the selection end from the clones.
if (endClone == null) {
throw Context.reportRuntimeError("Unable to find end node clone.");
}
deleteAfter(endClone, endOffset_);
for (DomNode n = endClone; n != null; n = n.getParentNode()) {
while (n.getNextSibling() != null) {
n.getNextSibling().remove();
}
}
// Remove everything prior to the selection start from the clones.
if (startClone == null) {
throw Context.reportRuntimeError("Unable to find start node clone.");
}
deleteBefore(startClone, startOffset_);
for (DomNode n = startClone; n != null; n = n.getParentNode()) {
while (n.getPreviousSibling() != null) {
n.getPreviousSibling().remove();
}
}
final SgmlPage page = ancestor.getPage();
final DomDocumentFragment fragment = new DomDocumentFragment(page);
if (start == end) {
fragment.appendChild(ancestorClone);
} else {
for (final DomNode n : ancestorClone.getChildNodes()) {
fragment.appendChild(n);
}
}
return fragment;
}
use of com.gargoylesoftware.htmlunit.html.DomDocumentFragment in project htmlunit by HtmlUnit.
the class SimpleRange method extractContents.
/**
* {@inheritDoc}
*/
@Override
public DomDocumentFragment extractContents() throws DOMException {
final DomDocumentFragment fragment = cloneContents();
// Remove everything inside the range from the original nodes.
deleteContents();
// Build the document fragment using the cloned nodes, and return it.
return fragment;
}
Aggregations