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 * Implementation of DOM Core's Attr node.
11 #include "mozilla/dom/Attr.h"
12 #include "mozilla/dom/AttrBinding.h"
13 #include "mozilla/dom/Element.h"
14 #include "mozilla/EventDispatcher.h"
15 #include "mozilla/InternalMutationEvent.h"
16 #include "mozilla/StaticPrefs_dom.h"
17 #include "nsContentCreatorFunctions.h"
19 #include "nsUnicharUtils.h"
20 #include "nsDOMString.h"
21 #include "nsIContentInlines.h"
22 #include "mozilla/dom/Document.h"
23 #include "nsGkAtoms.h"
24 #include "nsCOMArray.h"
25 #include "nsNameSpaceManager.h"
26 #include "nsTextNode.h"
27 #include "mozAutoDocUpdate.h"
28 #include "nsWrapperCacheInlines.h"
29 #include "NodeUbiReporting.h"
31 namespace mozilla::dom
{
33 //----------------------------------------------------------------------
34 bool Attr::sInitialized
;
36 Attr::Attr(nsDOMAttributeMap
* aAttrMap
,
37 already_AddRefed
<dom::NodeInfo
>&& aNodeInfo
, const nsAString
& aValue
)
38 : nsINode(std::move(aNodeInfo
)), mAttrMap(aAttrMap
), mValue(aValue
) {
39 MOZ_ASSERT(mNodeInfo
, "We must get a nodeinfo here!");
40 MOZ_ASSERT(mNodeInfo
->NodeType() == ATTRIBUTE_NODE
, "Wrong nodeType");
42 // We don't add a reference to our content. It will tell us
43 // to drop our reference when it goes away.
46 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(Attr
)
48 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Attr
)
49 if (!nsINode::Traverse(tmp
, cb
)) {
50 return NS_SUCCESS_INTERRUPTED_TRAVERSE
;
52 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAttrMap
)
53 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
55 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Attr
)
57 NS_IMPL_CYCLE_COLLECTION_UNLINK(mAttrMap
)
58 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
60 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(Attr
)
61 Element
* ownerElement
= tmp
->GetElement();
62 if (tmp
->HasKnownLiveWrapper()) {
64 // The attribute owns the element via attribute map so we can
65 // mark it when the attribute is certainly alive.
66 mozilla::dom::FragmentOrElement::MarkNodeChildren(ownerElement
);
71 mozilla::dom::FragmentOrElement::CanSkip(ownerElement
, true)) {
74 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END
76 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(Attr
)
77 return tmp
->HasKnownLiveWrapperAndDoesNotNeedTracing(tmp
);
78 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END
80 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(Attr
)
81 return tmp
->HasKnownLiveWrapper();
82 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
84 // QueryInterface implementation for Attr
85 NS_INTERFACE_TABLE_HEAD(Attr
)
86 NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY
87 NS_INTERFACE_TABLE(Attr
, nsINode
, EventTarget
)
88 NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(Attr
)
89 NS_INTERFACE_MAP_ENTRY_TEAROFF(nsISupportsWeakReference
,
90 new nsNodeSupportsWeakRefTearoff(this))
93 NS_IMPL_CYCLE_COLLECTING_ADDREF(Attr
)
95 NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_LAST_RELEASE_AND_DESTROY(Attr
,
99 NS_IMPL_DOMARENA_DESTROY(Attr
)
101 void Attr::SetMap(nsDOMAttributeMap
* aMap
) {
102 if (mAttrMap
&& !aMap
&& sInitialized
) {
103 // We're breaking a relationship with content and not getting a new one,
104 // need to locally cache value. GetValue() does that.
111 Element
* Attr::GetElement() const {
115 nsIContent
* content
= mAttrMap
->GetContent();
116 return content
? content
->AsElement() : nullptr;
119 nsresult
Attr::SetOwnerDocument(Document
* aDocument
) {
120 NS_ASSERTION(aDocument
, "Missing document");
122 Document
* doc
= OwnerDoc();
123 NS_ASSERTION(doc
!= aDocument
, "bad call to Attr::SetOwnerDocument");
124 doc
->RemoveAllPropertiesFor(this);
126 RefPtr
<dom::NodeInfo
> newNodeInfo
= aDocument
->NodeInfoManager()->GetNodeInfo(
127 mNodeInfo
->NameAtom(), mNodeInfo
->GetPrefixAtom(),
128 mNodeInfo
->NamespaceID(), ATTRIBUTE_NODE
);
129 NS_ASSERTION(newNodeInfo
, "GetNodeInfo lies");
130 mNodeInfo
.swap(newNodeInfo
);
135 void Attr::GetName(nsAString
& aName
) { aName
= NodeName(); }
137 void Attr::GetValue(nsAString
& aValue
) {
138 Element
* element
= GetElement();
140 RefPtr
<nsAtom
> nameAtom
= mNodeInfo
->NameAtom();
141 element
->GetAttr(mNodeInfo
->NamespaceID(), nameAtom
, aValue
);
147 void Attr::SetValue(const nsAString
& aValue
, nsIPrincipal
* aTriggeringPrincipal
,
149 Element
* element
= GetElement();
155 RefPtr
<nsAtom
> nameAtom
= mNodeInfo
->NameAtom();
156 aRv
= element
->SetAttr(mNodeInfo
->NamespaceID(), nameAtom
,
157 mNodeInfo
->GetPrefixAtom(), aValue
,
158 aTriggeringPrincipal
, true);
161 void Attr::SetValue(const nsAString
& aValue
, ErrorResult
& aRv
) {
162 SetValue(aValue
, nullptr, aRv
);
165 bool Attr::Specified() const { return true; }
167 Element
* Attr::GetOwnerElement() { return GetElement(); }
169 void Attr::GetNodeValueInternal(nsAString
& aNodeValue
) { GetValue(aNodeValue
); }
171 void Attr::SetNodeValueInternal(const nsAString
& aNodeValue
,
172 ErrorResult
& aError
) {
173 SetValue(aNodeValue
, nullptr, aError
);
176 nsresult
Attr::Clone(dom::NodeInfo
* aNodeInfo
, nsINode
** aResult
) const {
178 const_cast<Attr
*>(this)->GetValue(value
);
179 *aResult
= new (aNodeInfo
->NodeInfoManager())
180 Attr(nullptr, do_AddRef(aNodeInfo
), value
);
187 nsIURI
* Attr::GetBaseURI(bool aTryUseXHRDocBaseURI
) const {
188 Element
* parent
= GetElement();
190 return parent
? parent
->GetBaseURI(aTryUseXHRDocBaseURI
)
191 : OwnerDoc()->GetBaseURI(aTryUseXHRDocBaseURI
);
194 void Attr::GetTextContentInternal(nsAString
& aTextContent
,
195 OOMReporter
& aError
) {
196 GetValue(aTextContent
);
199 void Attr::SetTextContentInternal(const nsAString
& aTextContent
,
200 nsIPrincipal
* aSubjectPrincipal
,
201 ErrorResult
& aError
) {
202 SetNodeValueInternal(aTextContent
, aError
);
205 void Attr::GetEventTargetParent(EventChainPreVisitor
& aVisitor
) {
206 aVisitor
.mCanHandle
= true;
209 void Attr::Initialize() { sInitialized
= true; }
211 void Attr::Shutdown() { sInitialized
= false; }
213 JSObject
* Attr::WrapNode(JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) {
214 return Attr_Binding::Wrap(aCx
, this, aGivenProto
);
217 void Attr::ConstructUbiNode(void* storage
) {
218 JS::ubi::Concrete
<Attr
>::construct(storage
, this);
221 } // namespace mozilla::dom