2001-09-25 01:32:19 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-03-31 20:49:25 +00:00
|
|
|
|
2006-03-31 08:41:49 +00:00
|
|
|
/*
|
|
|
|
* Implementation of DOM Core's nsIDOMText node.
|
|
|
|
*/
|
|
|
|
|
2008-10-15 09:40:28 +00:00
|
|
|
#include "nsTextNode.h"
|
2013-01-04 17:02:14 +00:00
|
|
|
#include "mozilla/dom/TextBinding.h"
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 16:46:42 +00:00
|
|
|
#include "nsContentUtils.h"
|
2012-11-21 10:13:57 +00:00
|
|
|
#include "mozilla/dom/DirectionalityUtils.h"
|
2004-01-27 04:22:58 +00:00
|
|
|
#include "nsIDOMEventListener.h"
|
|
|
|
#include "nsIDOMMutationEvent.h"
|
2004-08-10 10:22:36 +00:00
|
|
|
#include "nsIDocument.h"
|
2006-09-20 03:17:11 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2012-09-05 20:49:53 +00:00
|
|
|
#include "nsStubMutationObserver.h"
|
2011-12-20 09:15:41 +00:00
|
|
|
#ifdef DEBUG
|
2012-01-10 14:19:54 +00:00
|
|
|
#include "nsRange.h"
|
2011-12-20 09:15:41 +00:00
|
|
|
#endif
|
2004-01-27 04:22:58 +00:00
|
|
|
|
2012-11-21 10:13:57 +00:00
|
|
|
using namespace mozilla;
|
2010-08-24 07:05:56 +00:00
|
|
|
using namespace mozilla::dom;
|
1998-09-06 00:16:36 +00:00
|
|
|
|
2004-01-27 04:22:58 +00:00
|
|
|
/**
|
|
|
|
* class used to implement attr() generated content
|
|
|
|
*/
|
2006-09-20 03:17:11 +00:00
|
|
|
class nsAttributeTextNode : public nsTextNode,
|
|
|
|
public nsStubMutationObserver
|
|
|
|
{
|
2004-01-27 04:22:58 +00:00
|
|
|
public:
|
2006-09-20 03:17:11 +00:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2004-01-27 04:22:58 +00:00
|
|
|
|
2010-07-23 09:49:57 +00:00
|
|
|
nsAttributeTextNode(already_AddRefed<nsINodeInfo> aNodeInfo,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aNameSpaceID,
|
2006-09-20 03:17:11 +00:00
|
|
|
nsIAtom* aAttrName) :
|
|
|
|
nsTextNode(aNodeInfo),
|
2012-07-30 14:20:58 +00:00
|
|
|
mGrandparent(nullptr),
|
2006-09-20 03:17:11 +00:00
|
|
|
mNameSpaceID(aNameSpaceID),
|
|
|
|
mAttrName(aAttrName)
|
2005-09-24 18:43:15 +00:00
|
|
|
{
|
2006-09-20 03:17:11 +00:00
|
|
|
NS_ASSERTION(mNameSpaceID != kNameSpaceID_Unknown, "Must know namespace");
|
|
|
|
NS_ASSERTION(mAttrName, "Must have attr name");
|
2004-08-10 10:22:36 +00:00
|
|
|
}
|
2006-09-20 03:17:11 +00:00
|
|
|
|
2004-01-27 04:22:58 +00:00
|
|
|
virtual ~nsAttributeTextNode() {
|
2008-08-18 01:16:42 +00:00
|
|
|
NS_ASSERTION(!mGrandparent, "We were not unbound!");
|
2004-01-27 04:22:58 +00:00
|
|
|
}
|
2005-04-05 23:54:35 +00:00
|
|
|
|
|
|
|
virtual nsresult BindToTree(nsIDocument* aDocument, nsIContent* aParent,
|
|
|
|
nsIContent* aBindingParent,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aCompileEventHandlers);
|
|
|
|
virtual void UnbindFromTree(bool aDeep = true,
|
|
|
|
bool aNullParent = true);
|
2004-01-27 04:22:58 +00:00
|
|
|
|
2007-07-12 20:05:45 +00:00
|
|
|
NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTECHANGED
|
2008-10-08 11:13:26 +00:00
|
|
|
NS_DECL_NSIMUTATIONOBSERVER_NODEWILLBEDESTROYED
|
2006-09-20 03:17:11 +00:00
|
|
|
|
2006-09-05 10:22:54 +00:00
|
|
|
virtual nsGenericDOMDataNode *CloneDataNode(nsINodeInfo *aNodeInfo,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aCloneText) const
|
2005-09-11 17:15:08 +00:00
|
|
|
{
|
2010-07-23 09:49:57 +00:00
|
|
|
nsCOMPtr<nsINodeInfo> ni = aNodeInfo;
|
|
|
|
nsAttributeTextNode *it = new nsAttributeTextNode(ni.forget(),
|
2006-09-20 03:17:11 +00:00
|
|
|
mNameSpaceID,
|
|
|
|
mAttrName);
|
2005-09-11 17:15:08 +00:00
|
|
|
if (it && aCloneText) {
|
|
|
|
it->mText = mText;
|
|
|
|
}
|
|
|
|
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
2006-09-20 03:17:11 +00:00
|
|
|
// Public method for the event to run
|
|
|
|
void UpdateText() {
|
2011-10-17 14:59:28 +00:00
|
|
|
UpdateText(true);
|
2006-09-20 03:17:11 +00:00
|
|
|
}
|
|
|
|
|
2004-01-27 04:22:58 +00:00
|
|
|
private:
|
2006-09-20 03:17:11 +00:00
|
|
|
// Update our text to our parent's current attr value
|
2011-09-29 06:19:26 +00:00
|
|
|
void UpdateText(bool aNotify);
|
2006-09-20 03:17:11 +00:00
|
|
|
|
2008-08-18 01:16:42 +00:00
|
|
|
// This doesn't need to be a strong pointer because it's only non-null
|
|
|
|
// while we're bound to the document tree, and it points to an ancestor
|
|
|
|
// so the ancestor must be bound to the document tree the whole time
|
|
|
|
// and can't be deleted.
|
|
|
|
nsIContent* mGrandparent;
|
2006-09-20 03:17:11 +00:00
|
|
|
// What attribute we're showing
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t mNameSpaceID;
|
2006-09-20 03:17:11 +00:00
|
|
|
nsCOMPtr<nsIAtom> mAttrName;
|
2004-01-27 04:22:58 +00:00
|
|
|
};
|
|
|
|
|
1998-09-06 00:16:36 +00:00
|
|
|
nsTextNode::~nsTextNode()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-01-10 09:54:46 +00:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED3(nsTextNode, nsGenericDOMDataNode, nsIDOMNode,
|
|
|
|
nsIDOMText, nsIDOMCharacterData)
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 16:46:42 +00:00
|
|
|
|
2013-01-04 17:02:14 +00:00
|
|
|
JSObject*
|
2013-04-25 16:29:54 +00:00
|
|
|
nsTextNode::WrapNode(JSContext *aCx, JS::Handle<JSObject*> aScope)
|
2013-01-04 17:02:14 +00:00
|
|
|
{
|
2013-03-11 23:03:47 +00:00
|
|
|
return TextBinding::Wrap(aCx, aScope, this);
|
2013-01-04 17:02:14 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2012-08-22 15:56:38 +00:00
|
|
|
nsTextNode::IsNodeOfType(uint32_t aFlags) const
|
2011-06-14 14:25:51 +00:00
|
|
|
{
|
2011-06-14 07:56:48 +00:00
|
|
|
return !(aFlags & ~(eCONTENT | eTEXT | eDATA_NODE));
|
2011-06-14 14:25:51 +00:00
|
|
|
}
|
|
|
|
|
2005-09-11 17:15:08 +00:00
|
|
|
nsGenericDOMDataNode*
|
2011-09-29 06:19:26 +00:00
|
|
|
nsTextNode::CloneDataNode(nsINodeInfo *aNodeInfo, bool aCloneText) const
|
1999-12-22 01:51:58 +00:00
|
|
|
{
|
2010-07-23 09:49:57 +00:00
|
|
|
nsCOMPtr<nsINodeInfo> ni = aNodeInfo;
|
|
|
|
nsTextNode *it = new nsTextNode(ni.forget());
|
2005-09-11 17:15:08 +00:00
|
|
|
if (it && aCloneText) {
|
2001-12-16 06:59:31 +00:00
|
|
|
it->mText = mText;
|
1999-12-22 01:51:58 +00:00
|
|
|
}
|
2001-12-16 06:59:31 +00:00
|
|
|
|
2004-05-07 20:55:17 +00:00
|
|
|
return it;
|
2001-12-16 06:59:31 +00:00
|
|
|
}
|
|
|
|
|
2011-08-16 00:55:20 +00:00
|
|
|
nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
nsTextNode::AppendTextForNormalize(const PRUnichar* aBuffer, uint32_t aLength,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aNotify, nsIContent* aNextSibling)
|
2011-08-16 00:55:20 +00:00
|
|
|
{
|
|
|
|
CharacterDataChangeInfo::Details details = {
|
|
|
|
CharacterDataChangeInfo::Details::eMerge, aNextSibling
|
|
|
|
};
|
|
|
|
return SetTextInternal(mText.GetLength(), 0, aBuffer, aLength, aNotify, &details);
|
|
|
|
}
|
|
|
|
|
2012-11-21 10:13:57 +00:00
|
|
|
nsresult
|
|
|
|
nsTextNode::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
|
|
|
|
nsIContent* aBindingParent, bool aCompileEventHandlers)
|
|
|
|
{
|
|
|
|
nsresult rv = nsGenericDOMDataNode::BindToTree(aDocument, aParent,
|
|
|
|
aBindingParent,
|
|
|
|
aCompileEventHandlers);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
SetDirectionFromNewTextNode(this);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsTextNode::UnbindFromTree(bool aDeep, bool aNullParent)
|
|
|
|
{
|
|
|
|
ResetDirectionSetByTextNode(this);
|
|
|
|
|
|
|
|
nsGenericDOMDataNode::UnbindFromTree(aDeep, aNullParent);
|
|
|
|
}
|
|
|
|
|
2001-10-16 05:31:36 +00:00
|
|
|
#ifdef DEBUG
|
2004-01-09 23:54:21 +00:00
|
|
|
void
|
2012-08-22 15:56:38 +00:00
|
|
|
nsTextNode::List(FILE* out, int32_t aIndent) const
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t index;
|
1998-09-06 00:16:36 +00:00
|
|
|
for (index = aIndent; --index >= 0; ) fputs(" ", out);
|
|
|
|
|
2010-01-17 23:11:02 +00:00
|
|
|
fprintf(out, "Text@%p", static_cast<const void*>(this));
|
2010-05-12 00:30:42 +00:00
|
|
|
fprintf(out, " flags=[%08x]", static_cast<unsigned int>(GetFlags()));
|
2011-12-20 09:15:41 +00:00
|
|
|
if (IsCommonAncestorForRangeInSelection()) {
|
2012-01-10 14:19:54 +00:00
|
|
|
typedef nsTHashtable<nsPtrHashKey<nsRange> > RangeHashTable;
|
2011-12-20 09:15:41 +00:00
|
|
|
RangeHashTable* ranges =
|
|
|
|
static_cast<RangeHashTable*>(GetProperty(nsGkAtoms::range));
|
|
|
|
fprintf(out, " ranges:%d", ranges ? ranges->Count() : 0);
|
|
|
|
}
|
2010-05-01 18:42:47 +00:00
|
|
|
fprintf(out, " primaryframe=%p", static_cast<void*>(GetPrimaryFrame()));
|
2007-06-28 02:48:16 +00:00
|
|
|
fprintf(out, " refcount=%d<", mRefCnt.get());
|
1998-09-06 00:16:36 +00:00
|
|
|
|
|
|
|
nsAutoString tmp;
|
2001-12-16 06:59:31 +00:00
|
|
|
ToCString(tmp, 0, mText.GetLength());
|
2006-02-03 14:18:39 +00:00
|
|
|
fputs(NS_LossyConvertUTF16toASCII(tmp).get(), out);
|
1998-09-06 00:16:36 +00:00
|
|
|
|
|
|
|
fputs(">\n", out);
|
|
|
|
}
|
|
|
|
|
2004-01-09 23:54:21 +00:00
|
|
|
void
|
2012-08-22 15:56:38 +00:00
|
|
|
nsTextNode::DumpContent(FILE* out, int32_t aIndent, bool aDumpAll) const
|
2000-05-26 19:45:37 +00:00
|
|
|
{
|
|
|
|
if(aDumpAll) {
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t index;
|
2000-05-26 19:45:37 +00:00
|
|
|
for (index = aIndent; --index >= 0; ) fputs(" ", out);
|
|
|
|
|
|
|
|
nsAutoString tmp;
|
2001-12-16 06:59:31 +00:00
|
|
|
ToCString(tmp, 0, mText.GetLength());
|
|
|
|
|
2004-05-22 22:15:22 +00:00
|
|
|
if(!tmp.EqualsLiteral("\\n")) {
|
2006-02-03 14:18:39 +00:00
|
|
|
fputs(NS_LossyConvertUTF16toASCII(tmp).get(), out);
|
2000-05-26 19:45:37 +00:00
|
|
|
if(aIndent) fputs("\n", out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-10-16 05:31:36 +00:00
|
|
|
#endif
|
2004-01-27 04:22:58 +00:00
|
|
|
|
|
|
|
nsresult
|
2005-09-24 18:43:15 +00:00
|
|
|
NS_NewAttributeContent(nsNodeInfoManager *aNodeInfoManager,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aNameSpaceID, nsIAtom* aAttrName,
|
2005-09-24 18:43:15 +00:00
|
|
|
nsIContent** aResult)
|
2004-01-27 04:22:58 +00:00
|
|
|
{
|
2005-09-24 18:43:15 +00:00
|
|
|
NS_PRECONDITION(aNodeInfoManager, "Missing nodeInfoManager");
|
2004-01-27 04:22:58 +00:00
|
|
|
NS_PRECONDITION(aAttrName, "Must have an attr name");
|
|
|
|
NS_PRECONDITION(aNameSpaceID != kNameSpaceID_Unknown, "Must know namespace");
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
*aResult = nullptr;
|
2005-09-11 17:15:08 +00:00
|
|
|
|
2005-09-24 18:43:15 +00:00
|
|
|
nsCOMPtr<nsINodeInfo> ni = aNodeInfoManager->GetTextNodeInfo();
|
|
|
|
if (!ni) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2010-07-23 09:49:57 +00:00
|
|
|
nsAttributeTextNode* textNode = new nsAttributeTextNode(ni.forget(),
|
|
|
|
aNameSpaceID,
|
2006-09-20 03:17:11 +00:00
|
|
|
aAttrName);
|
2005-09-24 18:43:15 +00:00
|
|
|
if (!textNode) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2004-01-27 04:22:58 +00:00
|
|
|
|
2005-04-05 23:54:35 +00:00
|
|
|
NS_ADDREF(*aResult = textNode);
|
2005-09-24 18:43:15 +00:00
|
|
|
|
2005-04-05 23:54:35 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2004-01-27 04:22:58 +00:00
|
|
|
|
2006-09-20 03:17:11 +00:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED1(nsAttributeTextNode, nsTextNode,
|
|
|
|
nsIMutationObserver)
|
|
|
|
|
2005-04-05 23:54:35 +00:00
|
|
|
nsresult
|
|
|
|
nsAttributeTextNode::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
|
|
|
|
nsIContent* aBindingParent,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aCompileEventHandlers)
|
2005-04-05 23:54:35 +00:00
|
|
|
{
|
2008-08-18 01:16:42 +00:00
|
|
|
NS_PRECONDITION(aParent && aParent->GetParent(),
|
|
|
|
"This node can't be a child of the document or of the document root");
|
2004-01-27 04:22:58 +00:00
|
|
|
|
2005-04-05 23:54:35 +00:00
|
|
|
nsresult rv = nsTextNode::BindToTree(aDocument, aParent,
|
|
|
|
aBindingParent, aCompileEventHandlers);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-08-18 01:16:42 +00:00
|
|
|
NS_ASSERTION(!mGrandparent, "We were already bound!");
|
|
|
|
mGrandparent = aParent->GetParent();
|
|
|
|
mGrandparent->AddMutationObserver(this);
|
2006-09-20 03:17:11 +00:00
|
|
|
|
|
|
|
// Note that there is no need to notify here, since we have no
|
|
|
|
// frame yet at this point.
|
2011-10-17 14:59:28 +00:00
|
|
|
UpdateText(false);
|
2004-01-27 04:22:58 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-04-05 23:54:35 +00:00
|
|
|
void
|
2011-09-29 06:19:26 +00:00
|
|
|
nsAttributeTextNode::UnbindFromTree(bool aDeep, bool aNullParent)
|
2005-04-05 23:54:35 +00:00
|
|
|
{
|
2008-08-18 01:16:42 +00:00
|
|
|
// UnbindFromTree can be called anytime so we have to be safe.
|
|
|
|
if (mGrandparent) {
|
|
|
|
// aNullParent might not be true here, but we want to remove the
|
|
|
|
// mutation observer anyway since we only need it while we're
|
|
|
|
// in the document.
|
|
|
|
mGrandparent->RemoveMutationObserver(this);
|
2012-07-30 14:20:58 +00:00
|
|
|
mGrandparent = nullptr;
|
2005-04-05 23:54:35 +00:00
|
|
|
}
|
|
|
|
nsTextNode::UnbindFromTree(aDeep, aNullParent);
|
|
|
|
}
|
|
|
|
|
2004-01-27 04:22:58 +00:00
|
|
|
void
|
2006-09-20 03:17:11 +00:00
|
|
|
nsAttributeTextNode::AttributeChanged(nsIDocument* aDocument,
|
2010-08-24 07:05:56 +00:00
|
|
|
Element* aElement,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aNameSpaceID,
|
2006-09-20 03:17:11 +00:00
|
|
|
nsIAtom* aAttribute,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aModType)
|
2004-01-27 04:22:58 +00:00
|
|
|
{
|
2006-09-20 03:17:11 +00:00
|
|
|
if (aNameSpaceID == mNameSpaceID && aAttribute == mAttrName &&
|
2010-08-24 07:05:56 +00:00
|
|
|
aElement == mGrandparent) {
|
2010-07-21 15:33:32 +00:00
|
|
|
// Since UpdateText notifies, do it when it's safe to run script. Note
|
|
|
|
// that if we get unbound while the event is up that's ok -- we'll just
|
|
|
|
// have no grandparent when it fires, and will do nothing.
|
2010-04-20 23:21:35 +00:00
|
|
|
void (nsAttributeTextNode::*update)() = &nsAttributeTextNode::UpdateText;
|
|
|
|
nsCOMPtr<nsIRunnable> ev = NS_NewRunnableMethod(this, update);
|
2010-07-21 15:33:32 +00:00
|
|
|
nsContentUtils::AddScriptRunner(ev);
|
2006-09-20 03:17:11 +00:00
|
|
|
}
|
|
|
|
}
|
2004-01-27 04:22:58 +00:00
|
|
|
|
2008-10-08 11:13:26 +00:00
|
|
|
void
|
|
|
|
nsAttributeTextNode::NodeWillBeDestroyed(const nsINode* aNode)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aNode == static_cast<nsINode*>(mGrandparent), "Wrong node!");
|
2012-07-30 14:20:58 +00:00
|
|
|
mGrandparent = nullptr;
|
2008-10-08 11:13:26 +00:00
|
|
|
}
|
|
|
|
|
2006-09-20 03:17:11 +00:00
|
|
|
void
|
2011-09-29 06:19:26 +00:00
|
|
|
nsAttributeTextNode::UpdateText(bool aNotify)
|
2006-09-20 03:17:11 +00:00
|
|
|
{
|
2008-08-18 01:16:42 +00:00
|
|
|
if (mGrandparent) {
|
2006-09-20 03:17:11 +00:00
|
|
|
nsAutoString attrValue;
|
2008-08-18 01:16:42 +00:00
|
|
|
mGrandparent->GetAttr(mNameSpaceID, mAttrName, attrValue);
|
2006-09-20 03:17:11 +00:00
|
|
|
SetText(attrValue, aNotify);
|
|
|
|
}
|
|
|
|
}
|
2013-01-04 17:02:14 +00:00
|
|
|
|