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