if (parentIsSelect1) {  nsCOMPtr<nsIXFormsItemSetUIElement> uiItemSet(do_QueryInterface(mElement)); nsCOMPtr<nsIDOMElement> anonContent; if (uiItemSet) { uiItemSet->GetAnonymousItemSetContent(getter_AddRefs(anonContent));
nsCOMPtr<nsIXFormsItemSetUIElement> uiItemSet(do_QueryInterface(mElement)); nsCOMPtr<nsIDOMElement> anonContent; if (uiItemSet) { uiItemSet->GetAnonymousItemSetContent(getter_AddRefs(anonContent)); }  NS_ENSURE_STATE(anonContent);  nsCOMPtr<nsIDOMNode> childNode, nodeReturn; while (NS_SUCCEEDED(anonContent->GetFirstChild(getter_AddRefs(childNode))) && childNode) { anonContent->RemoveChild(childNode, getter_AddRefs(nodeReturn)); }  for (PRUint32 i = 0; i < nodeCount; ++i) { result->SnapshotItem(i, getter_AddRefs(node)); NS_ASSERTION(node, "incorrect snapshot length");  nsresult rv = domDoc->CreateElementNS(NS_LITERAL_STRING(NS_NAMESPACE_XFORMS), NS_LITERAL_STRING("item"), getter_AddRefs(itemNode)); NS_ENSURE_SUCCESS(rv, rv);   rv = domDoc->CreateElementNS(NS_LITERAL_STRING(NS_NAMESPACE_XFORMS), NS_LITERAL_STRING("contextcontainer"), getter_AddRefs(contextContainer));  NS_ENSURE_SUCCESS(rv, rv);  nsCOMPtr<nsIDOMElement> modelElement = do_QueryInterface(model); nsAutoString modelID; modelElement->GetAttribute(NS_LITERAL_STRING("id"), modelID);  contextContainer->SetAttribute(NS_LITERAL_STRING("model"), modelID);  nsCOMPtr<nsIXFormsContextControl> ctx(do_QueryInterface(contextContainer)); if (ctx) { ctx->SetContext(nsCOMPtr<nsIDOMElement>(do_QueryInterface(node)), i + 1, nodeCount); }  for (PRUint32 j = 0; j < templateNodeCount; ++j) { templateNodes->Item(j, getter_AddRefs(templateNode)); templateNode->CloneNode(PR_TRUE, getter_AddRefs(cloneNode)); contextContainer->AppendChild(cloneNode, getter_AddRefs(templateNode));
nsXFormsItemSetElement::Refresh(){  mItems.Clear();  // We need to create item elements for each element referenced by the  // nodeset.  Each of these items will create an anonymous HTML option element  // which will return from GetAnonymousNodes.  We then clone our template  // content and insert the cloned content as children of the HTML option.  nsCOMPtr<nsIDOMDocument> domDoc;  mElement->GetOwnerDocument(getter_AddRefs(domDoc));  if (!nsXFormsUtils::IsDocumentReadyForBind(domDoc)) {    // not ready to bind yet, defer    nsXFormsModelElement::DeferElementBind(domDoc, this);    return NS_OK;  }  nsCOMPtr<nsIModelElementPrivate> model;  nsCOMPtr<nsIDOMXPathResult> result;  nsXFormsUtils::EvaluateNodeBinding(mElement,                                     nsXFormsUtils::ELEMENT_WITH_MODEL_ATTR,                                     NS_LITERAL_STRING("nodeset"),                                     EmptyString(),                                     nsIDOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE,                                     getter_AddRefs(model),                                     getter_AddRefs(result));  if (!result)    return NS_OK;  nsCOMPtr<nsIDOMNode> node, templateNode, cloneNode, tmpNode;  nsCOMPtr<nsIDOMElement> itemNode, itemWrapperNode, contextContainer;  nsCOMPtr<nsIDOMNodeList> templateNodes;  mElement->GetChildNodes(getter_AddRefs(templateNodes));  PRUint32 templateNodeCount = 0;  if (templateNodes)    templateNodes->GetLength(&templateNodeCount);    nsCOMPtr<nsIDOMDocument> document;  mElement->GetOwnerDocument(getter_AddRefs(document));  if (!document)    return NS_OK;  PRUint32 nodeCount;  result->GetSnapshotLength(&nodeCount);  // XXX Possibly cleanup this when XBLizing <select>.  nsCOMPtr<nsIDOMNode> parent, tmp;  mElement->GetParentNode(getter_AddRefs(parent));  PRBool parentIsSelect1 = PR_FALSE;  while (parent &&    !nsXFormsUtils::IsXFormsElement(parent, NS_LITERAL_STRING("select"))) {    if (nsXFormsUtils::IsXFormsElement(parent, NS_LITERAL_STRING("select1"))) {      parentIsSelect1 = PR_TRUE;      break;    }    tmp.swap(parent);    tmp->GetParentNode(getter_AddRefs(parent));  }    if (parentIsSelect1) {    // For select1    nsCOMPtr<nsIXFormsItemSetUIElement> uiItemSet(do_QueryInterface(mElement));    nsCOMPtr<nsIDOMElement> anonContent;    if (uiItemSet) {      uiItemSet->GetAnonymousItemSetContent(getter_AddRefs(anonContent));    }    NS_ENSURE_STATE(anonContent);    nsCOMPtr<nsIDOMNode> childNode, nodeReturn;    while (NS_SUCCEEDED(anonContent->GetFirstChild(getter_AddRefs(childNode))) &&         childNode) {      anonContent->RemoveChild(childNode, getter_AddRefs(nodeReturn));    }    for (PRUint32 i = 0; i < nodeCount; ++i) {      result->SnapshotItem(i, getter_AddRefs(node));      NS_ASSERTION(node, "incorrect snapshot length");      nsresult rv = document->CreateElementNS(NS_LITERAL_STRING(NS_NAMESPACE_XFORMS),                                              NS_LITERAL_STRING("item"),                                              getter_AddRefs(itemNode));      NS_ENSURE_SUCCESS(rv, rv);      // XXX Could we get rid of the <contextcontainer>?      rv = document->CreateElementNS(NS_LITERAL_STRING(NS_NAMESPACE_XFORMS),                                     NS_LITERAL_STRING("contextcontainer"),                                     getter_AddRefs(contextContainer));      NS_ENSURE_SUCCESS(rv, rv);      nsCOMPtr<nsIDOMElement> modelElement = do_QueryInterface(model);      nsAutoString modelID;      modelElement->GetAttribute(NS_LITERAL_STRING("id"), modelID);      contextContainer->SetAttribute(NS_LITERAL_STRING("model"), modelID);      nsCOMPtr<nsIXFormsContextControl> ctx(do_QueryInterface(contextContainer));      if (ctx) {        ctx->SetContext(nsCOMPtr<nsIDOMElement>(do_QueryInterface(node)),                        i + 1, nodeCount);      }      // Clone the template content under the item      for (PRUint32 j = 0; j < templateNodeCount; ++j) {        templateNodes->Item(j, getter_AddRefs(templateNode));        templateNode->CloneNode(PR_TRUE, getter_AddRefs(cloneNode));        contextContainer->AppendChild(cloneNode, getter_AddRefs(templateNode));      }      itemNode->AppendChild(contextContainer, getter_AddRefs(tmpNode));      anonContent->AppendChild(itemNode, getter_AddRefs(tmpNode));    }  } else {    for (PRUint32 i = 0; i < nodeCount; ++i) {      result->SnapshotItem(i, getter_AddRefs(node));      NS_ASSERTION(node, "incorrect snapshot length");      document->CreateElementNS(NS_LITERAL_STRING(NS_NAMESPACE_XFORMS),                                NS_LITERAL_STRING("item"),                                getter_AddRefs(itemNode));      if (!itemNode)        return NS_OK;      nsCOMPtr<nsIDOMElement> modelElement = do_QueryInterface(model);      nsAutoString modelID;      modelElement->GetAttribute(NS_LITERAL_STRING("id"), modelID);      itemNode->SetAttribute(NS_LITERAL_STRING("model"), modelID);      nsCOMPtr<nsIXFormsSelectChild> item = do_QueryInterface(itemNode);      NS_ASSERTION(item, "item must be a SelectChild!");      item->SetContext(nsCOMPtr<nsIDOMElement>(do_QueryInterface(node)),                       i + 1, nodeCount);      // Clone the template content under the item      for (PRUint32 j = 0; j < templateNodeCount; ++j) {        templateNodes->Item(j, getter_AddRefs(templateNode));        templateNode->CloneNode(PR_TRUE, getter_AddRefs(cloneNode));        itemNode->AppendChild(cloneNode, getter_AddRefs(templateNode));      }      mItems.AppendObject(item);    }  }  // refresh parent control, since we are being defered  nsCOMPtr<nsIDOMNode> parentNode;  mElement->GetParentNode(getter_AddRefs(parentNode));  if (parentNode) {    nsCOMPtr<nsIXFormsControlBase> stub = do_QueryInterface(parentNode);    if (stub) {      stub->Refresh();    }  }  return NS_OK;}