if (index <= -1) { PRUint32 formsLength;  mHTMLForms->GetLength(&formsLength, PR_FALSE); index = (PRInt32)formsLength; }
FrameManager::GenerateStateKey(nsIContent* aContent,                               nsIStatefulFrame::SpecialStateID aID,                               nsCString& aKey){  aKey.Truncate();  // SpecialStateID case - e.g. scrollbars around the content window  // The key in this case is the special state id (always < min(contentID))  if (nsIStatefulFrame::eNoID != aID) {    KeyAppendInt(aID, aKey);    return NS_OK;  }  // We must have content if we're not using a special state id  NS_ENSURE_TRUE(aContent, NS_ERROR_FAILURE);  // Don't capture state for anonymous content  PRUint32 contentID;  aContent->GetContentID(&contentID);  if (!contentID) {    return NS_OK;  }  nsCOMPtr<nsIDOMElement> element(do_QueryInterface(aContent));  if (element && IsAutocompleteOff(element)) {    return NS_OK;  }  // If we have a dom element, add tag/type/name to hash key  // This is paranoia, but guarantees that we won't restore  // state to the wrong type of control.  nsCOMPtr<nsIAtom> tag;  if (aContent->IsContentOfType(nsIContent::eHTML_FORM_CONTROL)) {    aContent->GetTag(*getter_AddRefs(tag));    KeyAppendAtom(tag, aKey);    nsAutoString name;    aContent->GetAttr(kNameSpaceID_HTML, nsHTMLAtoms::name, name);    KeyAppendString(name, aKey);  }  // If we have a form control and can calculate form information, use  // that as the key - it is more reliable than contentID.  // Important to have a unique key, and tag/type/name may not be.  nsCOMPtr<nsIFormControl> control(do_QueryInterface(aContent));  PRBool generatedUniqueKey = PR_FALSE;  if (control && mHTMLFormControls && mHTMLForms) {    if (tag == nsHTMLAtoms::input) {      PRInt32 type;      control->GetType(&type);      KeyAppendInt(type, aKey);    }    // If in a form, add form name / index of form / index in form    PRInt32 index = -1;    nsCOMPtr<nsIDOMHTMLFormElement> formElement;    control->GetForm(getter_AddRefs(formElement));    if (formElement) {      if (IsAutocompleteOff(formElement)) {        aKey.Truncate();        return NS_OK;      }      nsAutoString formName;      formElement->GetName(formName);      KeyAppendString(formName, aKey);      nsCOMPtr<nsIContent> formContent(do_QueryInterface(formElement));      mHTMLForms->IndexOf(formContent, index, PR_FALSE);      NS_ASSERTION(index > -1,                   "nsFrameManager::GenerateStateKey didn't find form index!");      if (index > -1) {        KeyAppendInt(index, aKey);        nsCOMPtr<nsIForm> form(do_QueryInterface(formElement));        form->IndexOfControl(control, &index);        NS_ASSERTION(index > -1,                     "nsFrameManager::GenerateStateKey didn't find form control index!");        if (index > -1) {          KeyAppendInt(index, aKey);          generatedUniqueKey = PR_TRUE;        }      }    } else {      // If not in a form, add index of control in document      // Less desirable than indexing by form info.       // Hash by index of control in doc (we are not in a form)      // These are important as they are unique, and type/name may not be.      mHTMLFormControls->IndexOf(aContent, index, PR_FALSE);      NS_ASSERTION(index > -1,                   "nsFrameManager::GenerateStateKey didn't find content by type!");      if (index > -1) {        KeyAppendInt(index, aKey);        generatedUniqueKey = PR_TRUE;      }    }  }  if (!generatedUniqueKey) {    // Either we didn't have a form control or we aren't in an HTML document    // so we can't figure out form info, hash by content ID instead :(    KeyAppendInt(contentID, aKey);  }  return NS_OK;}