Make nsXULElement::GetChildNodes use the same (live) node list that

nsGenericElement uses.  Bug 240633, r+sr=jst on the C++ changes, r=neil, sr=jst
on the JS changes.


git-svn-id: svn://10.0.0.236/trunk@154950 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
bzbarsky%mit.edu
2004-04-15 22:14:39 +00:00
parent cf213cea0a
commit 1c7a6361be
25 changed files with 71 additions and 282 deletions

View File

@@ -1962,7 +1962,7 @@ function createRadioMenuItem( aParent, aIndex, aLabel, aChecked)
function deleteHistoryItems(aParent)
{
var children = aParent.childNodes;
for (var i = 0; i < children.length; i++)
for (var i = children.length - 1; i >= 0; --i)
{
var index = children[i].getAttribute("index");
if (index)

View File

@@ -51,7 +51,6 @@ REQUIRES = xpcom \
$(NULL)
CPPSRCS = \
nsRDFDOMNodeList.cpp \
nsXULElement.cpp \
nsXULPopupListener.cpp \
$(NULL)

View File

@@ -1,133 +0,0 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Pierre Phaneuf <pp@ludusdesign.com>
*
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the NPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the NPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
Helper class to implement the nsIDOMNodeList interface.
XXX It's probably wrong in some sense, as it uses the "naked"
content interface to look for kids. (I assume in general this is
bad because there may be pseudo-elements created for presentation
that aren't visible to the DOM.)
*/
#include "nsIDOMNode.h"
#include "nsRDFDOMNodeList.h"
#include "nsContentUtils.h"
////////////////////////////////////////////////////////////////////////
// ctors & dtors
nsRDFDOMNodeList::nsRDFDOMNodeList(void)
{
}
nsRDFDOMNodeList::~nsRDFDOMNodeList(void)
{
}
////////////////////////////////////////////////////////////////////////
// nsISupports interface
// QueryInterface implementation for nsRDFDOMNodeList
NS_INTERFACE_MAP_BEGIN(nsRDFDOMNodeList)
NS_INTERFACE_MAP_ENTRY(nsIDOMNodeList)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMNodeList)
NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(XULNodeList)
NS_INTERFACE_MAP_END
NS_IMPL_ADDREF(nsRDFDOMNodeList)
NS_IMPL_RELEASE(nsRDFDOMNodeList)
////////////////////////////////////////////////////////////////////////
// nsIDOMNodeList interface
NS_IMETHODIMP
nsRDFDOMNodeList::GetLength(PRUint32* aLength)
{
NS_ASSERTION(aLength != nsnull, "null ptr");
if (! aLength)
return NS_ERROR_NULL_POINTER;
*aLength = mElements.Count();
return NS_OK;
}
NS_IMETHODIMP
nsRDFDOMNodeList::Item(PRUint32 aIndex, nsIDOMNode** aReturn)
{
if (aIndex >= (PRUint32) mElements.Count()) {
// We simply return nsnull, as per the DOM spec.
*aReturn = nsnull;
return NS_OK;
}
NS_ADDREF(*aReturn = mElements[aIndex]);
return NS_OK;
}
////////////////////////////////////////////////////////////////////////
// Implementation methods
nsresult
nsRDFDOMNodeList::AppendNode(nsIDOMNode* aNode)
{
NS_PRECONDITION(aNode != nsnull, "null ptr");
if (! aNode)
return NS_ERROR_NULL_POINTER;
return mElements.AppendObject(aNode);
}
nsresult
nsRDFDOMNodeList::RemoveNode(nsIDOMNode* aNode)
{
NS_PRECONDITION(aNode != nsnull, "null ptr");
if (! aNode)
return NS_ERROR_NULL_POINTER;
return mElements.RemoveObject(aNode);
}

View File

@@ -1,68 +0,0 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the NPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the NPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef nsRDFDOMNodeList_h__
#define nsRDFDOMNodeList_h__
#include "nsIDOMNodeList.h"
#include "nsCOMArray.h"
class nsIDOMNode;
class nsRDFDOMNodeList : public nsIDOMNodeList
{
private:
nsCOMArray<nsIDOMNode> mElements;
public:
nsRDFDOMNodeList(void);
virtual ~nsRDFDOMNodeList(void);
// nsISupports interface
NS_DECL_ISUPPORTS
// nsIDOMNodeList interface
NS_DECL_NSIDOMNODELIST
// Implementation methods
nsresult AppendNode(nsIDOMNode* aNode);
nsresult RemoveNode(nsIDOMNode* aNode);
};
#endif // nsRDFDOMNodeList_h__

View File

@@ -112,7 +112,6 @@
#include "nsLayoutCID.h"
#include "nsContentCID.h"
#include "nsRDFCID.h"
#include "nsRDFDOMNodeList.h"
#include "nsStyleConsts.h"
#include "nsXPIDLString.h"
#include "nsXULControllers.h"
@@ -125,7 +124,6 @@
#include "nsCSSDeclaration.h"
#include "nsIListBoxObject.h"
#include "nsContentUtils.h"
#include "nsGenericElement.h"
#include "nsContentList.h"
#include "nsMutationEvent.h"
#include "nsIDOMMutationEvent.h"
@@ -642,31 +640,17 @@ nsXULElement::GetParentNode(nsIDOMNode** aParentNode)
NS_IMETHODIMP
nsXULElement::GetChildNodes(nsIDOMNodeList** aChildNodes)
{
nsresult rv;
nsresult rv = EnsureSlots();
NS_ENSURE_SUCCESS(rv, rv);
nsRDFDOMNodeList* children = new nsRDFDOMNodeList();
NS_ENSURE_TRUE(children, NS_ERROR_OUT_OF_MEMORY);
NS_ADDREF(children);
PRUint32 count = GetChildCount();
for (PRUint32 i = 0; i < count; ++i) {
nsIContent *child = GetChildAt(i);
nsCOMPtr<nsIDOMNode> domNode = do_QueryInterface(child);
if (!domNode) {
NS_WARNING("child content doesn't support nsIDOMNode");
continue;
if (!mSlots->mChildNodes) {
mSlots->mChildNodes = new nsChildContentList(this);
if (!mSlots->mChildNodes) {
return NS_ERROR_OUT_OF_MEMORY;
}
rv = children->AppendNode(domNode);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to append node to list");
if (NS_FAILED(rv))
break;
}
// Create() addref'd for us
*aChildNodes = children;
NS_ADDREF(*aChildNodes = mSlots->mChildNodes);
return NS_OK;
}
@@ -4029,6 +4013,10 @@ nsXULElement::Slots::Slots()
nsXULElement::Slots::~Slots()
{
MOZ_COUNT_DTOR(nsXULElement::Slots);
if (mChildNodes) {
mChildNodes->DropReference();
}
}

View File

@@ -82,13 +82,13 @@
#include "nsAttrAndChildArray.h"
#include "nsXULAtoms.h"
#include "nsAutoPtr.h"
#include "nsGenericElement.h"
class nsIDocument;
class nsIRDFService;
class nsISupportsArray;
class nsIXULContentUtils;
class nsIXULPrototypeDocument;
class nsRDFDOMNodeList;
class nsString;
class nsVoidArray;
class nsIDocShell;
@@ -595,6 +595,7 @@ protected:
nsCOMPtr<nsIControllers> mControllers; // [OWNER]
nsRefPtr<nsDOMCSSDeclaration> mDOMStyle; // [OWNER]
nsRefPtr<nsDOMAttributeMap> mAttributeMap; // [OWNER]
nsRefPtr<nsChildContentList> mChildNodes; // [OWNER]
PRUint32 mLazyState;
};

View File

@@ -94,7 +94,6 @@
#include "nsPIBoxObject.h"
#include "nsRDFCID.h"
#include "nsILocalStore.h"
#include "nsRDFDOMNodeList.h"
#include "nsXPIDLString.h"
#include "nsPIDOMWindow.h"
#include "nsXULCommandDispatcher.h"

View File

@@ -259,9 +259,11 @@ DeleteRangeTxn::CreateTxnsToDeleteBetween(nsIDOMNode *aStartParent,
if (NS_FAILED(result)) return result;
if (!children) return NS_ERROR_NULL_POINTER;
#ifdef DEBUG
PRUint32 childCount;
children->GetLength(&childCount);
NS_ASSERTION(aEndOffset<=childCount, "bad aEndOffset");
#endif
PRUint32 i;
for (i=aStartOffset; i<aEndOffset; i++)
{

View File

@@ -3251,8 +3251,9 @@ function UpdateStructToolbar()
var childNodesLength = childNodes.length;
// We need to leave the <label> to flex the buttons to the left
// so, don't remove the last child at position length - 1
for (var i = childNodesLength - 2; i >= 0; i--) {
toolbar.removeChild(childNodes.item(i));
while (childNodes.length > 1) {
// Remove back to front so there's less moving about.
toolbar.removeChild(childNodes.item(childNodes.length - 2));
}
toolbar.removeAttribute("label");

View File

@@ -295,9 +295,8 @@ function BuildTOC(update)
}
else {
// we can keep the list itself but let's get rid of the TOC entries
var nodeList = toc.childNodes, l = nodeList.length;
for (j = l - 1; j >= 0; --j)
toc.removeChild(nodeList.item(j));
while (toc.hasChildNodes()) {
toc.removeChild(toc.lastChild);
}
}
var commentText = "mozToc ";

View File

@@ -257,7 +257,7 @@ inTreeBuilder.prototype =
resetTree: function()
{
var kids = this.mTree.childNodes;
for (var i = 0 ; i < kids.length; ++i)
for (var i = kids.length - 1; i >= 0; --i)
if (kids[i].localName != "treechildren")
this.mTree.removeChild(kids[i]);
@@ -268,7 +268,7 @@ inTreeBuilder.prototype =
clearChildren: function(aEl)
{
while (aEl.childNodes.length)
while (aEl.hasChildNodes())
aEl.removeChild(aEl.lastChild);
},

View File

@@ -184,9 +184,8 @@ JSObjectViewer.prototype =
emptyTree: function(aTreeKids)
{
var kids = aTreeKids.childNodes;
for (var i = 0; i < kids.length; ++i) {
aTreeKids.removeChild(kids[i]);
while (aTreeKids.hasChildNodes()) {
aTreeKids.removeChild(aTreeKids.lastChild);
}
},

View File

@@ -386,9 +386,8 @@ XBLBindings.prototype =
clearChildren: function(aEl)
{
var kids = aEl.childNodes;
for (var i = kids.length-1; i >=0; --i)
aEl.removeChild(kids[i]);
while (aEl.hasChildNodes())
aEl.removeChild(aEl.lastChild);
},
readDOMText: function(aEl)

View File

@@ -116,6 +116,12 @@ function transferToDocument(aResult, aResultDocument)
else {
aResultDocument.insertBefore(childNode, aResultDocument.documentElement);
}
// We removed the node from the list, which will cause it
// to shrink by one element. "i" is now pointing to the
// node that used to be _after_ the node we just removed.
// Decrement i, so when it increments (at the start of the
// next iteration) we'll point at the right place.
--i;
}
}
}

View File

@@ -1250,8 +1250,8 @@ function ClearAttachmentList()
// clear selection
var list = document.getElementById('attachmentList');
while (list.childNodes.length)
list.removeChild(list.firstChild);
while (list.hasChildNodes())
list.removeChild(list.lastChild);
}
function ShowEditMessageButton()

View File

@@ -402,7 +402,7 @@ function cvSetNode(node, text)
{
if ( node )
{
if ( node.childNodes.length == 0 )
if ( !node.hasChildNodes() )
{
var textNode = document.createTextNode(text);
node.appendChild(textNode);
@@ -437,8 +437,8 @@ function cvAddAddressNodes(node, uri)
if (addressList) {
var total = addressList.Count();
if (total > 0) {
for (var i = node.childNodes.length - 1; i >= 0; i--) {
node.removeChild(node.childNodes[i]);
while (node.hasChildNodes()) {
node.removeChild(node.lastChild);
}
for (i = 0; i < total; i++ ) {
var descNode = document.createElement("description");

View File

@@ -1996,8 +1996,8 @@ function queryISupportsArray(supportsArray, iid) {
function ClearIdentityListPopup(popup)
{
if (popup)
for (var i = popup.childNodes.length - 1; i >= 0; i--)
popup.removeChild(popup.childNodes[i]);
while (popup.hasChildNodes())
popup.removeChild(popup.lastChild);
}
function compareAccountSortOrder(account1, account2)
@@ -2432,9 +2432,9 @@ function RemoveAllAttachments()
{
var child;
var bucket = document.getElementById("attachmentBucket");
for (var i = bucket.childNodes.length - 1; i >= 0; i--)
while (bucket.hasChildNodes())
{
child = bucket.removeChild(bucket.childNodes[i]);
child = bucket.removeChild(bucket.lastChild);
// Let's release the attachment object hold by the node else it won't go away until the window is destroyed
child.attachment = null;
}

View File

@@ -30,8 +30,8 @@ function onEditDirectories()
var popup = document.getElementById("directoriesListPopup");
if (popup)
{
while (popup.childNodes.length)
popup.removeChild(popup.childNodes[0]);
while (popup.hasChildNodes())
popup.removeChild(popup.lastChild);
}
gAvailDirectories = null;
LoadDirectories(popup);

View File

@@ -403,7 +403,7 @@ function cvSetNode(node, text)
{
if ( node )
{
if ( node.childNodes.length == 0 )
if ( !node.hasChildNodes() )
{
var textNode = document.createTextNode(text);
node.appendChild(textNode);
@@ -438,8 +438,8 @@ function cvAddAddressNodes(node, uri)
if (addressList) {
var total = addressList.Count();
if (total > 0) {
for (var i = node.childNodes.length - 1; i >= 0; i--) {
node.removeChild(node.childNodes[i]);
while (node.hasChildNodes()) {
node.removeChild(node.lastChild);
}
for (i = 0; i < total; i++ ) {
var descNode = document.createElement("description");

View File

@@ -1894,8 +1894,8 @@ function queryISupportsArray(supportsArray, iid) {
function ClearIdentityListPopup(popup)
{
if (popup)
for (var i = popup.childNodes.length - 1; i >= 0; i--)
popup.removeChild(popup.childNodes[i]);
while (popup.hasChildNodes())
popup.removeChild(popup.lastChild);
}
function compareAccountSortOrder(account1, account2)
@@ -2300,9 +2300,9 @@ function RemoveAllAttachments()
{
var child;
var bucket = document.getElementById("attachmentBucket");
for (var i = bucket.childNodes.length - 1; i >= 0; i--)
while (bucket.hasChildNodes())
{
child = bucket.removeChild(bucket.childNodes[i]);
child = bucket.removeChild(bucket.lastChild);
// Let's release the attachment object hold by the node else it won't go away until the window is destroyed
child.attachment = null;
}

View File

@@ -98,7 +98,7 @@ function SetDivText(id, text)
var div = document.getElementById(id);
if (div) {
if (!div.childNodes.length) {
if (!div.hasChildNodes()) {
var textNode = document.createTextNode(text);
div.appendChild(textNode);
}
@@ -321,10 +321,8 @@ function ListModules() {
return;
var body = document.getElementById( "moduleList");
var max = body.childNodes.length - 1;
while (max >= 0) {
body.removeChild( body.childNodes[max]);
max--;
while (body.hasChildNodes()) {
body.removeChild(body.lastChild);
}
var count = top.importService.GetModuleCount(top.importType);

View File

@@ -413,7 +413,7 @@ function createMenuItem( aParent, aIndex, aLabel)
function deleteHistoryItems(aParent)
{
var children = aParent.childNodes;
for (var i = 0; i < children.length; i++)
for (var i = children.length - 1; i >= 0; --i)
{
var index = children[i].getAttribute("index");
if (index)

View File

@@ -304,7 +304,7 @@ function createRadioMenuItem( aParent, aIndex, aLabel, aChecked)
function deleteHistoryItems(aParent)
{
var children = aParent.childNodes;
for (var i = 0; i < children.length; i++ )
for (var i = children.length - 1; i >= 0; --i )
{
var index = children[i].getAttribute( "index" );
if (index)

View File

@@ -191,9 +191,8 @@
<method name="clearEngines">
<body><![CDATA[
var kids = this.mSearchBox.childNodes;
for (var i = kids.length-1; i >= 0; --i)
this.mSearchBox.removeChild(kids[i]);
while (this.mSearchBox.hasChildNodes())
this.mSearchBox.removeChild(this.mSearchBox.lastChild);
]]></body>
</method>

View File

@@ -42,10 +42,10 @@ function fillContextMenu(name, treeName)
if (!popupNode) return(false);
// remove the menu node (which tosses all of its kids);
// do this in case any old command nodes are hanging around
while (popupNode.childNodes.length)
{
popupNode.removeChild(popupNode.childNodes[0]);
}
while (popupNode.hasChildNodes())
{
popupNode.removeChild(popupNode.lastChild);
}
var treeNode = document.getElementById(treeName);
if (!treeNode) return(false);
@@ -185,17 +185,17 @@ function fillContextMenu(name, treeName)
}
// strip off leading/trailing menuseparators
while (popupNode.childNodes.length > 0)
while (popupNode.hasChildNodes())
{
if (popupNode.childNodes[0].tagName != "menuseparator")
if (popupNode.firstChild.tagName != "menuseparator")
break;
popupNode.removeChild(popupNode.childNodes[0]);
popupNode.removeChild(popupNode.firstChild);
}
while (popupNode.childNodes.length > 0)
while (popupNode.hasChildNodes())
{
if (popupNode.childNodes[popupNode.childNodes.length - 1].tagName != "menuseparator")
if (popupNode.lastChild.tagName != "menuseparator")
break;
popupNode.removeChild(popupNode.childNodes[popupNode.childNodes.length - 1]);
popupNode.removeChild(popupNode.lastChild);
}
var searchMode = 0;
@@ -203,9 +203,9 @@ function fillContextMenu(name, treeName)
if (pref && bundle)
{
// then add a menu separator (if necessary)
if (popupNode.childNodes.length > 0)
if (popupNode.hasChildNodes())
{
if (popupNode.childNodes[popupNode.childNodes.length - 1].tagName != "menuseparator")
if (popupNode.lastChild.tagName != "menuseparator")
{
var menuSep = document.createElement("menuseparator");
popupNode.appendChild(menuSep);