nsCOMPtr<nsIDOMDocument> instanceDoc; resNode->GetOwnerDocument(getter_AddRefs(instanceDoc)); NS_ENSURE_STATE(instanceDoc);
nsXFormsInsertDeleteElement::HandleAction(nsIDOMEvent            *aEvent,                                          nsIXFormsActionElement *aParentAction){  if (!mElement)    return NS_OK;  // First we need to get the three attributes: @nodeset, @at, and possibly  // @position  //  // 1) Get @nodeset  nsresult rv;  nsCOMPtr<nsIModelElementPrivate> model;  nsCOMPtr<nsIDOMXPathResult> nodeset;  rv = nsXFormsUtils::EvaluateNodeBinding(mElement,                                          0,                                          NS_LITERAL_STRING("nodeset"),                                          EmptyString(),                                          nsIDOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE,                                          getter_AddRefs(model),                                          getter_AddRefs(nodeset));  NS_ENSURE_SUCCESS(rv, rv);  if (!model || !nodeset)    return NS_OK;  PRUint32 setSize;  rv = nodeset->GetSnapshotLength(&setSize);  NS_ENSURE_SUCCESS(rv, rv);  // Unfortunately we cannot insert into an empty set, as there's nothing to  // clone.  if (setSize < 1)    return NS_OK;  //  // 2) Get @at  nsCOMPtr<nsIDOMXPathResult> at;  nsCOMPtr<nsIModelElementPrivate> modelTemp;  rv = nsXFormsUtils::EvaluateNodeBinding(mElement,                                          0,                                          NS_LITERAL_STRING("at"),                                          EmptyString(),                                          nsIDOMXPathResult::NUMBER_TYPE,                                          getter_AddRefs(modelTemp),                                          getter_AddRefs(at));  NS_ENSURE_SUCCESS(rv, rv);  PRUint32 atInt = 0;  if (at) {    double atDoub;    rv = at->GetNumberValue(&atDoub);    NS_ENSURE_SUCCESS(rv, rv);    if (atDoub < 1) {      atInt = 1;    } else {      atInt = (PRInt32) floor(atDoub + 0.5);    }  }  if (!atInt || atInt > setSize)    atInt = setSize;    //  // 3) Get @position (if \<insert\>)  nsAutoString position;  PRBool insertAfter = PR_FALSE;  if (mIsInsert) {    mElement->GetAttribute(NS_LITERAL_STRING("position"), position);    if (position.EqualsLiteral("after")) {      insertAfter = PR_TRUE;    } else if (!position.EqualsLiteral("before")) {      // This is not a valid document...      return NS_ERROR_ABORT;    }  }  #ifdef DEBUG_XF_INSERTDELETE  if (mIsInsert) {    printf("Will try to INSERT node _%s_ index %d (set size: %d)\n",           NS_ConvertUCS2toUTF8(position).get(),           atInt,           setSize);  } else {    printf("Will try to DELETE node at index %d (set size: %d)\n",           atInt,           setSize);  }  #endif  // Find location  nsCOMPtr<nsIDOMNode> location;  nodeset->SnapshotItem(atInt - 1, getter_AddRefs(location));  NS_ENSURE_STATE(location);  if (mIsInsert && insertAfter) {    nsCOMPtr<nsIDOMNode> temp;    // If we're at the end of the nodeset, this returns nsnull, which is fine,    // because InsertBefore then inserts at the end of the nodeset    location->GetNextSibling(getter_AddRefs(temp));    location.swap(temp);  }    nsCOMPtr<nsIDOMNode> parent;  location->GetParentNode(getter_AddRefs(parent));  NS_ENSURE_STATE(parent);  nsCOMPtr<nsIDOMNode> resNode;  if (mIsInsert) {    //    // Get prototype and clone it (last member of nodeset)    nsCOMPtr<nsIDOMNode> prototype;    nodeset->SnapshotItem(setSize - 1, getter_AddRefs(prototype));    NS_ENSURE_STATE(prototype);      nsCOMPtr<nsIDOMNode> newNode;    prototype->CloneNode(PR_TRUE, getter_AddRefs(newNode));    NS_ENSURE_STATE(newNode);    parent->InsertBefore(newNode,                         location,                         getter_AddRefs(resNode));    NS_ENSURE_STATE(resNode);  } else {    rv = parent->RemoveChild(location, getter_AddRefs(resNode));    NS_ENSURE_SUCCESS(rv, rv);  }  // Dispatch xforms-insert/delete event to the instance node we have modified  // data for  nsCOMPtr<nsIDOMDocument> instanceDoc;  resNode->GetOwnerDocument(getter_AddRefs(instanceDoc));  NS_ENSURE_STATE(instanceDoc);  nsCOMPtr<nsIDOMElement> modelElem = do_QueryInterface(model);  NS_ENSURE_STATE(modelElem);  nsCOMPtr<nsIDOMNodeList> nodeList;  rv = modelElem->GetElementsByTagNameNS(NS_LITERAL_STRING(NS_NAMESPACE_XFORMS),                                         NS_LITERAL_STRING("instance"),                                         getter_AddRefs(nodeList));  NS_ENSURE_SUCCESS(rv, rv);  PRUint32 childCount = 0;  nodeList->GetLength(&childCount);  nsCOMPtr<nsIDOMNode> instNode;  PRUint32 i;  for (i = 0; i < childCount; ++i) {    nodeList->Item(i, getter_AddRefs(instNode));    nsCOMPtr<nsIInstanceElementPrivate> instPriv = do_QueryInterface(instNode);    if (!instPriv)      continue;    nsCOMPtr<nsIDOMDocument> tmpDoc;    instPriv->GetDocument(getter_AddRefs(tmpDoc));    if (tmpDoc == instanceDoc)      break;  }    if (!childCount || i == childCount)    // No instance nodes in model or instance node not found?    // Doesn't make sense!    return NS_ERROR_ABORT;  rv = nsXFormsUtils::DispatchEvent(instNode,                                    mIsInsert ? eEvent_Insert : eEvent_Delete);  NS_ENSURE_SUCCESS(rv, rv);  // Dispatch refreshing events to the model  if (aParentAction) {    aParentAction->SetRebuild(modelElem, PR_TRUE);    aParentAction->SetRecalculate(modelElem, PR_TRUE);    aParentAction->SetRevalidate(modelElem, PR_TRUE);    aParentAction->SetRefresh(modelElem, PR_TRUE);  } else {    nsXFormsUtils::DispatchEvent(modelElem, eEvent_Rebuild);    nsXFormsUtils::DispatchEvent(modelElem, eEvent_Recalculate);    nsXFormsUtils::DispatchEvent(modelElem, eEvent_Revalidate);    nsXFormsUtils::DispatchEvent(modelElem, eEvent_Refresh);  }  return NS_OK;}