}
throw Context.reportRuntimeError("Node cannot be inserted at the specified point in the hierarchy");
}
if (newChildNode instanceof DomDocumentFragment) {
final DomDocumentFragment fragment = (DomDocumentFragment) newChildNode;
for (final DomNode child : fragment.getChildren()) {
jsxFunction_insertBefore(new Object[] {child.getScriptObject(), refChildObject});
}
return newChildObject;
}
final DomNode refChildNode;
// IE accepts non standard calls with only one arg
if (refChildObject == Undefined.instance) {
if (getBrowserVersion().isIE()) {
if (args.length > 1) {
throw Context.reportRuntimeError("Invalid argument.");
}
refChildNode = null;
}
else {
if (args.length == 2) {
refChildNode = null;
}
else {
throw Context.reportRuntimeError("insertBefore: not enough arguments");
}
}
}
else if (refChildObject != null) {
refChildNode = ((Node) refChildObject).getDomNodeOrDie();
}
else {
refChildNode = null;
}
final DomNode domNode = getDomNodeOrDie();
// Append the child to the parent node
if (refChildNode != null) {
refChildNode.insertBefore(newChildNode);
appendedChild = newChildObject;
}
else {
domNode.appendChild(newChildNode);
appendedChild = newChildObject;
}
//if parentNode is null in IE, create a DocumentFragment to be the parentNode
if (domNode.getParentNode() == null
&& getWindow().getWebWindow().getWebClient().getBrowserVersion().isIE()) {
final DomDocumentFragment fragment = domNode.getPage().createDomDocumentFragment();
fragment.appendChild(domNode);
}
}
return appendedChild;
}