Line data Source code
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/. */
6 :
7 : /*
8 : * Base class for DOM Core's nsIDOMComment, nsIDOMDocumentType, nsIDOMText,
9 : * nsIDOMCDATASection, and nsIDOMProcessingInstruction nodes.
10 : */
11 :
12 : #ifndef nsGenericDOMDataNode_h___
13 : #define nsGenericDOMDataNode_h___
14 :
15 : #include "mozilla/Attributes.h"
16 : #include "nsIContent.h"
17 :
18 : #include "nsTextFragment.h"
19 : #include "nsError.h"
20 : #include "mozilla/dom/Element.h"
21 : #include "nsCycleCollectionParticipant.h"
22 :
23 : #include "nsISMILAttr.h"
24 : #include "mozilla/dom/ShadowRoot.h"
25 :
26 : class nsIDocument;
27 : class nsIDOMText;
28 :
29 : #define DATA_NODE_FLAG_BIT(n_) NODE_FLAG_BIT(NODE_TYPE_SPECIFIC_BITS_OFFSET + (n_))
30 :
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 = DATA_NODE_FLAG_BIT(0),
37 :
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 = DATA_NODE_FLAG_BIT(1),
41 :
42 : // This bit is set to indicate that we have a cached
43 : // TextIsOnlyWhitespace value
44 : NS_CACHED_TEXT_IS_ONLY_WHITESPACE = DATA_NODE_FLAG_BIT(2),
45 :
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 = DATA_NODE_FLAG_BIT(3),
50 : };
51 :
52 : // Make sure we have enough space for those bits
53 : ASSERT_NODE_FLAGS_SPACE(NODE_TYPE_SPECIFIC_BITS_OFFSET + 4);
54 :
55 : #undef DATA_NODE_FLAG_BIT
56 :
57 : class nsGenericDOMDataNode : public nsIContent
58 : {
59 : public:
60 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
61 :
62 : NS_DECL_SIZEOF_EXCLUDING_THIS
63 :
64 : explicit nsGenericDOMDataNode(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo);
65 : explicit nsGenericDOMDataNode(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo);
66 :
67 : virtual void GetNodeValueInternal(nsAString& aNodeValue) override;
68 : virtual void SetNodeValueInternal(const nsAString& aNodeValue,
69 : mozilla::ErrorResult& aError) override;
70 :
71 : // Implementation for nsIDOMCharacterData
72 : nsresult GetData(nsAString& aData) const;
73 : nsresult SetData(const nsAString& aData);
74 : nsresult GetLength(uint32_t* aLength);
75 : nsresult SubstringData(uint32_t aOffset, uint32_t aCount,
76 : nsAString& aReturn);
77 : nsresult AppendData(const nsAString& aArg);
78 : nsresult InsertData(uint32_t aOffset, const nsAString& aArg);
79 : nsresult DeleteData(uint32_t aOffset, uint32_t aCount);
80 : nsresult ReplaceData(uint32_t aOffset, uint32_t aCount,
81 : const nsAString& aArg);
82 :
83 : // nsINode methods
84 : virtual uint32_t GetChildCount() const override;
85 : virtual nsIContent *GetChildAt(uint32_t aIndex) const override;
86 : virtual nsIContent * const * GetChildArray(uint32_t* aChildCount) const override;
87 : virtual int32_t IndexOf(const nsINode* aPossibleChild) const override;
88 : virtual nsresult InsertChildAt(nsIContent* aKid, uint32_t aIndex,
89 : bool aNotify) override;
90 : virtual void RemoveChildAt(uint32_t aIndex, bool aNotify) override;
91 2 : virtual void GetTextContentInternal(nsAString& aTextContent,
92 : mozilla::OOMReporter& aError) override
93 : {
94 2 : GetNodeValue(aTextContent);
95 2 : }
96 0 : virtual void SetTextContentInternal(const nsAString& aTextContent,
97 : mozilla::ErrorResult& aError) override
98 : {
99 : // Batch possible DOMSubtreeModified events.
100 0 : mozAutoSubtreeModified subtree(OwnerDoc(), nullptr);
101 0 : return SetNodeValue(aTextContent, aError);
102 : }
103 :
104 : // Implementation for nsIContent
105 : virtual nsresult BindToTree(nsIDocument* aDocument, nsIContent* aParent,
106 : nsIContent* aBindingParent,
107 : bool aCompileEventHandlers) override;
108 : virtual void UnbindFromTree(bool aDeep = true,
109 : bool aNullParent = true) override;
110 :
111 : virtual already_AddRefed<nsINodeList> GetChildren(uint32_t aFilter) override;
112 :
113 :
114 : nsresult SetAttr(int32_t aNameSpaceID, nsIAtom* aName,
115 : const nsAString& aValue, bool aNotify)
116 : {
117 : return SetAttr(aNameSpaceID, aName, nullptr, aValue, aNotify);
118 : }
119 : virtual nsresult SetAttr(int32_t aNameSpaceID, nsIAtom* aAttribute,
120 : nsIAtom* aPrefix, const nsAString& aValue,
121 : bool aNotify) override;
122 : virtual nsresult UnsetAttr(int32_t aNameSpaceID, nsIAtom* aAttribute,
123 : bool aNotify) override;
124 : virtual const nsAttrName* GetAttrNameAt(uint32_t aIndex) const override;
125 : virtual mozilla::dom::BorrowedAttrInfo GetAttrInfoAt(uint32_t aIndex) const override;
126 : virtual uint32_t GetAttrCount() const override;
127 : virtual const nsTextFragment *GetText() override;
128 : virtual uint32_t TextLength() const override;
129 : virtual nsresult SetText(const char16_t* aBuffer, uint32_t aLength,
130 : bool aNotify) override;
131 : // Need to implement this here too to avoid hiding.
132 140 : nsresult SetText(const nsAString& aStr, bool aNotify)
133 : {
134 140 : return SetText(aStr.BeginReading(), aStr.Length(), aNotify);
135 : }
136 : virtual nsresult AppendText(const char16_t* aBuffer, uint32_t aLength,
137 : bool aNotify) override;
138 : virtual bool TextIsOnlyWhitespace() override;
139 : virtual bool ThreadSafeTextIsOnlyWhitespace() const final;
140 : virtual bool HasTextForTranslation() override;
141 : virtual void AppendTextTo(nsAString& aResult) override;
142 : MOZ_MUST_USE
143 : virtual bool AppendTextTo(nsAString& aResult,
144 : const mozilla::fallible_t&) override;
145 : virtual void SaveSubtreeState() override;
146 :
147 : #ifdef DEBUG
148 : virtual void List(FILE* out, int32_t aIndent) const override;
149 : virtual void DumpContent(FILE* out, int32_t aIndent, bool aDumpAll) const override;
150 : #endif
151 :
152 : virtual nsIContent *GetBindingParent() const override;
153 : virtual nsXBLBinding *GetXBLBinding() const override;
154 : virtual void SetXBLBinding(nsXBLBinding* aBinding,
155 : nsBindingManager* aOldBindingManager = nullptr) override;
156 : virtual mozilla::dom::ShadowRoot *GetContainingShadow() const override;
157 : virtual nsTArray<nsIContent*> &DestInsertionPoints() override;
158 : virtual nsTArray<nsIContent*> *GetExistingDestInsertionPoints() const override;
159 : virtual void SetShadowRoot(mozilla::dom::ShadowRoot* aShadowRoot) override;
160 : virtual nsIContent *GetXBLInsertionParent() const override;
161 : virtual void SetXBLInsertionParent(nsIContent* aContent) override;
162 : virtual bool IsNodeOfType(uint32_t aFlags) const override;
163 : virtual bool IsLink(nsIURI** aURI) const override;
164 :
165 : NS_IMETHOD WalkContentStyleRules(nsRuleWalker* aRuleWalker) override;
166 : NS_IMETHOD_(bool) IsAttributeMapped(const nsIAtom* aAttribute) const;
167 : virtual nsChangeHint GetAttributeChangeHint(const nsIAtom* aAttribute,
168 : int32_t aModType) const;
169 :
170 23 : virtual nsresult Clone(mozilla::dom::NodeInfo *aNodeInfo, nsINode **aResult,
171 : bool aPreallocateChildren) const override
172 : {
173 46 : nsCOMPtr<nsINode> result = CloneDataNode(aNodeInfo, true);
174 23 : result.forget(aResult);
175 :
176 23 : if (!*aResult) {
177 0 : return NS_ERROR_OUT_OF_MEMORY;
178 : }
179 :
180 23 : return NS_OK;
181 : }
182 :
183 : nsresult SplitData(uint32_t aOffset, nsIContent** aReturn,
184 : bool aCloneAfterOriginal = true);
185 :
186 : // WebIDL API
187 : // Our XPCOM GetData is just fine for WebIDL
188 0 : virtual void SetData(const nsAString& aData, mozilla::ErrorResult& rv)
189 : {
190 0 : rv = SetData(aData);
191 0 : }
192 : // nsINode::Length() returns the right thing for our length attribute
193 : void SubstringData(uint32_t aStart, uint32_t aCount, nsAString& aReturn,
194 : mozilla::ErrorResult& rv);
195 0 : void AppendData(const nsAString& aData, mozilla::ErrorResult& rv)
196 : {
197 0 : rv = AppendData(aData);
198 0 : }
199 0 : void InsertData(uint32_t aOffset, const nsAString& aData,
200 : mozilla::ErrorResult& rv)
201 : {
202 0 : rv = InsertData(aOffset, aData);
203 0 : }
204 0 : void DeleteData(uint32_t aOffset, uint32_t aCount, mozilla::ErrorResult& rv)
205 : {
206 0 : rv = DeleteData(aOffset, aCount);
207 0 : }
208 0 : void ReplaceData(uint32_t aOffset, uint32_t aCount, const nsAString& aData,
209 : mozilla::ErrorResult& rv)
210 : {
211 0 : rv = ReplaceData(aOffset, aCount, aData);
212 0 : }
213 :
214 : //----------------------------------------
215 :
216 : #ifdef DEBUG
217 : void ToCString(nsAString& aBuf, int32_t aOffset, int32_t aLen) const;
218 : #endif
219 :
220 3626 : NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(nsGenericDOMDataNode)
221 :
222 : protected:
223 : virtual ~nsGenericDOMDataNode();
224 :
225 0 : virtual mozilla::dom::Element* GetNameSpaceElement() override
226 : {
227 0 : nsINode *parent = GetParentNode();
228 :
229 0 : return parent && parent->IsElement() ? parent->AsElement() : nullptr;
230 : }
231 :
232 : /**
233 : * There are a set of DOM- and scripting-specific instance variables
234 : * that may only be instantiated when a content object is accessed
235 : * through the DOM. Rather than burn actual slots in the content
236 : * objects for each of these instance variables, we put them off
237 : * in a side structure that's only allocated when the content is
238 : * accessed through the DOM.
239 : */
240 9 : class nsDataSlots : public nsINode::nsSlots
241 : {
242 : public:
243 : nsDataSlots();
244 :
245 : void Traverse(nsCycleCollectionTraversalCallback &cb);
246 : void Unlink();
247 :
248 : /**
249 : * The nearest enclosing content node with a binding that created us.
250 : * @see nsIContent::GetBindingParent
251 : */
252 : nsIContent* mBindingParent; // [Weak]
253 :
254 : /**
255 : * @see nsIContent::GetXBLInsertionParent
256 : */
257 : nsCOMPtr<nsIContent> mXBLInsertionParent;
258 :
259 : /**
260 : * @see nsIContent::GetContainingShadow
261 : */
262 : RefPtr<mozilla::dom::ShadowRoot> mContainingShadow;
263 :
264 : /**
265 : * @see nsIContent::GetDestInsertionPoints
266 : */
267 : nsTArray<nsIContent*> mDestInsertionPoints;
268 : };
269 :
270 : // Override from nsINode
271 : virtual nsINode::nsSlots* CreateSlots() override;
272 :
273 66 : nsDataSlots* DataSlots()
274 : {
275 66 : return static_cast<nsDataSlots*>(Slots());
276 : }
277 :
278 2075 : nsDataSlots *GetExistingDataSlots() const
279 : {
280 2075 : return static_cast<nsDataSlots*>(GetExistingSlots());
281 : }
282 :
283 : nsresult SplitText(uint32_t aOffset, nsIDOMText** aReturn);
284 :
285 : nsresult GetWholeText(nsAString& aWholeText);
286 :
287 : static int32_t FirstLogicallyAdjacentTextNode(nsIContent* aParent,
288 : int32_t aIndex);
289 :
290 : static int32_t LastLogicallyAdjacentTextNode(nsIContent* aParent,
291 : int32_t aIndex,
292 : uint32_t aCount);
293 :
294 : nsresult SetTextInternal(uint32_t aOffset, uint32_t aCount,
295 : const char16_t* aBuffer, uint32_t aLength,
296 : bool aNotify,
297 : CharacterDataChangeInfo::Details* aDetails = nullptr);
298 :
299 : /**
300 : * Method to clone this node. This needs to be overriden by all derived
301 : * classes. If aCloneText is true the text content will be cloned too.
302 : *
303 : * @param aOwnerDocument the ownerDocument of the clone
304 : * @param aCloneText if true the text content will be cloned too
305 : * @return the clone
306 : */
307 : virtual nsGenericDOMDataNode *CloneDataNode(mozilla::dom::NodeInfo *aNodeInfo,
308 : bool aCloneText) const = 0;
309 :
310 : nsTextFragment mText;
311 :
312 : public:
313 0 : virtual bool OwnedOnlyByTheDOMTree() override
314 : {
315 0 : return GetParent() && mRefCnt.get() == 1;
316 : }
317 :
318 0 : virtual bool IsPurple() override
319 : {
320 0 : return mRefCnt.IsPurple();
321 : }
322 0 : virtual void RemovePurple() override
323 : {
324 0 : mRefCnt.RemovePurple();
325 0 : }
326 :
327 : private:
328 : already_AddRefed<nsIAtom> GetCurrentValueAtom();
329 : };
330 :
331 : #endif /* nsGenericDOMDataNode_h___ */
|