1998-07-22 23:32:19 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.0 (the "NPL"); you may not use this file except in
|
|
|
|
* compliance with the NPL. You may obtain a copy of the NPL at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* NPL.
|
|
|
|
*
|
|
|
|
* The Initial Developer of this code under the NPL is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
|
|
|
* Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsContentList.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIDocument.h"
|
1998-12-20 01:21:23 +00:00
|
|
|
#include "nsINameSpaceManager.h"
|
1999-01-18 03:43:43 +00:00
|
|
|
#include "nsIDOMScriptObjectFactory.h"
|
|
|
|
#include "nsGenericElement.h"
|
1998-12-20 01:21:23 +00:00
|
|
|
|
1999-01-22 22:48:00 +00:00
|
|
|
#include "nsLayoutAtoms.h"
|
1998-12-20 01:21:23 +00:00
|
|
|
#include "nsHTMLAtoms.h" // XXX until atoms get factored into nsLayoutAtoms
|
1998-07-22 23:32:19 +00:00
|
|
|
|
1999-01-15 19:18:30 +00:00
|
|
|
nsIAtom* nsContentList::gWildCardAtom = nsnull;
|
|
|
|
|
1998-08-04 00:05:22 +00:00
|
|
|
nsContentList::nsContentList(nsIDocument *aDocument)
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
mScriptObject = nsnull;
|
|
|
|
mFunc = nsnull;
|
1999-01-15 19:18:30 +00:00
|
|
|
mMatchAtom = nsnull;
|
1998-08-04 00:05:22 +00:00
|
|
|
mDocument = aDocument;
|
1999-05-13 00:21:50 +00:00
|
|
|
mData = nsnull;
|
1999-01-15 19:18:30 +00:00
|
|
|
mMatchAll = PR_FALSE;
|
1998-08-04 00:05:22 +00:00
|
|
|
}
|
1998-07-22 23:32:19 +00:00
|
|
|
|
|
|
|
nsContentList::nsContentList(nsIDocument *aDocument,
|
1999-01-15 19:18:30 +00:00
|
|
|
nsIAtom* aMatchAtom,
|
|
|
|
PRInt32 aMatchNameSpaceId,
|
|
|
|
nsIContent* aRootContent)
|
1998-07-22 23:32:19 +00:00
|
|
|
{
|
1999-01-15 19:18:30 +00:00
|
|
|
mMatchAtom = aMatchAtom;
|
|
|
|
NS_IF_ADDREF(mMatchAtom);
|
|
|
|
if (nsnull == gWildCardAtom) {
|
|
|
|
gWildCardAtom = NS_NewAtom("*");
|
|
|
|
}
|
|
|
|
if (gWildCardAtom == mMatchAtom) {
|
|
|
|
mMatchAll = PR_TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mMatchAll = PR_FALSE;
|
|
|
|
}
|
|
|
|
mMatchNameSpaceId = aMatchNameSpaceId;
|
1998-07-22 23:32:19 +00:00
|
|
|
mFunc = nsnull;
|
1999-02-04 23:23:07 +00:00
|
|
|
mData = nsnull;
|
1999-01-15 19:18:30 +00:00
|
|
|
mRootContent = aRootContent;
|
1998-07-22 23:32:19 +00:00
|
|
|
Init(aDocument);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsContentList::nsContentList(nsIDocument *aDocument,
|
1999-01-15 19:18:30 +00:00
|
|
|
nsContentListMatchFunc aFunc,
|
1999-05-04 20:53:44 +00:00
|
|
|
const nsString* aData,
|
1999-01-15 19:18:30 +00:00
|
|
|
nsIContent* aRootContent)
|
1998-07-22 23:32:19 +00:00
|
|
|
{
|
|
|
|
mFunc = aFunc;
|
1999-05-04 20:53:44 +00:00
|
|
|
if (nsnull != aData) {
|
|
|
|
mData = new nsString(*aData);
|
|
|
|
// If this fails, fail silently
|
|
|
|
}
|
1999-06-12 00:37:36 +00:00
|
|
|
else {
|
|
|
|
mData = nsnull;
|
|
|
|
}
|
1999-01-15 19:18:30 +00:00
|
|
|
mMatchAtom = nsnull;
|
|
|
|
mRootContent = aRootContent;
|
|
|
|
mMatchAll = PR_FALSE;
|
1998-07-22 23:32:19 +00:00
|
|
|
Init(aDocument);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsContentList::Init(nsIDocument *aDocument)
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
mScriptObject = nsnull;
|
1999-01-18 03:43:43 +00:00
|
|
|
// We don't reference count the reference to the document
|
1998-07-22 23:32:19 +00:00
|
|
|
// If the document goes away first, we'll be informed and we
|
|
|
|
// can drop our reference.
|
|
|
|
// If we go away first, we'll get rid of ourselves from the
|
|
|
|
// document's observer list.
|
|
|
|
mDocument = aDocument;
|
1999-01-18 03:43:43 +00:00
|
|
|
if (nsnull != mDocument) {
|
|
|
|
mDocument->AddObserver(this);
|
1999-01-15 19:18:30 +00:00
|
|
|
}
|
1999-01-18 03:43:43 +00:00
|
|
|
PopulateSelf();
|
1998-07-22 23:32:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsContentList::~nsContentList()
|
|
|
|
{
|
|
|
|
if (nsnull != mDocument) {
|
|
|
|
mDocument->RemoveObserver(this);
|
|
|
|
}
|
|
|
|
|
1999-01-15 19:18:30 +00:00
|
|
|
NS_IF_RELEASE(mMatchAtom);
|
1999-02-04 23:23:07 +00:00
|
|
|
|
|
|
|
if (nsnull != mData) {
|
|
|
|
delete mData;
|
|
|
|
}
|
1998-07-22 23:32:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static NS_DEFINE_IID(kIDOMNodeListIID, NS_IDOMNODELIST_IID);
|
|
|
|
static NS_DEFINE_IID(kIDOMHTMLCollectionIID, NS_IDOMHTMLCOLLECTION_IID);
|
|
|
|
|
|
|
|
nsresult nsContentList::QueryInterface(REFNSIID aIID, void** aInstancePtr)
|
|
|
|
{
|
|
|
|
if (nsnull == aInstancePtr) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
if (aIID.Equals(kIDOMNodeListIID)) {
|
|
|
|
*aInstancePtr = (void*)(nsIDOMNodeList*)this;
|
1998-09-12 19:33:48 +00:00
|
|
|
NS_ADDREF_THIS();
|
1998-07-22 23:32:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (aIID.Equals(kIDOMHTMLCollectionIID)) {
|
|
|
|
*aInstancePtr = (void*)(nsIDOMHTMLCollection*)this;
|
1998-09-12 19:33:48 +00:00
|
|
|
NS_ADDREF_THIS();
|
1998-07-22 23:32:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (aIID.Equals(kIScriptObjectOwnerIID)) {
|
|
|
|
*aInstancePtr = (void*)(nsIScriptObjectOwner*)this;
|
1998-09-12 19:33:48 +00:00
|
|
|
NS_ADDREF_THIS();
|
1998-07-22 23:32:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (aIID.Equals(kISupportsIID)) {
|
|
|
|
*aInstancePtr = (void*)(nsISupports*)(nsIDOMNodeList*)this;
|
1998-09-12 19:33:48 +00:00
|
|
|
NS_ADDREF_THIS();
|
1998-07-22 23:32:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsContentList)
|
|
|
|
NS_IMPL_RELEASE(nsContentList)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-01-18 03:43:43 +00:00
|
|
|
nsContentList::GetLength(PRUint32* aLength)
|
|
|
|
{
|
|
|
|
nsresult result = CheckDocumentExistence();
|
|
|
|
if (NS_OK == result) {
|
|
|
|
*aLength = mContent.Count();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentList::Item(PRUint32 aIndex, nsIDOMNode** aReturn)
|
|
|
|
{
|
|
|
|
nsresult result = CheckDocumentExistence();
|
|
|
|
if (NS_OK == result) {
|
|
|
|
nsISupports *element = (nsISupports *)mContent.ElementAt(aIndex);
|
|
|
|
|
|
|
|
if (nsnull != element) {
|
|
|
|
result = element->QueryInterface(kIDOMNodeIID, (void **)aReturn);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*aReturn = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentList::NamedItem(const nsString& aName, nsIDOMNode** aReturn)
|
|
|
|
{
|
|
|
|
nsresult result = CheckDocumentExistence();
|
|
|
|
|
|
|
|
if (NS_OK == result) {
|
|
|
|
PRInt32 i, count = mContent.Count();
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
nsIContent *content = (nsIContent *)mContent.ElementAt(i);
|
|
|
|
if (nsnull != content) {
|
|
|
|
nsAutoString name;
|
|
|
|
// XXX Should it be an EqualsIgnoreCase?
|
|
|
|
if (((content->GetAttribute(kNameSpaceID_HTML, nsHTMLAtoms::name, name) == NS_CONTENT_ATTR_HAS_VALUE) &&
|
|
|
|
(aName.Equals(name))) ||
|
|
|
|
((content->GetAttribute(kNameSpaceID_HTML, nsHTMLAtoms::id, name) == NS_CONTENT_ATTR_HAS_VALUE) &&
|
|
|
|
(aName.Equals(name)))) {
|
|
|
|
return content->QueryInterface(kIDOMNodeIID, (void **)aReturn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aReturn = nsnull;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentList::GetScriptObject(nsIScriptContext *aContext, void** aScriptObject)
|
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
nsIScriptGlobalObject *global = aContext->GetGlobalObject();
|
|
|
|
|
|
|
|
if (nsnull == mScriptObject) {
|
|
|
|
nsIDOMScriptObjectFactory *factory;
|
|
|
|
|
|
|
|
res = nsGenericElement::GetScriptObjectFactory(&factory);
|
|
|
|
if (NS_OK != res) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = factory->NewScriptHTMLCollection(aContext,
|
|
|
|
(nsISupports*)(nsIDOMHTMLCollection*)this,
|
|
|
|
global,
|
|
|
|
(void**)&mScriptObject);
|
|
|
|
NS_RELEASE(factory);
|
|
|
|
}
|
|
|
|
*aScriptObject = mScriptObject;
|
|
|
|
|
|
|
|
NS_RELEASE(global);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentList::SetScriptObject(void *aScriptObject)
|
|
|
|
{
|
|
|
|
mScriptObject = aScriptObject;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentList::ContentAppended(nsIDocument *aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
PRInt32 aNewIndexInContainer)
|
|
|
|
{
|
|
|
|
PRInt32 i, count;
|
|
|
|
aContainer->ChildCount(count);
|
|
|
|
if ((count > 0) && IsDescendantOfRoot(aContainer)) {
|
|
|
|
PRBool repopulate = PR_FALSE;
|
|
|
|
for (i = aNewIndexInContainer; i <= count-1; i++) {
|
|
|
|
nsIContent *content;
|
|
|
|
aContainer->ChildAt(i, content);
|
|
|
|
if (mMatchAll || MatchSelf(content)) {
|
|
|
|
repopulate = PR_TRUE;
|
|
|
|
}
|
|
|
|
NS_RELEASE(content);
|
|
|
|
}
|
|
|
|
if (repopulate) {
|
|
|
|
PopulateSelf();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentList::ContentInserted(nsIDocument *aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
|
|
|
PRInt32 aIndexInContainer)
|
|
|
|
{
|
|
|
|
if (IsDescendantOfRoot(aContainer)) {
|
|
|
|
if (mMatchAll || MatchSelf(aChild)) {
|
|
|
|
PopulateSelf();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentList::ContentReplaced(nsIDocument *aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aOldChild,
|
|
|
|
nsIContent* aNewChild,
|
|
|
|
PRInt32 aIndexInContainer)
|
|
|
|
{
|
|
|
|
if (IsDescendantOfRoot(aContainer)) {
|
|
|
|
if (mMatchAll || MatchSelf(aOldChild) || MatchSelf(aNewChild)) {
|
|
|
|
PopulateSelf();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ContainsRoot(aOldChild)) {
|
|
|
|
DisconnectFromDocument();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentList::ContentRemoved(nsIDocument *aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
|
|
|
PRInt32 aIndexInContainer)
|
|
|
|
{
|
|
|
|
if (IsDescendantOfRoot(aContainer) && MatchSelf(aChild)) {
|
|
|
|
PopulateSelf();
|
|
|
|
}
|
|
|
|
else if (ContainsRoot(aChild)) {
|
|
|
|
DisconnectFromDocument();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentList::DocumentWillBeDestroyed(nsIDocument *aDocument)
|
|
|
|
{
|
|
|
|
if (nsnull != mDocument) {
|
|
|
|
aDocument->RemoveObserver(this);
|
|
|
|
mDocument = nsnull;
|
|
|
|
}
|
|
|
|
Reset();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Returns whether the content element matches the
|
|
|
|
// criterion
|
|
|
|
nsresult
|
1998-07-22 23:32:19 +00:00
|
|
|
nsContentList::Match(nsIContent *aContent, PRBool *aMatch)
|
|
|
|
{
|
1999-01-15 19:18:30 +00:00
|
|
|
if (nsnull != mMatchAtom) {
|
1998-08-29 20:20:38 +00:00
|
|
|
nsIAtom *name = nsnull;
|
|
|
|
aContent->GetTag(name);
|
1999-01-15 19:18:30 +00:00
|
|
|
|
1999-01-18 03:43:43 +00:00
|
|
|
// If we have to match all, only do those that have
|
|
|
|
// a tagName i.e. only the elements.
|
1999-01-22 22:48:00 +00:00
|
|
|
if (mMatchAll && (nsLayoutAtoms::textTagName != name) &&
|
1999-03-31 20:49:25 +00:00
|
|
|
(nsLayoutAtoms::commentTagName != name) &&
|
|
|
|
(nsLayoutAtoms::processingInstructionTagName != name)) {
|
1999-01-15 19:18:30 +00:00
|
|
|
*aMatch = PR_TRUE;
|
|
|
|
}
|
|
|
|
// XXX We don't yet match on namespace. Maybe we should??
|
|
|
|
else if (name == mMatchAtom) {
|
1998-07-22 23:32:19 +00:00
|
|
|
*aMatch = PR_TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*aMatch = PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IF_RELEASE(name);
|
|
|
|
}
|
|
|
|
else if (nsnull != mFunc) {
|
1999-02-04 23:23:07 +00:00
|
|
|
*aMatch = (*mFunc)(aContent, mData);
|
1998-07-22 23:32:19 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
*aMatch = PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-01-18 03:43:43 +00:00
|
|
|
nsresult
|
1998-07-22 23:32:19 +00:00
|
|
|
nsContentList::Add(nsIContent *aContent)
|
|
|
|
{
|
1999-01-15 19:18:30 +00:00
|
|
|
// Shouldn't hold a reference since we'll be
|
|
|
|
// told when the content leaves the document or
|
|
|
|
// the document will be destroyed.
|
1998-07-22 23:32:19 +00:00
|
|
|
mContent.AppendElement(aContent);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-01-18 03:43:43 +00:00
|
|
|
nsresult
|
1998-07-22 23:32:19 +00:00
|
|
|
nsContentList::Remove(nsIContent *aContent)
|
|
|
|
{
|
|
|
|
mContent.RemoveElement(aContent);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-01-18 03:43:43 +00:00
|
|
|
nsresult
|
1998-07-22 23:32:19 +00:00
|
|
|
nsContentList::Reset()
|
|
|
|
{
|
|
|
|
mContent.Clear();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-01-18 03:43:43 +00:00
|
|
|
// If we were created outside the context of a document and we
|
|
|
|
// have root content, then check if our content has been added
|
|
|
|
// to a document yet. If so, we'll become an observer of the document.
|
|
|
|
nsresult
|
|
|
|
nsContentList::CheckDocumentExistence()
|
1998-07-22 23:32:19 +00:00
|
|
|
{
|
1999-01-18 03:43:43 +00:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
if ((nsnull == mDocument) && (nsnull != mRootContent)) {
|
|
|
|
result = mRootContent->GetDocument(mDocument);
|
|
|
|
if (nsnull != mDocument) {
|
|
|
|
mDocument->AddObserver(this);
|
|
|
|
PopulateSelf();
|
1998-07-22 23:32:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-18 03:43:43 +00:00
|
|
|
return result;
|
1998-07-22 23:32:19 +00:00
|
|
|
}
|
|
|
|
|
1999-01-18 03:43:43 +00:00
|
|
|
// Match recursively. See if anything in the subtree
|
|
|
|
// matches the criterion.
|
|
|
|
PRBool
|
|
|
|
nsContentList::MatchSelf(nsIContent *aContent)
|
1998-07-22 23:32:19 +00:00
|
|
|
{
|
|
|
|
PRBool match;
|
|
|
|
PRInt32 i, count;
|
|
|
|
|
|
|
|
Match(aContent, &match);
|
|
|
|
if (match) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
1998-08-29 20:20:38 +00:00
|
|
|
aContent->ChildCount(count);
|
1998-07-22 23:32:19 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
1998-08-29 20:20:38 +00:00
|
|
|
nsIContent *child;
|
|
|
|
aContent->ChildAt(i, child);
|
1998-07-22 23:32:19 +00:00
|
|
|
if (MatchSelf(child)) {
|
|
|
|
NS_RELEASE(child);
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
NS_RELEASE(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
1999-01-18 03:43:43 +00:00
|
|
|
// Add all elements in this subtree that match to
|
|
|
|
// our list.
|
|
|
|
void
|
|
|
|
nsContentList::PopulateWith(nsIContent *aContent, PRBool aIncludeRoot)
|
1998-07-22 23:32:19 +00:00
|
|
|
{
|
|
|
|
PRBool match;
|
|
|
|
PRInt32 i, count;
|
|
|
|
|
1999-01-18 03:43:43 +00:00
|
|
|
if (aIncludeRoot) {
|
|
|
|
Match(aContent, &match);
|
|
|
|
if (match) {
|
|
|
|
Add(aContent);
|
|
|
|
}
|
1998-07-22 23:32:19 +00:00
|
|
|
}
|
|
|
|
|
1998-08-29 20:20:38 +00:00
|
|
|
aContent->ChildCount(count);
|
1998-07-22 23:32:19 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
1998-08-29 20:20:38 +00:00
|
|
|
nsIContent *child;
|
|
|
|
aContent->ChildAt(i, child);
|
1999-01-18 03:43:43 +00:00
|
|
|
PopulateWith(child, PR_TRUE);
|
1998-07-22 23:32:19 +00:00
|
|
|
NS_RELEASE(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-18 03:43:43 +00:00
|
|
|
// Clear out our old list and build up a new one
|
|
|
|
void
|
|
|
|
nsContentList::PopulateSelf()
|
1998-07-22 23:32:19 +00:00
|
|
|
{
|
1999-01-18 03:43:43 +00:00
|
|
|
Reset();
|
|
|
|
if (nsnull != mRootContent) {
|
|
|
|
PopulateWith(mRootContent, PR_FALSE);
|
1998-07-22 23:32:19 +00:00
|
|
|
}
|
1999-01-18 03:43:43 +00:00
|
|
|
else if (nsnull != mDocument) {
|
|
|
|
nsIContent *root;
|
|
|
|
root = mDocument->GetRootContent();
|
|
|
|
PopulateWith(root, PR_TRUE);
|
1998-07-22 23:32:19 +00:00
|
|
|
NS_RELEASE(root);
|
|
|
|
}
|
|
|
|
}
|
1999-01-18 03:43:43 +00:00
|
|
|
|
|
|
|
// Is the specified element a descendant of the root? If there
|
|
|
|
// is no root, then yes. Otherwise keep tracing up the tree from
|
|
|
|
// the element till we find our root, or until we reach the
|
|
|
|
// document root.
|
|
|
|
PRBool
|
|
|
|
nsContentList::IsDescendantOfRoot(nsIContent* aContainer)
|
1998-07-22 23:32:19 +00:00
|
|
|
{
|
1999-01-18 03:43:43 +00:00
|
|
|
if (nsnull == mRootContent) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
else if (mRootContent == aContainer) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
else if (nsnull == aContainer) {
|
|
|
|
return PR_FALSE;
|
1998-07-22 23:32:19 +00:00
|
|
|
}
|
1999-01-18 03:43:43 +00:00
|
|
|
else {
|
|
|
|
nsIContent* parent;
|
|
|
|
PRBool ret;
|
1998-07-22 23:32:19 +00:00
|
|
|
|
1999-01-18 03:43:43 +00:00
|
|
|
aContainer->GetParent(parent);
|
|
|
|
ret = IsDescendantOfRoot(parent);
|
|
|
|
NS_IF_RELEASE(parent);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
1998-07-22 23:32:19 +00:00
|
|
|
}
|
|
|
|
|
1999-01-18 03:43:43 +00:00
|
|
|
// Does this subtree contain the root?
|
|
|
|
PRBool
|
|
|
|
nsContentList::ContainsRoot(nsIContent* aContent)
|
1998-07-22 23:32:19 +00:00
|
|
|
{
|
1999-01-18 03:43:43 +00:00
|
|
|
if (nsnull == mRootContent) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
else if (mRootContent == aContent) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PRInt32 i, count;
|
|
|
|
|
|
|
|
aContent->ChildCount(count);
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
nsIContent *child;
|
|
|
|
aContent->ChildAt(i, child);
|
|
|
|
if (ContainsRoot(child)) {
|
|
|
|
NS_RELEASE(child);
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
NS_RELEASE(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_FALSE;
|
1998-07-22 23:32:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-18 03:43:43 +00:00
|
|
|
// Our root content has been disconnected from the
|
|
|
|
// document, so stop observing. The list then becomes
|
|
|
|
// a snapshot rather than a dynamic list.
|
|
|
|
void
|
|
|
|
nsContentList::DisconnectFromDocument()
|
1998-07-22 23:32:19 +00:00
|
|
|
{
|
|
|
|
if (nsnull != mDocument) {
|
1999-01-18 03:43:43 +00:00
|
|
|
mDocument->RemoveObserver(this);
|
1998-07-22 23:32:19 +00:00
|
|
|
mDocument = nsnull;
|
|
|
|
}
|
|
|
|
}
|