rv = GetIntAttr(NS_LITERAL_STRING("startindex"), (PRInt32*) &mCurrentIndex, nsISchemaBuiltinType::BUILTIN_TYPE_POSITIVEINTEGER); if (NS_FAILED(rv)) { if (rv == NS_ERROR_NOT_AVAILABLE) { mCurrentIndex = 1; } else { return rv; } } if (mCurrentIndex < 1) { mCurrentIndex = 1; } else if (mCurrentIndex > mMaxIndex) { mCurrentIndex = mMaxIndex; }
GetStartingIndex(&mCurrentIndex);
nsXFormsRepeatElement::Refresh(){  if (!mHTMLElement || mAddingChildren || mIsParent)    return NS_OK;  nsPostRefresh postRefresh = nsPostRefresh();  nsresult rv;  // Clear any existing children  nsCOMPtr<nsIDOMNode> cNode;  mHTMLElement->GetFirstChild(getter_AddRefs(cNode));  while (cNode) {    nsCOMPtr<nsIDOMNode> retNode;    mHTMLElement->RemoveChild(cNode, getter_AddRefs(retNode));    mHTMLElement->GetFirstChild(getter_AddRefs(cNode));  }  // Get the nodeset we are bound to  nsCOMPtr<nsIDOMXPathResult> result;  nsCOMPtr<nsIModelElementPrivate> model;  rv = ProcessNodeBinding(NS_LITERAL_STRING("nodeset"),                          nsIDOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE,                          getter_AddRefs(result),                          getter_AddRefs(model));  if (NS_FAILED(rv) | !result | !model)    return rv;                                           /// @todo The spec says: "This node-set must consist of contiguous child  /// element nodes, with the same local name and namespace name of a common  /// parent node. The behavior of element repeat with respect to  /// non-homogeneous node-sets is undefined."  /// @see http://www.w3.org/TR/xforms/slice9.html#ui-repeat  ///  /// Can/should we check this somehow? (XXX)    PRUint32 contextSize;  rv = result->GetSnapshotLength(&contextSize);  NS_ENSURE_SUCCESS(rv, rv);  if (!contextSize)    return NS_OK;    // Get model ID  nsCOMPtr<nsIDOMElement> modelElement = do_QueryInterface(model);  NS_ENSURE_TRUE(modelElement, NS_ERROR_FAILURE);  nsAutoString modelID;  modelElement->GetAttribute(NS_LITERAL_STRING("id"), modelID);  // Get DOM document  nsCOMPtr<nsIDOMDocument> domDoc;  rv = mHTMLElement->GetOwnerDocument(getter_AddRefs(domDoc));  NS_ENSURE_SUCCESS(rv, rv);  mMaxIndex = contextSize;  for (PRUint32 i = 1; i < mMaxIndex + 1; ++i) {    // Create <contextcontainer>    nsCOMPtr<nsIDOMElement> riElement;    rv = domDoc->CreateElementNS(NS_LITERAL_STRING("http://www.w3.org/2002/xforms"),                                 NS_LITERAL_STRING("contextcontainer"),                                 getter_AddRefs(riElement));    NS_ENSURE_SUCCESS(rv, rv);          // Set model as attribute    if (!modelID.IsEmpty()) {      riElement->SetAttribute(NS_LITERAL_STRING("model"), modelID);    }      // Get context node    nsCOMPtr<nsIXFormsContextControl> riContext = do_QueryInterface(riElement);    NS_ENSURE_TRUE(riContext, NS_ERROR_FAILURE);    nsCOMPtr<nsIDOMNode> contextNode;    rv = result->SnapshotItem(i - 1, getter_AddRefs(contextNode));    NS_ENSURE_SUCCESS(rv, rv);            nsCOMPtr<nsIDOMElement> contextElement = do_QueryInterface(contextNode);    NS_ENSURE_TRUE(contextElement, NS_ERROR_FAILURE);    // Set context node, position, and size    rv = riContext->SetContext(contextElement, i, contextSize);    NS_ENSURE_SUCCESS(rv, rv);    // Iterate over template children, clone them, and append them to <contextcontainer>    nsCOMPtr<nsIDOMNode> child;    rv = mElement->GetFirstChild(getter_AddRefs(child));    NS_ENSURE_SUCCESS(rv, rv);    while (child) {      nsCOMPtr<nsIDOMNode> childClone;      rv = CloneNode(child, getter_AddRefs(childClone));      NS_ENSURE_SUCCESS(rv, rv);              nsCOMPtr<nsIDOMNode> newNode;      rv = riElement->AppendChild(childClone, getter_AddRefs(newNode));      NS_ENSURE_SUCCESS(rv, rv);              rv = child->GetNextSibling(getter_AddRefs(newNode));      NS_ENSURE_SUCCESS(rv, rv);      child = newNode;    }    // Append node    nsCOMPtr<nsIDOMNode> domNode;    rv = mHTMLElement->AppendChild(riElement, getter_AddRefs(domNode));    NS_ENSURE_SUCCESS(rv, rv);    // There is an awfull lot of evaluating being done by all the    // children, as they are created and inserted into the different    // places in the DOM, the only refresh necessary is the one when they    // are appended in mHTMLElement.  }  // XForms errata states that startindex is always '1' for nested repeats  // (http://www.w3.org/MarkUp/Forms/Group/Drafts/Sources/errata.html#E35a)  // so we'll not check it for those.  if (!mParent && !mCurrentIndex && mMaxIndex) {    // repeat-index has not been initialized, set it.    rv = GetIntAttr(NS_LITERAL_STRING("startindex"),                    (PRInt32*) &mCurrentIndex,                    nsISchemaBuiltinType::BUILTIN_TYPE_POSITIVEINTEGER);    if (NS_FAILED(rv)) {      if (rv == NS_ERROR_NOT_AVAILABLE) {        mCurrentIndex = 1;      } else {        return rv;      }    }    if (mCurrentIndex < 1) {      mCurrentIndex = 1;    } else if (mCurrentIndex > mMaxIndex) {      mCurrentIndex = mMaxIndex;    }  }  // If we have the repeat-index, set it.  if (mCurrentIndex) {     SetChildIndex(mCurrentIndex, PR_TRUE, PR_TRUE);  }  return NS_OK;}