зеркало из https://github.com/mozilla/pjs.git
create element transaction WIP
This commit is contained in:
Родитель
80e7dfeb96
Коммит
3534fc762a
|
@ -0,0 +1,128 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
|
||||
#include "CreateElementTxn.h"
|
||||
#include "editor.h"
|
||||
#include "nsIDOMNode.h"
|
||||
#include "nsIDOMNodeList.h"
|
||||
#include "nsIDOMDocument.h"
|
||||
#include "nsIDOMElement.h"
|
||||
|
||||
// note that aEditor is not refcounted
|
||||
CreateElementTxn::CreateElementTxn(nsEditor *aEditor,
|
||||
nsIDOMDocument *aDoc,
|
||||
nsString& aTag,
|
||||
nsIDOMNode *aParent,
|
||||
PRUint32 aOffsetInParent)
|
||||
: EditTxn(aEditor)
|
||||
{
|
||||
mDoc = aDoc;
|
||||
NS_ADDREF(mDoc);
|
||||
mTag = aTag;
|
||||
mParent = aParent;
|
||||
NS_ADDREF(mParent);
|
||||
mOffsetInParent = aOffsetInParent;
|
||||
mNewNode = nsnull;
|
||||
}
|
||||
|
||||
CreateElementTxn::~CreateElementTxn()
|
||||
{
|
||||
NS_IF_RELEASE(mDoc);
|
||||
NS_IF_RELEASE(mParent);
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::Do(void)
|
||||
{
|
||||
// create a new node
|
||||
nsresult result = mDoc->CreateElement(mTag, &mNewNode);
|
||||
NS_ASSERTION(((NS_SUCCEEDED(result)) && (nsnull!=mNewNode)), "could not create element.");
|
||||
|
||||
if ((NS_SUCCEEDED(result)) && (nsnull!=mNewNode))
|
||||
{
|
||||
// insert the new node
|
||||
nsIDOMNode *resultNode=nsnull;
|
||||
if (CreateElementTxn::eAppend==mOffsetInParent)
|
||||
{
|
||||
result = mParent->AppendChild(mNewNode, &resultNode);
|
||||
//XXX: do we need to do anything with resultNode? release a refcount?
|
||||
}
|
||||
else
|
||||
{
|
||||
nsCOMPtr<nsIDOMNodeList> childNodes;
|
||||
result = mParent->GetChildNodes(getter_AddRefs(childNodes));
|
||||
if ((NS_SUCCEEDED(result)) && (childNodes))
|
||||
{
|
||||
result = childNodes->Item(mOffsetInParent, &mRefNode);
|
||||
if ((NS_SUCCEEDED(result)) && (nsnull!=mRefNode))
|
||||
{
|
||||
result = mParent->InsertBefore(mNewNode, mRefNode, &resultNode);
|
||||
//XXX: do we need to do anything with resultNode? release a refcount?
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::Undo(void)
|
||||
{
|
||||
return NS_OK;//(mParent->InsertData(mOffset, mDeletedText));
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::Redo(void)
|
||||
{
|
||||
nsIDOMNode *resultNode=nsnull;
|
||||
return (mParent->InsertBefore(mNewNode, mRefNode, &resultNode));
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::GetIsTransient(PRBool *aIsTransient)
|
||||
{
|
||||
if (nsnull!=aIsTransient)
|
||||
*aIsTransient = PR_FALSE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::Merge(PRBool *aDidMerge, nsITransaction *aTransaction)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::Write(nsIOutputStream *aOutputStream)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::GetUndoString(nsString **aString)
|
||||
{
|
||||
if (nsnull!=aString)
|
||||
{
|
||||
**aString="Remove Element: ";
|
||||
**aString += mTag;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::GetRedoString(nsString **aString)
|
||||
{
|
||||
if (nsnull!=aString)
|
||||
{
|
||||
**aString="Create Element: ";
|
||||
**aString += mTag;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
|
||||
#ifndef CreateElementTxn_h__
|
||||
#define CreateElementTxn_h__
|
||||
|
||||
#include "EditTxn.h"
|
||||
|
||||
class nsIDOMDocument;
|
||||
class nsIDOMNode;
|
||||
class nsIDOMElement;
|
||||
|
||||
/**
|
||||
* A transaction that creates a new node in the content tree.
|
||||
*/
|
||||
class CreateElementTxn : public EditTxn
|
||||
{
|
||||
public:
|
||||
|
||||
enum { eAppend=-1 };
|
||||
|
||||
CreateElementTxn(nsEditor *aEditor,
|
||||
nsIDOMDocument *aDoc,
|
||||
nsString& aTag,
|
||||
nsIDOMNode *aParent,
|
||||
PRUint32 aOffsetInParent);
|
||||
|
||||
virtual ~CreateElementTxn();
|
||||
|
||||
virtual nsresult Do(void);
|
||||
|
||||
virtual nsresult Undo(void);
|
||||
|
||||
virtual nsresult Redo(void);
|
||||
|
||||
virtual nsresult GetIsTransient(PRBool *aIsTransient);
|
||||
|
||||
virtual nsresult Merge(PRBool *aDidMerge, nsITransaction *aTransaction);
|
||||
|
||||
virtual nsresult Write(nsIOutputStream *aOutputStream);
|
||||
|
||||
virtual nsresult GetUndoString(nsString **aString);
|
||||
|
||||
virtual nsresult GetRedoString(nsString **aString);
|
||||
|
||||
protected:
|
||||
|
||||
/** the document into which the new node will be inserted */
|
||||
nsIDOMDocument *mDoc;
|
||||
|
||||
/** the tag (mapping to object type) for the new element */
|
||||
nsString mTag;
|
||||
|
||||
/** the node into which the new node will be inserted */
|
||||
nsIDOMNode *mParent;
|
||||
|
||||
/** the index in mParent for the new node */
|
||||
PRUint32 mOffsetInParent;
|
||||
|
||||
/** the new node to insert */
|
||||
nsIDOMElement *mNewNode;
|
||||
|
||||
/** the node we will insert mNewNode before. We compute this ourselves. */
|
||||
nsIDOMNode *mRefNode;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -32,6 +32,7 @@ CPPSRCS = \
|
|||
EditTxn.cpp \
|
||||
InsertTextTxn.cpp \
|
||||
DeleteTextTxn.cpp \
|
||||
CreateElementTxn.cpp \
|
||||
$(NULL)
|
||||
|
||||
MODULE = editor
|
||||
|
|
|
@ -16,15 +16,19 @@
|
|||
* Reserved.
|
||||
*/
|
||||
#include "editorInterfaces.h"
|
||||
#include "nsString.h"
|
||||
#include "editor.h"
|
||||
|
||||
#include "ChangeAttributeTxn.h"
|
||||
#include "InsertTextTxn.h"
|
||||
#include "DeleteTextTxn.h"
|
||||
#include "CreateElementTxn.h"
|
||||
|
||||
#include "nsIDOMDocument.h"
|
||||
#include "nsIDOMElement.h"
|
||||
#include "nsIDOMCharacterData.h"
|
||||
|
||||
#include "nsString.h"
|
||||
|
||||
static NS_DEFINE_IID(kIDOMElementIID, NS_IDOMELEMENT_IID);
|
||||
static NS_DEFINE_IID(kIDOMCharacterDataIID, NS_IDOMCHARACTERDATA_IID);
|
||||
|
||||
|
@ -282,6 +286,45 @@ nsEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aProces
|
|||
aProcessed=PR_TRUE;
|
||||
break;
|
||||
|
||||
case nsIDOMEvent::VK_INSERT:
|
||||
{
|
||||
nsresult result;
|
||||
//XXX: should be from a factory
|
||||
//XXX: should manage the refcount of txn
|
||||
nsString attribute("src");
|
||||
nsString value("resource:/res/samples/raptor.jpg");
|
||||
|
||||
nsString imgTag("IMG");
|
||||
nsString bodyTag("BODY");
|
||||
nsCOMPtr<nsIDOMNode> currentNode;
|
||||
result = mEditor->GetFirstNodeOfType(nsnull, bodyTag, getter_AddRefs(currentNode));
|
||||
if (NS_SUCCEEDED(result))
|
||||
{
|
||||
nsIDOMDocument *doc=nsnull;
|
||||
result = mEditor->GetDomInterface(&doc);
|
||||
if (NS_SUCCEEDED(result))
|
||||
{
|
||||
CreateElementTxn *txn;
|
||||
txn = new CreateElementTxn(mEditor, doc, imgTag, currentNode, 0);
|
||||
mEditor->ExecuteTransaction(txn);
|
||||
}
|
||||
}
|
||||
|
||||
/* for building a composite transaction */
|
||||
nsCOMPtr<nsIDOMElement> element;
|
||||
if (NS_SUCCEEDED(mEditor->GetFirstNodeOfType(nsnull, imgTag, getter_AddRefs(currentNode))))
|
||||
{
|
||||
if (NS_SUCCEEDED(currentNode->QueryInterface(kIDOMElementIID, getter_AddRefs(element))))
|
||||
{
|
||||
ChangeAttributeTxn *txn;
|
||||
txn = new ChangeAttributeTxn(mEditor, element, attribute, value, PR_FALSE);
|
||||
mEditor->ExecuteTransaction(txn);
|
||||
}
|
||||
}
|
||||
}
|
||||
aProcessed=PR_TRUE;
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
return NS_OK;
|
||||
|
|
|
@ -28,6 +28,7 @@ CPPSRCS = \
|
|||
ChangeAttributeTxn.cpp \
|
||||
InsertTextTxn.cpp \
|
||||
DeleteTextTxn.cpp \
|
||||
CreateElementTxn.cpp \
|
||||
$(NULL)
|
||||
|
||||
CPP_OBJS = \
|
||||
|
@ -38,6 +39,7 @@ CPP_OBJS = \
|
|||
.\$(OBJDIR)\ChangeAttributeTxn.obj \
|
||||
.\$(OBJDIR)\InsertTextTxn.obj \
|
||||
.\$(OBJDIR)\DeleteTextTxn.obj \
|
||||
.\$(OBJDIR)\CreateElementTxn.obj \
|
||||
$(NULL)
|
||||
|
||||
MODULE=editor
|
||||
|
|
|
@ -0,0 +1,128 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
|
||||
#include "CreateElementTxn.h"
|
||||
#include "editor.h"
|
||||
#include "nsIDOMNode.h"
|
||||
#include "nsIDOMNodeList.h"
|
||||
#include "nsIDOMDocument.h"
|
||||
#include "nsIDOMElement.h"
|
||||
|
||||
// note that aEditor is not refcounted
|
||||
CreateElementTxn::CreateElementTxn(nsEditor *aEditor,
|
||||
nsIDOMDocument *aDoc,
|
||||
nsString& aTag,
|
||||
nsIDOMNode *aParent,
|
||||
PRUint32 aOffsetInParent)
|
||||
: EditTxn(aEditor)
|
||||
{
|
||||
mDoc = aDoc;
|
||||
NS_ADDREF(mDoc);
|
||||
mTag = aTag;
|
||||
mParent = aParent;
|
||||
NS_ADDREF(mParent);
|
||||
mOffsetInParent = aOffsetInParent;
|
||||
mNewNode = nsnull;
|
||||
}
|
||||
|
||||
CreateElementTxn::~CreateElementTxn()
|
||||
{
|
||||
NS_IF_RELEASE(mDoc);
|
||||
NS_IF_RELEASE(mParent);
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::Do(void)
|
||||
{
|
||||
// create a new node
|
||||
nsresult result = mDoc->CreateElement(mTag, &mNewNode);
|
||||
NS_ASSERTION(((NS_SUCCEEDED(result)) && (nsnull!=mNewNode)), "could not create element.");
|
||||
|
||||
if ((NS_SUCCEEDED(result)) && (nsnull!=mNewNode))
|
||||
{
|
||||
// insert the new node
|
||||
nsIDOMNode *resultNode=nsnull;
|
||||
if (CreateElementTxn::eAppend==mOffsetInParent)
|
||||
{
|
||||
result = mParent->AppendChild(mNewNode, &resultNode);
|
||||
//XXX: do we need to do anything with resultNode? release a refcount?
|
||||
}
|
||||
else
|
||||
{
|
||||
nsCOMPtr<nsIDOMNodeList> childNodes;
|
||||
result = mParent->GetChildNodes(getter_AddRefs(childNodes));
|
||||
if ((NS_SUCCEEDED(result)) && (childNodes))
|
||||
{
|
||||
result = childNodes->Item(mOffsetInParent, &mRefNode);
|
||||
if ((NS_SUCCEEDED(result)) && (nsnull!=mRefNode))
|
||||
{
|
||||
result = mParent->InsertBefore(mNewNode, mRefNode, &resultNode);
|
||||
//XXX: do we need to do anything with resultNode? release a refcount?
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::Undo(void)
|
||||
{
|
||||
return NS_OK;//(mParent->InsertData(mOffset, mDeletedText));
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::Redo(void)
|
||||
{
|
||||
nsIDOMNode *resultNode=nsnull;
|
||||
return (mParent->InsertBefore(mNewNode, mRefNode, &resultNode));
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::GetIsTransient(PRBool *aIsTransient)
|
||||
{
|
||||
if (nsnull!=aIsTransient)
|
||||
*aIsTransient = PR_FALSE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::Merge(PRBool *aDidMerge, nsITransaction *aTransaction)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::Write(nsIOutputStream *aOutputStream)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::GetUndoString(nsString **aString)
|
||||
{
|
||||
if (nsnull!=aString)
|
||||
{
|
||||
**aString="Remove Element: ";
|
||||
**aString += mTag;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::GetRedoString(nsString **aString)
|
||||
{
|
||||
if (nsnull!=aString)
|
||||
{
|
||||
**aString="Create Element: ";
|
||||
**aString += mTag;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
|
||||
#ifndef CreateElementTxn_h__
|
||||
#define CreateElementTxn_h__
|
||||
|
||||
#include "EditTxn.h"
|
||||
|
||||
class nsIDOMDocument;
|
||||
class nsIDOMNode;
|
||||
class nsIDOMElement;
|
||||
|
||||
/**
|
||||
* A transaction that creates a new node in the content tree.
|
||||
*/
|
||||
class CreateElementTxn : public EditTxn
|
||||
{
|
||||
public:
|
||||
|
||||
enum { eAppend=-1 };
|
||||
|
||||
CreateElementTxn(nsEditor *aEditor,
|
||||
nsIDOMDocument *aDoc,
|
||||
nsString& aTag,
|
||||
nsIDOMNode *aParent,
|
||||
PRUint32 aOffsetInParent);
|
||||
|
||||
virtual ~CreateElementTxn();
|
||||
|
||||
virtual nsresult Do(void);
|
||||
|
||||
virtual nsresult Undo(void);
|
||||
|
||||
virtual nsresult Redo(void);
|
||||
|
||||
virtual nsresult GetIsTransient(PRBool *aIsTransient);
|
||||
|
||||
virtual nsresult Merge(PRBool *aDidMerge, nsITransaction *aTransaction);
|
||||
|
||||
virtual nsresult Write(nsIOutputStream *aOutputStream);
|
||||
|
||||
virtual nsresult GetUndoString(nsString **aString);
|
||||
|
||||
virtual nsresult GetRedoString(nsString **aString);
|
||||
|
||||
protected:
|
||||
|
||||
/** the document into which the new node will be inserted */
|
||||
nsIDOMDocument *mDoc;
|
||||
|
||||
/** the tag (mapping to object type) for the new element */
|
||||
nsString mTag;
|
||||
|
||||
/** the node into which the new node will be inserted */
|
||||
nsIDOMNode *mParent;
|
||||
|
||||
/** the index in mParent for the new node */
|
||||
PRUint32 mOffsetInParent;
|
||||
|
||||
/** the new node to insert */
|
||||
nsIDOMElement *mNewNode;
|
||||
|
||||
/** the node we will insert mNewNode before. We compute this ourselves. */
|
||||
nsIDOMNode *mRefNode;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -32,6 +32,7 @@ CPPSRCS = \
|
|||
EditTxn.cpp \
|
||||
InsertTextTxn.cpp \
|
||||
DeleteTextTxn.cpp \
|
||||
CreateElementTxn.cpp \
|
||||
$(NULL)
|
||||
|
||||
MODULE = editor
|
||||
|
|
|
@ -16,15 +16,19 @@
|
|||
* Reserved.
|
||||
*/
|
||||
#include "editorInterfaces.h"
|
||||
#include "nsString.h"
|
||||
#include "editor.h"
|
||||
|
||||
#include "ChangeAttributeTxn.h"
|
||||
#include "InsertTextTxn.h"
|
||||
#include "DeleteTextTxn.h"
|
||||
#include "CreateElementTxn.h"
|
||||
|
||||
#include "nsIDOMDocument.h"
|
||||
#include "nsIDOMElement.h"
|
||||
#include "nsIDOMCharacterData.h"
|
||||
|
||||
#include "nsString.h"
|
||||
|
||||
static NS_DEFINE_IID(kIDOMElementIID, NS_IDOMELEMENT_IID);
|
||||
static NS_DEFINE_IID(kIDOMCharacterDataIID, NS_IDOMCHARACTERDATA_IID);
|
||||
|
||||
|
@ -282,6 +286,45 @@ nsEditorKeyListener::ProcessShortCutKeys(nsIDOMEvent* aKeyEvent, PRBool& aProces
|
|||
aProcessed=PR_TRUE;
|
||||
break;
|
||||
|
||||
case nsIDOMEvent::VK_INSERT:
|
||||
{
|
||||
nsresult result;
|
||||
//XXX: should be from a factory
|
||||
//XXX: should manage the refcount of txn
|
||||
nsString attribute("src");
|
||||
nsString value("resource:/res/samples/raptor.jpg");
|
||||
|
||||
nsString imgTag("IMG");
|
||||
nsString bodyTag("BODY");
|
||||
nsCOMPtr<nsIDOMNode> currentNode;
|
||||
result = mEditor->GetFirstNodeOfType(nsnull, bodyTag, getter_AddRefs(currentNode));
|
||||
if (NS_SUCCEEDED(result))
|
||||
{
|
||||
nsIDOMDocument *doc=nsnull;
|
||||
result = mEditor->GetDomInterface(&doc);
|
||||
if (NS_SUCCEEDED(result))
|
||||
{
|
||||
CreateElementTxn *txn;
|
||||
txn = new CreateElementTxn(mEditor, doc, imgTag, currentNode, 0);
|
||||
mEditor->ExecuteTransaction(txn);
|
||||
}
|
||||
}
|
||||
|
||||
/* for building a composite transaction */
|
||||
nsCOMPtr<nsIDOMElement> element;
|
||||
if (NS_SUCCEEDED(mEditor->GetFirstNodeOfType(nsnull, imgTag, getter_AddRefs(currentNode))))
|
||||
{
|
||||
if (NS_SUCCEEDED(currentNode->QueryInterface(kIDOMElementIID, getter_AddRefs(element))))
|
||||
{
|
||||
ChangeAttributeTxn *txn;
|
||||
txn = new ChangeAttributeTxn(mEditor, element, attribute, value, PR_FALSE);
|
||||
mEditor->ExecuteTransaction(txn);
|
||||
}
|
||||
}
|
||||
}
|
||||
aProcessed=PR_TRUE;
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
return NS_OK;
|
||||
|
|
|
@ -28,6 +28,7 @@ CPPSRCS = \
|
|||
ChangeAttributeTxn.cpp \
|
||||
InsertTextTxn.cpp \
|
||||
DeleteTextTxn.cpp \
|
||||
CreateElementTxn.cpp \
|
||||
$(NULL)
|
||||
|
||||
CPP_OBJS = \
|
||||
|
@ -38,6 +39,7 @@ CPP_OBJS = \
|
|||
.\$(OBJDIR)\ChangeAttributeTxn.obj \
|
||||
.\$(OBJDIR)\InsertTextTxn.obj \
|
||||
.\$(OBJDIR)\DeleteTextTxn.obj \
|
||||
.\$(OBJDIR)\CreateElementTxn.obj \
|
||||
$(NULL)
|
||||
|
||||
MODULE=editor
|
||||
|
|
|
@ -0,0 +1,128 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
|
||||
#include "CreateElementTxn.h"
|
||||
#include "editor.h"
|
||||
#include "nsIDOMNode.h"
|
||||
#include "nsIDOMNodeList.h"
|
||||
#include "nsIDOMDocument.h"
|
||||
#include "nsIDOMElement.h"
|
||||
|
||||
// note that aEditor is not refcounted
|
||||
CreateElementTxn::CreateElementTxn(nsEditor *aEditor,
|
||||
nsIDOMDocument *aDoc,
|
||||
nsString& aTag,
|
||||
nsIDOMNode *aParent,
|
||||
PRUint32 aOffsetInParent)
|
||||
: EditTxn(aEditor)
|
||||
{
|
||||
mDoc = aDoc;
|
||||
NS_ADDREF(mDoc);
|
||||
mTag = aTag;
|
||||
mParent = aParent;
|
||||
NS_ADDREF(mParent);
|
||||
mOffsetInParent = aOffsetInParent;
|
||||
mNewNode = nsnull;
|
||||
}
|
||||
|
||||
CreateElementTxn::~CreateElementTxn()
|
||||
{
|
||||
NS_IF_RELEASE(mDoc);
|
||||
NS_IF_RELEASE(mParent);
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::Do(void)
|
||||
{
|
||||
// create a new node
|
||||
nsresult result = mDoc->CreateElement(mTag, &mNewNode);
|
||||
NS_ASSERTION(((NS_SUCCEEDED(result)) && (nsnull!=mNewNode)), "could not create element.");
|
||||
|
||||
if ((NS_SUCCEEDED(result)) && (nsnull!=mNewNode))
|
||||
{
|
||||
// insert the new node
|
||||
nsIDOMNode *resultNode=nsnull;
|
||||
if (CreateElementTxn::eAppend==mOffsetInParent)
|
||||
{
|
||||
result = mParent->AppendChild(mNewNode, &resultNode);
|
||||
//XXX: do we need to do anything with resultNode? release a refcount?
|
||||
}
|
||||
else
|
||||
{
|
||||
nsCOMPtr<nsIDOMNodeList> childNodes;
|
||||
result = mParent->GetChildNodes(getter_AddRefs(childNodes));
|
||||
if ((NS_SUCCEEDED(result)) && (childNodes))
|
||||
{
|
||||
result = childNodes->Item(mOffsetInParent, &mRefNode);
|
||||
if ((NS_SUCCEEDED(result)) && (nsnull!=mRefNode))
|
||||
{
|
||||
result = mParent->InsertBefore(mNewNode, mRefNode, &resultNode);
|
||||
//XXX: do we need to do anything with resultNode? release a refcount?
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::Undo(void)
|
||||
{
|
||||
return NS_OK;//(mParent->InsertData(mOffset, mDeletedText));
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::Redo(void)
|
||||
{
|
||||
nsIDOMNode *resultNode=nsnull;
|
||||
return (mParent->InsertBefore(mNewNode, mRefNode, &resultNode));
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::GetIsTransient(PRBool *aIsTransient)
|
||||
{
|
||||
if (nsnull!=aIsTransient)
|
||||
*aIsTransient = PR_FALSE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::Merge(PRBool *aDidMerge, nsITransaction *aTransaction)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::Write(nsIOutputStream *aOutputStream)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::GetUndoString(nsString **aString)
|
||||
{
|
||||
if (nsnull!=aString)
|
||||
{
|
||||
**aString="Remove Element: ";
|
||||
**aString += mTag;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult CreateElementTxn::GetRedoString(nsString **aString)
|
||||
{
|
||||
if (nsnull!=aString)
|
||||
{
|
||||
**aString="Create Element: ";
|
||||
**aString += mTag;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
|
||||
#ifndef CreateElementTxn_h__
|
||||
#define CreateElementTxn_h__
|
||||
|
||||
#include "EditTxn.h"
|
||||
|
||||
class nsIDOMDocument;
|
||||
class nsIDOMNode;
|
||||
class nsIDOMElement;
|
||||
|
||||
/**
|
||||
* A transaction that creates a new node in the content tree.
|
||||
*/
|
||||
class CreateElementTxn : public EditTxn
|
||||
{
|
||||
public:
|
||||
|
||||
enum { eAppend=-1 };
|
||||
|
||||
CreateElementTxn(nsEditor *aEditor,
|
||||
nsIDOMDocument *aDoc,
|
||||
nsString& aTag,
|
||||
nsIDOMNode *aParent,
|
||||
PRUint32 aOffsetInParent);
|
||||
|
||||
virtual ~CreateElementTxn();
|
||||
|
||||
virtual nsresult Do(void);
|
||||
|
||||
virtual nsresult Undo(void);
|
||||
|
||||
virtual nsresult Redo(void);
|
||||
|
||||
virtual nsresult GetIsTransient(PRBool *aIsTransient);
|
||||
|
||||
virtual nsresult Merge(PRBool *aDidMerge, nsITransaction *aTransaction);
|
||||
|
||||
virtual nsresult Write(nsIOutputStream *aOutputStream);
|
||||
|
||||
virtual nsresult GetUndoString(nsString **aString);
|
||||
|
||||
virtual nsresult GetRedoString(nsString **aString);
|
||||
|
||||
protected:
|
||||
|
||||
/** the document into which the new node will be inserted */
|
||||
nsIDOMDocument *mDoc;
|
||||
|
||||
/** the tag (mapping to object type) for the new element */
|
||||
nsString mTag;
|
||||
|
||||
/** the node into which the new node will be inserted */
|
||||
nsIDOMNode *mParent;
|
||||
|
||||
/** the index in mParent for the new node */
|
||||
PRUint32 mOffsetInParent;
|
||||
|
||||
/** the new node to insert */
|
||||
nsIDOMElement *mNewNode;
|
||||
|
||||
/** the node we will insert mNewNode before. We compute this ourselves. */
|
||||
nsIDOMNode *mRefNode;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
Загрузка…
Ссылка в новой задаче