1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2003
20 * IBM Corporation. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
40 * Storage of the children and attributes of a DOM node; storage for
41 * the two is unified to minimize footprint.
44 #ifndef nsAttrAndChildArray_h___
45 #define nsAttrAndChildArray_h___
48 #include "nsAttrName.h"
49 #include "nsAttrValue.h"
53 class nsMappedAttributes
;
54 class nsHTMLStyleSheet
;
56 class nsMappedAttributeElement
;
58 #define ATTRCHILD_ARRAY_GROWSIZE 8
59 #define ATTRCHILD_ARRAY_LINEAR_THRESHOLD 32
61 #define ATTRCHILD_ARRAY_ATTR_SLOTS_BITS 10
63 #define ATTRCHILD_ARRAY_MAX_ATTR_COUNT \
64 ((1 << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS) - 1)
66 #define ATTRCHILD_ARRAY_MAX_CHILD_COUNT \
67 (~PtrBits(0) >> ATTRCHILD_ARRAY_ATTR_SLOTS_BITS)
69 #define ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK \
70 ((1 << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS) - 1)
73 #define ATTRSIZE (sizeof(InternalAttr) / sizeof(void*))
75 class nsAttrAndChildArray
78 nsAttrAndChildArray();
79 ~nsAttrAndChildArray();
81 PRUint32
ChildCount() const
83 return mImpl
? (mImpl
->mAttrAndChildCount
>> ATTRCHILD_ARRAY_ATTR_SLOTS_BITS
) : 0;
85 nsIContent
* ChildAt(PRUint32 aPos
) const
87 NS_ASSERTION(aPos
< ChildCount(), "out-of-bounds access in nsAttrAndChildArray");
88 return reinterpret_cast<nsIContent
*>(mImpl
->mBuffer
[AttrSlotsSize() + aPos
]);
90 nsIContent
* GetSafeChildAt(PRUint32 aPos
) const;
91 nsIContent
* const * GetChildArray(PRUint32
* aChildCount
) const;
92 nsresult
AppendChild(nsIContent
* aChild
)
94 return InsertChildAt(aChild
, ChildCount());
96 nsresult
InsertChildAt(nsIContent
* aChild
, PRUint32 aPos
);
97 void RemoveChildAt(PRUint32 aPos
);
98 PRInt32
IndexOfChild(nsINode
* aPossibleChild
) const;
100 PRUint32
AttrCount() const;
101 const nsAttrValue
* GetAttr(nsIAtom
* aLocalName
, PRInt32 aNamespaceID
= kNameSpaceID_None
) const;
102 const nsAttrValue
* AttrAt(PRUint32 aPos
) const;
103 nsresult
SetAttr(nsIAtom
* aLocalName
, const nsAString
& aValue
);
104 nsresult
SetAndTakeAttr(nsIAtom
* aLocalName
, nsAttrValue
& aValue
);
105 nsresult
SetAndTakeAttr(nsINodeInfo
* aName
, nsAttrValue
& aValue
);
107 // Remove the attr at position aPos. The value of the attr is placed in
108 // aValue; any value that was already in aValue is destroyed.
109 nsresult
RemoveAttrAt(PRUint32 aPos
, nsAttrValue
& aValue
);
111 // Returns attribute name at given position, *not* out-of-bounds safe
112 const nsAttrName
* AttrNameAt(PRUint32 aPos
) const;
114 // Returns attribute name at given position or null if aPos is out-of-bounds
115 const nsAttrName
* GetSafeAttrNameAt(PRUint32 aPos
) const;
117 // aName is UTF-8 encoded
118 const nsAttrName
* GetExistingAttrNameFromQName(const nsACString
& aName
) const;
119 PRInt32
IndexOfAttr(nsIAtom
* aLocalName
, PRInt32 aNamespaceID
= kNameSpaceID_None
) const;
121 nsresult
SetAndTakeMappedAttr(nsIAtom
* aLocalName
, nsAttrValue
& aValue
,
122 nsMappedAttributeElement
* aContent
,
123 nsHTMLStyleSheet
* aSheet
);
124 nsresult
SetMappedAttrStyleSheet(nsHTMLStyleSheet
* aSheet
);
125 void WalkMappedAttributeStyleRules(nsRuleWalker
* aRuleWalker
);
130 nsAttrAndChildArray(const nsAttrAndChildArray
& aOther
); // Not to be implemented
131 nsAttrAndChildArray
& operator=(const nsAttrAndChildArray
& aOther
); // Not to be implemented
135 PRUint32
NonMappedAttrCount() const;
136 PRUint32
MappedAttrCount() const;
138 nsresult
GetModifiableMapped(nsMappedAttributeElement
* aContent
,
139 nsHTMLStyleSheet
* aSheet
,
141 nsMappedAttributes
** aModifiable
);
142 nsresult
MakeMappedUnique(nsMappedAttributes
* aAttributes
);
144 PRUint32
AttrSlotsSize() const
146 return AttrSlotCount() * ATTRSIZE
;
149 PRUint32
AttrSlotCount() const
151 return mImpl
? mImpl
->mAttrAndChildCount
& ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK
: 0;
154 void SetChildCount(PRUint32 aCount
)
156 mImpl
->mAttrAndChildCount
=
157 (mImpl
->mAttrAndChildCount
& ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK
) |
158 (aCount
<< ATTRCHILD_ARRAY_ATTR_SLOTS_BITS
);
161 void SetAttrSlotCount(PRUint32 aCount
)
163 mImpl
->mAttrAndChildCount
=
164 (mImpl
->mAttrAndChildCount
& ~ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK
) |
168 void SetAttrSlotAndChildCount(PRUint32 aSlotCount
, PRUint32 aChildCount
)
170 mImpl
->mAttrAndChildCount
= aSlotCount
|
171 (aChildCount
<< ATTRCHILD_ARRAY_ATTR_SLOTS_BITS
);
174 PRBool
GrowBy(PRUint32 aGrowSize
);
175 PRBool
AddAttrSlot();
184 PRUint32 mAttrAndChildCount
;
185 PRUint32 mBufferSize
;
186 nsMappedAttributes
* mMappedAttrs
;