Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
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/. */
6 /*
7 * Base class for DOM Core's nsIDOMComment, nsIDOMDocumentType, nsIDOMText,
8 * nsIDOMCDATASection, and nsIDOMProcessingInstruction nodes.
9 */
11 #ifndef nsGenericDOMDataNode_h___
12 #define nsGenericDOMDataNode_h___
14 #include "mozilla/Attributes.h"
15 #include "nsIContent.h"
17 #include "nsTextFragment.h"
18 #include "nsError.h"
19 #include "mozilla/dom/Element.h"
20 #include "nsCycleCollectionParticipant.h"
22 #include "nsISMILAttr.h"
23 #include "nsIDocument.h"
25 class nsIDOMAttr;
26 class nsIDOMEventListener;
27 class nsIDOMNodeList;
28 class nsIFrame;
29 class nsIDOMText;
30 class nsINodeInfo;
31 class nsURI;
33 #define DATA_NODE_FLAG_BIT(n_) NODE_FLAG_BIT(NODE_TYPE_SPECIFIC_BITS_OFFSET + (n_))
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),
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),
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),
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 };
56 // Make sure we have enough space for those bits
57 ASSERT_NODE_FLAGS_SPACE(NODE_TYPE_SPECIFIC_BITS_OFFSET + 4);
59 #undef DATA_NODE_FLAG_BIT
61 class nsGenericDOMDataNode : public nsIContent
62 {
63 public:
64 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
66 NS_DECL_SIZEOF_EXCLUDING_THIS
68 nsGenericDOMDataNode(already_AddRefed<nsINodeInfo>& aNodeInfo);
69 nsGenericDOMDataNode(already_AddRefed<nsINodeInfo>&& aNodeInfo);
70 virtual ~nsGenericDOMDataNode();
72 virtual void GetNodeValueInternal(nsAString& aNodeValue) MOZ_OVERRIDE;
73 virtual void SetNodeValueInternal(const nsAString& aNodeValue,
74 mozilla::ErrorResult& aError) MOZ_OVERRIDE;
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();
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 }
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;
116 virtual already_AddRefed<nsINodeList> GetChildren(uint32_t aFilter) MOZ_OVERRIDE;
118 virtual nsIAtom *GetIDAttributeName() const MOZ_OVERRIDE;
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;
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
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;
168 virtual mozilla::dom::CustomElementData* GetCustomElementData() const MOZ_OVERRIDE;
169 virtual void SetCustomElementData(mozilla::dom::CustomElementData* aData) MOZ_OVERRIDE;
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;
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 }
186 NS_ADDREF(*aResult);
188 return NS_OK;
189 }
191 nsresult SplitData(uint32_t aOffset, nsIContent** aReturn,
192 bool aCloneAfterOriginal = true);
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 }
222 //----------------------------------------
224 #ifdef DEBUG
225 void ToCString(nsAString& aBuf, int32_t aOffset, int32_t aLen) const;
226 #endif
228 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(nsGenericDOMDataNode)
230 protected:
231 virtual mozilla::dom::Element* GetNameSpaceElement()
232 {
233 nsINode *parent = GetParentNode();
235 return parent && parent->IsElement() ? parent->AsElement() : nullptr;
236 }
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();
251 void Traverse(nsCycleCollectionTraversalCallback &cb);
252 void Unlink();
254 /**
255 * The nearest enclosing content node with a binding that created us.
256 * @see nsIContent::GetBindingParent
257 */
258 nsIContent* mBindingParent; // [Weak]
260 /**
261 * @see nsIContent::GetXBLInsertionParent
262 */
263 nsCOMPtr<nsIContent> mXBLInsertionParent;
265 /**
266 * @see nsIContent::GetContainingShadow
267 */
268 nsRefPtr<mozilla::dom::ShadowRoot> mContainingShadow;
269 };
271 // Override from nsINode
272 virtual nsINode::nsSlots* CreateSlots() MOZ_OVERRIDE;
274 nsDataSlots* DataSlots()
275 {
276 return static_cast<nsDataSlots*>(Slots());
277 }
279 nsDataSlots *GetExistingDataSlots() const
280 {
281 return static_cast<nsDataSlots*>(GetExistingSlots());
282 }
284 nsresult SplitText(uint32_t aOffset, nsIDOMText** aReturn);
286 nsresult GetWholeText(nsAString& aWholeText);
288 static int32_t FirstLogicallyAdjacentTextNode(nsIContent* aParent,
289 int32_t aIndex);
291 static int32_t LastLogicallyAdjacentTextNode(nsIContent* aParent,
292 int32_t aIndex,
293 uint32_t aCount);
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);
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;
311 nsTextFragment mText;
313 public:
314 virtual bool OwnedOnlyByTheDOMTree() MOZ_OVERRIDE
315 {
316 return GetParent() && mRefCnt.get() == 1;
317 }
319 virtual bool IsPurple() MOZ_OVERRIDE
320 {
321 return mRefCnt.IsPurple();
322 }
323 virtual void RemovePurple() MOZ_OVERRIDE
324 {
325 mRefCnt.RemovePurple();
326 }
328 private:
329 already_AddRefed<nsIAtom> GetCurrentValueAtom();
330 };
332 #endif /* nsGenericDOMDataNode_h___ */