2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 sw=2 et tw=78: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla 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/MPL/
|
|
|
|
*
|
|
|
|
* 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.org 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 of 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 MPL, 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 MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nsBaseContentList is a basic list of content nodes; nsContentList
|
|
|
|
* is a commonly used NodeList implementation (used for
|
|
|
|
* getElementsByTagName, some properties on nsIDOMHTMLDocument, etc).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsContentList.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsGenericElement.h"
|
|
|
|
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
|
2011-05-23 08:39:52 -07:00
|
|
|
#include "dombindings.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Form related includes
|
|
|
|
#include "nsIDOMHTMLFormElement.h"
|
|
|
|
|
|
|
|
#include "pldhash.h"
|
|
|
|
|
|
|
|
#ifdef DEBUG_CONTENT_LIST
|
|
|
|
#include "nsIContentIterator.h"
|
|
|
|
nsresult
|
|
|
|
NS_NewPreContentIterator(nsIContentIterator** aInstancePtrResult);
|
|
|
|
#define ASSERT_IN_SYNC AssertInSync()
|
|
|
|
#else
|
|
|
|
#define ASSERT_IN_SYNC PR_BEGIN_MACRO PR_END_MACRO
|
|
|
|
#endif
|
|
|
|
|
2010-04-30 06:12:05 -07:00
|
|
|
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsBaseContentList::~nsBaseContentList()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsBaseContentList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsBaseContentList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMARRAY(mElements)
|
2011-05-26 12:58:35 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsBaseContentList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMARRAY(mElements)
|
2011-05-26 12:58:35 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2011-05-26 12:58:35 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsBaseContentList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-11-03 02:31:47 -08:00
|
|
|
#define NS_CONTENT_LIST_INTERFACES(_class) \
|
|
|
|
NS_INTERFACE_TABLE_ENTRY(_class, nsINodeList) \
|
|
|
|
NS_INTERFACE_TABLE_ENTRY(_class, nsIDOMNodeList)
|
|
|
|
|
2010-01-12 05:08:43 -08:00
|
|
|
DOMCI_DATA(NodeList, nsBaseContentList)
|
2008-11-03 02:31:47 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// QueryInterface implementation for nsBaseContentList
|
2008-11-03 02:31:47 -08:00
|
|
|
NS_INTERFACE_TABLE_HEAD(nsBaseContentList)
|
2009-06-15 23:32:10 -07:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2008-11-03 02:31:47 -08:00
|
|
|
NS_NODELIST_OFFSET_AND_INTERFACE_TABLE_BEGIN(nsBaseContentList)
|
|
|
|
NS_CONTENT_LIST_INTERFACES(nsBaseContentList)
|
|
|
|
NS_OFFSET_AND_INTERFACE_TABLE_END
|
|
|
|
NS_OFFSET_AND_INTERFACE_TABLE_TO_MAP_SEGUE
|
|
|
|
NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(nsBaseContentList)
|
2010-03-17 08:09:05 -07:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(NodeList)
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsBaseContentList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsBaseContentList)
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseContentList::GetLength(PRUint32* aLength)
|
|
|
|
{
|
|
|
|
*aLength = mElements.Count();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseContentList::Item(PRUint32 aIndex, nsIDOMNode** aReturn)
|
|
|
|
{
|
2008-09-10 20:15:29 -07:00
|
|
|
nsISupports *tmp = GetNodeAt(aIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!tmp) {
|
|
|
|
*aReturn = nsnull;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CallQueryInterface(tmp, aReturn);
|
|
|
|
}
|
|
|
|
|
2009-01-29 11:46:18 -08:00
|
|
|
nsIContent*
|
2008-09-10 20:15:29 -07:00
|
|
|
nsBaseContentList::GetNodeAt(PRUint32 aIndex)
|
|
|
|
{
|
|
|
|
return mElements.SafeObjectAt(aIndex);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
PRInt32
|
2011-09-28 23:19:26 -07:00
|
|
|
nsBaseContentList::IndexOf(nsIContent *aContent, bool aDoFlush)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return mElements.IndexOf(aContent);
|
|
|
|
}
|
|
|
|
|
2009-01-29 11:46:18 -08:00
|
|
|
PRInt32
|
|
|
|
nsBaseContentList::IndexOf(nsIContent* aContent)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return IndexOf(aContent, true);
|
2009-01-29 11:46:18 -08:00
|
|
|
}
|
|
|
|
|
2009-07-24 06:33:20 -07:00
|
|
|
void nsBaseContentList::AppendElement(nsIContent *aContent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-07-24 06:33:20 -07:00
|
|
|
mElements.AppendObject(aContent);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-07-24 06:33:20 -07:00
|
|
|
void nsBaseContentList::RemoveElement(nsIContent *aContent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-07-24 06:33:20 -07:00
|
|
|
mElements.RemoveObject(aContent);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-07-24 06:33:20 -07:00
|
|
|
void nsBaseContentList::InsertElementAt(nsIContent* aContent, PRInt32 aIndex)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aContent, "Element to insert must not be null");
|
|
|
|
mElements.InsertObjectAt(aContent, aIndex);
|
|
|
|
}
|
|
|
|
|
2009-06-15 23:32:10 -07:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsSimpleContentList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsSimpleContentList,
|
|
|
|
nsBaseContentList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mRoot)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsSimpleContentList,
|
|
|
|
nsBaseContentList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mRoot)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsSimpleContentList)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsBaseContentList)
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(nsSimpleContentList, nsBaseContentList)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsSimpleContentList, nsBaseContentList)
|
|
|
|
|
2011-05-31 14:47:17 -07:00
|
|
|
JSObject*
|
2011-06-29 10:17:37 -07:00
|
|
|
nsSimpleContentList::WrapObject(JSContext *cx, XPCWrappedNativeScope *scope,
|
2011-08-20 06:53:32 -07:00
|
|
|
bool *triedToWrap)
|
2011-05-31 14:47:17 -07:00
|
|
|
{
|
2011-08-20 06:53:33 -07:00
|
|
|
return mozilla::dom::binding::NodeList::create(cx, scope, this, triedToWrap);
|
2011-05-31 14:47:17 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// nsFormContentList
|
|
|
|
|
2009-06-15 23:32:10 -07:00
|
|
|
nsFormContentList::nsFormContentList(nsIContent *aForm,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsBaseContentList& aContentList)
|
2009-06-15 23:32:10 -07:00
|
|
|
: nsSimpleContentList(aForm)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
|
|
|
|
// move elements that belong to mForm into this content list
|
|
|
|
|
|
|
|
PRUint32 i, length = 0;
|
|
|
|
aContentList.GetLength(&length);
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++) {
|
2010-05-09 11:32:57 -07:00
|
|
|
nsIContent *c = aContentList.GetNodeAt(i);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (c && nsContentUtils::BelongsInForm(aForm, c)) {
|
|
|
|
AppendElement(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hashtable for storing nsContentLists
|
|
|
|
static PLDHashTable gContentListHashTable;
|
|
|
|
|
|
|
|
struct ContentListHashEntry : public PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
nsContentList* mContentList;
|
|
|
|
};
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static PLDHashNumber
|
2007-03-22 10:30:00 -07:00
|
|
|
ContentListHashtableHashKey(PLDHashTable *table, const void *key)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
const nsContentListKey* list = static_cast<const nsContentListKey *>(key);
|
2007-03-22 10:30:00 -07:00
|
|
|
return list->GetHash();
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2007-03-22 10:30:00 -07:00
|
|
|
ContentListHashtableMatchEntry(PLDHashTable *table,
|
|
|
|
const PLDHashEntryHdr *entry,
|
|
|
|
const void *key)
|
|
|
|
{
|
|
|
|
const ContentListHashEntry *e =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<const ContentListHashEntry *>(entry);
|
2010-11-23 11:10:56 -08:00
|
|
|
const nsContentList* list = e->mContentList;
|
|
|
|
const nsContentListKey* ourKey = static_cast<const nsContentListKey *>(key);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-23 11:10:56 -08:00
|
|
|
return list->MatchesKey(*ourKey);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsContentList>
|
2010-08-31 19:47:00 -07:00
|
|
|
NS_GetContentList(nsINode* aRootNode,
|
|
|
|
PRInt32 aMatchNameSpaceId,
|
2010-11-23 11:10:56 -08:00
|
|
|
const nsAString& aTagname)
|
2010-08-31 19:47:00 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aRootNode, "content list has to have a root");
|
|
|
|
|
|
|
|
nsContentList* list = nsnull;
|
|
|
|
|
|
|
|
static PLDHashTableOps hash_table_ops =
|
|
|
|
{
|
|
|
|
PL_DHashAllocTable,
|
|
|
|
PL_DHashFreeTable,
|
|
|
|
ContentListHashtableHashKey,
|
|
|
|
ContentListHashtableMatchEntry,
|
|
|
|
PL_DHashMoveEntryStub,
|
|
|
|
PL_DHashClearEntryStub,
|
|
|
|
PL_DHashFinalizeStub
|
|
|
|
};
|
|
|
|
|
|
|
|
// Initialize the hashtable if needed.
|
|
|
|
if (!gContentListHashTable.ops) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool success = PL_DHashTableInit(&gContentListHashTable,
|
2007-03-22 10:30:00 -07:00
|
|
|
&hash_table_ops, nsnull,
|
|
|
|
sizeof(ContentListHashEntry),
|
|
|
|
16);
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
gContentListHashTable.ops = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ContentListHashEntry *entry = nsnull;
|
|
|
|
// First we look in our hashtable. Then we create a content list if needed
|
|
|
|
if (gContentListHashTable.ops) {
|
2010-11-23 11:10:56 -08:00
|
|
|
nsContentListKey hashKey(aRootNode, aMatchNameSpaceId, aTagname);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// A PL_DHASH_ADD is equivalent to a PL_DHASH_LOOKUP for cases
|
|
|
|
// when the entry is already in the hashtable.
|
2007-07-08 00:08:04 -07:00
|
|
|
entry = static_cast<ContentListHashEntry *>
|
|
|
|
(PL_DHashTableOperate(&gContentListHashTable,
|
2010-11-23 11:10:56 -08:00
|
|
|
&hashKey,
|
|
|
|
PL_DHASH_ADD));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (entry)
|
|
|
|
list = entry->mContentList;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!list) {
|
|
|
|
// We need to create a ContentList and add it to our new entry, if
|
|
|
|
// we have an entry
|
2010-11-23 11:10:56 -08:00
|
|
|
nsCOMPtr<nsIAtom> xmlAtom = do_GetAtom(aTagname);
|
|
|
|
nsCOMPtr<nsIAtom> htmlAtom;
|
|
|
|
if (aMatchNameSpaceId == kNameSpaceID_Unknown) {
|
|
|
|
nsAutoString lowercaseName;
|
|
|
|
nsContentUtils::ASCIIToLower(aTagname, lowercaseName);
|
|
|
|
htmlAtom = do_GetAtom(lowercaseName);
|
|
|
|
} else {
|
|
|
|
htmlAtom = xmlAtom;
|
|
|
|
}
|
2010-08-31 19:47:00 -07:00
|
|
|
list = new nsContentList(aRootNode, aMatchNameSpaceId,
|
2010-11-23 11:10:56 -08:00
|
|
|
htmlAtom, xmlAtom);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (entry) {
|
2010-08-31 19:47:00 -07:00
|
|
|
entry->mContentList = list;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(list);
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2010-02-09 09:09:06 -08:00
|
|
|
// Hashtable for storing nsCacheableFuncStringContentList
|
|
|
|
static PLDHashTable gFuncStringContentListHashTable;
|
|
|
|
|
|
|
|
struct FuncStringContentListHashEntry : public PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
nsCacheableFuncStringContentList* mContentList;
|
|
|
|
};
|
|
|
|
|
|
|
|
static PLDHashNumber
|
|
|
|
FuncStringContentListHashtableHashKey(PLDHashTable *table, const void *key)
|
|
|
|
{
|
|
|
|
const nsFuncStringCacheKey* funcStringKey =
|
|
|
|
static_cast<const nsFuncStringCacheKey *>(key);
|
|
|
|
return funcStringKey->GetHash();
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2010-02-09 09:09:06 -08:00
|
|
|
FuncStringContentListHashtableMatchEntry(PLDHashTable *table,
|
|
|
|
const PLDHashEntryHdr *entry,
|
|
|
|
const void *key)
|
|
|
|
{
|
|
|
|
const FuncStringContentListHashEntry *e =
|
|
|
|
static_cast<const FuncStringContentListHashEntry *>(entry);
|
|
|
|
const nsFuncStringCacheKey* ourKey =
|
|
|
|
static_cast<const nsFuncStringCacheKey *>(key);
|
|
|
|
|
|
|
|
return e->mContentList->Equals(ourKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsContentList>
|
|
|
|
NS_GetFuncStringContentList(nsINode* aRootNode,
|
|
|
|
nsContentListMatchFunc aFunc,
|
|
|
|
nsContentListDestroyFunc aDestroyFunc,
|
2010-06-08 12:58:26 -07:00
|
|
|
nsFuncStringContentListDataAllocator aDataAllocator,
|
2010-02-09 09:09:06 -08:00
|
|
|
const nsAString& aString)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aRootNode, "content list has to have a root");
|
|
|
|
|
|
|
|
nsCacheableFuncStringContentList* list = nsnull;
|
|
|
|
|
|
|
|
static PLDHashTableOps hash_table_ops =
|
|
|
|
{
|
|
|
|
PL_DHashAllocTable,
|
|
|
|
PL_DHashFreeTable,
|
|
|
|
FuncStringContentListHashtableHashKey,
|
|
|
|
FuncStringContentListHashtableMatchEntry,
|
|
|
|
PL_DHashMoveEntryStub,
|
|
|
|
PL_DHashClearEntryStub,
|
|
|
|
PL_DHashFinalizeStub
|
|
|
|
};
|
|
|
|
|
|
|
|
// Initialize the hashtable if needed.
|
|
|
|
if (!gFuncStringContentListHashTable.ops) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool success = PL_DHashTableInit(&gFuncStringContentListHashTable,
|
2010-02-09 09:09:06 -08:00
|
|
|
&hash_table_ops, nsnull,
|
|
|
|
sizeof(FuncStringContentListHashEntry),
|
|
|
|
16);
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
gFuncStringContentListHashTable.ops = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FuncStringContentListHashEntry *entry = nsnull;
|
|
|
|
// First we look in our hashtable. Then we create a content list if needed
|
|
|
|
if (gFuncStringContentListHashTable.ops) {
|
|
|
|
nsFuncStringCacheKey hashKey(aRootNode, aFunc, aString);
|
|
|
|
|
|
|
|
// A PL_DHASH_ADD is equivalent to a PL_DHASH_LOOKUP for cases
|
|
|
|
// when the entry is already in the hashtable.
|
|
|
|
entry = static_cast<FuncStringContentListHashEntry *>
|
|
|
|
(PL_DHashTableOperate(&gFuncStringContentListHashTable,
|
|
|
|
&hashKey,
|
|
|
|
PL_DHASH_ADD));
|
|
|
|
if (entry)
|
|
|
|
list = entry->mContentList;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!list) {
|
|
|
|
// We need to create a ContentList and add it to our new entry, if
|
|
|
|
// we have an entry
|
2010-06-08 12:58:26 -07:00
|
|
|
list = new nsCacheableFuncStringContentList(aRootNode, aFunc, aDestroyFunc,
|
|
|
|
aDataAllocator, aString);
|
|
|
|
if (list && !list->AllocatedData()) {
|
|
|
|
// Failed to allocate the data
|
|
|
|
delete list;
|
|
|
|
list = nsnull;
|
|
|
|
}
|
|
|
|
|
2010-02-09 09:09:06 -08:00
|
|
|
if (entry) {
|
|
|
|
if (list)
|
|
|
|
entry->mContentList = list;
|
|
|
|
else
|
|
|
|
PL_DHashTableRawRemove(&gContentListHashTable, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(list, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(list);
|
|
|
|
|
|
|
|
// Don't cache these lists globally
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// nsContentList implementation
|
|
|
|
|
|
|
|
nsContentList::nsContentList(nsINode* aRootNode,
|
|
|
|
PRInt32 aMatchNameSpaceId,
|
2010-08-31 19:47:00 -07:00
|
|
|
nsIAtom* aHTMLMatchAtom,
|
|
|
|
nsIAtom* aXMLMatchAtom,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aDeep)
|
2007-03-22 10:30:00 -07:00
|
|
|
: nsBaseContentList(),
|
2010-11-23 11:10:56 -08:00
|
|
|
mRootNode(aRootNode),
|
|
|
|
mMatchNameSpaceId(aMatchNameSpaceId),
|
|
|
|
mHTMLMatchAtom(aHTMLMatchAtom),
|
|
|
|
mXMLMatchAtom(aXMLMatchAtom),
|
2007-03-22 10:30:00 -07:00
|
|
|
mFunc(nsnull),
|
|
|
|
mDestroyFunc(nsnull),
|
|
|
|
mData(nsnull),
|
|
|
|
mState(LIST_DIRTY),
|
|
|
|
mDeep(aDeep),
|
2011-10-17 07:59:28 -07:00
|
|
|
mFuncMayDependOnAttr(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mRootNode, "Must have root");
|
2010-08-31 19:47:00 -07:00
|
|
|
if (nsGkAtoms::_asterix == mHTMLMatchAtom) {
|
|
|
|
NS_ASSERTION(mXMLMatchAtom == nsGkAtoms::_asterix, "HTML atom and XML atom are not both asterix?");
|
2011-10-17 07:59:28 -07:00
|
|
|
mMatchAll = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
2011-10-17 07:59:28 -07:00
|
|
|
mMatchAll = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
mRootNode->AddMutationObserver(this);
|
2010-09-28 10:46:08 -07:00
|
|
|
|
|
|
|
// We only need to flush if we're in an non-HTML document, since the
|
|
|
|
// HTML5 parser doesn't need flushing. Further, if we're not in a
|
|
|
|
// document at all right now (in the GetCurrentDoc() sense), we're
|
|
|
|
// not parser-created and don't need to be flushing stuff under us
|
|
|
|
// to get our kids right.
|
|
|
|
nsIDocument* doc = mRootNode->GetCurrentDoc();
|
|
|
|
mFlushesNeeded = doc && !doc->IsHTML();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsContentList::nsContentList(nsINode* aRootNode,
|
|
|
|
nsContentListMatchFunc aFunc,
|
|
|
|
nsContentListDestroyFunc aDestroyFunc,
|
|
|
|
void* aData,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aDeep,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIAtom* aMatchAtom,
|
|
|
|
PRInt32 aMatchNameSpaceId,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aFuncMayDependOnAttr)
|
2007-03-22 10:30:00 -07:00
|
|
|
: nsBaseContentList(),
|
2010-11-23 11:10:56 -08:00
|
|
|
mRootNode(aRootNode),
|
|
|
|
mMatchNameSpaceId(aMatchNameSpaceId),
|
|
|
|
mHTMLMatchAtom(aMatchAtom),
|
|
|
|
mXMLMatchAtom(aMatchAtom),
|
2007-03-22 10:30:00 -07:00
|
|
|
mFunc(aFunc),
|
|
|
|
mDestroyFunc(aDestroyFunc),
|
|
|
|
mData(aData),
|
|
|
|
mState(LIST_DIRTY),
|
2011-10-17 07:59:28 -07:00
|
|
|
mMatchAll(false),
|
2007-03-22 10:30:00 -07:00
|
|
|
mDeep(aDeep),
|
|
|
|
mFuncMayDependOnAttr(aFuncMayDependOnAttr)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mRootNode, "Must have root");
|
|
|
|
mRootNode->AddMutationObserver(this);
|
2011-03-28 21:41:01 -07:00
|
|
|
|
|
|
|
// We only need to flush if we're in an non-HTML document, since the
|
|
|
|
// HTML5 parser doesn't need flushing. Further, if we're not in a
|
|
|
|
// document at all right now (in the GetCurrentDoc() sense), we're
|
|
|
|
// not parser-created and don't need to be flushing stuff under us
|
|
|
|
// to get our kids right.
|
|
|
|
nsIDocument* doc = mRootNode->GetCurrentDoc();
|
|
|
|
mFlushesNeeded = doc && !doc->IsHTML();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsContentList::~nsContentList()
|
|
|
|
{
|
|
|
|
RemoveFromHashtable();
|
|
|
|
if (mRootNode) {
|
|
|
|
mRootNode->RemoveMutationObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mDestroyFunc) {
|
|
|
|
// Clean up mData
|
|
|
|
(*mDestroyFunc)(mData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-23 08:39:52 -07:00
|
|
|
JSObject*
|
2011-06-29 10:17:37 -07:00
|
|
|
nsContentList::WrapObject(JSContext *cx, XPCWrappedNativeScope *scope,
|
2011-08-20 06:53:32 -07:00
|
|
|
bool *triedToWrap)
|
2011-05-23 08:39:52 -07:00
|
|
|
{
|
2011-09-22 08:50:16 -07:00
|
|
|
return mozilla::dom::binding::HTMLCollection::create(cx, scope, this,
|
2011-08-20 06:53:33 -07:00
|
|
|
triedToWrap);
|
2011-05-23 08:39:52 -07:00
|
|
|
}
|
|
|
|
|
2010-01-12 05:08:43 -08:00
|
|
|
DOMCI_DATA(ContentList, nsContentList)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// QueryInterface implementation for nsContentList
|
2008-11-03 02:31:47 -08:00
|
|
|
NS_INTERFACE_TABLE_HEAD(nsContentList)
|
|
|
|
NS_NODELIST_OFFSET_AND_INTERFACE_TABLE_BEGIN(nsContentList)
|
|
|
|
NS_CONTENT_LIST_INTERFACES(nsContentList)
|
|
|
|
NS_INTERFACE_TABLE_ENTRY(nsContentList, nsIHTMLCollection)
|
|
|
|
NS_INTERFACE_TABLE_ENTRY(nsContentList, nsIDOMHTMLCollection)
|
|
|
|
NS_INTERFACE_TABLE_ENTRY(nsContentList, nsIMutationObserver)
|
|
|
|
NS_OFFSET_AND_INTERFACE_TABLE_END
|
|
|
|
NS_OFFSET_AND_INTERFACE_TABLE_TO_MAP_SEGUE
|
2010-03-17 08:09:05 -07:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(ContentList)
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsBaseContentList)
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(nsContentList, nsBaseContentList)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsContentList, nsBaseContentList)
|
|
|
|
|
|
|
|
PRUint32
|
2011-09-28 23:19:26 -07:00
|
|
|
nsContentList::Length(bool aDoFlush)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
BringSelfUpToDate(aDoFlush);
|
|
|
|
|
|
|
|
return mElements.Count();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent *
|
2011-09-28 23:19:26 -07:00
|
|
|
nsContentList::Item(PRUint32 aIndex, bool aDoFlush)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-09-28 10:46:08 -07:00
|
|
|
if (mRootNode && aDoFlush && mFlushesNeeded) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// XXX sXBL/XBL2 issue
|
|
|
|
nsIDocument* doc = mRootNode->GetCurrentDoc();
|
|
|
|
if (doc) {
|
|
|
|
// Flush pending content changes Bug 4891.
|
|
|
|
doc->FlushPendingNotifications(Flush_ContentAndNotify);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState != LIST_UP_TO_DATE)
|
2011-07-26 07:01:31 -07:00
|
|
|
PopulateSelf(NS_MIN(aIndex, PR_UINT32_MAX - 1) + 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
ASSERT_IN_SYNC;
|
|
|
|
NS_ASSERTION(!mRootNode || mState != LIST_DIRTY,
|
|
|
|
"PopulateSelf left the list in a dirty (useless) state!");
|
|
|
|
|
|
|
|
return mElements.SafeObjectAt(aIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent *
|
2011-09-28 23:19:26 -07:00
|
|
|
nsContentList::NamedItem(const nsAString& aName, bool aDoFlush)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
BringSelfUpToDate(aDoFlush);
|
|
|
|
|
|
|
|
PRInt32 i, count = mElements.Count();
|
|
|
|
|
|
|
|
// Typically IDs and names are atomized
|
|
|
|
nsCOMPtr<nsIAtom> name = do_GetAtom(aName);
|
|
|
|
NS_ENSURE_TRUE(name, nsnull);
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
nsIContent *content = mElements[i];
|
|
|
|
// XXX Should this pass eIgnoreCase?
|
|
|
|
if (content &&
|
|
|
|
(content->AttrValueIs(kNameSpaceID_None, nsGkAtoms::name,
|
|
|
|
name, eCaseMatters) ||
|
|
|
|
content->AttrValueIs(kNameSpaceID_None, nsGkAtoms::id,
|
|
|
|
name, eCaseMatters))) {
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
2011-09-28 23:19:26 -07:00
|
|
|
nsContentList::IndexOf(nsIContent *aContent, bool aDoFlush)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
BringSelfUpToDate(aDoFlush);
|
|
|
|
|
|
|
|
return mElements.IndexOf(aContent);
|
|
|
|
}
|
|
|
|
|
2009-01-29 11:46:18 -08:00
|
|
|
PRInt32
|
|
|
|
nsContentList::IndexOf(nsIContent* aContent)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return IndexOf(aContent, true);
|
2009-01-29 11:46:18 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
nsContentList::NodeWillBeDestroyed(const nsINode* aNode)
|
|
|
|
{
|
|
|
|
// We shouldn't do anything useful from now on
|
|
|
|
|
2010-02-09 09:09:06 -08:00
|
|
|
RemoveFromCaches();
|
2007-03-22 10:30:00 -07:00
|
|
|
mRootNode = nsnull;
|
|
|
|
|
|
|
|
// We will get no more updates, so we can never know we're up to
|
|
|
|
// date
|
|
|
|
SetDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentList::GetLength(PRUint32* aLength)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
*aLength = Length(true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentList::Item(PRUint32 aIndex, nsIDOMNode** aReturn)
|
|
|
|
{
|
2008-09-10 20:15:29 -07:00
|
|
|
nsINode* node = GetNodeAt(aIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-09-10 20:15:29 -07:00
|
|
|
if (node) {
|
|
|
|
return CallQueryInterface(node, aReturn);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
*aReturn = nsnull;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentList::NamedItem(const nsAString& aName, nsIDOMNode** aReturn)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
nsIContent *content = NamedItem(aName, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (content) {
|
|
|
|
return CallQueryInterface(content, aReturn);
|
|
|
|
}
|
|
|
|
|
|
|
|
*aReturn = nsnull;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-01-29 11:46:18 -08:00
|
|
|
nsIContent*
|
2008-09-10 20:15:29 -07:00
|
|
|
nsContentList::GetNodeAt(PRUint32 aIndex)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return Item(aIndex, true);
|
2008-09-10 20:15:29 -07:00
|
|
|
}
|
|
|
|
|
2008-10-31 14:40:35 -07:00
|
|
|
nsISupports*
|
2011-04-25 10:46:59 -07:00
|
|
|
nsContentList::GetNamedItem(const nsAString& aName, nsWrapperCache **aCache)
|
2008-10-31 14:40:35 -07:00
|
|
|
{
|
2010-05-09 12:41:19 -07:00
|
|
|
nsIContent *item;
|
2011-10-17 07:59:28 -07:00
|
|
|
*aCache = item = NamedItem(aName, true);
|
2010-05-09 12:41:19 -07:00
|
|
|
return item;
|
2008-10-31 14:40:35 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
2010-08-24 00:05:56 -07:00
|
|
|
nsContentList::AttributeChanged(nsIDocument *aDocument, Element* aElement,
|
2007-03-22 10:30:00 -07:00
|
|
|
PRInt32 aNameSpaceID, nsIAtom* aAttribute,
|
2009-12-10 14:36:04 -08:00
|
|
|
PRInt32 aModType)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-08-24 00:05:56 -07:00
|
|
|
NS_PRECONDITION(aElement, "Must have a content node to work with");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!mFunc || !mFuncMayDependOnAttr || mState == LIST_DIRTY ||
|
2010-08-24 00:05:56 -07:00
|
|
|
!MayContainRelevantNodes(aElement->GetNodeParent()) ||
|
|
|
|
!nsContentUtils::IsInSameAnonymousTree(mRootNode, aElement)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// Either we're already dirty or this notification doesn't affect
|
2010-08-24 00:05:56 -07:00
|
|
|
// whether we might match aElement.
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-24 00:05:56 -07:00
|
|
|
if (Match(aElement)) {
|
|
|
|
if (mElements.IndexOf(aElement) == -1) {
|
|
|
|
// We match aElement now, and it's not in our list already. Just dirty
|
2007-03-22 10:30:00 -07:00
|
|
|
// ourselves; this is simpler than trying to figure out where to insert
|
2010-08-24 00:05:56 -07:00
|
|
|
// aElement.
|
2007-03-22 10:30:00 -07:00
|
|
|
SetDirty();
|
|
|
|
}
|
|
|
|
} else {
|
2010-08-24 00:05:56 -07:00
|
|
|
// We no longer match aElement. Remove it from our list. If it's
|
2007-03-22 10:30:00 -07:00
|
|
|
// already not there, this is a no-op (though a potentially
|
|
|
|
// expensive one). Either way, no change of mState is required
|
|
|
|
// here.
|
2010-08-24 00:05:56 -07:00
|
|
|
mElements.RemoveObject(aElement);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-08-24 00:05:56 -07:00
|
|
|
nsContentList::ContentAppended(nsIDocument* aDocument, nsIContent* aContainer,
|
2010-05-10 18:12:34 -07:00
|
|
|
nsIContent* aFirstNewContent,
|
2007-03-22 10:30:00 -07:00
|
|
|
PRInt32 aNewIndexInContainer)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aContainer, "Can't get at the new content if no container!");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the state is LIST_DIRTY then we have no useful information in our list
|
|
|
|
* and we want to put off doing work as much as possible. Also, if
|
|
|
|
* aContainer is anonymous from our point of view, we know that we can't
|
|
|
|
* possibly be matching any of the kids.
|
|
|
|
*/
|
|
|
|
if (mState == LIST_DIRTY ||
|
|
|
|
!nsContentUtils::IsInSameAnonymousTree(mRootNode, aContainer) ||
|
|
|
|
!MayContainRelevantNodes(aContainer))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We want to handle the case of ContentAppended by sometimes
|
|
|
|
* appending the content to our list, not just setting state to
|
|
|
|
* LIST_DIRTY, since most of our ContentAppended notifications
|
|
|
|
* should come during pageload and be at the end of the document.
|
|
|
|
* Do a bit of work to see whether we could just append to what we
|
|
|
|
* already have.
|
|
|
|
*/
|
|
|
|
|
|
|
|
PRInt32 count = aContainer->GetChildCount();
|
|
|
|
|
|
|
|
if (count > 0) {
|
|
|
|
PRInt32 ourCount = mElements.Count();
|
2011-09-28 23:19:26 -07:00
|
|
|
bool appendToList = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (ourCount == 0) {
|
2011-10-17 07:59:28 -07:00
|
|
|
appendToList = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
nsIContent* ourLastContent = mElements[ourCount - 1];
|
|
|
|
/*
|
|
|
|
* We want to append instead of invalidating if the first thing
|
|
|
|
* that got appended comes after ourLastContent.
|
|
|
|
*/
|
2010-05-10 18:12:34 -07:00
|
|
|
if (nsContentUtils::PositionIsBefore(ourLastContent, aFirstNewContent)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
appendToList = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-18 11:02:24 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!appendToList) {
|
|
|
|
// The new stuff is somewhere in the middle of our list; check
|
|
|
|
// whether we need to invalidate
|
2010-05-10 18:12:34 -07:00
|
|
|
for (nsIContent* cur = aFirstNewContent; cur; cur = cur->GetNextSibling()) {
|
2010-05-10 18:12:34 -07:00
|
|
|
if (MatchSelf(cur)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// Uh-oh. We're gonna have to add elements into the middle
|
|
|
|
// of our list. That's not worth the effort.
|
|
|
|
SetDirty();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-04-18 11:02:24 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
ASSERT_IN_SYNC;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point we know we could append. If we're not up to
|
|
|
|
* date, however, that would be a bad idea -- it could miss some
|
|
|
|
* content that we never picked up due to being lazy. Further, we
|
|
|
|
* may never get asked for this content... so don't grab it yet.
|
|
|
|
*/
|
|
|
|
if (mState == LIST_LAZY) // be lazy
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We're up to date. That means someone's actively using us; we
|
|
|
|
* may as well grab this content....
|
|
|
|
*/
|
2010-05-10 18:12:34 -07:00
|
|
|
if (mDeep) {
|
2010-05-10 18:12:34 -07:00
|
|
|
for (nsIContent* cur = aFirstNewContent;
|
|
|
|
cur;
|
|
|
|
cur = cur->GetNextNode(aContainer)) {
|
2010-05-10 18:12:34 -07:00
|
|
|
if (cur->IsElement() && Match(cur->AsElement())) {
|
|
|
|
mElements.AppendObject(cur);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2010-05-10 18:12:34 -07:00
|
|
|
for (nsIContent* cur = aFirstNewContent; cur; cur = cur->GetNextSibling()) {
|
2010-05-10 18:12:34 -07:00
|
|
|
if (cur->IsElement() && Match(cur->AsElement())) {
|
|
|
|
mElements.AppendObject(cur);
|
|
|
|
}
|
2008-09-12 15:35:15 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_IN_SYNC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsContentList::ContentInserted(nsIDocument *aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
|
|
|
PRInt32 aIndexInContainer)
|
|
|
|
{
|
|
|
|
// Note that aContainer can be null here if we are inserting into
|
|
|
|
// the document itself; any attempted optimizations to this method
|
|
|
|
// should deal with that.
|
|
|
|
if (mState != LIST_DIRTY &&
|
|
|
|
MayContainRelevantNodes(NODE_FROM(aContainer, aDocument)) &&
|
|
|
|
nsContentUtils::IsInSameAnonymousTree(mRootNode, aChild) &&
|
|
|
|
MatchSelf(aChild)) {
|
|
|
|
SetDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_IN_SYNC;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsContentList::ContentRemoved(nsIDocument *aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
2010-07-21 15:05:17 -07:00
|
|
|
PRInt32 aIndexInContainer,
|
|
|
|
nsIContent* aPreviousSibling)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Note that aContainer can be null here if we are removing from
|
|
|
|
// the document itself; any attempted optimizations to this method
|
|
|
|
// should deal with that.
|
|
|
|
if (mState != LIST_DIRTY &&
|
|
|
|
MayContainRelevantNodes(NODE_FROM(aContainer, aDocument)) &&
|
|
|
|
nsContentUtils::IsInSameAnonymousTree(mRootNode, aChild) &&
|
|
|
|
MatchSelf(aChild)) {
|
|
|
|
SetDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_IN_SYNC;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-04-30 06:12:05 -07:00
|
|
|
nsContentList::Match(Element *aElement)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mFunc) {
|
2010-08-31 19:47:00 -07:00
|
|
|
return (*mFunc)(aElement, mMatchNameSpaceId, mXMLMatchAtom, mData);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-31 19:47:00 -07:00
|
|
|
if (!mXMLMatchAtom)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-31 19:47:00 -07:00
|
|
|
nsINodeInfo *ni = aElement->NodeInfo();
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool unknown = mMatchNameSpaceId == kNameSpaceID_Unknown;
|
|
|
|
bool wildcard = mMatchNameSpaceId == kNameSpaceID_Wildcard;
|
|
|
|
bool toReturn = mMatchAll;
|
2010-08-31 19:47:00 -07:00
|
|
|
if (!unknown && !wildcard)
|
|
|
|
toReturn &= ni->NamespaceEquals(mMatchNameSpaceId);
|
|
|
|
|
|
|
|
if (toReturn)
|
|
|
|
return toReturn;
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool matchHTML = aElement->GetNameSpaceID() == kNameSpaceID_XHTML &&
|
2011-10-18 04:19:44 -07:00
|
|
|
aElement->OwnerDoc()->IsHTML();
|
2010-08-31 19:47:00 -07:00
|
|
|
|
|
|
|
if (unknown) {
|
|
|
|
return matchHTML ? ni->QualifiedNameEquals(mHTMLMatchAtom) :
|
|
|
|
ni->QualifiedNameEquals(mXMLMatchAtom);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-08-31 19:47:00 -07:00
|
|
|
|
|
|
|
if (wildcard) {
|
|
|
|
return matchHTML ? ni->Equals(mHTMLMatchAtom) :
|
|
|
|
ni->Equals(mXMLMatchAtom);
|
|
|
|
}
|
|
|
|
|
|
|
|
return matchHTML ? ni->Equals(mHTMLMatchAtom, mMatchNameSpaceId) :
|
|
|
|
ni->Equals(mXMLMatchAtom, mMatchNameSpaceId);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsContentList::MatchSelf(nsIContent *aContent)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aContent, "Can't match null stuff, you know");
|
|
|
|
NS_PRECONDITION(mDeep || aContent->GetNodeParent() == mRootNode,
|
|
|
|
"MatchSelf called on a node that we can't possibly match");
|
2008-09-12 15:35:15 -07:00
|
|
|
|
2010-04-30 06:12:05 -07:00
|
|
|
if (!aContent->IsElement()) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-09-12 15:35:15 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-30 06:12:05 -07:00
|
|
|
if (Match(aContent->AsElement()))
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!mDeep)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-10 18:12:34 -07:00
|
|
|
for (nsIContent* cur = aContent->GetFirstChild();
|
|
|
|
cur;
|
|
|
|
cur = cur->GetNextNode(aContent)) {
|
|
|
|
if (cur->IsElement() && Match(cur->AsElement())) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsContentList::PopulateSelf(PRUint32 aNeededLength)
|
|
|
|
{
|
|
|
|
if (!mRootNode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_IN_SYNC;
|
|
|
|
|
|
|
|
PRUint32 count = mElements.Count();
|
|
|
|
NS_ASSERTION(mState != LIST_DIRTY || count == 0,
|
|
|
|
"Reset() not called when setting state to LIST_DIRTY?");
|
|
|
|
|
|
|
|
if (count >= aNeededLength) // We're all set
|
|
|
|
return;
|
|
|
|
|
|
|
|
PRUint32 elementsToAppend = aNeededLength - count;
|
|
|
|
#ifdef DEBUG
|
|
|
|
PRUint32 invariant = elementsToAppend + mElements.Count();
|
|
|
|
#endif
|
|
|
|
|
2010-05-10 18:12:34 -07:00
|
|
|
if (mDeep) {
|
|
|
|
// If we already have nodes start searching at the last one, otherwise
|
|
|
|
// start searching at the root.
|
|
|
|
nsINode* cur = count ? mElements[count - 1] : mRootNode;
|
|
|
|
do {
|
|
|
|
cur = cur->GetNextNode(mRootNode);
|
|
|
|
if (!cur) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (cur->IsElement() && Match(cur->AsElement())) {
|
|
|
|
mElements.AppendObject(cur->AsElement());
|
|
|
|
--elementsToAppend;
|
|
|
|
}
|
|
|
|
} while (elementsToAppend);
|
|
|
|
} else {
|
|
|
|
nsIContent* cur =
|
|
|
|
count ? mElements[count-1]->GetNextSibling() : mRootNode->GetFirstChild();
|
|
|
|
for ( ; cur && elementsToAppend; cur = cur->GetNextSibling()) {
|
|
|
|
if (cur->IsElement() && Match(cur->AsElement())) {
|
|
|
|
mElements.AppendObject(cur);
|
|
|
|
--elementsToAppend;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_ASSERTION(elementsToAppend + mElements.Count() == invariant,
|
2010-05-10 18:12:34 -07:00
|
|
|
"Something is awry!");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (elementsToAppend != 0)
|
|
|
|
mState = LIST_UP_TO_DATE;
|
|
|
|
else
|
|
|
|
mState = LIST_LAZY;
|
|
|
|
|
|
|
|
ASSERT_IN_SYNC;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsContentList::RemoveFromHashtable()
|
|
|
|
{
|
|
|
|
if (mFunc) {
|
|
|
|
// This can't be in the table anyway
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gContentListHashTable.ops)
|
|
|
|
return;
|
|
|
|
|
2010-11-23 11:10:56 -08:00
|
|
|
nsDependentAtomString str(mXMLMatchAtom);
|
|
|
|
nsContentListKey key(mRootNode, mMatchNameSpaceId, str);
|
2007-03-22 10:30:00 -07:00
|
|
|
PL_DHashTableOperate(&gContentListHashTable,
|
2010-11-23 11:10:56 -08:00
|
|
|
&key,
|
2007-03-22 10:30:00 -07:00
|
|
|
PL_DHASH_REMOVE);
|
|
|
|
|
|
|
|
if (gContentListHashTable.entryCount == 0) {
|
|
|
|
PL_DHashTableFinish(&gContentListHashTable);
|
|
|
|
gContentListHashTable.ops = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-28 23:19:26 -07:00
|
|
|
nsContentList::BringSelfUpToDate(bool aDoFlush)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-09-28 10:46:08 -07:00
|
|
|
if (mRootNode && aDoFlush && mFlushesNeeded) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// XXX sXBL/XBL2 issue
|
|
|
|
nsIDocument* doc = mRootNode->GetCurrentDoc();
|
|
|
|
if (doc) {
|
|
|
|
// Flush pending content changes Bug 4891.
|
|
|
|
doc->FlushPendingNotifications(Flush_ContentAndNotify);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState != LIST_UP_TO_DATE)
|
|
|
|
PopulateSelf(PRUint32(-1));
|
|
|
|
|
|
|
|
ASSERT_IN_SYNC;
|
|
|
|
NS_ASSERTION(!mRootNode || mState == LIST_UP_TO_DATE,
|
|
|
|
"PopulateSelf dod not bring content list up to date!");
|
|
|
|
}
|
|
|
|
|
2010-02-09 09:09:06 -08:00
|
|
|
nsCacheableFuncStringContentList::~nsCacheableFuncStringContentList()
|
|
|
|
{
|
|
|
|
RemoveFromFuncStringHashtable();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCacheableFuncStringContentList::RemoveFromFuncStringHashtable()
|
|
|
|
{
|
|
|
|
if (!gFuncStringContentListHashTable.ops) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsFuncStringCacheKey key(mRootNode, mFunc, mString);
|
|
|
|
PL_DHashTableOperate(&gFuncStringContentListHashTable,
|
|
|
|
&key,
|
|
|
|
PL_DHASH_REMOVE);
|
|
|
|
|
|
|
|
if (gFuncStringContentListHashTable.entryCount == 0) {
|
|
|
|
PL_DHashTableFinish(&gFuncStringContentListHashTable);
|
|
|
|
gFuncStringContentListHashTable.ops = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef DEBUG_CONTENT_LIST
|
|
|
|
void
|
|
|
|
nsContentList::AssertInSync()
|
|
|
|
{
|
|
|
|
if (mState == LIST_DIRTY) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mRootNode) {
|
|
|
|
NS_ASSERTION(mElements.Count() == 0 && mState == LIST_DIRTY,
|
|
|
|
"Empty iterator isn't quite empty?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX This code will need to change if nsContentLists can ever match
|
|
|
|
// elements that are outside of the document element.
|
|
|
|
nsIContent *root;
|
|
|
|
if (mRootNode->IsNodeOfType(nsINode::eDOCUMENT)) {
|
2010-04-30 06:12:05 -07:00
|
|
|
root = static_cast<nsIDocument*>(mRootNode)->GetRootElement();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
2007-07-08 00:08:04 -07:00
|
|
|
root = static_cast<nsIContent*>(mRootNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContentIterator> iter;
|
|
|
|
if (mDeep) {
|
|
|
|
NS_NewPreContentIterator(getter_AddRefs(iter));
|
|
|
|
iter->Init(root);
|
|
|
|
iter->First();
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32 cnt = 0, index = 0;
|
2011-10-17 07:59:28 -07:00
|
|
|
while (true) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (cnt == mElements.Count() && mState == LIST_LAZY) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent *cur = mDeep ? iter->GetCurrentNode() :
|
|
|
|
mRootNode->GetChildAt(index++);
|
|
|
|
if (!cur) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-30 06:12:05 -07:00
|
|
|
if (cur->IsElement() && Match(cur->AsElement())) {
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(cnt < mElements.Count() && mElements[cnt] == cur,
|
|
|
|
"Elements is out of sync");
|
|
|
|
++cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mDeep) {
|
|
|
|
iter->Next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(cnt == mElements.Count(), "Too few elements");
|
|
|
|
}
|
|
|
|
#endif
|