michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Implementation of DOM Core's nsIDOMText node. michael@0: */ michael@0: michael@0: #include "nsTextNode.h" michael@0: #include "mozilla/dom/TextBinding.h" michael@0: #include "nsContentUtils.h" michael@0: #include "mozilla/dom/DirectionalityUtils.h" michael@0: #include "nsIDOMEventListener.h" michael@0: #include "nsIDOMMutationEvent.h" michael@0: #include "nsIDocument.h" michael@0: #include "nsThreadUtils.h" michael@0: #include "nsStubMutationObserver.h" michael@0: #include "mozilla/IntegerPrintfMacros.h" michael@0: #ifdef DEBUG michael@0: #include "nsRange.h" michael@0: #endif michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::dom; michael@0: michael@0: /** michael@0: * class used to implement attr() generated content michael@0: */ michael@0: class nsAttributeTextNode MOZ_FINAL : public nsTextNode, michael@0: public nsStubMutationObserver michael@0: { michael@0: public: michael@0: NS_DECL_ISUPPORTS_INHERITED michael@0: michael@0: nsAttributeTextNode(already_AddRefed& aNodeInfo, michael@0: int32_t aNameSpaceID, michael@0: nsIAtom* aAttrName) : michael@0: nsTextNode(aNodeInfo), michael@0: mGrandparent(nullptr), michael@0: mNameSpaceID(aNameSpaceID), michael@0: mAttrName(aAttrName) michael@0: { michael@0: NS_ASSERTION(mNameSpaceID != kNameSpaceID_Unknown, "Must know namespace"); michael@0: NS_ASSERTION(mAttrName, "Must have attr name"); michael@0: } michael@0: michael@0: virtual ~nsAttributeTextNode() { michael@0: NS_ASSERTION(!mGrandparent, "We were not unbound!"); michael@0: } michael@0: michael@0: virtual nsresult BindToTree(nsIDocument* aDocument, nsIContent* aParent, michael@0: nsIContent* aBindingParent, michael@0: bool aCompileEventHandlers); michael@0: virtual void UnbindFromTree(bool aDeep = true, michael@0: bool aNullParent = true); michael@0: michael@0: NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTECHANGED michael@0: NS_DECL_NSIMUTATIONOBSERVER_NODEWILLBEDESTROYED michael@0: michael@0: virtual nsGenericDOMDataNode *CloneDataNode(nsINodeInfo *aNodeInfo, michael@0: bool aCloneText) const michael@0: { michael@0: already_AddRefed ni = michael@0: nsCOMPtr(aNodeInfo).forget(); michael@0: nsAttributeTextNode *it = new nsAttributeTextNode(ni, michael@0: mNameSpaceID, michael@0: mAttrName); michael@0: if (it && aCloneText) { michael@0: it->mText = mText; michael@0: } michael@0: michael@0: return it; michael@0: } michael@0: michael@0: // Public method for the event to run michael@0: void UpdateText() { michael@0: UpdateText(true); michael@0: } michael@0: michael@0: private: michael@0: // Update our text to our parent's current attr value michael@0: void UpdateText(bool aNotify); michael@0: michael@0: // This doesn't need to be a strong pointer because it's only non-null michael@0: // while we're bound to the document tree, and it points to an ancestor michael@0: // so the ancestor must be bound to the document tree the whole time michael@0: // and can't be deleted. michael@0: nsIContent* mGrandparent; michael@0: // What attribute we're showing michael@0: int32_t mNameSpaceID; michael@0: nsCOMPtr mAttrName; michael@0: }; michael@0: michael@0: nsTextNode::~nsTextNode() michael@0: { michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS_INHERITED(nsTextNode, nsGenericDOMDataNode, nsIDOMNode, michael@0: nsIDOMText, nsIDOMCharacterData) michael@0: michael@0: JSObject* michael@0: nsTextNode::WrapNode(JSContext *aCx) michael@0: { michael@0: return TextBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: bool michael@0: nsTextNode::IsNodeOfType(uint32_t aFlags) const michael@0: { michael@0: return !(aFlags & ~(eCONTENT | eTEXT | eDATA_NODE)); michael@0: } michael@0: michael@0: nsGenericDOMDataNode* michael@0: nsTextNode::CloneDataNode(nsINodeInfo *aNodeInfo, bool aCloneText) const michael@0: { michael@0: already_AddRefed ni = nsCOMPtr(aNodeInfo).forget(); michael@0: nsTextNode *it = new nsTextNode(ni); michael@0: if (aCloneText) { michael@0: it->mText = mText; michael@0: } michael@0: michael@0: return it; michael@0: } michael@0: michael@0: nsresult michael@0: nsTextNode::AppendTextForNormalize(const char16_t* aBuffer, uint32_t aLength, michael@0: bool aNotify, nsIContent* aNextSibling) michael@0: { michael@0: CharacterDataChangeInfo::Details details = { michael@0: CharacterDataChangeInfo::Details::eMerge, aNextSibling michael@0: }; michael@0: return SetTextInternal(mText.GetLength(), 0, aBuffer, aLength, aNotify, &details); michael@0: } michael@0: michael@0: nsresult michael@0: nsTextNode::BindToTree(nsIDocument* aDocument, nsIContent* aParent, michael@0: nsIContent* aBindingParent, bool aCompileEventHandlers) michael@0: { michael@0: nsresult rv = nsGenericDOMDataNode::BindToTree(aDocument, aParent, michael@0: aBindingParent, michael@0: aCompileEventHandlers); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: SetDirectionFromNewTextNode(this); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void nsTextNode::UnbindFromTree(bool aDeep, bool aNullParent) michael@0: { michael@0: ResetDirectionSetByTextNode(this, aNullParent); michael@0: michael@0: nsGenericDOMDataNode::UnbindFromTree(aDeep, aNullParent); michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: void michael@0: nsTextNode::List(FILE* out, int32_t aIndent) const michael@0: { michael@0: int32_t index; michael@0: for (index = aIndent; --index >= 0; ) fputs(" ", out); michael@0: michael@0: fprintf(out, "Text@%p", static_cast(this)); michael@0: fprintf(out, " flags=[%08x]", static_cast(GetFlags())); michael@0: if (IsCommonAncestorForRangeInSelection()) { michael@0: typedef nsTHashtable > RangeHashTable; michael@0: RangeHashTable* ranges = michael@0: static_cast(GetProperty(nsGkAtoms::range)); michael@0: fprintf(out, " ranges:%d", ranges ? ranges->Count() : 0); michael@0: } michael@0: fprintf(out, " primaryframe=%p", static_cast(GetPrimaryFrame())); michael@0: fprintf(out, " refcount=%" PRIuPTR "<", mRefCnt.get()); michael@0: michael@0: nsAutoString tmp; michael@0: ToCString(tmp, 0, mText.GetLength()); michael@0: fputs(NS_LossyConvertUTF16toASCII(tmp).get(), out); michael@0: michael@0: fputs(">\n", out); michael@0: } michael@0: michael@0: void michael@0: nsTextNode::DumpContent(FILE* out, int32_t aIndent, bool aDumpAll) const michael@0: { michael@0: if(aDumpAll) { michael@0: int32_t index; michael@0: for (index = aIndent; --index >= 0; ) fputs(" ", out); michael@0: michael@0: nsAutoString tmp; michael@0: ToCString(tmp, 0, mText.GetLength()); michael@0: michael@0: if(!tmp.EqualsLiteral("\\n")) { michael@0: fputs(NS_LossyConvertUTF16toASCII(tmp).get(), out); michael@0: if(aIndent) fputs("\n", out); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: nsresult michael@0: NS_NewAttributeContent(nsNodeInfoManager *aNodeInfoManager, michael@0: int32_t aNameSpaceID, nsIAtom* aAttrName, michael@0: nsIContent** aResult) michael@0: { michael@0: NS_PRECONDITION(aNodeInfoManager, "Missing nodeInfoManager"); michael@0: NS_PRECONDITION(aAttrName, "Must have an attr name"); michael@0: NS_PRECONDITION(aNameSpaceID != kNameSpaceID_Unknown, "Must know namespace"); michael@0: michael@0: *aResult = nullptr; michael@0: michael@0: already_AddRefed ni = aNodeInfoManager->GetTextNodeInfo(); michael@0: michael@0: nsAttributeTextNode* textNode = new nsAttributeTextNode(ni, michael@0: aNameSpaceID, michael@0: aAttrName); michael@0: if (!textNode) { michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: NS_ADDREF(*aResult = textNode); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS_INHERITED(nsAttributeTextNode, nsTextNode, michael@0: nsIMutationObserver) michael@0: michael@0: nsresult michael@0: nsAttributeTextNode::BindToTree(nsIDocument* aDocument, nsIContent* aParent, michael@0: nsIContent* aBindingParent, michael@0: bool aCompileEventHandlers) michael@0: { michael@0: NS_PRECONDITION(aParent && aParent->GetParent(), michael@0: "This node can't be a child of the document or of the document root"); michael@0: michael@0: nsresult rv = nsTextNode::BindToTree(aDocument, aParent, michael@0: aBindingParent, aCompileEventHandlers); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: NS_ASSERTION(!mGrandparent, "We were already bound!"); michael@0: mGrandparent = aParent->GetParent(); michael@0: mGrandparent->AddMutationObserver(this); michael@0: michael@0: // Note that there is no need to notify here, since we have no michael@0: // frame yet at this point. michael@0: UpdateText(false); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsAttributeTextNode::UnbindFromTree(bool aDeep, bool aNullParent) michael@0: { michael@0: // UnbindFromTree can be called anytime so we have to be safe. michael@0: if (mGrandparent) { michael@0: // aNullParent might not be true here, but we want to remove the michael@0: // mutation observer anyway since we only need it while we're michael@0: // in the document. michael@0: mGrandparent->RemoveMutationObserver(this); michael@0: mGrandparent = nullptr; michael@0: } michael@0: nsTextNode::UnbindFromTree(aDeep, aNullParent); michael@0: } michael@0: michael@0: void michael@0: nsAttributeTextNode::AttributeChanged(nsIDocument* aDocument, michael@0: Element* aElement, michael@0: int32_t aNameSpaceID, michael@0: nsIAtom* aAttribute, michael@0: int32_t aModType) michael@0: { michael@0: if (aNameSpaceID == mNameSpaceID && aAttribute == mAttrName && michael@0: aElement == mGrandparent) { michael@0: // Since UpdateText notifies, do it when it's safe to run script. Note michael@0: // that if we get unbound while the event is up that's ok -- we'll just michael@0: // have no grandparent when it fires, and will do nothing. michael@0: void (nsAttributeTextNode::*update)() = &nsAttributeTextNode::UpdateText; michael@0: nsCOMPtr ev = NS_NewRunnableMethod(this, update); michael@0: nsContentUtils::AddScriptRunner(ev); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsAttributeTextNode::NodeWillBeDestroyed(const nsINode* aNode) michael@0: { michael@0: NS_ASSERTION(aNode == static_cast(mGrandparent), "Wrong node!"); michael@0: mGrandparent = nullptr; michael@0: } michael@0: michael@0: void michael@0: nsAttributeTextNode::UpdateText(bool aNotify) michael@0: { michael@0: if (mGrandparent) { michael@0: nsAutoString attrValue; michael@0: mGrandparent->GetAttr(mNameSpaceID, mAttrName, attrValue); michael@0: SetText(attrValue, aNotify); michael@0: } michael@0: } michael@0: