1998-09-06 00:16:36 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
1999-11-06 03:40:37 +00:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1998-09-06 00:16:36 +00:00
|
|
|
*
|
1999-11-06 03:40:37 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1998-09-06 00:16:36 +00:00
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape Communications
|
1999-11-06 03:40:37 +00:00
|
|
|
* Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1998-09-06 00:16:36 +00:00
|
|
|
*/
|
1998-09-06 04:16:22 +00:00
|
|
|
#include "nsGenericDOMDataNode.h"
|
1999-01-14 23:14:02 +00:00
|
|
|
#include "nsGenericElement.h"
|
1999-01-21 19:33:03 +00:00
|
|
|
#include "nsIDocument.h"
|
1998-09-06 00:16:36 +00:00
|
|
|
#include "nsIEventListenerManager.h"
|
|
|
|
#include "nsIDocument.h"
|
1998-12-17 07:22:28 +00:00
|
|
|
#include "nsIDOMRange.h"
|
1999-01-21 19:33:03 +00:00
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIDOMDocumentFragment.h"
|
1998-09-06 00:16:36 +00:00
|
|
|
#include "nsXIFConverter.h"
|
1999-01-03 14:29:54 +00:00
|
|
|
#include "nsRange.h"
|
1999-10-21 20:40:51 +00:00
|
|
|
#include "nsTextContentChangeData.h"
|
1999-02-11 23:12:28 +00:00
|
|
|
#include "nsIDOMSelection.h"
|
1999-01-21 21:45:17 +00:00
|
|
|
#include "nsIEnumerator.h"
|
|
|
|
|
|
|
|
|
1998-09-06 00:16:36 +00:00
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsIEventStateManager.h"
|
|
|
|
#include "nsIPrivateDOMEvent.h"
|
|
|
|
#include "nsISizeOfHandler.h"
|
|
|
|
#include "nsDOMEvent.h"
|
1999-03-31 20:49:25 +00:00
|
|
|
#include "nsIDOMText.h"
|
1998-09-10 17:11:46 +00:00
|
|
|
#include "nsIDOMScriptObjectFactory.h"
|
1999-12-03 09:24:22 +00:00
|
|
|
#include "nsIScriptGlobalObject.h"
|
1998-09-29 22:34:30 +00:00
|
|
|
#include "prprf.h"
|
1999-01-21 21:45:17 +00:00
|
|
|
#include "nsCOMPtr.h"
|
1998-09-06 00:16:36 +00:00
|
|
|
|
|
|
|
// XXX share all id's in this dir
|
|
|
|
|
1998-10-20 17:07:23 +00:00
|
|
|
NS_DEFINE_IID(kIDOMCharacterDataIID, NS_IDOMCHARACTERDATA_IID);
|
1998-09-06 00:16:36 +00:00
|
|
|
|
|
|
|
static NS_DEFINE_IID(kIPrivateDOMEventIID, NS_IPRIVATEDOMEVENT_IID);
|
1999-01-21 21:45:17 +00:00
|
|
|
static NS_DEFINE_IID(kIEnumeratorIID, NS_IENUMERATOR_IID);
|
1999-01-21 19:33:03 +00:00
|
|
|
static NS_DEFINE_IID(kIDOMDocumentIID, NS_IDOMDOCUMENT_IID);
|
1999-03-31 20:49:25 +00:00
|
|
|
static NS_DEFINE_IID(kIDOMTextIID, NS_IDOMTEXT_IID);
|
|
|
|
static NS_DEFINE_IID(kITextContentIID, NS_ITEXT_CONTENT_IID);
|
1999-08-25 07:35:45 +00:00
|
|
|
static NS_DEFINE_IID(kIDOMNodeListIID, NS_IDOMNODELIST_IID);
|
1998-09-06 00:16:36 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
1998-09-06 04:16:22 +00:00
|
|
|
nsGenericDOMDataNode::nsGenericDOMDataNode()
|
1998-10-20 00:20:04 +00:00
|
|
|
: mText()
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
|
|
|
mDocument = nsnull;
|
|
|
|
mParent = nsnull;
|
|
|
|
mContent = nsnull;
|
|
|
|
mScriptObject = nsnull;
|
|
|
|
mListenerManager = nsnull;
|
1998-12-17 07:22:28 +00:00
|
|
|
mRangeList = nsnull;
|
1999-03-28 22:22:54 +00:00
|
|
|
mCapturer = nsnull;
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
|
1998-09-06 04:16:22 +00:00
|
|
|
nsGenericDOMDataNode::~nsGenericDOMDataNode()
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
|
|
|
NS_IF_RELEASE(mListenerManager);
|
1998-12-17 07:22:28 +00:00
|
|
|
delete mRangeList;
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-01-14 23:14:02 +00:00
|
|
|
nsGenericDOMDataNode::Init(nsIContent* aOuterContentObject)
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION((nsnull == mContent) && (nsnull != aOuterContentObject),
|
|
|
|
"null ptr");
|
|
|
|
mContent = aOuterContentObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1998-09-06 04:16:22 +00:00
|
|
|
nsGenericDOMDataNode::GetNodeValue(nsString& aNodeValue)
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
1999-01-21 19:33:03 +00:00
|
|
|
return GetData(aNodeValue);
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1998-09-06 04:16:22 +00:00
|
|
|
nsGenericDOMDataNode::SetNodeValue(const nsString& aNodeValue)
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
1999-01-21 19:33:03 +00:00
|
|
|
return SetData(aNodeValue);
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1998-09-06 04:16:22 +00:00
|
|
|
nsGenericDOMDataNode::GetParentNode(nsIDOMNode** aParentNode)
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
1999-04-17 00:56:25 +00:00
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
1998-09-06 00:16:36 +00:00
|
|
|
if (nsnull != mParent) {
|
1999-04-17 00:56:25 +00:00
|
|
|
res = mParent->QueryInterface(kIDOMNodeIID, (void**)aParentNode);
|
1998-09-06 00:16:36 +00:00
|
|
|
NS_ASSERTION(NS_OK == res, "Must be a DOM Node");
|
|
|
|
}
|
1999-01-21 19:33:03 +00:00
|
|
|
else if (nsnull == mDocument) {
|
1999-04-17 00:56:25 +00:00
|
|
|
*aParentNode = nsnull;
|
1999-01-21 19:33:03 +00:00
|
|
|
}
|
1998-09-06 00:16:36 +00:00
|
|
|
else {
|
1999-01-21 19:33:03 +00:00
|
|
|
// If we don't have a parent, but we're in the document, we must
|
1999-04-17 00:56:25 +00:00
|
|
|
// be the root node of the document. The DOM says that the root
|
|
|
|
// is the document.
|
|
|
|
res = mDocument->QueryInterface(kIDOMNodeIID, (void**)aParentNode);
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
1999-04-17 00:56:25 +00:00
|
|
|
|
|
|
|
return res;
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1999-03-31 20:49:25 +00:00
|
|
|
nsGenericDOMDataNode::GetPreviousSibling(nsIDOMNode** aPrevSibling)
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
1999-03-31 20:49:25 +00:00
|
|
|
nsIContent* sibling = nsnull;
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
1998-09-06 00:16:36 +00:00
|
|
|
if (nsnull != mParent) {
|
|
|
|
PRInt32 pos;
|
|
|
|
mParent->IndexOf(mContent, pos);
|
1999-03-31 20:49:25 +00:00
|
|
|
if (pos > -1 ) {
|
|
|
|
mParent->ChildAt(--pos, sibling);
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
}
|
1999-03-31 20:49:25 +00:00
|
|
|
else if (nsnull != mDocument) {
|
|
|
|
// Nodes that are just below the document (their parent is the
|
|
|
|
// document) need to go to the document to find their next sibling.
|
|
|
|
PRInt32 pos;
|
|
|
|
mDocument->IndexOf(mContent, pos);
|
|
|
|
if (pos > -1 ) {
|
|
|
|
mDocument->ChildAt(--pos, sibling);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nsnull != sibling) {
|
|
|
|
result = sibling->QueryInterface(kIDOMNodeIID,(void**)aPrevSibling);
|
|
|
|
NS_ASSERTION(NS_OK == result, "Must be a DOM Node");
|
|
|
|
NS_RELEASE(sibling); // balance the AddRef in ChildAt()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*aPrevSibling = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1998-09-06 04:16:22 +00:00
|
|
|
nsGenericDOMDataNode::GetNextSibling(nsIDOMNode** aNextSibling)
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
1999-03-31 20:49:25 +00:00
|
|
|
nsIContent* sibling = nsnull;
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
1998-09-06 00:16:36 +00:00
|
|
|
if (nsnull != mParent) {
|
|
|
|
PRInt32 pos;
|
|
|
|
mParent->IndexOf(mContent, pos);
|
|
|
|
if (pos > -1 ) {
|
1999-03-31 20:49:25 +00:00
|
|
|
mParent->ChildAt(++pos, sibling);
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
}
|
1999-03-31 20:49:25 +00:00
|
|
|
else if (nsnull != mDocument) {
|
|
|
|
// Nodes that are just below the document (their parent is the
|
|
|
|
// document) need to go to the document to find their next sibling.
|
|
|
|
PRInt32 pos;
|
|
|
|
mDocument->IndexOf(mContent, pos);
|
|
|
|
if (pos > -1 ) {
|
|
|
|
mDocument->ChildAt(++pos, sibling);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nsnull != sibling) {
|
|
|
|
result = sibling->QueryInterface(kIDOMNodeIID,(void**)aNextSibling);
|
|
|
|
NS_ASSERTION(NS_OK == result, "Must be a DOM Node");
|
|
|
|
NS_RELEASE(sibling); // balance the AddRef in ChildAt()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*aNextSibling = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
|
1999-08-25 07:35:45 +00:00
|
|
|
nsresult
|
|
|
|
nsGenericDOMDataNode::GetChildNodes(nsIDOMNodeList** aChildNodes)
|
|
|
|
{
|
|
|
|
// XXX Since we believe this won't be done very often, we won't
|
|
|
|
// burn another slot in the data node and just create a new
|
|
|
|
// (empty) childNodes list every time we're asked.
|
|
|
|
nsChildContentList* list = new nsChildContentList(nsnull);
|
|
|
|
if (nsnull == list) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return list->QueryInterface(kIDOMNodeListIID, (void**)aChildNodes);
|
|
|
|
}
|
|
|
|
|
1998-10-20 17:07:23 +00:00
|
|
|
nsresult
|
|
|
|
nsGenericDOMDataNode::GetOwnerDocument(nsIDOMDocument** aOwnerDocument)
|
|
|
|
{
|
|
|
|
// XXX Actually the owner document is the document in whose context
|
|
|
|
// the node has been created. We should be able to get at it
|
|
|
|
// whether or not we are attached to the document.
|
|
|
|
if (nsnull != mDocument) {
|
|
|
|
return mDocument->QueryInterface(kIDOMDocumentIID, (void **)aOwnerDocument);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*aOwnerDocument = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-09-06 00:16:36 +00:00
|
|
|
#if 0
|
|
|
|
nsresult
|
1998-09-06 04:16:22 +00:00
|
|
|
nsGenericDOMDataNode::Equals(nsIDOMNode* aNode, PRBool aDeep, PRBool* aReturn)
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
|
|
|
*aReturn = PR_FALSE;
|
|
|
|
PRInt32 nt1, nt2;
|
|
|
|
GetNodeType(&nt1);
|
|
|
|
aNode->GetNodeType(&nt2);
|
|
|
|
if (nt1 != nt2) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
1998-10-20 17:07:23 +00:00
|
|
|
// Implementation of nsIDOMCharacterData
|
1998-09-06 00:16:36 +00:00
|
|
|
|
|
|
|
nsresult
|
1998-09-06 04:16:22 +00:00
|
|
|
nsGenericDOMDataNode::GetData(nsString& aData)
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
1998-10-20 00:20:04 +00:00
|
|
|
if (mText.Is2b()) {
|
|
|
|
aData.SetString(mText.Get2b(), mText.GetLength());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aData.SetString(mText.Get1b(), mText.GetLength());
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1998-09-06 04:16:22 +00:00
|
|
|
nsGenericDOMDataNode::SetData(const nsString& aData)
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
1999-01-03 14:29:54 +00:00
|
|
|
// inform any enclosed ranges of change
|
|
|
|
// we can lie and say we are deleting all the text, since in a total
|
|
|
|
// text replacement we should just collapse all the ranges.
|
|
|
|
if (mRangeList) nsRange::TextOwnerChanged(mContent, 0, mText.GetLength(), 0);
|
1998-09-06 00:16:36 +00:00
|
|
|
|
1999-08-25 07:35:45 +00:00
|
|
|
nsresult result;
|
|
|
|
nsCOMPtr<nsITextContent> textContent = do_QueryInterface(mContent, &result);
|
|
|
|
|
|
|
|
// If possible, let the container content object have a go at it.
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
result = textContent->SetText(aData.GetUnicode(),
|
|
|
|
aData.Length(),
|
|
|
|
PR_TRUE);
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
1999-08-25 07:35:45 +00:00
|
|
|
else {
|
|
|
|
result = SetText(aData.GetUnicode(), aData.Length(), PR_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1998-10-20 17:07:23 +00:00
|
|
|
nsGenericDOMDataNode::GetLength(PRUint32* aLength)
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
1998-10-20 17:07:23 +00:00
|
|
|
*aLength = mText.GetLength();
|
1998-09-06 00:16:36 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1998-10-20 17:07:23 +00:00
|
|
|
nsGenericDOMDataNode::SubstringData(PRUint32 aStart,
|
|
|
|
PRUint32 aCount,
|
|
|
|
nsString& aReturn)
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
|
|
|
aReturn.Truncate();
|
1998-10-20 00:20:04 +00:00
|
|
|
|
|
|
|
// XXX add <0 checks if types change
|
|
|
|
PRUint32 textLength = PRUint32( mText.GetLength() );
|
1999-10-15 22:18:23 +00:00
|
|
|
if (aStart > textLength) {
|
1999-10-06 00:06:53 +00:00
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
1998-10-20 00:20:04 +00:00
|
|
|
|
|
|
|
PRUint32 amount = aCount;
|
|
|
|
if (aStart + amount > textLength) {
|
|
|
|
amount = textLength - aStart;
|
|
|
|
}
|
|
|
|
if (mText.Is2b()) {
|
|
|
|
aReturn.SetString(mText.Get2b() + aStart, amount);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aReturn.SetString(mText.Get1b() + aStart, amount);
|
|
|
|
}
|
|
|
|
|
1998-09-06 00:16:36 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-10-21 20:40:51 +00:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
1998-09-06 00:16:36 +00:00
|
|
|
nsresult
|
1998-10-20 17:07:23 +00:00
|
|
|
nsGenericDOMDataNode::AppendData(const nsString& aData)
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
1999-10-21 20:40:51 +00:00
|
|
|
#if 1
|
|
|
|
// Allocate new buffer
|
1999-11-11 01:28:32 +00:00
|
|
|
nsresult result = NS_OK;
|
1999-10-21 20:40:51 +00:00
|
|
|
PRInt32 dataLength = aData.Length();
|
|
|
|
PRInt32 textLength = mText.GetLength();
|
|
|
|
PRInt32 newSize = textLength + dataLength;
|
1999-11-23 23:13:03 +00:00
|
|
|
PRUnichar* to = new PRUnichar[newSize + 1];
|
1999-10-21 20:40:51 +00:00
|
|
|
if (nsnull == to) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX This is slow...
|
|
|
|
|
|
|
|
// Fill new buffer with old data and new data
|
|
|
|
if (textLength) {
|
|
|
|
mText.CopyTo(to, 0, textLength);
|
|
|
|
}
|
|
|
|
nsCRT::memcpy(to + textLength, aData.GetUnicode(),
|
|
|
|
sizeof(PRUnichar) * dataLength);
|
|
|
|
|
1999-11-23 23:13:03 +00:00
|
|
|
// Null terminate the new buffer...
|
|
|
|
to[newSize] = (PRUnichar)0;
|
|
|
|
|
1999-11-11 01:28:32 +00:00
|
|
|
nsCOMPtr<nsITextContent> textContent = do_QueryInterface(mContent, &result);
|
|
|
|
|
1999-10-21 20:40:51 +00:00
|
|
|
// Switch to new buffer
|
1999-11-11 01:28:32 +00:00
|
|
|
// Dont do notification in SetText, since we will do it later
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
result = textContent->SetText(to, newSize, PR_FALSE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = SetText(to, newSize, PR_FALSE);
|
|
|
|
}
|
1999-10-21 20:40:51 +00:00
|
|
|
|
|
|
|
delete [] to;
|
|
|
|
|
|
|
|
// Trigger a reflow
|
|
|
|
if (nsnull != mDocument) {
|
|
|
|
nsTextContentChangeData* tccd = nsnull;
|
1999-11-11 01:28:32 +00:00
|
|
|
result = NS_NewTextContentChangeData(&tccd);
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
1999-10-21 20:40:51 +00:00
|
|
|
tccd->SetData(nsITextContentChangeData::Append, textLength, dataLength);
|
1999-11-11 01:28:32 +00:00
|
|
|
result = mDocument->ContentChanged(mContent, tccd);
|
1999-10-21 20:40:51 +00:00
|
|
|
NS_RELEASE(tccd);
|
|
|
|
}
|
|
|
|
else {
|
1999-11-11 01:28:32 +00:00
|
|
|
result = mDocument->ContentChanged(mContent, nsnull);
|
1999-10-21 20:40:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-11 01:28:32 +00:00
|
|
|
return result;
|
1999-10-21 20:40:51 +00:00
|
|
|
#else
|
1998-10-20 17:07:23 +00:00
|
|
|
return ReplaceData(mText.GetLength(), 0, aData);
|
1999-10-21 20:40:51 +00:00
|
|
|
#endif
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1998-10-20 17:07:23 +00:00
|
|
|
nsGenericDOMDataNode::InsertData(PRUint32 aOffset, const nsString& aData)
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
1998-10-20 17:07:23 +00:00
|
|
|
return ReplaceData(aOffset, 0, aData);
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1998-10-20 17:07:23 +00:00
|
|
|
nsGenericDOMDataNode::DeleteData(PRUint32 aOffset, PRUint32 aCount)
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
|
|
|
nsAutoString empty;
|
1998-10-20 17:07:23 +00:00
|
|
|
return ReplaceData(aOffset, aCount, empty);
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1998-10-20 17:07:23 +00:00
|
|
|
nsGenericDOMDataNode::ReplaceData(PRUint32 aOffset, PRUint32 aCount,
|
|
|
|
const nsString& aData)
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
1999-08-25 07:35:45 +00:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
1998-09-06 00:16:36 +00:00
|
|
|
// sanitize arguments
|
1998-10-20 00:20:04 +00:00
|
|
|
PRUint32 textLength = mText.GetLength();
|
1999-11-11 01:48:25 +00:00
|
|
|
if ((aOffset > textLength) || (aOffset < 0) || (aCount < 0)) {
|
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate new buffer
|
1998-10-20 00:20:04 +00:00
|
|
|
PRUint32 endOffset = aOffset + aCount;
|
|
|
|
if (endOffset > textLength) {
|
|
|
|
aCount = textLength - aOffset;
|
|
|
|
endOffset = textLength;
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
PRInt32 dataLength = aData.Length();
|
1998-10-20 00:20:04 +00:00
|
|
|
PRInt32 newLength = textLength - aCount + dataLength;
|
1999-08-28 05:12:11 +00:00
|
|
|
PRUnichar* to = new PRUnichar[newLength ? newLength+1 : 1];
|
1998-09-06 00:16:36 +00:00
|
|
|
if (nsnull == to) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
1999-01-03 14:29:54 +00:00
|
|
|
// inform any enclosed ranges of change
|
|
|
|
if (mRangeList) nsRange::TextOwnerChanged(mContent, aOffset, endOffset, dataLength);
|
|
|
|
|
1998-09-06 00:16:36 +00:00
|
|
|
// Copy over appropriate data
|
|
|
|
if (0 != aOffset) {
|
1998-10-20 00:20:04 +00:00
|
|
|
mText.CopyTo(to, 0, aOffset);
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
if (0 != dataLength) {
|
|
|
|
nsCRT::memcpy(to + aOffset, aData.GetUnicode(),
|
|
|
|
sizeof(PRUnichar) * dataLength);
|
|
|
|
}
|
1998-10-20 00:20:04 +00:00
|
|
|
if (endOffset != textLength) {
|
|
|
|
mText.CopyTo(to + aOffset + dataLength, endOffset, textLength - endOffset);
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
|
1999-08-28 05:12:11 +00:00
|
|
|
// Null terminate the new buffer...
|
|
|
|
to[newLength] = (PRUnichar)0;
|
|
|
|
|
1998-09-06 00:16:36 +00:00
|
|
|
// Switch to new buffer
|
1999-08-25 07:35:45 +00:00
|
|
|
nsCOMPtr<nsITextContent> textContent = do_QueryInterface(mContent, &result);
|
1998-09-06 00:16:36 +00:00
|
|
|
|
1999-08-25 07:35:45 +00:00
|
|
|
// If possible, let the container content object have a go at it.
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
result = textContent->SetText(to, newLength, PR_TRUE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = SetText(to, newLength, PR_TRUE);
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
1999-08-25 07:35:45 +00:00
|
|
|
delete [] to;
|
1998-09-06 00:16:36 +00:00
|
|
|
|
1999-08-25 07:35:45 +00:00
|
|
|
return result;
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
// nsIScriptObjectOwner implementation
|
|
|
|
|
|
|
|
nsresult
|
1998-09-06 04:16:22 +00:00
|
|
|
nsGenericDOMDataNode::GetScriptObject(nsIScriptContext* aContext,
|
1998-09-06 00:16:36 +00:00
|
|
|
void** aScriptObject)
|
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
if (nsnull == mScriptObject) {
|
1998-09-10 17:11:46 +00:00
|
|
|
nsIDOMScriptObjectFactory *factory;
|
|
|
|
|
1998-11-11 22:06:16 +00:00
|
|
|
res = nsGenericElement::GetScriptObjectFactory(&factory);
|
1998-09-10 17:11:46 +00:00
|
|
|
if (NS_OK != res) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1999-01-21 19:33:03 +00:00
|
|
|
nsIDOMNode* node;
|
|
|
|
PRUint16 nodeType;
|
|
|
|
|
|
|
|
res = mContent->QueryInterface(kIDOMNodeIID, (void**)&node);
|
|
|
|
if (NS_OK != res) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->GetNodeType(&nodeType);
|
|
|
|
res = factory->NewScriptCharacterData(nodeType,
|
1998-10-20 17:07:23 +00:00
|
|
|
aContext, mContent,
|
|
|
|
mParent, (void**)&mScriptObject);
|
1998-10-26 23:26:01 +00:00
|
|
|
if (nsnull != mDocument) {
|
1999-01-21 19:33:03 +00:00
|
|
|
nsAutoString nodeName;
|
|
|
|
char nameBuf[128];
|
|
|
|
|
|
|
|
node->GetNodeName(nodeName);
|
|
|
|
nodeName.ToCString(nameBuf, sizeof(nameBuf));
|
|
|
|
|
1998-10-26 23:26:01 +00:00
|
|
|
aContext->AddNamedReference((void *)&mScriptObject,
|
|
|
|
mScriptObject,
|
1999-01-21 19:33:03 +00:00
|
|
|
nameBuf);
|
1998-10-26 23:26:01 +00:00
|
|
|
}
|
1999-01-21 19:33:03 +00:00
|
|
|
NS_RELEASE(node);
|
1998-09-10 17:11:46 +00:00
|
|
|
NS_RELEASE(factory);
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
*aScriptObject = mScriptObject;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1998-09-17 01:53:52 +00:00
|
|
|
nsGenericDOMDataNode::SetScriptObject(void *aScriptObject)
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
1998-09-17 01:53:52 +00:00
|
|
|
mScriptObject = aScriptObject;
|
1998-09-06 00:16:36 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
// nsIDOMEventReceiver implementation
|
|
|
|
|
|
|
|
nsresult
|
1998-09-06 04:16:22 +00:00
|
|
|
nsGenericDOMDataNode::GetListenerManager(nsIEventListenerManager** aResult)
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
|
|
|
if (nsnull != mListenerManager) {
|
|
|
|
NS_ADDREF(mListenerManager);
|
|
|
|
*aResult = mListenerManager;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsresult rv = NS_NewEventListenerManager(aResult);
|
|
|
|
if (NS_OK == rv) {
|
|
|
|
mListenerManager = *aResult;
|
|
|
|
NS_ADDREF(mListenerManager);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1998-09-06 04:16:22 +00:00
|
|
|
nsGenericDOMDataNode::GetNewListenerManager(nsIEventListenerManager** aResult)
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
|
|
|
return NS_NewEventListenerManager(aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1999-03-28 22:22:54 +00:00
|
|
|
nsGenericDOMDataNode::AddEventListenerByIID(nsIDOMEventListener* aListener,
|
1998-09-06 00:16:36 +00:00
|
|
|
const nsIID& aIID)
|
|
|
|
{
|
|
|
|
nsIEventListenerManager *manager;
|
|
|
|
|
|
|
|
if (NS_OK == GetListenerManager(&manager)) {
|
1999-03-28 22:22:54 +00:00
|
|
|
manager->AddEventListenerByIID(aListener, aIID, NS_EVENT_FLAG_BUBBLE);
|
1998-09-06 00:16:36 +00:00
|
|
|
NS_RELEASE(manager);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1999-03-28 22:22:54 +00:00
|
|
|
nsGenericDOMDataNode::RemoveEventListenerByIID(nsIDOMEventListener* aListener,
|
1998-09-06 00:16:36 +00:00
|
|
|
const nsIID& aIID)
|
|
|
|
{
|
|
|
|
if (nsnull != mListenerManager) {
|
1999-03-28 22:22:54 +00:00
|
|
|
mListenerManager->RemoveEventListenerByIID(aListener, aIID, NS_EVENT_FLAG_BUBBLE);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsGenericDOMDataNode::AddEventListener(const nsString& aType, nsIDOMEventListener* aListener,
|
1999-07-19 19:54:34 +00:00
|
|
|
PRBool aUseCapture)
|
1999-03-28 22:22:54 +00:00
|
|
|
{
|
|
|
|
nsIEventListenerManager *manager;
|
|
|
|
|
|
|
|
if (NS_OK == GetListenerManager(&manager)) {
|
1999-07-19 19:54:34 +00:00
|
|
|
PRInt32 flags = aUseCapture ? NS_EVENT_FLAG_CAPTURE : NS_EVENT_FLAG_BUBBLE;
|
1999-03-28 22:22:54 +00:00
|
|
|
|
|
|
|
manager->AddEventListenerByType(aListener, aType, flags);
|
|
|
|
NS_RELEASE(manager);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsGenericDOMDataNode::RemoveEventListener(const nsString& aType, nsIDOMEventListener* aListener,
|
1999-07-19 19:54:34 +00:00
|
|
|
PRBool aUseCapture)
|
1999-03-28 22:22:54 +00:00
|
|
|
{
|
|
|
|
if (nsnull != mListenerManager) {
|
1999-07-19 19:54:34 +00:00
|
|
|
PRInt32 flags = aUseCapture ? NS_EVENT_FLAG_CAPTURE : NS_EVENT_FLAG_BUBBLE;
|
1999-03-28 22:22:54 +00:00
|
|
|
|
|
|
|
mListenerManager->RemoveEventListenerByType(aListener, aType, flags);
|
1998-09-06 00:16:36 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Implementation of nsIContent
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
1998-09-06 04:16:22 +00:00
|
|
|
nsGenericDOMDataNode::BeginConvertToXIF(nsXIFConverter& aConverter) const
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1998-09-06 04:16:22 +00:00
|
|
|
nsGenericDOMDataNode::FinishConvertToXIF(nsXIFConverter& aConverter) const
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translate the content object into the (XIF) XML Interchange Format
|
|
|
|
* XIF is an intermediate form of the content model, the buffer
|
|
|
|
* will then be parsed into any number of formats including HTML, TXT, etc.
|
|
|
|
*/
|
|
|
|
nsresult
|
1998-09-06 04:16:22 +00:00
|
|
|
nsGenericDOMDataNode::ConvertContentToXIF(nsXIFConverter& aConverter) const
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
|
|
|
const nsIContent* content = mContent;
|
1999-02-11 23:12:28 +00:00
|
|
|
nsIDOMSelection* sel = aConverter.GetSelection();
|
1998-09-06 00:16:36 +00:00
|
|
|
|
1999-01-21 21:45:17 +00:00
|
|
|
if (sel != nsnull && mDocument->IsInSelection(sel,content))
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
2000-01-19 22:56:25 +00:00
|
|
|
nsCOMPtr<nsIEnumerator> enumerator;
|
|
|
|
if (NS_SUCCEEDED(sel->GetEnumerator(getter_AddRefs(enumerator)))) {
|
1999-01-21 21:45:17 +00:00
|
|
|
for (enumerator->First();NS_OK != enumerator->IsDone(); enumerator->Next()) {
|
|
|
|
nsIDOMRange* range = nsnull;
|
|
|
|
if (NS_SUCCEEDED(enumerator->CurrentItem((nsISupports**)&range))) {
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> startNode;
|
|
|
|
nsCOMPtr<nsIDOMNode> endNode;
|
|
|
|
PRInt32 startOffset = 0;
|
|
|
|
PRInt32 endOffset = 0;
|
|
|
|
|
|
|
|
range->GetStartParent(getter_AddRefs(startNode));
|
|
|
|
range->GetEndParent(getter_AddRefs(endNode));
|
|
|
|
|
|
|
|
range->GetStartOffset(&startOffset);
|
|
|
|
range->GetEndOffset(&endOffset);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> startContent;
|
|
|
|
nsCOMPtr<nsIContent> endContent;
|
1999-02-17 02:08:00 +00:00
|
|
|
startContent = do_QueryInterface(startNode);
|
|
|
|
endContent = do_QueryInterface(endNode);
|
1999-01-21 21:45:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
nsString buffer;
|
|
|
|
mText.AppendTo(buffer);
|
1999-02-17 04:39:22 +00:00
|
|
|
if (startContent.get() == content || endContent.get() == content)
|
1999-01-21 21:45:17 +00:00
|
|
|
{
|
|
|
|
// NOTE: ORDER MATTERS!
|
|
|
|
// This must go before the Cut
|
1999-02-17 04:39:22 +00:00
|
|
|
if (endContent.get() == content)
|
1999-01-21 21:45:17 +00:00
|
|
|
buffer.Truncate(endOffset);
|
|
|
|
|
|
|
|
// This must go after the Trunctate
|
1999-02-17 04:39:22 +00:00
|
|
|
if (startContent.get() == content)
|
1999-01-21 21:45:17 +00:00
|
|
|
buffer.Cut(0,startOffset);
|
|
|
|
}
|
|
|
|
aConverter.AddContent(buffer);
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsString buffer;
|
1998-10-20 00:20:04 +00:00
|
|
|
mText.AppendTo(buffer);
|
1998-09-06 00:16:36 +00:00
|
|
|
aConverter.AddContent(buffer);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-09-06 04:16:22 +00:00
|
|
|
nsGenericDOMDataNode::ToCString(nsString& aBuf, PRInt32 aOffset,
|
1998-09-06 00:16:36 +00:00
|
|
|
PRInt32 aLen) const
|
|
|
|
{
|
1998-10-20 00:20:04 +00:00
|
|
|
if (mText.Is2b()) {
|
|
|
|
const PRUnichar* cp = mText.Get2b() + aOffset;
|
|
|
|
const PRUnichar* end = cp + aLen;
|
|
|
|
while (cp < end) {
|
|
|
|
PRUnichar ch = *cp++;
|
|
|
|
if (ch == '\r') {
|
|
|
|
aBuf.Append("\\r");
|
|
|
|
} else if (ch == '\n') {
|
|
|
|
aBuf.Append("\\n");
|
|
|
|
} else if (ch == '\t') {
|
|
|
|
aBuf.Append("\\t");
|
|
|
|
} else if ((ch < ' ') || (ch >= 127)) {
|
|
|
|
char buf[10];
|
|
|
|
PR_snprintf(buf, sizeof(buf), "\\u%04x", ch);
|
|
|
|
aBuf.Append(buf);
|
|
|
|
} else {
|
|
|
|
aBuf.Append(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
unsigned char* cp = (unsigned char*)mText.Get1b() + aOffset;
|
|
|
|
const unsigned char* end = cp + aLen;
|
|
|
|
while (cp < end) {
|
|
|
|
PRUnichar ch = *cp++;
|
|
|
|
if (ch == '\r') {
|
|
|
|
aBuf.Append("\\r");
|
|
|
|
} else if (ch == '\n') {
|
|
|
|
aBuf.Append("\\n");
|
|
|
|
} else if (ch == '\t') {
|
|
|
|
aBuf.Append("\\t");
|
|
|
|
} else if ((ch < ' ') || (ch >= 127)) {
|
|
|
|
char buf[10];
|
|
|
|
PR_snprintf(buf, sizeof(buf), "\\u%04x", ch);
|
|
|
|
aBuf.Append(buf);
|
|
|
|
} else {
|
|
|
|
aBuf.Append(ch);
|
|
|
|
}
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1998-09-06 04:16:22 +00:00
|
|
|
nsGenericDOMDataNode::GetDocument(nsIDocument*& aResult) const
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
|
|
|
aResult = mDocument;
|
|
|
|
NS_IF_ADDREF(mDocument);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1998-10-26 23:26:01 +00:00
|
|
|
|
1998-09-06 00:16:36 +00:00
|
|
|
nsresult
|
1998-10-26 23:26:01 +00:00
|
|
|
nsGenericDOMDataNode::SetDocument(nsIDocument* aDocument, PRBool aDeep)
|
|
|
|
{
|
|
|
|
// If we were part of a document, make sure we get rid of the
|
|
|
|
// script context reference to our script object so that our
|
|
|
|
// script object can be freed (or collected).
|
|
|
|
if ((nsnull != mDocument) && (nsnull != mScriptObject)) {
|
1999-12-03 09:24:22 +00:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> globalObject;
|
|
|
|
mDocument->GetScriptGlobalObject(getter_AddRefs(globalObject));
|
|
|
|
if (globalObject) {
|
|
|
|
nsCOMPtr<nsIScriptContext> context;
|
2000-01-04 21:24:37 +00:00
|
|
|
if (NS_OK == globalObject->GetContext(getter_AddRefs(context)) && context) {
|
1998-10-26 23:26:01 +00:00
|
|
|
context->RemoveReference((void *)&mScriptObject,
|
|
|
|
mScriptObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-09-06 00:16:36 +00:00
|
|
|
mDocument = aDocument;
|
1998-10-26 23:26:01 +00:00
|
|
|
|
|
|
|
// If we already have a script object and now we're being added
|
|
|
|
// to a document, make sure that the script context adds a
|
|
|
|
// reference to our script object. This will ensure that it
|
|
|
|
// won't be freed (or collected) out from under us.
|
|
|
|
if ((nsnull != mDocument) && (nsnull != mScriptObject)) {
|
1999-12-03 09:24:22 +00:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> globalObject;
|
|
|
|
mDocument->GetScriptGlobalObject(getter_AddRefs(globalObject));
|
|
|
|
if (globalObject) {
|
|
|
|
nsCOMPtr<nsIScriptContext> context;
|
2000-01-04 21:24:37 +00:00
|
|
|
if (NS_OK == globalObject->GetContext(getter_AddRefs(context)) && context) {
|
1998-10-26 23:26:01 +00:00
|
|
|
context->AddNamedReference((void *)&mScriptObject,
|
|
|
|
mScriptObject,
|
|
|
|
"Text");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-09-06 00:16:36 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1998-09-06 04:16:22 +00:00
|
|
|
nsGenericDOMDataNode::GetParent(nsIContent*& aResult) const
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
|
|
|
NS_IF_ADDREF(mParent);
|
|
|
|
aResult = mParent;
|
|
|
|
return NS_OK;;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1998-09-06 04:16:22 +00:00
|
|
|
nsGenericDOMDataNode::SetParent(nsIContent* aParent)
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
|
|
|
mParent = aParent;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1999-11-24 06:03:41 +00:00
|
|
|
nsGenericDOMDataNode::HandleDOMEvent(nsIPresContext* aPresContext,
|
1998-09-06 00:16:36 +00:00
|
|
|
nsEvent* aEvent,
|
|
|
|
nsIDOMEvent** aDOMEvent,
|
|
|
|
PRUint32 aFlags,
|
1999-11-24 06:03:41 +00:00
|
|
|
nsEventStatus* aEventStatus)
|
1998-09-06 00:16:36 +00:00
|
|
|
{
|
|
|
|
nsresult ret = NS_OK;
|
|
|
|
nsIDOMEvent* domEvent = nsnull;
|
1999-03-28 22:22:54 +00:00
|
|
|
|
|
|
|
if (NS_EVENT_FLAG_INIT == aFlags) {
|
1998-09-06 00:16:36 +00:00
|
|
|
aDOMEvent = &domEvent;
|
1999-07-19 19:54:34 +00:00
|
|
|
aEvent->flags = NS_EVENT_FLAG_NONE;
|
1999-03-28 22:22:54 +00:00
|
|
|
|
|
|
|
//Initiate capturing phase. Special case first call to document
|
|
|
|
if (nsnull != mDocument) {
|
|
|
|
mDocument->HandleDOMEvent(aPresContext, aEvent, aDOMEvent, NS_EVENT_FLAG_CAPTURE, aEventStatus);
|
|
|
|
}
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
|
1999-03-28 22:22:54 +00:00
|
|
|
//Capturing stage evaluation
|
|
|
|
//Always pass capturing up the tree before local evaulation
|
|
|
|
if (NS_EVENT_FLAG_BUBBLE != aFlags && nsnull != mCapturer) {
|
|
|
|
mCapturer->HandleDOMEvent(aPresContext, aEvent, aDOMEvent, NS_EVENT_FLAG_CAPTURE, aEventStatus);
|
|
|
|
}
|
1998-09-06 00:16:36 +00:00
|
|
|
|
|
|
|
//Local handling stage
|
1999-07-19 19:54:34 +00:00
|
|
|
if (!(aEvent->flags & NS_EVENT_FLAG_STOP_DISPATCH) && nsnull != mListenerManager) {
|
1999-03-28 22:22:54 +00:00
|
|
|
mListenerManager->HandleEvent(aPresContext, aEvent, aDOMEvent, aFlags, aEventStatus);
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Bubbling stage
|
1999-03-28 22:22:54 +00:00
|
|
|
if (NS_EVENT_FLAG_CAPTURE != aFlags && mParent != nsnull) {
|
1998-09-06 00:16:36 +00:00
|
|
|
ret = mParent->HandleDOMEvent(aPresContext, aEvent, aDOMEvent,
|
1999-03-28 22:22:54 +00:00
|
|
|
NS_EVENT_FLAG_BUBBLE, aEventStatus);
|
1998-09-06 00:16:36 +00:00
|
|
|
}
|
|
|
|
|
1999-03-28 22:22:54 +00:00
|
|
|
if (NS_EVENT_FLAG_INIT == aFlags) {
|
1998-09-06 00:16:36 +00:00
|
|
|
// We're leaving the DOM event loop so if we created a DOM event,
|
|
|
|
// release here.
|
|
|
|
if (nsnull != *aDOMEvent) {
|
|
|
|
if (0 != (*aDOMEvent)->Release()) {
|
|
|
|
// Okay, so someone in the DOM loop (a listener, JS object)
|
|
|
|
// still has a ref to the DOM Event but the internal data
|
|
|
|
// hasn't been malloc'd. Force a copy of the data here so the
|
|
|
|
// DOM Event is still valid.
|
|
|
|
nsIPrivateDOMEvent *privateEvent;
|
|
|
|
if (NS_OK == (*aDOMEvent)->QueryInterface(kIPrivateDOMEventIID, (void**)&privateEvent)) {
|
|
|
|
privateEvent->DuplicatePrivateData();
|
|
|
|
NS_RELEASE(privateEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
aDOMEvent = nsnull;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
1998-12-17 07:22:28 +00:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsGenericDOMDataNode::RangeAdd(nsIDOMRange& aRange)
|
|
|
|
{
|
1999-01-04 16:48:33 +00:00
|
|
|
// lazy allocation of range list
|
1998-12-17 07:22:28 +00:00
|
|
|
if (nsnull == mRangeList) {
|
|
|
|
mRangeList = new nsVoidArray();
|
|
|
|
}
|
|
|
|
if (nsnull == mRangeList) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
1999-08-06 10:33:09 +00:00
|
|
|
|
|
|
|
// Make sure we don't add a range that is already
|
|
|
|
// in the list!
|
|
|
|
PRInt32 i = mRangeList->IndexOf(&aRange);
|
|
|
|
if (i >= 0) {
|
|
|
|
// Range is already in the list, so there
|
|
|
|
// is nothing to do!
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1998-12-17 07:22:28 +00:00
|
|
|
// dont need to addref - this call is made by the range object itself
|
|
|
|
PRBool rv = mRangeList->AppendElement(&aRange);
|
|
|
|
if (rv) return NS_OK;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsGenericDOMDataNode::RangeRemove(nsIDOMRange& aRange)
|
|
|
|
{
|
1999-01-04 16:48:33 +00:00
|
|
|
if (mRangeList) {
|
1998-12-17 07:22:28 +00:00
|
|
|
// dont need to release - this call is made by the range object itself
|
|
|
|
PRBool rv = mRangeList->RemoveElement(&aRange);
|
1998-12-18 02:51:34 +00:00
|
|
|
if (rv) {
|
|
|
|
if (mRangeList->Count() == 0) {
|
|
|
|
delete mRangeList;
|
1999-01-04 16:48:33 +00:00
|
|
|
mRangeList = nsnull;
|
1998-12-18 02:51:34 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-12-17 07:22:28 +00:00
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
1998-12-18 02:51:34 +00:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsGenericDOMDataNode::GetRangeList(nsVoidArray*& aResult) const
|
|
|
|
{
|
|
|
|
aResult = mRangeList;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-03-31 20:49:25 +00:00
|
|
|
|
1999-10-05 23:47:46 +00:00
|
|
|
nsresult
|
|
|
|
nsGenericDOMDataNode::SizeOf(nsISizeOfHandler* aSizer, PRUint32* aResult,
|
|
|
|
size_t aInstanceSize) const
|
|
|
|
{
|
|
|
|
if (!aResult) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
PRUint32 sum = 0;
|
|
|
|
#ifdef DEBUG
|
|
|
|
sum += (PRUint32) aInstanceSize;
|
|
|
|
sum += mText.GetLength() *
|
|
|
|
(mText.Is2b() ? sizeof(PRUnichar) : sizeof(char));
|
|
|
|
#endif
|
|
|
|
*aResult = sum;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-03-31 20:49:25 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Implementation of the nsIDOMText interface
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsGenericDOMDataNode::SplitText(PRUint32 aOffset, nsIDOMText** aReturn)
|
|
|
|
{
|
1999-12-22 01:51:58 +00:00
|
|
|
nsresult rv = NS_OK;
|
1999-03-31 20:49:25 +00:00
|
|
|
nsAutoString cutText;
|
|
|
|
PRUint32 length;
|
|
|
|
|
|
|
|
GetLength(&length);
|
1999-12-22 01:51:58 +00:00
|
|
|
if (aOffset > length) {
|
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
1999-03-31 20:49:25 +00:00
|
|
|
}
|
|
|
|
|
1999-12-22 01:51:58 +00:00
|
|
|
rv = SubstringData(aOffset, length-aOffset, cutText);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = DeleteData(aOffset, length-aOffset);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Use CloneContent() for creating the new node so that the new node is of
|
|
|
|
* same class as this node!
|
|
|
|
*/
|
|
|
|
|
|
|
|
nsCOMPtr<nsITextContent> tmpContent(do_QueryInterface(mContent, &rv));
|
|
|
|
nsCOMPtr<nsITextContent> newContent;
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = tmpContent->CloneContent(PR_FALSE, getter_AddRefs(newContent));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> newNode = do_QueryInterface(newContent, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = newNode->SetNodeValue(cutText);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> parentNode;
|
|
|
|
GetParent(*getter_AddRefs(parentNode));
|
|
|
|
|
|
|
|
if (parentNode) {
|
|
|
|
PRInt32 index;
|
|
|
|
|
|
|
|
rv = parentNode->IndexOf(mContent, index);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsIContent> content(do_QueryInterface(newNode));
|
|
|
|
|
|
|
|
rv = parentNode->InsertChildAt(content, index+1, PR_TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newNode->QueryInterface(kIDOMTextIID, (void**)aReturn);
|
1999-03-31 20:49:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Implementation of the nsITextContent interface
|
|
|
|
|
|
|
|
nsresult
|
1999-10-15 23:36:07 +00:00
|
|
|
nsGenericDOMDataNode::GetText(const nsTextFragment** aFragmentsResult)
|
1999-03-31 20:49:25 +00:00
|
|
|
{
|
1999-10-15 23:36:07 +00:00
|
|
|
*aFragmentsResult = &mText;
|
1999-03-31 20:49:25 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-08-27 21:40:47 +00:00
|
|
|
nsresult
|
|
|
|
nsGenericDOMDataNode::GetTextLength(PRInt32* aLengthResult)
|
|
|
|
{
|
|
|
|
if (!aLengthResult) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
*aLengthResult = mText.GetLength();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsGenericDOMDataNode::CopyText(nsString& aResult)
|
|
|
|
{
|
|
|
|
if (mText.Is2b()) {
|
|
|
|
aResult.SetString(mText.Get2b(), mText.GetLength());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aResult.SetString(mText.Get1b(), mText.GetLength());
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-03-31 20:49:25 +00:00
|
|
|
nsresult
|
|
|
|
nsGenericDOMDataNode::SetText(const PRUnichar* aBuffer, PRInt32 aLength,
|
|
|
|
PRBool aNotify)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION((aLength >= 0) && (nsnull != aBuffer), "bad args");
|
|
|
|
if (aLength < 0) {
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
if (nsnull == aBuffer) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
mText.SetTo(aBuffer, aLength);
|
|
|
|
|
|
|
|
// Trigger a reflow
|
|
|
|
if (aNotify && (nsnull != mDocument)) {
|
|
|
|
mDocument->ContentChanged(mContent, nsnull);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsGenericDOMDataNode::SetText(const char* aBuffer,
|
|
|
|
PRInt32 aLength,
|
|
|
|
PRBool aNotify)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION((aLength >= 0) && (nsnull != aBuffer), "bad args");
|
|
|
|
if (aLength < 0) {
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
if (nsnull == aBuffer) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
mText.SetTo(aBuffer, aLength);
|
|
|
|
|
|
|
|
// Trigger a reflow
|
|
|
|
if (aNotify && (nsnull != mDocument)) {
|
|
|
|
mDocument->ContentChanged(mContent, nsnull);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsGenericDOMDataNode::IsOnlyWhitespace(PRBool* aResult)
|
|
|
|
{
|
|
|
|
nsTextFragment& frag = mText;
|
|
|
|
if (frag.Is2b()) {
|
|
|
|
const PRUnichar* cp = frag.Get2b();
|
|
|
|
const PRUnichar* end = cp + frag.GetLength();
|
|
|
|
while (cp < end) {
|
|
|
|
PRUnichar ch = *cp++;
|
|
|
|
if (!XP_IS_SPACE(ch)) {
|
|
|
|
*aResult = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const char* cp = frag.Get1b();
|
|
|
|
const char* end = cp + frag.GetLength();
|
|
|
|
while (cp < end) {
|
|
|
|
PRUnichar ch = PRUnichar(*(unsigned char*)cp);
|
|
|
|
cp++;
|
|
|
|
if (!XP_IS_SPACE(ch)) {
|
|
|
|
*aResult = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|