content/base/src/nsTextNode.cpp

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

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.)

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 * Implementation of DOM Core's nsIDOMText node.
michael@0 8 */
michael@0 9
michael@0 10 #include "nsTextNode.h"
michael@0 11 #include "mozilla/dom/TextBinding.h"
michael@0 12 #include "nsContentUtils.h"
michael@0 13 #include "mozilla/dom/DirectionalityUtils.h"
michael@0 14 #include "nsIDOMEventListener.h"
michael@0 15 #include "nsIDOMMutationEvent.h"
michael@0 16 #include "nsIDocument.h"
michael@0 17 #include "nsThreadUtils.h"
michael@0 18 #include "nsStubMutationObserver.h"
michael@0 19 #include "mozilla/IntegerPrintfMacros.h"
michael@0 20 #ifdef DEBUG
michael@0 21 #include "nsRange.h"
michael@0 22 #endif
michael@0 23
michael@0 24 using namespace mozilla;
michael@0 25 using namespace mozilla::dom;
michael@0 26
michael@0 27 /**
michael@0 28 * class used to implement attr() generated content
michael@0 29 */
michael@0 30 class nsAttributeTextNode MOZ_FINAL : public nsTextNode,
michael@0 31 public nsStubMutationObserver
michael@0 32 {
michael@0 33 public:
michael@0 34 NS_DECL_ISUPPORTS_INHERITED
michael@0 35
michael@0 36 nsAttributeTextNode(already_AddRefed<nsINodeInfo>& aNodeInfo,
michael@0 37 int32_t aNameSpaceID,
michael@0 38 nsIAtom* aAttrName) :
michael@0 39 nsTextNode(aNodeInfo),
michael@0 40 mGrandparent(nullptr),
michael@0 41 mNameSpaceID(aNameSpaceID),
michael@0 42 mAttrName(aAttrName)
michael@0 43 {
michael@0 44 NS_ASSERTION(mNameSpaceID != kNameSpaceID_Unknown, "Must know namespace");
michael@0 45 NS_ASSERTION(mAttrName, "Must have attr name");
michael@0 46 }
michael@0 47
michael@0 48 virtual ~nsAttributeTextNode() {
michael@0 49 NS_ASSERTION(!mGrandparent, "We were not unbound!");
michael@0 50 }
michael@0 51
michael@0 52 virtual nsresult BindToTree(nsIDocument* aDocument, nsIContent* aParent,
michael@0 53 nsIContent* aBindingParent,
michael@0 54 bool aCompileEventHandlers);
michael@0 55 virtual void UnbindFromTree(bool aDeep = true,
michael@0 56 bool aNullParent = true);
michael@0 57
michael@0 58 NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTECHANGED
michael@0 59 NS_DECL_NSIMUTATIONOBSERVER_NODEWILLBEDESTROYED
michael@0 60
michael@0 61 virtual nsGenericDOMDataNode *CloneDataNode(nsINodeInfo *aNodeInfo,
michael@0 62 bool aCloneText) const
michael@0 63 {
michael@0 64 already_AddRefed<nsINodeInfo> ni =
michael@0 65 nsCOMPtr<nsINodeInfo>(aNodeInfo).forget();
michael@0 66 nsAttributeTextNode *it = new nsAttributeTextNode(ni,
michael@0 67 mNameSpaceID,
michael@0 68 mAttrName);
michael@0 69 if (it && aCloneText) {
michael@0 70 it->mText = mText;
michael@0 71 }
michael@0 72
michael@0 73 return it;
michael@0 74 }
michael@0 75
michael@0 76 // Public method for the event to run
michael@0 77 void UpdateText() {
michael@0 78 UpdateText(true);
michael@0 79 }
michael@0 80
michael@0 81 private:
michael@0 82 // Update our text to our parent's current attr value
michael@0 83 void UpdateText(bool aNotify);
michael@0 84
michael@0 85 // This doesn't need to be a strong pointer because it's only non-null
michael@0 86 // while we're bound to the document tree, and it points to an ancestor
michael@0 87 // so the ancestor must be bound to the document tree the whole time
michael@0 88 // and can't be deleted.
michael@0 89 nsIContent* mGrandparent;
michael@0 90 // What attribute we're showing
michael@0 91 int32_t mNameSpaceID;
michael@0 92 nsCOMPtr<nsIAtom> mAttrName;
michael@0 93 };
michael@0 94
michael@0 95 nsTextNode::~nsTextNode()
michael@0 96 {
michael@0 97 }
michael@0 98
michael@0 99 NS_IMPL_ISUPPORTS_INHERITED(nsTextNode, nsGenericDOMDataNode, nsIDOMNode,
michael@0 100 nsIDOMText, nsIDOMCharacterData)
michael@0 101
michael@0 102 JSObject*
michael@0 103 nsTextNode::WrapNode(JSContext *aCx)
michael@0 104 {
michael@0 105 return TextBinding::Wrap(aCx, this);
michael@0 106 }
michael@0 107
michael@0 108 bool
michael@0 109 nsTextNode::IsNodeOfType(uint32_t aFlags) const
michael@0 110 {
michael@0 111 return !(aFlags & ~(eCONTENT | eTEXT | eDATA_NODE));
michael@0 112 }
michael@0 113
michael@0 114 nsGenericDOMDataNode*
michael@0 115 nsTextNode::CloneDataNode(nsINodeInfo *aNodeInfo, bool aCloneText) const
michael@0 116 {
michael@0 117 already_AddRefed<nsINodeInfo> ni = nsCOMPtr<nsINodeInfo>(aNodeInfo).forget();
michael@0 118 nsTextNode *it = new nsTextNode(ni);
michael@0 119 if (aCloneText) {
michael@0 120 it->mText = mText;
michael@0 121 }
michael@0 122
michael@0 123 return it;
michael@0 124 }
michael@0 125
michael@0 126 nsresult
michael@0 127 nsTextNode::AppendTextForNormalize(const char16_t* aBuffer, uint32_t aLength,
michael@0 128 bool aNotify, nsIContent* aNextSibling)
michael@0 129 {
michael@0 130 CharacterDataChangeInfo::Details details = {
michael@0 131 CharacterDataChangeInfo::Details::eMerge, aNextSibling
michael@0 132 };
michael@0 133 return SetTextInternal(mText.GetLength(), 0, aBuffer, aLength, aNotify, &details);
michael@0 134 }
michael@0 135
michael@0 136 nsresult
michael@0 137 nsTextNode::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
michael@0 138 nsIContent* aBindingParent, bool aCompileEventHandlers)
michael@0 139 {
michael@0 140 nsresult rv = nsGenericDOMDataNode::BindToTree(aDocument, aParent,
michael@0 141 aBindingParent,
michael@0 142 aCompileEventHandlers);
michael@0 143 NS_ENSURE_SUCCESS(rv, rv);
michael@0 144
michael@0 145 SetDirectionFromNewTextNode(this);
michael@0 146
michael@0 147 return NS_OK;
michael@0 148 }
michael@0 149
michael@0 150 void nsTextNode::UnbindFromTree(bool aDeep, bool aNullParent)
michael@0 151 {
michael@0 152 ResetDirectionSetByTextNode(this, aNullParent);
michael@0 153
michael@0 154 nsGenericDOMDataNode::UnbindFromTree(aDeep, aNullParent);
michael@0 155 }
michael@0 156
michael@0 157 #ifdef DEBUG
michael@0 158 void
michael@0 159 nsTextNode::List(FILE* out, int32_t aIndent) const
michael@0 160 {
michael@0 161 int32_t index;
michael@0 162 for (index = aIndent; --index >= 0; ) fputs(" ", out);
michael@0 163
michael@0 164 fprintf(out, "Text@%p", static_cast<const void*>(this));
michael@0 165 fprintf(out, " flags=[%08x]", static_cast<unsigned int>(GetFlags()));
michael@0 166 if (IsCommonAncestorForRangeInSelection()) {
michael@0 167 typedef nsTHashtable<nsPtrHashKey<nsRange> > RangeHashTable;
michael@0 168 RangeHashTable* ranges =
michael@0 169 static_cast<RangeHashTable*>(GetProperty(nsGkAtoms::range));
michael@0 170 fprintf(out, " ranges:%d", ranges ? ranges->Count() : 0);
michael@0 171 }
michael@0 172 fprintf(out, " primaryframe=%p", static_cast<void*>(GetPrimaryFrame()));
michael@0 173 fprintf(out, " refcount=%" PRIuPTR "<", mRefCnt.get());
michael@0 174
michael@0 175 nsAutoString tmp;
michael@0 176 ToCString(tmp, 0, mText.GetLength());
michael@0 177 fputs(NS_LossyConvertUTF16toASCII(tmp).get(), out);
michael@0 178
michael@0 179 fputs(">\n", out);
michael@0 180 }
michael@0 181
michael@0 182 void
michael@0 183 nsTextNode::DumpContent(FILE* out, int32_t aIndent, bool aDumpAll) const
michael@0 184 {
michael@0 185 if(aDumpAll) {
michael@0 186 int32_t index;
michael@0 187 for (index = aIndent; --index >= 0; ) fputs(" ", out);
michael@0 188
michael@0 189 nsAutoString tmp;
michael@0 190 ToCString(tmp, 0, mText.GetLength());
michael@0 191
michael@0 192 if(!tmp.EqualsLiteral("\\n")) {
michael@0 193 fputs(NS_LossyConvertUTF16toASCII(tmp).get(), out);
michael@0 194 if(aIndent) fputs("\n", out);
michael@0 195 }
michael@0 196 }
michael@0 197 }
michael@0 198 #endif
michael@0 199
michael@0 200 nsresult
michael@0 201 NS_NewAttributeContent(nsNodeInfoManager *aNodeInfoManager,
michael@0 202 int32_t aNameSpaceID, nsIAtom* aAttrName,
michael@0 203 nsIContent** aResult)
michael@0 204 {
michael@0 205 NS_PRECONDITION(aNodeInfoManager, "Missing nodeInfoManager");
michael@0 206 NS_PRECONDITION(aAttrName, "Must have an attr name");
michael@0 207 NS_PRECONDITION(aNameSpaceID != kNameSpaceID_Unknown, "Must know namespace");
michael@0 208
michael@0 209 *aResult = nullptr;
michael@0 210
michael@0 211 already_AddRefed<nsINodeInfo> ni = aNodeInfoManager->GetTextNodeInfo();
michael@0 212
michael@0 213 nsAttributeTextNode* textNode = new nsAttributeTextNode(ni,
michael@0 214 aNameSpaceID,
michael@0 215 aAttrName);
michael@0 216 if (!textNode) {
michael@0 217 return NS_ERROR_OUT_OF_MEMORY;
michael@0 218 }
michael@0 219
michael@0 220 NS_ADDREF(*aResult = textNode);
michael@0 221
michael@0 222 return NS_OK;
michael@0 223 }
michael@0 224
michael@0 225 NS_IMPL_ISUPPORTS_INHERITED(nsAttributeTextNode, nsTextNode,
michael@0 226 nsIMutationObserver)
michael@0 227
michael@0 228 nsresult
michael@0 229 nsAttributeTextNode::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
michael@0 230 nsIContent* aBindingParent,
michael@0 231 bool aCompileEventHandlers)
michael@0 232 {
michael@0 233 NS_PRECONDITION(aParent && aParent->GetParent(),
michael@0 234 "This node can't be a child of the document or of the document root");
michael@0 235
michael@0 236 nsresult rv = nsTextNode::BindToTree(aDocument, aParent,
michael@0 237 aBindingParent, aCompileEventHandlers);
michael@0 238 NS_ENSURE_SUCCESS(rv, rv);
michael@0 239
michael@0 240 NS_ASSERTION(!mGrandparent, "We were already bound!");
michael@0 241 mGrandparent = aParent->GetParent();
michael@0 242 mGrandparent->AddMutationObserver(this);
michael@0 243
michael@0 244 // Note that there is no need to notify here, since we have no
michael@0 245 // frame yet at this point.
michael@0 246 UpdateText(false);
michael@0 247
michael@0 248 return NS_OK;
michael@0 249 }
michael@0 250
michael@0 251 void
michael@0 252 nsAttributeTextNode::UnbindFromTree(bool aDeep, bool aNullParent)
michael@0 253 {
michael@0 254 // UnbindFromTree can be called anytime so we have to be safe.
michael@0 255 if (mGrandparent) {
michael@0 256 // aNullParent might not be true here, but we want to remove the
michael@0 257 // mutation observer anyway since we only need it while we're
michael@0 258 // in the document.
michael@0 259 mGrandparent->RemoveMutationObserver(this);
michael@0 260 mGrandparent = nullptr;
michael@0 261 }
michael@0 262 nsTextNode::UnbindFromTree(aDeep, aNullParent);
michael@0 263 }
michael@0 264
michael@0 265 void
michael@0 266 nsAttributeTextNode::AttributeChanged(nsIDocument* aDocument,
michael@0 267 Element* aElement,
michael@0 268 int32_t aNameSpaceID,
michael@0 269 nsIAtom* aAttribute,
michael@0 270 int32_t aModType)
michael@0 271 {
michael@0 272 if (aNameSpaceID == mNameSpaceID && aAttribute == mAttrName &&
michael@0 273 aElement == mGrandparent) {
michael@0 274 // Since UpdateText notifies, do it when it's safe to run script. Note
michael@0 275 // that if we get unbound while the event is up that's ok -- we'll just
michael@0 276 // have no grandparent when it fires, and will do nothing.
michael@0 277 void (nsAttributeTextNode::*update)() = &nsAttributeTextNode::UpdateText;
michael@0 278 nsCOMPtr<nsIRunnable> ev = NS_NewRunnableMethod(this, update);
michael@0 279 nsContentUtils::AddScriptRunner(ev);
michael@0 280 }
michael@0 281 }
michael@0 282
michael@0 283 void
michael@0 284 nsAttributeTextNode::NodeWillBeDestroyed(const nsINode* aNode)
michael@0 285 {
michael@0 286 NS_ASSERTION(aNode == static_cast<nsINode*>(mGrandparent), "Wrong node!");
michael@0 287 mGrandparent = nullptr;
michael@0 288 }
michael@0 289
michael@0 290 void
michael@0 291 nsAttributeTextNode::UpdateText(bool aNotify)
michael@0 292 {
michael@0 293 if (mGrandparent) {
michael@0 294 nsAutoString attrValue;
michael@0 295 mGrandparent->GetAttr(mNameSpaceID, mAttrName, attrValue);
michael@0 296 SetText(attrValue, aNotify);
michael@0 297 }
michael@0 298 }
michael@0 299

mercurial