From 4cece07c24ecb7683d4ec079d3c1c1ef6fedf208 Mon Sep 17 00:00:00 2001 From: "jst%netscape.com" Date: Thu, 20 Apr 2000 12:34:35 +0000 Subject: [PATCH] Fixing bug #32376. Mozilla now populates a select list with new empty options if it's length is set to a bigger number than it already is, this is against the DOM but we need this to be compatible with older browsers. git-svn-id: svn://10.0.0.236/trunk@66552 18797224-902f-48f8-a5cc-f745e15eee43 --- .../html/content/src/nsHTMLSelectElement.cpp | 48 +++++++++++++++---- .../html/content/src/nsHTMLSelectElement.cpp | 48 +++++++++++++++---- 2 files changed, 80 insertions(+), 16 deletions(-) diff --git a/mozilla/content/html/content/src/nsHTMLSelectElement.cpp b/mozilla/content/html/content/src/nsHTMLSelectElement.cpp index 891c539447d..8857971484e 100644 --- a/mozilla/content/html/content/src/nsHTMLSelectElement.cpp +++ b/mozilla/content/html/content/src/nsHTMLSelectElement.cpp @@ -27,6 +27,7 @@ #include "nsIScriptObjectOwner.h" #include "nsIDOMEventReceiver.h" #include "nsIHTMLContent.h" +#include "nsITextContent.h" #include "nsGenericHTMLElement.h" #include "nsHTMLAtoms.h" #include "nsHTMLIIDs.h" @@ -467,14 +468,45 @@ nsHTMLSelectElement::SetLength(PRUint32 aLength) Init(); } + nsresult rv=NS_OK; + PRUint32 curlen; + PRInt32 i; GetLength(&curlen); if (curlen && (curlen > aLength)) { // Remove extra options - nsresult result=NS_OK; - for (PRInt32 i = (curlen - 1); (i>=(PRInt32)aLength) && NS_SUCCEEDED(result); i--) { - result = Remove(i); + for (i = (curlen - 1); (i >= (PRInt32)aLength) && NS_SUCCEEDED(rv); i--) { + rv = Remove(i); + } + } else if (aLength) { + // This violates the W3C DOM but we do this for backwards compatibility + nsCOMPtr element; + + rv = NS_NewHTMLOptionElement(getter_AddRefs(element), nsHTMLAtoms::option); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr text; + rv = NS_NewTextNode(getter_AddRefs(text)); + NS_ENSURE_SUCCESS(rv, rv); + + rv = element->AppendChildTo(text, PR_FALSE); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr node(do_QueryInterface(element)); + + for (i = curlen; i < aLength; i++) { + nsCOMPtr tmpNode; + + rv = AppendChild(node, getter_AddRefs(tmpNode)); + NS_ENSURE_SUCCESS(rv, rv); + + if (i < (aLength - 1)) { + nsCOMPtr newNode; + rv = node->CloneNode(PR_TRUE, getter_AddRefs(newNode)); + NS_ENSURE_SUCCESS(rv, rv); + + node = newNode; + } } - } else { // Add options? } return NS_OK; @@ -514,15 +546,15 @@ nsHTMLSelectElement::GetSelectedIndex(PRInt32* aValue) res = NS_ERROR_NULL_POINTER; if (supp) { - nsCOMPtr value = do_QueryInterface(supp); - if (value) { + nsCOMPtr svalue = do_QueryInterface(supp); + if (svalue) { PRUint32 count = 0; - value->Count(&count); + svalue->Count(&count); nsCOMPtr thisVal; for (PRUint32 i=0; i suppval = getter_AddRefs(value->ElementAt(i)); + nsCOMPtr suppval = getter_AddRefs(svalue->ElementAt(i)); thisVal = do_QueryInterface(suppval); if (thisVal) { res = thisVal->GetData(aValue); diff --git a/mozilla/layout/html/content/src/nsHTMLSelectElement.cpp b/mozilla/layout/html/content/src/nsHTMLSelectElement.cpp index 891c539447d..8857971484e 100644 --- a/mozilla/layout/html/content/src/nsHTMLSelectElement.cpp +++ b/mozilla/layout/html/content/src/nsHTMLSelectElement.cpp @@ -27,6 +27,7 @@ #include "nsIScriptObjectOwner.h" #include "nsIDOMEventReceiver.h" #include "nsIHTMLContent.h" +#include "nsITextContent.h" #include "nsGenericHTMLElement.h" #include "nsHTMLAtoms.h" #include "nsHTMLIIDs.h" @@ -467,14 +468,45 @@ nsHTMLSelectElement::SetLength(PRUint32 aLength) Init(); } + nsresult rv=NS_OK; + PRUint32 curlen; + PRInt32 i; GetLength(&curlen); if (curlen && (curlen > aLength)) { // Remove extra options - nsresult result=NS_OK; - for (PRInt32 i = (curlen - 1); (i>=(PRInt32)aLength) && NS_SUCCEEDED(result); i--) { - result = Remove(i); + for (i = (curlen - 1); (i >= (PRInt32)aLength) && NS_SUCCEEDED(rv); i--) { + rv = Remove(i); + } + } else if (aLength) { + // This violates the W3C DOM but we do this for backwards compatibility + nsCOMPtr element; + + rv = NS_NewHTMLOptionElement(getter_AddRefs(element), nsHTMLAtoms::option); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr text; + rv = NS_NewTextNode(getter_AddRefs(text)); + NS_ENSURE_SUCCESS(rv, rv); + + rv = element->AppendChildTo(text, PR_FALSE); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr node(do_QueryInterface(element)); + + for (i = curlen; i < aLength; i++) { + nsCOMPtr tmpNode; + + rv = AppendChild(node, getter_AddRefs(tmpNode)); + NS_ENSURE_SUCCESS(rv, rv); + + if (i < (aLength - 1)) { + nsCOMPtr newNode; + rv = node->CloneNode(PR_TRUE, getter_AddRefs(newNode)); + NS_ENSURE_SUCCESS(rv, rv); + + node = newNode; + } } - } else { // Add options? } return NS_OK; @@ -514,15 +546,15 @@ nsHTMLSelectElement::GetSelectedIndex(PRInt32* aValue) res = NS_ERROR_NULL_POINTER; if (supp) { - nsCOMPtr value = do_QueryInterface(supp); - if (value) { + nsCOMPtr svalue = do_QueryInterface(supp); + if (svalue) { PRUint32 count = 0; - value->Count(&count); + svalue->Count(&count); nsCOMPtr thisVal; for (PRUint32 i=0; i suppval = getter_AddRefs(value->ElementAt(i)); + nsCOMPtr suppval = getter_AddRefs(svalue->ElementAt(i)); thisVal = do_QueryInterface(suppval); if (thisVal) { res = thisVal->GetData(aValue);