Bug 1842773 - Part 5: Add ArrayBuffer.prototype.{maxByteLength,resizable} getters...
[gecko.git] / dom / base / nsTextNode.cpp
blobdf08c547be4de3e3eb4907f0a7b59d2b735b97a9
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/. */
7 /*
8 * Implementation of DOM Core's Text node.
9 */
11 #include "nsTextNode.h"
12 #include "mozilla/dom/TextBinding.h"
13 #include "nsContentUtils.h"
14 #include "mozilla/dom/DirectionalityUtils.h"
15 #include "mozilla/dom/Document.h"
16 #include "nsThreadUtils.h"
17 #include "nsStubMutationObserver.h"
18 #include "mozilla/IntegerPrintfMacros.h"
19 #ifdef MOZ_DOM_LIST
20 # include "nsRange.h"
21 #endif
23 using namespace mozilla;
24 using namespace mozilla::dom;
26 /**
27 * class used to implement attr() generated content
29 class nsAttributeTextNode final : public nsTextNode,
30 public nsStubMutationObserver {
31 public:
32 NS_DECL_ISUPPORTS_INHERITED
34 nsAttributeTextNode(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
35 int32_t aNameSpaceID, nsAtom* aAttrName,
36 nsAtom* aFallback)
37 : nsTextNode(std::move(aNodeInfo)),
38 mGrandparent(nullptr),
39 mNameSpaceID(aNameSpaceID),
40 mAttrName(aAttrName),
41 mFallback(aFallback) {
42 NS_ASSERTION(mNameSpaceID != kNameSpaceID_Unknown, "Must know namespace");
43 NS_ASSERTION(mAttrName, "Must have attr name");
46 virtual nsresult BindToTree(BindContext&, nsINode& aParent) override;
47 virtual void UnbindFromTree(bool aNullParent = true) override;
49 NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTECHANGED
50 NS_DECL_NSIMUTATIONOBSERVER_NODEWILLBEDESTROYED
52 virtual already_AddRefed<CharacterData> CloneDataNode(
53 mozilla::dom::NodeInfo* aNodeInfo, bool aCloneText) const override {
54 RefPtr<nsAttributeTextNode> it =
55 new (aNodeInfo->NodeInfoManager()) nsAttributeTextNode(
56 do_AddRef(aNodeInfo), mNameSpaceID, mAttrName, mFallback);
57 if (aCloneText) {
58 it->mText = mText;
61 return it.forget();
64 // Public method for the event to run
65 void UpdateText() { UpdateText(true); }
67 private:
68 virtual ~nsAttributeTextNode() {
69 NS_ASSERTION(!mGrandparent, "We were not unbound!");
72 // Update our text to our parent's current attr value
73 void UpdateText(bool aNotify);
75 // This doesn't need to be a strong pointer because it's only non-null
76 // while we're bound to the document tree, and it points to an ancestor
77 // so the ancestor must be bound to the document tree the whole time
78 // and can't be deleted.
79 Element* mGrandparent;
80 // What attribute we're showing
81 int32_t mNameSpaceID;
82 RefPtr<nsAtom> mAttrName;
83 RefPtr<nsAtom> mFallback;
86 nsTextNode::~nsTextNode() = default;
88 // Use the CC variant of this, even though this class does not define
89 // a new CC participant, to make QIing to the CC interfaces faster.
90 NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(nsTextNode, CharacterData)
92 JSObject* nsTextNode::WrapNode(JSContext* aCx,
93 JS::Handle<JSObject*> aGivenProto) {
94 return Text_Binding::Wrap(aCx, this, aGivenProto);
97 already_AddRefed<CharacterData> nsTextNode::CloneDataNode(
98 mozilla::dom::NodeInfo* aNodeInfo, bool aCloneText) const {
99 RefPtr<nsTextNode> it =
100 new (aNodeInfo->NodeInfoManager()) nsTextNode(do_AddRef(aNodeInfo));
101 if (aCloneText) {
102 it->mText = mText;
105 return it.forget();
108 nsresult nsTextNode::AppendTextForNormalize(const char16_t* aBuffer,
109 uint32_t aLength, bool aNotify,
110 nsIContent* aNextSibling) {
111 CharacterDataChangeInfo::Details details = {
112 CharacterDataChangeInfo::Details::eMerge, aNextSibling};
113 return SetTextInternal(mText.GetLength(), 0, aBuffer, aLength, aNotify,
114 &details);
117 nsresult nsTextNode::BindToTree(BindContext& aContext, nsINode& aParent) {
118 nsresult rv = CharacterData::BindToTree(aContext, aParent);
119 NS_ENSURE_SUCCESS(rv, rv);
121 SetDirectionFromNewTextNode(this);
123 return NS_OK;
126 void nsTextNode::UnbindFromTree(bool aNullParent) {
127 ResetDirectionSetByTextNode(this);
129 CharacterData::UnbindFromTree(aNullParent);
132 #ifdef MOZ_DOM_LIST
133 void nsTextNode::List(FILE* out, int32_t aIndent) const {
134 int32_t index;
135 for (index = aIndent; --index >= 0;) fputs(" ", out);
137 fprintf(out, "Text@%p", static_cast<const void*>(this));
138 fprintf(out, " flags=[%08x]", static_cast<unsigned int>(GetFlags()));
139 if (IsClosestCommonInclusiveAncestorForRangeInSelection()) {
140 const LinkedList<AbstractRange>* ranges =
141 GetExistingClosestCommonInclusiveAncestorRanges();
142 uint32_t count = ranges ? ranges->length() : 0;
143 fprintf(out, " ranges:%d", count);
145 fprintf(out, " primaryframe=%p", static_cast<void*>(GetPrimaryFrame()));
146 fprintf(out, " refcount=%" PRIuPTR "<", mRefCnt.get());
148 nsAutoString tmp;
149 ToCString(tmp, 0, mText.GetLength());
150 fputs(NS_LossyConvertUTF16toASCII(tmp).get(), out);
152 fputs(">\n", out);
155 void nsTextNode::DumpContent(FILE* out, int32_t aIndent, bool aDumpAll) const {
156 if (aDumpAll) {
157 int32_t index;
158 for (index = aIndent; --index >= 0;) fputs(" ", out);
160 nsAutoString tmp;
161 ToCString(tmp, 0, mText.GetLength());
163 if (!tmp.EqualsLiteral("\\n")) {
164 fputs(NS_LossyConvertUTF16toASCII(tmp).get(), out);
165 if (aIndent) fputs("\n", out);
169 #endif
171 nsresult NS_NewAttributeContent(nsNodeInfoManager* aNodeInfoManager,
172 int32_t aNameSpaceID, nsAtom* aAttrName,
173 nsAtom* aFallback, nsIContent** aResult) {
174 MOZ_ASSERT(aNodeInfoManager, "Missing nodeInfoManager");
175 MOZ_ASSERT(aAttrName, "Must have an attr name");
176 MOZ_ASSERT(aNameSpaceID != kNameSpaceID_Unknown, "Must know namespace");
178 *aResult = nullptr;
180 RefPtr<mozilla::dom::NodeInfo> ni = aNodeInfoManager->GetTextNodeInfo();
182 RefPtr<nsAttributeTextNode> textNode = new (aNodeInfoManager)
183 nsAttributeTextNode(ni.forget(), aNameSpaceID, aAttrName, aFallback);
184 textNode.forget(aResult);
186 return NS_OK;
189 NS_IMPL_ISUPPORTS_INHERITED(nsAttributeTextNode, nsTextNode,
190 nsIMutationObserver)
192 nsresult nsAttributeTextNode::BindToTree(BindContext& aContext,
193 nsINode& aParent) {
194 MOZ_ASSERT(aParent.IsContent() && aParent.GetParent(),
195 "This node can't be a child of the document or of "
196 "the document root");
198 nsresult rv = nsTextNode::BindToTree(aContext, aParent);
199 NS_ENSURE_SUCCESS(rv, rv);
201 NS_ASSERTION(!mGrandparent, "We were already bound!");
202 mGrandparent = aParent.GetParent()->AsElement();
203 mGrandparent->AddMutationObserver(this);
205 // Note that there is no need to notify here, since we have no
206 // frame yet at this point.
207 UpdateText(false);
209 return NS_OK;
212 void nsAttributeTextNode::UnbindFromTree(bool aNullParent) {
213 // UnbindFromTree can be called anytime so we have to be safe.
214 if (mGrandparent) {
215 // aNullParent might not be true here, but we want to remove the
216 // mutation observer anyway since we only need it while we're
217 // in the document.
218 mGrandparent->RemoveMutationObserver(this);
219 mGrandparent = nullptr;
221 nsTextNode::UnbindFromTree(aNullParent);
224 void nsAttributeTextNode::AttributeChanged(Element* aElement,
225 int32_t aNameSpaceID,
226 nsAtom* aAttribute, int32_t aModType,
227 const nsAttrValue* aOldValue) {
228 if (aNameSpaceID == mNameSpaceID && aAttribute == mAttrName &&
229 aElement == mGrandparent) {
230 // Since UpdateText notifies, do it when it's safe to run script. Note
231 // that if we get unbound while the event is up that's ok -- we'll just
232 // have no grandparent when it fires, and will do nothing.
233 void (nsAttributeTextNode::*update)() = &nsAttributeTextNode::UpdateText;
234 nsContentUtils::AddScriptRunner(NewRunnableMethod(
235 "nsAttributeTextNode::AttributeChanged", this, update));
239 void nsAttributeTextNode::NodeWillBeDestroyed(nsINode* aNode) {
240 NS_ASSERTION(aNode == static_cast<nsINode*>(mGrandparent), "Wrong node!");
241 mGrandparent = nullptr;
244 void nsAttributeTextNode::UpdateText(bool aNotify) {
245 if (mGrandparent) {
246 nsAutoString attrValue;
248 if (!mGrandparent->GetAttr(mNameSpaceID, mAttrName, attrValue)) {
249 // Attr value does not exist, use fallback instead
250 mFallback->ToString(attrValue);
253 SetText(attrValue, aNotify);