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
4 : *
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/
9 : *
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
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * IBM Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 2003
20 : * IBM Corporation. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * IBM Corporation
24 : *
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.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : /*
40 : * Storage of the children and attributes of a DOM node; storage for
41 : * the two is unified to minimize footprint.
42 : */
43 :
44 : #ifndef nsAttrAndChildArray_h___
45 : #define nsAttrAndChildArray_h___
46 :
47 : #include "mozilla/Attributes.h"
48 :
49 : #include "nscore.h"
50 : #include "nsAttrName.h"
51 : #include "nsAttrValue.h"
52 :
53 : class nsINode;
54 : class nsIContent;
55 : class nsMappedAttributes;
56 : class nsHTMLStyleSheet;
57 : class nsRuleWalker;
58 : class nsMappedAttributeElement;
59 :
60 : #define ATTRCHILD_ARRAY_GROWSIZE 8
61 : #define ATTRCHILD_ARRAY_LINEAR_THRESHOLD 32
62 :
63 : #define ATTRCHILD_ARRAY_ATTR_SLOTS_BITS 10
64 :
65 : #define ATTRCHILD_ARRAY_MAX_ATTR_COUNT \
66 : ((1 << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS) - 1)
67 :
68 : #define ATTRCHILD_ARRAY_MAX_CHILD_COUNT \
69 : (~PRUint32(0) >> ATTRCHILD_ARRAY_ATTR_SLOTS_BITS)
70 :
71 : #define ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK \
72 : ((1 << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS) - 1)
73 :
74 :
75 : #define ATTRSIZE (sizeof(InternalAttr) / sizeof(void*))
76 :
77 : class nsAttrAndChildArray
78 : {
79 : public:
80 : nsAttrAndChildArray();
81 : ~nsAttrAndChildArray();
82 :
83 1506121 : PRUint32 ChildCount() const
84 : {
85 1506121 : return mImpl ? (mImpl->mAttrAndChildCount >> ATTRCHILD_ARRAY_ATTR_SLOTS_BITS) : 0;
86 : }
87 402455 : nsIContent* ChildAt(PRUint32 aPos) const
88 : {
89 402455 : NS_ASSERTION(aPos < ChildCount(), "out-of-bounds access in nsAttrAndChildArray");
90 402455 : return reinterpret_cast<nsIContent*>(mImpl->mBuffer[AttrSlotsSize() + aPos]);
91 : }
92 : nsIContent* GetSafeChildAt(PRUint32 aPos) const;
93 : nsIContent * const * GetChildArray(PRUint32* aChildCount) const;
94 : nsresult AppendChild(nsIContent* aChild)
95 : {
96 : return InsertChildAt(aChild, ChildCount());
97 : }
98 : nsresult InsertChildAt(nsIContent* aChild, PRUint32 aPos);
99 : void RemoveChildAt(PRUint32 aPos);
100 : // Like RemoveChildAt but hands the reference to the child being
101 : // removed back to the caller instead of just releasing it.
102 : already_AddRefed<nsIContent> TakeChildAt(PRUint32 aPos);
103 : PRInt32 IndexOfChild(nsINode* aPossibleChild) const;
104 :
105 : PRUint32 AttrCount() const;
106 : const nsAttrValue* GetAttr(nsIAtom* aLocalName, PRInt32 aNamespaceID = kNameSpaceID_None) const;
107 : const nsAttrValue* AttrAt(PRUint32 aPos) const;
108 : nsresult SetAndTakeAttr(nsIAtom* aLocalName, nsAttrValue& aValue);
109 : nsresult SetAndTakeAttr(nsINodeInfo* aName, nsAttrValue& aValue);
110 :
111 : // Remove the attr at position aPos. The value of the attr is placed in
112 : // aValue; any value that was already in aValue is destroyed.
113 : nsresult RemoveAttrAt(PRUint32 aPos, nsAttrValue& aValue);
114 :
115 : // Returns attribute name at given position, *not* out-of-bounds safe
116 : const nsAttrName* AttrNameAt(PRUint32 aPos) const;
117 :
118 : // Returns attribute name at given position or null if aPos is out-of-bounds
119 : const nsAttrName* GetSafeAttrNameAt(PRUint32 aPos) const;
120 :
121 : const nsAttrName* GetExistingAttrNameFromQName(const nsAString& aName) const;
122 : PRInt32 IndexOfAttr(nsIAtom* aLocalName, PRInt32 aNamespaceID = kNameSpaceID_None) const;
123 :
124 : nsresult SetAndTakeMappedAttr(nsIAtom* aLocalName, nsAttrValue& aValue,
125 : nsMappedAttributeElement* aContent,
126 : nsHTMLStyleSheet* aSheet);
127 : nsresult SetMappedAttrStyleSheet(nsHTMLStyleSheet* aSheet);
128 : void WalkMappedAttributeStyleRules(nsRuleWalker* aRuleWalker);
129 :
130 : void Compact();
131 :
132 11455 : bool CanFitMoreAttrs() const
133 : {
134 11455 : return AttrSlotCount() < ATTRCHILD_ARRAY_MAX_ATTR_COUNT ||
135 11455 : !AttrSlotIsTaken(ATTRCHILD_ARRAY_MAX_ATTR_COUNT - 1);
136 : }
137 :
138 : size_t SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const;
139 0 : bool HasMappedAttrs() const
140 : {
141 0 : return MappedAttrCount();
142 : }
143 :
144 : private:
145 : nsAttrAndChildArray(const nsAttrAndChildArray& aOther) MOZ_DELETE;
146 : nsAttrAndChildArray& operator=(const nsAttrAndChildArray& aOther) MOZ_DELETE;
147 :
148 : void Clear();
149 :
150 : PRUint32 NonMappedAttrCount() const;
151 : PRUint32 MappedAttrCount() const;
152 :
153 : nsresult GetModifiableMapped(nsMappedAttributeElement* aContent,
154 : nsHTMLStyleSheet* aSheet,
155 : bool aWillAddAttr,
156 : nsMappedAttributes** aModifiable);
157 : nsresult MakeMappedUnique(nsMappedAttributes* aAttributes);
158 :
159 627043 : PRUint32 AttrSlotsSize() const
160 : {
161 627043 : return AttrSlotCount() * ATTRSIZE;
162 : }
163 :
164 906308 : PRUint32 AttrSlotCount() const
165 : {
166 906308 : return mImpl ? mImpl->mAttrAndChildCount & ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK : 0;
167 : }
168 :
169 106617 : bool AttrSlotIsTaken(PRUint32 aSlot) const
170 : {
171 106617 : NS_PRECONDITION(aSlot < AttrSlotCount(), "out-of-bounds");
172 106617 : return mImpl->mBuffer[aSlot * ATTRSIZE];
173 : }
174 :
175 223910 : void SetChildCount(PRUint32 aCount)
176 : {
177 : mImpl->mAttrAndChildCount =
178 : (mImpl->mAttrAndChildCount & ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK) |
179 223910 : (aCount << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS);
180 223910 : }
181 :
182 11448 : void SetAttrSlotCount(PRUint32 aCount)
183 : {
184 : mImpl->mAttrAndChildCount =
185 : (mImpl->mAttrAndChildCount & ~ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK) |
186 11448 : aCount;
187 11448 : }
188 :
189 77412 : void SetAttrSlotAndChildCount(PRUint32 aSlotCount, PRUint32 aChildCount)
190 : {
191 : mImpl->mAttrAndChildCount = aSlotCount |
192 77412 : (aChildCount << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS);
193 77412 : }
194 :
195 : bool GrowBy(PRUint32 aGrowSize);
196 : bool AddAttrSlot();
197 :
198 : /**
199 : * Set *aPos to aChild and update sibling pointers as needed. aIndex is the
200 : * index at which aChild is actually being inserted. aChildCount is the
201 : * number of kids we had before the insertion.
202 : */
203 : inline void SetChildAtPos(void** aPos, nsIContent* aChild, PRUint32 aIndex,
204 : PRUint32 aChildCount);
205 :
206 : struct InternalAttr
207 11446 : {
208 : nsAttrName mName;
209 : nsAttrValue mValue;
210 : };
211 :
212 : struct Impl {
213 : PRUint32 mAttrAndChildCount;
214 : PRUint32 mBufferSize;
215 : nsMappedAttributes* mMappedAttrs;
216 : void* mBuffer[1];
217 : };
218 :
219 : Impl* mImpl;
220 : };
221 :
222 : #endif
|