1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 * Storage of the attributes of a DOM node.
11 #ifndef AttrArray_h___
12 #define AttrArray_h___
14 #include "mozilla/Attributes.h"
15 #include "mozilla/MemoryReporting.h"
16 #include "mozilla/UniquePtr.h"
17 #include "mozilla/Span.h"
18 #include "mozilla/dom/BorrowedAttrInfo.h"
21 #include "nsAttrName.h"
22 #include "nsAttrValue.h"
23 #include "nsCaseTreatment.h"
27 class nsMappedAttributes
;
28 class nsHTMLStyleSheet
;
30 class nsMappedAttributeElement
;
33 typedef mozilla::dom::BorrowedAttrInfo BorrowedAttrInfo
;
36 AttrArray() = default;
37 ~AttrArray() = default;
39 bool HasAttrs() const { return NonMappedAttrCount() || MappedAttrCount(); }
41 uint32_t AttrCount() const {
42 return NonMappedAttrCount() + MappedAttrCount();
45 const nsAttrValue
* GetAttr(const nsAtom
* aLocalName
,
46 int32_t aNamespaceID
= kNameSpaceID_None
) const;
47 // As above but using a string attr name and always using
48 // kNameSpaceID_None. This is always case-sensitive.
49 const nsAttrValue
* GetAttr(const nsAString
& aName
) const;
50 // Get an nsAttrValue by qualified name. Can optionally do
51 // ASCII-case-insensitive name matching.
52 const nsAttrValue
* GetAttr(const nsAString
& aName
,
53 nsCaseTreatment aCaseSensitive
) const;
54 const nsAttrValue
* AttrAt(uint32_t aPos
) const;
55 // SetAndSwapAttr swaps the current attribute value with aValue.
56 // If the attribute was unset, an empty value will be swapped into aValue
57 // and aHadValue will be set to false. Otherwise, aHadValue will be set to
59 nsresult
SetAndSwapAttr(nsAtom
* aLocalName
, nsAttrValue
& aValue
,
61 nsresult
SetAndSwapAttr(mozilla::dom::NodeInfo
* aName
, nsAttrValue
& aValue
,
64 // Remove the attr at position aPos. The value of the attr is placed in
65 // aValue; any value that was already in aValue is destroyed.
66 nsresult
RemoveAttrAt(uint32_t aPos
, nsAttrValue
& aValue
);
68 // Returns attribute name at given position, *not* out-of-bounds safe
69 const nsAttrName
* AttrNameAt(uint32_t aPos
) const;
71 // Returns the attribute info at a given position, *not* out-of-bounds safe
72 BorrowedAttrInfo
AttrInfoAt(uint32_t aPos
) const;
74 // Returns attribute name at given position or null if aPos is out-of-bounds
75 const nsAttrName
* GetSafeAttrNameAt(uint32_t aPos
) const;
77 const nsAttrName
* GetExistingAttrNameFromQName(const nsAString
& aName
) const;
78 int32_t IndexOfAttr(const nsAtom
* aLocalName
,
79 int32_t aNamespaceID
= kNameSpaceID_None
) const;
81 // SetAndSwapMappedAttr swaps the current attribute value with aValue.
82 // If the attribute was unset, an empty value will be swapped into aValue
83 // and aHadValue will be set to false. Otherwise, aHadValue will be set to
85 nsresult
SetAndSwapMappedAttr(nsAtom
* aLocalName
, nsAttrValue
& aValue
,
86 nsMappedAttributeElement
* aContent
,
87 nsHTMLStyleSheet
* aSheet
, bool* aHadValue
);
88 nsresult
SetMappedAttrStyleSheet(nsHTMLStyleSheet
* aSheet
) {
89 if (!mImpl
|| !mImpl
->mMappedAttrs
) {
92 return DoSetMappedAttrStyleSheet(aSheet
);
95 // Update the rule mapping function on our mapped attributes, if we have any.
96 // We take a nsMappedAttributeElement, not a nsMapRuleToAttributesFunc,
97 // because the latter is defined in a header we can't include here.
98 nsresult
UpdateMappedAttrRuleMapper(nsMappedAttributeElement
& aElement
) {
99 if (!mImpl
|| !mImpl
->mMappedAttrs
) {
102 return DoUpdateMappedAttrRuleMapper(aElement
);
107 size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf
) const;
108 bool HasMappedAttrs() const { return MappedAttrCount(); }
109 const nsMappedAttributes
* GetMapped() const;
111 // Force this to have mapped attributes, even if those attributes are empty.
112 nsresult
ForceMapped(nsMappedAttributeElement
* aContent
,
113 mozilla::dom::Document
* aDocument
);
115 // Clear the servo declaration block on the mapped attributes, if any
116 // Will assert off main thread
117 void ClearMappedServoStyle();
119 // Increases capacity (if necessary) to have enough space to accomodate the
120 // unmapped attributes of |aOther|.
121 nsresult
EnsureCapacityToClone(const AttrArray
& aOther
);
123 struct InternalAttr
{
129 AttrArray(const AttrArray
& aOther
) = delete;
130 AttrArray
& operator=(const AttrArray
& aOther
) = delete;
132 uint32_t NonMappedAttrCount() const { return mImpl
? mImpl
->mAttrCount
: 0; }
134 uint32_t MappedAttrCount() const {
135 return mImpl
&& mImpl
->mMappedAttrs
? DoGetMappedAttrCount() : 0;
138 uint32_t DoGetMappedAttrCount() const;
140 // Returns a non-null zero-refcount object.
141 nsMappedAttributes
* GetModifiableMapped(nsMappedAttributeElement
* aContent
,
142 nsHTMLStyleSheet
* aSheet
,
144 int32_t aAttrCount
= 1);
145 nsresult
MakeMappedUnique(nsMappedAttributes
* aAttributes
);
147 bool GrowBy(uint32_t aGrowSize
);
149 // Tries to create an attribute, growing the buffer if needed, with the given
152 // The value is moved from the argument.
154 // `Name` can be anything you construct a `nsAttrName` with (either an atom or
155 // a NodeInfo pointer).
156 template <typename Name
>
157 nsresult
AddNewAttribute(Name
*, nsAttrValue
&);
160 * Guts of SetMappedAttrStyleSheet for the rare case when we have mapped attrs
162 nsresult
DoSetMappedAttrStyleSheet(nsHTMLStyleSheet
* aSheet
);
165 * Guts of UpdateMappedAttrRuleMapper for the case when we have mapped attrs.
167 nsresult
DoUpdateMappedAttrRuleMapper(nsMappedAttributeElement
& aElement
);
170 // Disable MSVC warning 'nonstandard extension used: zero-sized array in
172 # pragma warning(push)
173 # pragma warning(disable : 4200)
177 constexpr static size_t AllocationSizeForAttributes(uint32_t aAttrCount
) {
178 return sizeof(Impl
) + aAttrCount
* sizeof(InternalAttr
);
181 auto NonMappedAttrs() const {
182 return mozilla::Span
<const InternalAttr
>{mBuffer
, mAttrCount
};
185 auto NonMappedAttrs() {
186 return mozilla::Span
<InternalAttr
>{mBuffer
, mAttrCount
};
189 Impl(const Impl
&) = delete;
190 Impl(Impl
&&) = delete;
194 uint32_t mCapacity
; // In number of InternalAttrs
196 // Manually refcounted.
197 nsMappedAttributes
* mMappedAttrs
;
199 // Allocated in the same buffer as `Impl`.
200 InternalAttr mBuffer
[0];
203 # pragma warning(pop)
206 mozilla::Span
<InternalAttr
> NonMappedAttrs() {
207 return mImpl
? mImpl
->NonMappedAttrs() : mozilla::Span
<InternalAttr
>();
210 mozilla::Span
<const InternalAttr
> NonMappedAttrs() const {
211 return mImpl
? mImpl
->NonMappedAttrs()
212 : mozilla::Span
<const InternalAttr
>();
215 mozilla::UniquePtr
<Impl
> mImpl
;