Bug 1842773 - Part 5: Add ArrayBuffer.prototype.{maxByteLength,resizable} getters...
[gecko.git] / dom / base / CharacterData.h
blob8e008b134b3016c4cd2d54548ac588d815b9d83e
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 * Base class for DOM Core's Comment, DocumentType, Text,
9 * CDATASection, and ProcessingInstruction nodes.
12 #ifndef mozilla_dom_CharacterData_h
13 #define mozilla_dom_CharacterData_h
15 #include "mozilla/Attributes.h"
16 #include "nsIContent.h"
17 #include "nsIMutationObserver.h"
19 #include "nsTextFragment.h"
20 #include "nsError.h"
21 #include "nsCycleCollectionParticipant.h"
23 namespace mozilla::dom {
24 class Element;
25 class HTMLSlotElement;
26 } // namespace mozilla::dom
28 #define CHARACTER_DATA_FLAG_BIT(n_) \
29 NODE_FLAG_BIT(NODE_TYPE_SPECIFIC_BITS_OFFSET + (n_))
31 // Data node specific flags
32 enum {
33 // This bit is set to indicate that if the text node changes to
34 // non-whitespace, we may need to create a frame for it. This bit must
35 // not be set on nodes that already have a frame.
36 NS_CREATE_FRAME_IF_NON_WHITESPACE = CHARACTER_DATA_FLAG_BIT(0),
38 // This bit is set to indicate that if the text node changes to
39 // whitespace, we may need to reframe it (or its ancestors).
40 NS_REFRAME_IF_WHITESPACE = CHARACTER_DATA_FLAG_BIT(1),
42 // This bit is set to indicate that we have a cached
43 // TextIsOnlyWhitespace value
44 NS_CACHED_TEXT_IS_ONLY_WHITESPACE = CHARACTER_DATA_FLAG_BIT(2),
46 // This bit is only meaningful if the NS_CACHED_TEXT_IS_ONLY_WHITESPACE
47 // bit is set, and if so it indicates whether we're only whitespace or
48 // not.
49 NS_TEXT_IS_ONLY_WHITESPACE = CHARACTER_DATA_FLAG_BIT(3),
51 // This bit is set if there is a NewlineProperty attached to the node
52 // (used by nsTextFrame).
53 NS_HAS_NEWLINE_PROPERTY = CHARACTER_DATA_FLAG_BIT(4),
55 // This bit is set if there is a FlowLengthProperty attached to the node
56 // (used by nsTextFrame).
57 NS_HAS_FLOWLENGTH_PROPERTY = CHARACTER_DATA_FLAG_BIT(5),
59 // This bit is set if the node may be modified frequently. This is typically
60 // specified if the instance is in <input> or <textarea>.
61 NS_MAYBE_MODIFIED_FREQUENTLY = CHARACTER_DATA_FLAG_BIT(6),
63 // This bit is set if the node may be masked because of being in a password
64 // field.
65 NS_MAYBE_MASKED = CHARACTER_DATA_FLAG_BIT(7),
68 // Make sure we have enough space for those bits
69 ASSERT_NODE_FLAGS_SPACE(NODE_TYPE_SPECIFIC_BITS_OFFSET + 8);
71 #undef CHARACTER_DATA_FLAG_BIT
73 namespace mozilla::dom {
75 class CharacterData : public nsIContent {
76 public:
77 // We want to avoid the overhead of extra function calls for
78 // refcounting when we're not doing refcount logging, so we can't
79 // NS_DECL_ISUPPORTS_INHERITED.
80 NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
81 NS_INLINE_DECL_REFCOUNTING_INHERITED(CharacterData, nsIContent);
83 NS_DECL_ADDSIZEOFEXCLUDINGTHIS
85 explicit CharacterData(already_AddRefed<dom::NodeInfo>&& aNodeInfo);
87 void MarkAsMaybeModifiedFrequently() {
88 SetFlags(NS_MAYBE_MODIFIED_FREQUENTLY);
90 void MarkAsMaybeMasked() { SetFlags(NS_MAYBE_MASKED); }
92 NS_IMPL_FROMNODE_HELPER(CharacterData, IsCharacterData())
94 void GetNodeValueInternal(nsAString& aNodeValue) override;
95 void SetNodeValueInternal(const nsAString& aNodeValue,
96 ErrorResult& aError) override;
98 void GetTextContentInternal(nsAString& aTextContent, OOMReporter&) final {
99 GetNodeValue(aTextContent);
102 void SetTextContentInternal(const nsAString& aTextContent,
103 nsIPrincipal* aSubjectPrincipal,
104 ErrorResult& aError) final;
106 // Implementation for nsIContent
107 nsresult BindToTree(BindContext&, nsINode& aParent) override;
109 void UnbindFromTree(bool aNullParent = true) override;
111 const nsTextFragment* GetText() override { return &mText; }
112 uint32_t TextLength() const final { return TextDataLength(); }
114 const nsTextFragment& TextFragment() const { return mText; }
115 uint32_t TextDataLength() const { return mText.GetLength(); }
118 * Set the text to the given value. If aNotify is true then
119 * the document is notified of the content change.
121 nsresult SetText(const char16_t* aBuffer, uint32_t aLength, bool aNotify);
123 * Append the given value to the current text. If aNotify is true then
124 * the document is notified of the content change.
126 nsresult SetText(const nsAString& aStr, bool aNotify) {
127 return SetText(aStr.BeginReading(), aStr.Length(), aNotify);
131 * Append the given value to the current text. If aNotify is true then
132 * the document is notified of the content change.
134 nsresult AppendText(const char16_t* aBuffer, uint32_t aLength, bool aNotify);
136 bool TextIsOnlyWhitespace() final;
137 bool ThreadSafeTextIsOnlyWhitespace() const final;
140 * Append the text content to aResult.
142 void AppendTextTo(nsAString& aResult) const { mText.AppendTo(aResult); }
145 * Append the text content to aResult.
147 [[nodiscard]] bool AppendTextTo(nsAString& aResult,
148 const fallible_t& aFallible) const {
149 return mText.AppendTo(aResult, aFallible);
152 void SaveSubtreeState() final {}
154 #ifdef MOZ_DOM_LIST
155 void ToCString(nsAString& aBuf, int32_t aOffset, int32_t aLen) const;
157 void List(FILE* out, int32_t aIndent) const override {}
159 void DumpContent(FILE* out, int32_t aIndent, bool aDumpAll) const override {}
160 #endif
162 nsresult Clone(dom::NodeInfo* aNodeInfo, nsINode** aResult) const override {
163 RefPtr<CharacterData> result = CloneDataNode(aNodeInfo, true);
164 result.forget(aResult);
166 if (!*aResult) {
167 return NS_ERROR_OUT_OF_MEMORY;
170 return NS_OK;
173 // WebIDL API
174 void GetData(nsAString& aData) const;
175 virtual void SetData(const nsAString& aData, ErrorResult& rv);
176 // nsINode::Length() returns the right thing for our length attribute
177 void SubstringData(uint32_t aStart, uint32_t aCount, nsAString& aReturn,
178 ErrorResult& rv);
179 void AppendData(const nsAString& aData, ErrorResult& rv);
180 void InsertData(uint32_t aOffset, const nsAString& aData, ErrorResult& rv);
181 void DeleteData(uint32_t aOffset, uint32_t aCount, ErrorResult& rv);
182 void ReplaceData(uint32_t aOffset, uint32_t aCount, const nsAString& aData,
183 ErrorResult& rv);
185 //----------------------------------------
187 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_WRAPPERCACHE_CLASS_INHERITED(CharacterData,
188 nsIContent)
191 * Compare two CharacterData nodes for text equality.
193 [[nodiscard]] bool TextEquals(const CharacterData* aOther) const {
194 return mText.TextEquals(aOther->mText);
197 protected:
198 virtual ~CharacterData();
200 Element* GetNameSpaceElement() final;
202 nsresult SetTextInternal(
203 uint32_t aOffset, uint32_t aCount, const char16_t* aBuffer,
204 uint32_t aLength, bool aNotify,
205 CharacterDataChangeInfo::Details* aDetails = nullptr);
208 * Method to clone this node. This needs to be overriden by all derived
209 * classes. If aCloneText is true the text content will be cloned too.
211 * @param aOwnerDocument the ownerDocument of the clone
212 * @param aCloneText if true the text content will be cloned too
213 * @return the clone
215 virtual already_AddRefed<CharacterData> CloneDataNode(
216 dom::NodeInfo* aNodeInfo, bool aCloneText) const = 0;
218 nsTextFragment mText;
220 private:
221 already_AddRefed<nsAtom> GetCurrentValueAtom();
224 } // namespace mozilla::dom
226 #endif /* mozilla_dom_CharacterData_h */