1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Communicator client code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : /*
39 : * Implementation of DOM Core's nsIDOMText node.
40 : */
41 :
42 : #include "nsTextNode.h"
43 : #include "nsContentUtils.h"
44 : #include "nsIDOMEventListener.h"
45 : #include "nsIDOMMutationEvent.h"
46 : #include "nsIAttribute.h"
47 : #include "nsIDocument.h"
48 : #include "nsThreadUtils.h"
49 : #ifdef DEBUG
50 : #include "nsRange.h"
51 : #endif
52 :
53 : using namespace mozilla::dom;
54 :
55 : /**
56 : * class used to implement attr() generated content
57 : */
58 : class nsAttributeTextNode : public nsTextNode,
59 : public nsStubMutationObserver
60 : {
61 : public:
62 : NS_DECL_ISUPPORTS_INHERITED
63 :
64 0 : nsAttributeTextNode(already_AddRefed<nsINodeInfo> aNodeInfo,
65 : PRInt32 aNameSpaceID,
66 : nsIAtom* aAttrName) :
67 : nsTextNode(aNodeInfo),
68 : mGrandparent(nsnull),
69 : mNameSpaceID(aNameSpaceID),
70 0 : mAttrName(aAttrName)
71 : {
72 0 : NS_ASSERTION(mNameSpaceID != kNameSpaceID_Unknown, "Must know namespace");
73 0 : NS_ASSERTION(mAttrName, "Must have attr name");
74 0 : }
75 :
76 0 : virtual ~nsAttributeTextNode() {
77 0 : NS_ASSERTION(!mGrandparent, "We were not unbound!");
78 0 : }
79 :
80 : virtual nsresult BindToTree(nsIDocument* aDocument, nsIContent* aParent,
81 : nsIContent* aBindingParent,
82 : bool aCompileEventHandlers);
83 : virtual void UnbindFromTree(bool aDeep = true,
84 : bool aNullParent = true);
85 :
86 : NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTECHANGED
87 : NS_DECL_NSIMUTATIONOBSERVER_NODEWILLBEDESTROYED
88 :
89 0 : virtual nsGenericDOMDataNode *CloneDataNode(nsINodeInfo *aNodeInfo,
90 : bool aCloneText) const
91 : {
92 0 : nsCOMPtr<nsINodeInfo> ni = aNodeInfo;
93 : nsAttributeTextNode *it = new nsAttributeTextNode(ni.forget(),
94 : mNameSpaceID,
95 0 : mAttrName);
96 0 : if (it && aCloneText) {
97 0 : it->mText = mText;
98 : }
99 :
100 0 : return it;
101 : }
102 :
103 : // Public method for the event to run
104 0 : void UpdateText() {
105 0 : UpdateText(true);
106 0 : }
107 :
108 : private:
109 : // Update our text to our parent's current attr value
110 : void UpdateText(bool aNotify);
111 :
112 : // This doesn't need to be a strong pointer because it's only non-null
113 : // while we're bound to the document tree, and it points to an ancestor
114 : // so the ancestor must be bound to the document tree the whole time
115 : // and can't be deleted.
116 : nsIContent* mGrandparent;
117 : // What attribute we're showing
118 : PRInt32 mNameSpaceID;
119 : nsCOMPtr<nsIAtom> mAttrName;
120 : };
121 :
122 : nsresult
123 72639 : NS_NewTextNode(nsIContent** aInstancePtrResult,
124 : nsNodeInfoManager *aNodeInfoManager)
125 : {
126 72639 : NS_PRECONDITION(aNodeInfoManager, "Missing nodeInfoManager");
127 :
128 72639 : *aInstancePtrResult = nsnull;
129 :
130 145278 : nsCOMPtr<nsINodeInfo> ni = aNodeInfoManager->GetTextNodeInfo();
131 72639 : if (!ni) {
132 0 : return NS_ERROR_OUT_OF_MEMORY;
133 : }
134 :
135 145278 : nsTextNode *instance = new nsTextNode(ni.forget());
136 72639 : if (!instance) {
137 0 : return NS_ERROR_OUT_OF_MEMORY;
138 : }
139 :
140 72639 : NS_ADDREF(*aInstancePtrResult = instance);
141 :
142 72639 : return NS_OK;
143 : }
144 :
145 73023 : nsTextNode::nsTextNode(already_AddRefed<nsINodeInfo> aNodeInfo)
146 73023 : : nsGenericDOMDataNode(aNodeInfo)
147 : {
148 73023 : NS_ABORT_IF_FALSE(mNodeInfo->NodeType() == nsIDOMNode::TEXT_NODE,
149 : "Bad NodeType in aNodeInfo");
150 73023 : }
151 :
152 146042 : nsTextNode::~nsTextNode()
153 : {
154 292084 : }
155 :
156 466384 : NS_IMPL_ADDREF_INHERITED(nsTextNode, nsGenericDOMDataNode)
157 466382 : NS_IMPL_RELEASE_INHERITED(nsTextNode, nsGenericDOMDataNode)
158 :
159 5170 : DOMCI_NODE_DATA(Text, nsTextNode)
160 :
161 : // QueryInterface implementation for nsTextNode
162 1670379 : NS_INTERFACE_TABLE_HEAD(nsTextNode)
163 1670379 : NS_NODE_INTERFACE_TABLE3(nsTextNode, nsIDOMNode, nsIDOMText,
164 : nsIDOMCharacterData)
165 1638506 : NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(nsTextNode)
166 156222 : NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(Text)
167 155845 : NS_INTERFACE_MAP_END_INHERITING(nsGenericDOMDataNode)
168 :
169 : bool
170 168686 : nsTextNode::IsNodeOfType(PRUint32 aFlags) const
171 : {
172 168686 : return !(aFlags & ~(eCONTENT | eTEXT | eDATA_NODE));
173 : }
174 :
175 : nsGenericDOMDataNode*
176 384 : nsTextNode::CloneDataNode(nsINodeInfo *aNodeInfo, bool aCloneText) const
177 : {
178 768 : nsCOMPtr<nsINodeInfo> ni = aNodeInfo;
179 768 : nsTextNode *it = new nsTextNode(ni.forget());
180 384 : if (it && aCloneText) {
181 382 : it->mText = mText;
182 : }
183 :
184 384 : return it;
185 : }
186 :
187 : nsresult
188 984 : nsTextNode::BindToAttribute(nsIAttribute* aAttr)
189 : {
190 984 : NS_ASSERTION(!IsInDoc(), "Unbind before binding!");
191 984 : NS_ASSERTION(!GetNodeParent(), "Unbind before binding!");
192 984 : NS_ASSERTION(HasSameOwnerDoc(aAttr), "Wrong owner document!");
193 :
194 984 : mParent = aAttr;
195 984 : SetParentIsContent(false);
196 984 : ClearInDocument();
197 984 : return NS_OK;
198 : }
199 :
200 : nsresult
201 984 : nsTextNode::UnbindFromAttribute()
202 : {
203 984 : NS_ASSERTION(GetNodeParent(), "Bind before unbinding!");
204 984 : NS_ASSERTION(GetNodeParent() &&
205 : GetNodeParent()->IsNodeOfType(nsINode::eATTRIBUTE),
206 : "Use this method only to unbind from an attribute!");
207 984 : mParent = nsnull;
208 984 : return NS_OK;
209 : }
210 :
211 : nsresult
212 9 : nsTextNode::AppendTextForNormalize(const PRUnichar* aBuffer, PRUint32 aLength,
213 : bool aNotify, nsIContent* aNextSibling)
214 : {
215 : CharacterDataChangeInfo::Details details = {
216 : CharacterDataChangeInfo::Details::eMerge, aNextSibling
217 9 : };
218 9 : return SetTextInternal(mText.GetLength(), 0, aBuffer, aLength, aNotify, &details);
219 : }
220 :
221 : #ifdef DEBUG
222 : void
223 0 : nsTextNode::List(FILE* out, PRInt32 aIndent) const
224 : {
225 : PRInt32 index;
226 0 : for (index = aIndent; --index >= 0; ) fputs(" ", out);
227 :
228 0 : fprintf(out, "Text@%p", static_cast<const void*>(this));
229 0 : fprintf(out, " flags=[%08x]", static_cast<unsigned int>(GetFlags()));
230 0 : if (IsCommonAncestorForRangeInSelection()) {
231 : typedef nsTHashtable<nsPtrHashKey<nsRange> > RangeHashTable;
232 : RangeHashTable* ranges =
233 0 : static_cast<RangeHashTable*>(GetProperty(nsGkAtoms::range));
234 0 : fprintf(out, " ranges:%d", ranges ? ranges->Count() : 0);
235 : }
236 0 : fprintf(out, " primaryframe=%p", static_cast<void*>(GetPrimaryFrame()));
237 0 : fprintf(out, " refcount=%d<", mRefCnt.get());
238 :
239 0 : nsAutoString tmp;
240 0 : ToCString(tmp, 0, mText.GetLength());
241 0 : fputs(NS_LossyConvertUTF16toASCII(tmp).get(), out);
242 :
243 0 : fputs(">\n", out);
244 0 : }
245 :
246 : void
247 0 : nsTextNode::DumpContent(FILE* out, PRInt32 aIndent, bool aDumpAll) const
248 : {
249 0 : if(aDumpAll) {
250 : PRInt32 index;
251 0 : for (index = aIndent; --index >= 0; ) fputs(" ", out);
252 :
253 0 : nsAutoString tmp;
254 0 : ToCString(tmp, 0, mText.GetLength());
255 :
256 0 : if(!tmp.EqualsLiteral("\\n")) {
257 0 : fputs(NS_LossyConvertUTF16toASCII(tmp).get(), out);
258 0 : if(aIndent) fputs("\n", out);
259 : }
260 : }
261 0 : }
262 : #endif
263 :
264 : nsresult
265 0 : NS_NewAttributeContent(nsNodeInfoManager *aNodeInfoManager,
266 : PRInt32 aNameSpaceID, nsIAtom* aAttrName,
267 : nsIContent** aResult)
268 : {
269 0 : NS_PRECONDITION(aNodeInfoManager, "Missing nodeInfoManager");
270 0 : NS_PRECONDITION(aAttrName, "Must have an attr name");
271 0 : NS_PRECONDITION(aNameSpaceID != kNameSpaceID_Unknown, "Must know namespace");
272 :
273 0 : *aResult = nsnull;
274 :
275 0 : nsCOMPtr<nsINodeInfo> ni = aNodeInfoManager->GetTextNodeInfo();
276 0 : if (!ni) {
277 0 : return NS_ERROR_OUT_OF_MEMORY;
278 : }
279 :
280 : nsAttributeTextNode* textNode = new nsAttributeTextNode(ni.forget(),
281 : aNameSpaceID,
282 0 : aAttrName);
283 0 : if (!textNode) {
284 0 : return NS_ERROR_OUT_OF_MEMORY;
285 : }
286 :
287 0 : NS_ADDREF(*aResult = textNode);
288 :
289 0 : return NS_OK;
290 : }
291 :
292 0 : NS_IMPL_ISUPPORTS_INHERITED1(nsAttributeTextNode, nsTextNode,
293 : nsIMutationObserver)
294 :
295 : nsresult
296 0 : nsAttributeTextNode::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
297 : nsIContent* aBindingParent,
298 : bool aCompileEventHandlers)
299 : {
300 0 : NS_PRECONDITION(aParent && aParent->GetParent(),
301 : "This node can't be a child of the document or of the document root");
302 :
303 : nsresult rv = nsTextNode::BindToTree(aDocument, aParent,
304 0 : aBindingParent, aCompileEventHandlers);
305 0 : NS_ENSURE_SUCCESS(rv, rv);
306 :
307 0 : NS_ASSERTION(!mGrandparent, "We were already bound!");
308 0 : mGrandparent = aParent->GetParent();
309 0 : mGrandparent->AddMutationObserver(this);
310 :
311 : // Note that there is no need to notify here, since we have no
312 : // frame yet at this point.
313 0 : UpdateText(false);
314 :
315 0 : return NS_OK;
316 : }
317 :
318 : void
319 0 : nsAttributeTextNode::UnbindFromTree(bool aDeep, bool aNullParent)
320 : {
321 : // UnbindFromTree can be called anytime so we have to be safe.
322 0 : if (mGrandparent) {
323 : // aNullParent might not be true here, but we want to remove the
324 : // mutation observer anyway since we only need it while we're
325 : // in the document.
326 0 : mGrandparent->RemoveMutationObserver(this);
327 0 : mGrandparent = nsnull;
328 : }
329 0 : nsTextNode::UnbindFromTree(aDeep, aNullParent);
330 0 : }
331 :
332 : void
333 0 : nsAttributeTextNode::AttributeChanged(nsIDocument* aDocument,
334 : Element* aElement,
335 : PRInt32 aNameSpaceID,
336 : nsIAtom* aAttribute,
337 : PRInt32 aModType)
338 : {
339 0 : if (aNameSpaceID == mNameSpaceID && aAttribute == mAttrName &&
340 : aElement == mGrandparent) {
341 : // Since UpdateText notifies, do it when it's safe to run script. Note
342 : // that if we get unbound while the event is up that's ok -- we'll just
343 : // have no grandparent when it fires, and will do nothing.
344 0 : void (nsAttributeTextNode::*update)() = &nsAttributeTextNode::UpdateText;
345 0 : nsCOMPtr<nsIRunnable> ev = NS_NewRunnableMethod(this, update);
346 0 : nsContentUtils::AddScriptRunner(ev);
347 : }
348 0 : }
349 :
350 : void
351 0 : nsAttributeTextNode::NodeWillBeDestroyed(const nsINode* aNode)
352 : {
353 0 : NS_ASSERTION(aNode == static_cast<nsINode*>(mGrandparent), "Wrong node!");
354 0 : mGrandparent = nsnull;
355 0 : }
356 :
357 : void
358 0 : nsAttributeTextNode::UpdateText(bool aNotify)
359 : {
360 0 : if (mGrandparent) {
361 0 : nsAutoString attrValue;
362 0 : mGrandparent->GetAttr(mNameSpaceID, mAttrName, attrValue);
363 0 : SetText(attrValue, aNotify);
364 : }
365 0 : }
|