From b99bf67adc298564607cfc09b083d6da7c0ebf82 Mon Sep 17 00:00:00 2001 From: "jst%netscape.com" Date: Tue, 17 Apr 2001 08:35:49 +0000 Subject: [PATCH] Fixing regression bug 75645, anonymous form controls were ending up in form.elements. r=pollmann@netscape.com, sr=rpotts@netscape.com --- .../html/content/src/nsGenericHTMLElement.cpp | 105 ++++++++---------- .../html/content/src/nsGenericHTMLElement.h | 5 +- .../html/content/src/nsHTMLInputElement.cpp | 15 +-- 3 files changed, 59 insertions(+), 66 deletions(-) diff --git a/content/html/content/src/nsGenericHTMLElement.cpp b/content/html/content/src/nsGenericHTMLElement.cpp index c0aa026a5a4c..4d3789c716ea 100644 --- a/content/html/content/src/nsGenericHTMLElement.cpp +++ b/content/html/content/src/nsGenericHTMLElement.cpp @@ -1008,46 +1008,60 @@ nsGenericHTMLElement::SetDocument(nsIDocument* aDocument, PRBool aDeep, return result; } -nsIContent* -nsGenericHTMLElement::FindFormParentContent(nsIContent* aParent) +nsresult +nsGenericHTMLElement::FindForm(nsIDOMHTMLFormElement **aForm) { + // XXX: Namespaces!!! + + nsCOMPtr content(this); nsCOMPtr tag; - nsCOMPtr parent(aParent); + PRInt32 nameSpaceID; - while (parent) { - PRInt32 nameSpaceID; + *aForm = nsnull; - parent->GetTag(*getter_AddRefs(tag)); - parent->GetNameSpaceID(nameSpaceID); + while (content) { + content->GetTag(*getter_AddRefs(tag)); + content->GetNameSpaceID(nameSpaceID); - // If the current ancestor is a form, set it as our form + // If the current ancestor is a form, return it as our form if ((tag.get() == nsHTMLAtoms::form) && (kNameSpaceID_HTML == nameSpaceID)) { - nsIContent * content = parent.get(); - NS_IF_ADDREF(content); - return content; + return CallQueryInterface(content, aForm); } - nsIContent *tmp = parent; - tmp->GetParent(*getter_AddRefs(parent)); + nsIContent *tmp = content; + tmp->GetParent(*getter_AddRefs(content)); + + if (content) { + PRInt32 i; + + content->IndexOf(tmp, i); + + if (i < 0) { + // This means 'tmp' is anonymous content, form controls in + // anonymous content can't refer to the real form, if they do + // they end up in form.elements n' such, and that's wrong... + + return NS_OK; + } + } } - return nsnull; + return NS_OK; } nsresult -nsGenericHTMLElement::FindAndSetFormParent(nsIContent* aParent, - nsIFormControl* aControl) +nsGenericHTMLElement::FindAndSetForm(nsIFormControl *aFormControl) { - nsresult result = NS_OK; - nsCOMPtr formParent(getter_AddRefs(FindFormParentContent(aParent))); - if (formParent) { - nsCOMPtr form(do_QueryInterface(formParent)); - if (form) { - result = aControl->SetForm(form); - } + nsCOMPtr form; + + FindForm(getter_AddRefs(form)); + + if (form) { + return aFormControl->SetForm(form); } - return result; + + return NS_OK; } nsresult @@ -3749,7 +3763,7 @@ nsGenericHTMLContainerFormElement::SetParent(nsIContent* aParent) // search. In this case, someone (possibly the content sink) has // already set the form for us. - rv = FindAndSetFormParent(aParent, this); + rv = FindAndSetForm(this); } if (NS_SUCCEEDED(rv)) { @@ -3767,14 +3781,7 @@ nsGenericHTMLContainerFormElement::SetDocument(nsIDocument* aDocument, nsresult rv = NS_OK; if (aDocument && mParent && !mForm) { - // XXX Do this check since anonymous frame content was also being - // added to the form. To make sure that the form control isn't - // anonymous, we ask the parent if it knows about it. - PRInt32 index; - mParent->IndexOf(this, index); - if (-1 != index) { - rv = FindAndSetFormParent(mParent, this); - } + rv = FindAndSetForm(this); } if (NS_SUCCEEDED(rv)) { @@ -3953,15 +3960,6 @@ nsGenericHTMLLeafFormElement::GetForm(nsIDOMHTMLFormElement** aForm) NS_ENSURE_ARG_POINTER(aForm); *aForm = nsnull; - // This is here because radios can be created via script - // and depending on how the content in the script is constructed - // none of the code paths that set up the mForm may get called - // So this is a last stab effort to get the form before somebody - // uses the radiobutton. Bug 62799 - if (mForm == nsnull) { - FindAndSetFormParent(mParent, this); // ignore returned result - } - if (mForm) { mForm->QueryInterface(NS_GET_IID(nsIDOMHTMLFormElement), (void**)aForm); } @@ -3974,6 +3972,12 @@ nsGenericHTMLLeafFormElement::SetParent(nsIContent* aParent) { nsresult rv = NS_OK; + PRBool old_parent = (PRBool)mParent; + + if (NS_SUCCEEDED(rv)) { + rv = nsGenericElement::SetParent(aParent); + } + if (!aParent && mForm) { SetForm(nsnull); } @@ -3982,12 +3986,8 @@ nsGenericHTMLLeafFormElement::SetParent(nsIContent* aParent) // have an old parent, but we do have a form, we shouldn't do the // search. In this case, someone (possibly the content sink) has // already set the form for us. - else if (mDocument && aParent && (mParent || !mForm)) { - rv = FindAndSetFormParent(aParent, this); - } - - if (NS_SUCCEEDED(rv)) { - rv = nsGenericElement::SetParent(aParent); + else if (mDocument && aParent && (old_parent || !mForm)) { + rv = FindAndSetForm(this); } return rv; @@ -4001,14 +4001,7 @@ nsGenericHTMLLeafFormElement::SetDocument(nsIDocument* aDocument, nsresult rv = NS_OK; if (aDocument && mParent && !mForm) { - // XXX Do this check since anonymous frame content was also being - // added to the form. To make sure that the form control isn't - // anonymous, we ask the parent if it knows about it. - PRInt32 index; - mParent->IndexOf(this, index); - if (-1 != index) { - rv = FindAndSetFormParent(mParent, this); - } + rv = FindAndSetForm(this); } if (NS_SUCCEEDED(rv)) { diff --git a/content/html/content/src/nsGenericHTMLElement.h b/content/html/content/src/nsGenericHTMLElement.h index 2cf6c2862d95..e339aa0d9018 100644 --- a/content/html/content/src/nsGenericHTMLElement.h +++ b/content/html/content/src/nsGenericHTMLElement.h @@ -331,10 +331,9 @@ public: nsIURI** aResult); // Form Helper Routines - static nsIContent* FindFormParentContent(nsIContent* aParent); + nsresult FindForm(nsIDOMHTMLFormElement **aForm); - static nsresult FindAndSetFormParent(nsIContent* aParent, - nsIFormControl* aControl); + nsresult FindAndSetForm(nsIFormControl *aFormControl); // See if the content object is in a document that has nav-quirks // mode enabled. diff --git a/content/html/content/src/nsHTMLInputElement.cpp b/content/html/content/src/nsHTMLInputElement.cpp index 56ead2c527b4..35f433d18acc 100644 --- a/content/html/content/src/nsHTMLInputElement.cpp +++ b/content/html/content/src/nsHTMLInputElement.cpp @@ -710,9 +710,9 @@ nsHTMLInputElement::RemoveFocus(nsIPresContext* aPresContext) nsIFormControlFrame* formControlFrame = nsnull; - rv = GetPrimaryFrame(this, formControlFrame); + GetPrimaryFrame(this, formControlFrame); - if (NS_SUCCEEDED(rv)) { + if (formControlFrame) { formControlFrame->SetFocus(PR_FALSE, PR_FALSE); } @@ -873,16 +873,16 @@ nsHTMLInputElement::MouseClickForAltText(nsIPresContext* aPresContext) { NS_ENSURE_ARG_POINTER(aPresContext); PRBool disabled; + nsresult rv = GetDisabled(&disabled); if (NS_FAILED(rv) || disabled) { return rv; } // Generate a submit event targetted at the form content - nsCOMPtr form; - GetForm(getter_AddRefs(form)); - nsCOMPtr formContent(do_QueryInterface(form)); - if (formContent) { + nsCOMPtr form(do_QueryInterface(mForm)); + + if (form) { nsCOMPtr shell; aPresContext->GetShell(getter_AddRefs(shell)); if (shell) { @@ -890,9 +890,10 @@ nsHTMLInputElement::MouseClickForAltText(nsIPresContext* aPresContext) nsEvent event; event.eventStructType = NS_EVENT; event.message = NS_FORM_SUBMIT; - shell->HandleDOMEventWithTarget(formContent, &event, &status); + shell->HandleDOMEventWithTarget(form, &event, &status); } } + return rv; }