Fixed a ton of errors where we were not handling error results correctly.

Added some comments of the form "// XXX: ERROR_HANDLING <explanation>"
wherever I saw a potential error but wasn't 100% sure of the intention of the code.
Fixed a few bugs along the way.
This commit is contained in:
buster%netscape.com 1999-08-19 13:30:48 +00:00
Родитель 8dbe37f8bd
Коммит 3bc5912284
31 изменённых файлов: 5610 добавлений и 4772 удалений

Просмотреть файл

@ -35,23 +35,24 @@ NS_IMETHODIMP ChangeAttributeTxn::Init(nsIEditor *aEditor,
const nsString& aValue,
PRBool aRemoveAttribute)
{
if (nsnull!=aEditor && nsnull!=aElement)
{
mEditor = aEditor;
mElement = do_QueryInterface(aElement);
mAttribute = aAttribute;
mValue = aValue;
mRemoveAttribute = aRemoveAttribute;
mAttributeWasSet=PR_FALSE;
mUndoValue="";
return NS_OK;
}
else
return NS_ERROR_NULL_POINTER;
NS_ASSERTION(aEditor && aElement, "bad arg");
if (!aEditor || !aElement) { return NS_ERROR_NULL_POINTER; }
mEditor = aEditor;
mElement = do_QueryInterface(aElement);
mAttribute = aAttribute;
mValue = aValue;
mRemoveAttribute = aRemoveAttribute;
mAttributeWasSet=PR_FALSE;
mUndoValue="";
return NS_OK;
}
NS_IMETHODIMP ChangeAttributeTxn::Do(void)
{
NS_ASSERTION(mEditor && mElement, "bad state");
if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
// need to get the current value of the attribute and save it, and set mAttributeWasSet
nsresult result = mEditor->GetAttributeValue(mElement, mAttribute, mUndoValue, mAttributeWasSet);
// XXX: hack until attribute-was-set code is implemented
@ -70,6 +71,9 @@ NS_IMETHODIMP ChangeAttributeTxn::Do(void)
NS_IMETHODIMP ChangeAttributeTxn::Undo(void)
{
NS_ASSERTION(mEditor && mElement, "bad state");
if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
nsresult result=NS_OK;
if (PR_TRUE==mAttributeWasSet)
result = mElement->SetAttribute(mAttribute, mUndoValue);
@ -81,6 +85,9 @@ NS_IMETHODIMP ChangeAttributeTxn::Undo(void)
NS_IMETHODIMP ChangeAttributeTxn::Redo(void)
{
NS_ASSERTION(mEditor && mElement, "bad state");
if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
nsresult result;
if (PR_FALSE==mRemoveAttribute)

Просмотреть файл

@ -41,24 +41,21 @@ NS_IMETHODIMP CreateElementTxn::Init(nsIEditor *aEditor,
PRUint32 aOffsetInParent)
{
NS_ASSERTION(aEditor&&aParent, "null args");
if (aEditor && aParent)
{
mEditor = aEditor;
mTag = aTag;
mParent = do_QueryInterface(aParent);
mOffsetInParent = aOffsetInParent;
if (!aEditor || !aParent) { return NS_ERROR_NULL_POINTER; }
mEditor = aEditor;
mTag = aTag;
mParent = do_QueryInterface(aParent);
mOffsetInParent = aOffsetInParent;
#ifdef NS_DEBUG
{
nsCOMPtr<nsIDOMNodeList> testChildNodes;
nsresult testResult = mParent->GetChildNodes(getter_AddRefs(testChildNodes));
NS_ASSERTION(testChildNodes, "bad parent type, can't have children.");
NS_ASSERTION(NS_SUCCEEDED(testResult), "bad result.");
}
#endif
return NS_OK;
{
nsCOMPtr<nsIDOMNodeList> testChildNodes;
nsresult testResult = mParent->GetChildNodes(getter_AddRefs(testChildNodes));
NS_ASSERTION(testChildNodes, "bad parent type, can't have children.");
NS_ASSERTION(NS_SUCCEEDED(testResult), "bad result.");
}
else
return NS_ERROR_NULL_POINTER;
#endif
return NS_OK;
}
@ -70,69 +67,69 @@ NS_IMETHODIMP CreateElementTxn::Do(void)
{
if (gNoisy) { printf("Do Create Element parent = %p, offset = %d\n",
mParent.get(), mOffsetInParent); }
NS_ASSERTION(mEditor, "bad state -- null editor");
nsresult result = NS_ERROR_NULL_POINTER;
if (mEditor)
NS_ASSERTION(mEditor && mParent, "bad state");
if (!mEditor || !mParent) return NS_ERROR_NOT_INITIALIZED;
nsresult result;
// create a new node
nsCOMPtr<nsIDOMDocument>doc;
result = mEditor->GetDocument(getter_AddRefs(doc));
if (NS_FAILED(result)) return result;
if (!doc) return NS_ERROR_NULL_POINTER;
if (nsEditor::GetTextNodeTag() == mTag)
{
// create a new node
nsCOMPtr<nsIDOMDocument>doc;
result = mEditor->GetDocument(getter_AddRefs(doc));
if ((NS_SUCCEEDED(result)) && (doc))
const nsString stringData;
nsCOMPtr<nsIDOMText>newTextNode;
result = doc->CreateTextNode(stringData, getter_AddRefs(newTextNode));
if (NS_FAILED(result)) return result;
if (!newTextNode) return NS_ERROR_NULL_POINTER;
mNewNode = do_QueryInterface(newTextNode);
}
else
{
nsCOMPtr<nsIDOMElement>newElement;
result = doc->CreateElement(mTag, getter_AddRefs(newElement));
if (NS_FAILED(result)) return result;
if (!newElement) return NS_ERROR_NULL_POINTER;
mNewNode = do_QueryInterface(newElement);
}
NS_ASSERTION(((NS_SUCCEEDED(result)) && (mNewNode)), "could not create element.");
if (!mNewNode) return NS_ERROR_NULL_POINTER;
if (gNoisy) { printf(" newNode = %p\n", mNewNode.get()); }
// insert the new node
nsCOMPtr<nsIDOMNode> resultNode;
if (CreateElementTxn::eAppend==(PRInt32)mOffsetInParent)
{
result = mParent->AppendChild(mNewNode, getter_AddRefs(resultNode));
}
else
{
nsCOMPtr<nsIDOMNodeList> childNodes;
result = mParent->GetChildNodes(getter_AddRefs(childNodes));
if ((NS_SUCCEEDED(result)) && (childNodes))
{
if (nsEditor::GetTextNodeTag() == mTag)
PRUint32 count;
childNodes->GetLength(&count);
if (mOffsetInParent>count)
mOffsetInParent = count;
result = childNodes->Item(mOffsetInParent, getter_AddRefs(mRefNode));
if (NS_SUCCEEDED(result)) // note, it's ok for mRefNode to be null. that means append
{
const nsString stringData;
nsCOMPtr<nsIDOMText>newTextNode;
result = doc->CreateTextNode(stringData, getter_AddRefs(newTextNode));
if (NS_SUCCEEDED(result) && newTextNode) {
mNewNode = do_QueryInterface(newTextNode);
}
}
else
{
nsCOMPtr<nsIDOMElement>newElement;
result = doc->CreateElement(mTag, getter_AddRefs(newElement));
if (NS_SUCCEEDED(result) && newElement) {
mNewNode = do_QueryInterface(newElement);
}
}
NS_ASSERTION(((NS_SUCCEEDED(result)) && (mNewNode)), "could not create element.");
if ((NS_SUCCEEDED(result)) && (mNewNode))
{
if (gNoisy) { printf(" newNode = %p\n", mNewNode.get()); }
// insert the new node
nsCOMPtr<nsIDOMNode> resultNode;
if (CreateElementTxn::eAppend==(PRInt32)mOffsetInParent)
result = mParent->InsertBefore(mNewNode, mRefNode, getter_AddRefs(resultNode));
if (NS_SUCCEEDED(result))
{
result = mParent->AppendChild(mNewNode, getter_AddRefs(resultNode));
}
else
{
nsCOMPtr<nsIDOMNodeList> childNodes;
result = mParent->GetChildNodes(getter_AddRefs(childNodes));
if ((NS_SUCCEEDED(result)) && (childNodes))
{
PRUint32 count;
childNodes->GetLength(&count);
if (mOffsetInParent>count)
mOffsetInParent = count;
result = childNodes->Item(mOffsetInParent, getter_AddRefs(mRefNode));
if (NS_SUCCEEDED(result)) // note, it's ok for mRefNode to be null. that means append
{
result = mParent->InsertBefore(mNewNode, mRefNode, getter_AddRefs(resultNode));
if (NS_SUCCEEDED(result))
{
nsCOMPtr<nsIDOMSelection> selection;
nsresult selectionResult = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_SUCCEEDED(selectionResult) && selection) {
PRInt32 offset=0;
nsEditor::GetChildOffset(mNewNode, mParent, offset);
selectionResult = selection->Collapse(mParent, offset+1);
NS_ASSERTION((NS_SUCCEEDED(selectionResult)), "selection could not be collapsed after insert.");
}
}
}
}
nsCOMPtr<nsIDOMSelection> selection;
result = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
PRInt32 offset=0;
result = nsEditor::GetChildOffset(mNewNode, mParent, offset);
if (NS_FAILED(result)) return result;
result = selection->Collapse(mParent, offset+1);
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after insert.");
}
}
}
@ -144,20 +141,24 @@ NS_IMETHODIMP CreateElementTxn::Undo(void)
{
if (gNoisy) { printf("Undo Create Element, mParent = %p, node = %p\n",
mParent.get(), mNewNode.get()); }
NS_ASSERTION(mEditor && mParent, "bad state");
if (!mEditor || !mParent) return NS_ERROR_NOT_INITIALIZED;
nsCOMPtr<nsIDOMNode> resultNode;
nsresult result = mParent->RemoveChild(mNewNode, getter_AddRefs(resultNode));
if (NS_SUCCEEDED(result))
{
nsCOMPtr<nsIDOMSelection> selection;
nsresult selectionResult = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_SUCCEEDED(selectionResult) && selection) {
PRInt32 offset=0;
if (mRefNode) {
nsEditor::GetChildOffset(mRefNode, mParent, offset);
}
selectionResult = selection->Collapse(mParent, offset);
NS_ASSERTION((NS_SUCCEEDED(selectionResult)), "selection could not be collapsed after undo of insert.");
result = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
PRInt32 offset=0;
if (mRefNode) {
nsEditor::GetChildOffset(mRefNode, mParent, offset);
}
result = selection->Collapse(mParent, offset);
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of insert.");
}
return result;
}
@ -165,6 +166,8 @@ NS_IMETHODIMP CreateElementTxn::Undo(void)
NS_IMETHODIMP CreateElementTxn::Redo(void)
{
if (gNoisy) { printf("Redo Create Element\n"); }
NS_ASSERTION(mEditor && mParent, "bad state");
if (!mEditor || !mParent) return NS_ERROR_NOT_INITIALIZED;
// first, reset mNewNode so it has no attributes or content
nsCOMPtr<nsIDOMCharacterData>nodeAsText;
@ -182,12 +185,12 @@ NS_IMETHODIMP CreateElementTxn::Redo(void)
{
nsCOMPtr<nsIDOMSelection> selection;
result = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_SUCCEEDED(result) && selection) {
PRInt32 offset=0;
nsEditor::GetChildOffset(mNewNode, mParent, offset);
nsresult selectionResult = selection->Collapse(mParent, offset);
NS_ASSERTION((NS_SUCCEEDED(selectionResult)), "selection could not be collapsed after undo of insert.");
}
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
PRInt32 offset=0;
nsEditor::GetChildOffset(mNewNode, mParent, offset);
result = selection->Collapse(mParent, offset);
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of insert.");
}
return result;
}

Просмотреть файл

@ -51,16 +51,11 @@ DeleteElementTxn::~DeleteElementTxn()
NS_IMETHODIMP DeleteElementTxn::Do(void)
{
if (gNoisy) { printf("%p Do Delete Element element = %p\n", this, mElement.get()); }
if (!mElement)
return NS_ERROR_NULL_POINTER;
if (!mElement) return NS_ERROR_NOT_INITIALIZED;
nsresult result = mElement->GetParentNode(getter_AddRefs(mParent));
if (NS_FAILED(result)) {
return result;
}
if (!mParent) {
return NS_OK; // this is a no-op, there's no parent to delete mElement from
}
if (NS_FAILED(result)) { return result; }
if (!mParent) { return NS_OK; } // this is a no-op, there's no parent to delete mElement from
#ifdef NS_DEBUG
// begin debug output

Просмотреть файл

@ -46,21 +46,22 @@ DeleteRangeTxn::DeleteRangeTxn()
NS_IMETHODIMP DeleteRangeTxn::Init(nsIEditor *aEditor, nsIDOMRange *aRange)
{
if (aEditor && aRange)
{
mEditor = aEditor;
mRange = do_QueryInterface(aRange);
nsresult result = aRange->GetStartParent(getter_AddRefs(mStartParent));
NS_ASSERTION((NS_SUCCEEDED(result)), "GetStartParent failed.");
result = aRange->GetEndParent(getter_AddRefs(mEndParent));
NS_ASSERTION((NS_SUCCEEDED(result)), "GetEndParent failed.");
result = aRange->GetStartOffset(&mStartOffset);
NS_ASSERTION((NS_SUCCEEDED(result)), "GetStartOffset failed.");
result = aRange->GetEndOffset(&mEndOffset);
NS_ASSERTION((NS_SUCCEEDED(result)), "GetEndOffset failed.");
result = aRange->GetCommonParent(getter_AddRefs(mCommonParent));
NS_ASSERTION((NS_SUCCEEDED(result)), "GetCommonParent failed.");
NS_ASSERTION(aEditor && aRange, "bad state");
if (!aEditor || !aRange) { return NS_ERROR_NOT_INITIALIZED; }
mEditor = aEditor;
mRange = do_QueryInterface(aRange);
nsresult result = aRange->GetStartParent(getter_AddRefs(mStartParent));
NS_ASSERTION((NS_SUCCEEDED(result)), "GetStartParent failed.");
result = aRange->GetEndParent(getter_AddRefs(mEndParent));
NS_ASSERTION((NS_SUCCEEDED(result)), "GetEndParent failed.");
result = aRange->GetStartOffset(&mStartOffset);
NS_ASSERTION((NS_SUCCEEDED(result)), "GetStartOffset failed.");
result = aRange->GetEndOffset(&mEndOffset);
NS_ASSERTION((NS_SUCCEEDED(result)), "GetEndOffset failed.");
result = aRange->GetCommonParent(getter_AddRefs(mCommonParent));
NS_ASSERTION((NS_SUCCEEDED(result)), "GetCommonParent failed.");
#ifdef NS_DEBUG
{
@ -96,10 +97,8 @@ NS_IMETHODIMP DeleteRangeTxn::Init(nsIEditor *aEditor, nsIDOMRange *aRange)
}
}
#endif
return result;
}
else
return NS_ERROR_NULL_POINTER;
return result;
}
DeleteRangeTxn::~DeleteRangeTxn()
@ -109,11 +108,10 @@ DeleteRangeTxn::~DeleteRangeTxn()
NS_IMETHODIMP DeleteRangeTxn::Do(void)
{
if (gNoisy) { printf("Do Delete Range\n"); }
if (!mStartParent || !mEndParent || !mCommonParent)
return NS_ERROR_NULL_POINTER;
if (!mStartParent || !mEndParent || !mCommonParent || !mEditor)
return NS_ERROR_NOT_INITIALIZED;
nsresult result;
// build the child transactions
if (mStartParent==mEndParent)
@ -145,9 +143,9 @@ NS_IMETHODIMP DeleteRangeTxn::Do(void)
// set the resulting selection
nsCOMPtr<nsIDOMSelection> selection;
result = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_SUCCEEDED(result)) {
result = selection->Collapse(mStartParent, mStartOffset);
}
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
result = selection->Collapse(mStartParent, mStartOffset);
}
return result;
@ -156,19 +154,21 @@ NS_IMETHODIMP DeleteRangeTxn::Do(void)
NS_IMETHODIMP DeleteRangeTxn::Undo(void)
{
if (gNoisy) { printf("Undo Delete Range\n"); }
if (!mStartParent || !mEndParent || !mCommonParent)
return NS_ERROR_NULL_POINTER;
if (!mStartParent || !mEndParent || !mCommonParent || !mEditor)
return NS_ERROR_NOT_INITIALIZED;
nsresult result = EditAggregateTxn::Undo();
if (NS_SUCCEEDED(result)) {
if (NS_SUCCEEDED(result))
{
// set the resulting selection
nsCOMPtr<nsIDOMSelection> selection;
result = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_SUCCEEDED(result)) {
selection->Collapse(mStartParent, mStartOffset);
selection->Extend(mEndParent, mEndOffset);
}
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
result = selection->Collapse(mStartParent, mStartOffset);
if (NS_FAILED(result)) return result;
result = selection->Extend(mEndParent, mEndOffset);
}
return result;
@ -177,8 +177,8 @@ NS_IMETHODIMP DeleteRangeTxn::Undo(void)
NS_IMETHODIMP DeleteRangeTxn::Redo(void)
{
if (gNoisy) { printf("Redo Delete Range\n"); }
if (!mStartParent || !mEndParent || !mCommonParent)
return NS_ERROR_NULL_POINTER;
if (!mStartParent || !mEndParent || !mCommonParent || !mEditor)
return NS_ERROR_NOT_INITIALIZED;
nsresult result = EditAggregateTxn::Redo();
@ -186,9 +186,9 @@ NS_IMETHODIMP DeleteRangeTxn::Redo(void)
// set the resulting selection
nsCOMPtr<nsIDOMSelection> selection;
result = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_SUCCEEDED(result)) {
result = selection->Collapse(mStartParent, mStartOffset);
}
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
result = selection->Collapse(mStartParent, mStartOffset);
}
return result;
@ -224,9 +224,10 @@ NS_IMETHODIMP DeleteRangeTxn::GetRedoString(nsString *aString)
return NS_OK;
}
NS_IMETHODIMP DeleteRangeTxn::CreateTxnsToDeleteBetween(nsIDOMNode *aStartParent,
PRUint32 aStartOffset,
PRUint32 aEndOffset)
NS_IMETHODIMP
DeleteRangeTxn::CreateTxnsToDeleteBetween(nsIDOMNode *aStartParent,
PRUint32 aStartOffset,
PRUint32 aEndOffset)
{
nsresult result;
// see what kind of node we have
@ -236,44 +237,42 @@ NS_IMETHODIMP DeleteRangeTxn::CreateTxnsToDeleteBetween(nsIDOMNode *aStartParent
{ // if the node is a text node, then delete text content
DeleteTextTxn *txn;
result = TransactionFactory::GetNewTransaction(DeleteTextTxn::GetCID(), (EditTxn **)&txn);
if (nsnull!=txn)
{
PRInt32 numToDel;
if (aStartOffset==aEndOffset)
numToDel = 1;
else
numToDel = aEndOffset-aStartOffset;
txn->Init(mEditor, textNode, aStartOffset, numToDel);
AppendChild(txn);
}
if (NS_FAILED(result)) return result;
if (!txn) return NS_ERROR_NULL_POINTER;
PRInt32 numToDel;
if (aStartOffset==aEndOffset)
numToDel = 1;
else
numToDel = aEndOffset-aStartOffset;
txn->Init(mEditor, textNode, aStartOffset, numToDel);
AppendChild(txn);
}
else
{
PRUint32 childCount;
nsCOMPtr<nsIDOMNodeList> children;
result = aStartParent->GetChildNodes(getter_AddRefs(children));
if ((NS_SUCCEEDED(result)) && children)
if (NS_FAILED(result)) return result;
if (!children) return NS_ERROR_NULL_POINTER;
children->GetLength(&childCount);
NS_ASSERTION(aEndOffset<=childCount, "bad aEndOffset");
PRUint32 i;
for (i=aStartOffset; i<aEndOffset; i++)
{
children->GetLength(&childCount);
NS_ASSERTION(aEndOffset<=childCount, "bad aEndOffset");
PRUint32 i;
for (i=aStartOffset; i<aEndOffset; i++)
{
nsCOMPtr<nsIDOMNode> child;
result = children->Item(i, getter_AddRefs(child));
if ((NS_SUCCEEDED(result)) && child)
{
DeleteElementTxn *txn;
result = TransactionFactory::GetNewTransaction(DeleteElementTxn::GetCID(), (EditTxn **)&txn);
if (nsnull!=txn)
{
txn->Init(child);
AppendChild(txn);
}
else
return NS_ERROR_NULL_POINTER;
}
}
nsCOMPtr<nsIDOMNode> child;
result = children->Item(i, getter_AddRefs(child));
if (NS_FAILED(result)) return result;
if (!child) return NS_ERROR_NULL_POINTER;
DeleteElementTxn *txn;
result = TransactionFactory::GetNewTransaction(DeleteElementTxn::GetCID(), (EditTxn **)&txn);
if (NS_FAILED(result)) return result;
if (!txn) return NS_ERROR_NULL_POINTER;
txn->Init(child);
AppendChild(txn);
}
}
return result;
@ -306,13 +305,11 @@ NS_IMETHODIMP DeleteRangeTxn::CreateTxnsToDeleteContent(nsIDOMNode *aParent,
{
DeleteTextTxn *txn;
result = TransactionFactory::GetNewTransaction(DeleteTextTxn::GetCID(), (EditTxn **)&txn);
if (nsnull!=txn)
{
txn->Init(mEditor, textNode, start, numToDelete);
AppendChild(txn);
}
else
return NS_ERROR_NULL_POINTER;
if (NS_FAILED(result)) return result;
if (!txn) return NS_ERROR_NULL_POINTER;
txn->Init(mEditor, textNode, start, numToDelete);
AppendChild(txn);
}
}
@ -332,11 +329,11 @@ NS_IMETHODIMP DeleteRangeTxn::CreateTxnsToDeleteNodesBetween()
nsnull,
nsIContentIterator::GetIID(),
getter_AddRefs(iter));
if (!NS_SUCCEEDED(result))
return result;
if (NS_FAILED(result)) return result;
if (!iter) return NS_ERROR_NULL_POINTER;
result = iter->Init(mRange);
if (!NS_SUCCEEDED(result))
return result;
if (NS_FAILED(result)) return result;
while (NS_COMFALSE == iter->IsDone())
{
@ -344,18 +341,16 @@ NS_IMETHODIMP DeleteRangeTxn::CreateTxnsToDeleteNodesBetween()
nsCOMPtr<nsIContent> content;
result = iter->CurrentNode(getter_AddRefs(content));
node = do_QueryInterface(content);
if ((NS_SUCCEEDED(result)) && node)
{
DeleteElementTxn *txn;
result = TransactionFactory::GetNewTransaction(DeleteElementTxn::GetCID(), (EditTxn **)&txn);
if (nsnull!=txn)
{
txn->Init(node);
AppendChild(txn);
}
else
return NS_ERROR_NULL_POINTER;
}
if (NS_FAILED(result)) return result;
if (!node) return NS_ERROR_NULL_POINTER;
DeleteElementTxn *txn;
result = TransactionFactory::GetNewTransaction(DeleteElementTxn::GetCID(), (EditTxn **)&txn);
if (NS_FAILED(result)) return result;
if (!txn) return NS_ERROR_NULL_POINTER;
txn->Init(node);
AppendChild(txn);
iter->Next();
}
return result;

Просмотреть файл

@ -41,6 +41,8 @@ NS_IMETHODIMP DeleteTextTxn::Init(nsIEditor *aEditor,
PRUint32 aNumCharsToDelete)
{
NS_ASSERTION(aEditor&&aElement, "bad arg");
if (!aEditor || !aElement) { return NS_ERROR_NULL_POINTER; }
mEditor = aEditor;
mElement = do_QueryInterface(aElement);
mOffset = aOffset;
@ -57,22 +59,20 @@ NS_IMETHODIMP DeleteTextTxn::Init(nsIEditor *aEditor,
NS_IMETHODIMP DeleteTextTxn::Do(void)
{
if (gNoisy) { printf("Do Delete Text\n"); }
nsresult result = NS_ERROR_NULL_POINTER;
if (mEditor && mElement)
NS_ASSERTION(mEditor && mElement, "bad state");
if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
// get the text that we're about to delete
nsresult result = mElement->SubstringData(mOffset, mNumCharsToDelete, mDeletedText);
NS_ASSERTION(NS_SUCCEEDED(result), "could not get text to delete.");
result = mElement->DeleteData(mOffset, mNumCharsToDelete);
if (NS_SUCCEEDED(result))
{
// get the text that we're about to delete
result = mElement->SubstringData(mOffset, mNumCharsToDelete, mDeletedText);
NS_ASSERTION(NS_SUCCEEDED(result), "could not get text to delete.");
result = mElement->DeleteData(mOffset, mNumCharsToDelete);
if (NS_SUCCEEDED(result))
{
nsCOMPtr<nsIDOMSelection> selection;
nsresult selectionResult = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_SUCCEEDED(selectionResult) && selection) {
selectionResult = selection->Collapse(mElement, mOffset);
NS_ASSERTION((NS_SUCCEEDED(selectionResult)), "selection could not be collapsed after undo of insert.");
}
}
nsCOMPtr<nsIDOMSelection> selection;
result = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
result = selection->Collapse(mElement, mOffset);
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of insert.");
}
return result;
}
@ -82,19 +82,19 @@ NS_IMETHODIMP DeleteTextTxn::Do(void)
NS_IMETHODIMP DeleteTextTxn::Undo(void)
{
if (gNoisy) { printf("Undo Delete Text\n"); }
nsresult result = NS_ERROR_NULL_POINTER;
if (mEditor && mElement)
NS_ASSERTION(mEditor && mElement, "bad state");
if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
nsresult result;
result = mElement->InsertData(mOffset, mDeletedText);
if (NS_SUCCEEDED(result))
{
result = mElement->InsertData(mOffset, mDeletedText);
if (NS_SUCCEEDED(result))
{
nsCOMPtr<nsIDOMSelection> selection;
nsresult selectionResult = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_SUCCEEDED(selectionResult) && selection) {
selectionResult = selection->Collapse(mElement, mOffset);
NS_ASSERTION((NS_SUCCEEDED(selectionResult)), "selection could not be collapsed after undo of insert.");
}
}
nsCOMPtr<nsIDOMSelection> selection;
nsresult selectionResult = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
result = selection->Collapse(mElement, mOffset);
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of insert.");
}
return result;
}

Просмотреть файл

@ -64,8 +64,7 @@ NS_IMETHODIMP InsertElementTxn::Do(void)
printf("%p Do Insert Element of %p into parent %p at offset %d\n",
this, nodeAsContent.get(), parentAsContent.get(), mOffset);
}
if (!mNode || !mParent)
return NS_ERROR_NULL_POINTER;
if (!mNode || !mParent) return NS_ERROR_NOT_INITIALIZED;
nsresult result;
nsCOMPtr<nsIDOMNode>refNode;
@ -92,16 +91,16 @@ NS_IMETHODIMP InsertElementTxn::Do(void)
nsCOMPtr<nsIDOMNode> resultNode;
result = mParent->InsertBefore(mNode, refNode, getter_AddRefs(resultNode));
if (NS_SUCCEEDED(result) && resultNode)
{
nsCOMPtr<nsIDOMSelection> selection;
result = mEditor->GetSelection(getter_AddRefs(selection));
if ((NS_SUCCEEDED(result)) && selection)
{ // place the selection just after the inserted element
selection->Collapse(mParent, mOffset+1);
//selection->Extend(mParent, mOffset+1);
}
}
if (NS_FAILED(result)) return result;
if (!resultNode) return NS_ERROR_NULL_POINTER;
nsCOMPtr<nsIDOMSelection> selection;
result = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
// place the selection just after the inserted element
selection->Collapse(mParent, mOffset+1);
//selection->Extend(mParent, mOffset+1);
return result;
}
@ -109,8 +108,7 @@ NS_IMETHODIMP InsertElementTxn::Undo(void)
{
if (gNoisy) { printf("%p Undo Insert Element of %p into parent %p at offset %d\n",
this, mNode.get(), mParent.get(), mOffset); }
if (!mNode || !mParent)
return NS_ERROR_NULL_POINTER;
if (!mNode || !mParent) return NS_ERROR_NOT_INITIALIZED;
nsCOMPtr<nsIDOMNode> resultNode;
nsresult result = mParent->RemoveChild(mNode, getter_AddRefs(resultNode));

Просмотреть файл

@ -62,6 +62,9 @@ NS_IMETHODIMP InsertTextTxn::Init(nsIDOMCharacterData *aElement,
printf("\n");
#endif
NS_ASSERTION(aElement&&aPresShell, "bad args");
if (!aElement || !aPresShell) return NS_ERROR_NULL_POINTER;
mElement = do_QueryInterface(aElement);
mOffset = aOffset;
mStringToInsert = aStringToInsert;
@ -72,16 +75,19 @@ NS_IMETHODIMP InsertTextTxn::Init(nsIDOMCharacterData *aElement,
NS_IMETHODIMP InsertTextTxn::Do(void)
{
if (gNoisy) { printf("Do Insert Text element = %p\n", mElement.get()); }
NS_ASSERTION(mElement && mPresShell, "bad state");
if (!mElement || !mPresShell) { return NS_ERROR_NOT_INITIALIZED; }
// advance caret: This requires the presentation shell to get the selection.
nsCOMPtr<nsIDOMSelection> selection;
nsresult result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection));
NS_ASSERTION(selection,"Could not get selection in InsertTextTxn::Do\n");
if (NS_SUCCEEDED(result) && selection) {
result = mElement->InsertData(mOffset, mStringToInsert);
if (NS_SUCCEEDED(result)) {
result = selection->Collapse(mElement, mOffset+mStringToInsert.Length());
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after insert.");
}
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
result = mElement->InsertData(mOffset, mStringToInsert);
if (NS_SUCCEEDED(result))
{
result = selection->Collapse(mElement, mOffset+mStringToInsert.Length());
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after insert.");
}
return result;
}
@ -89,6 +95,9 @@ NS_IMETHODIMP InsertTextTxn::Do(void)
NS_IMETHODIMP InsertTextTxn::Undo(void)
{
if (gNoisy) { printf("Undo Insert Text element = %p\n", mElement.get()); }
NS_ASSERTION(mElement && mPresShell, "bad state");
if (!mElement || !mPresShell) { return NS_ERROR_NOT_INITIALIZED; }
nsresult result;
PRUint32 length = mStringToInsert.Length();
result = mElement->DeleteData(mOffset, length);
@ -96,10 +105,10 @@ NS_IMETHODIMP InsertTextTxn::Undo(void)
{ // set the selection to the insertion point where the string was removed
nsCOMPtr<nsIDOMSelection> selection;
result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection));
if (NS_SUCCEEDED(result) && selection) {
result = selection->Collapse(mElement, mOffset);
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of insert.");
}
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
result = selection->Collapse(mElement, mOffset);
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of insert.");
}
return result;
}

Просмотреть файл

@ -37,6 +37,8 @@ NS_IMETHODIMP JoinElementTxn::Init(nsIEditor *aEditor,
nsIDOMNode *aLeftNode,
nsIDOMNode *aRightNode)
{
NS_PRECONDITION((aEditor && aLeftNode && aRightNode), "null arg");
if (!aEditor || !aLeftNode || !aRightNode) { return NS_ERROR_NULL_POINTER; }
mEditor = aEditor;
mLeftNode = do_QueryInterface(aLeftNode);
mRightNode = do_QueryInterface(aRightNode);
@ -52,54 +54,54 @@ JoinElementTxn::~JoinElementTxn()
NS_IMETHODIMP JoinElementTxn::Do(void)
{
if (gNoisy) { printf("%p Do Join of %p and %p\n", this, mLeftNode.get(), mRightNode.get()); }
nsresult result;
NS_PRECONDITION((mEditor && mLeftNode && mRightNode), "null arg");
if (!mEditor || !mLeftNode || !mRightNode) { return NS_ERROR_NOT_INITIALIZED; }
if ((mLeftNode) && (mRightNode))
{ // get the parent node
nsCOMPtr<nsIDOMNode>leftParent;
result = mLeftNode->GetParentNode(getter_AddRefs(leftParent));
if ((NS_SUCCEEDED(result)) && (leftParent))
{ // verify that mLeftNode and mRightNode have the same parent
nsCOMPtr<nsIDOMNode>rightParent;
result = mRightNode->GetParentNode(getter_AddRefs(rightParent));
if ((NS_SUCCEEDED(result)) && (rightParent))
{
if (leftParent==rightParent)
{
mParent= do_QueryInterface(leftParent); // set this instance mParent.
// Other methods will see a non-null mParent and know all is well
nsCOMPtr<nsIDOMNodeList> childNodes;
result = mLeftNode->GetChildNodes(getter_AddRefs(childNodes));
if ((NS_SUCCEEDED(result)) && (childNodes)) {
childNodes->GetLength(&mOffset);
}
else
{
nsCOMPtr<nsIDOMCharacterData> leftNodeAsText;
leftNodeAsText = do_QueryInterface(mLeftNode);
if (leftNodeAsText) {
leftNodeAsText->GetLength(&mOffset);
}
}
result = nsEditor::JoinNodesImpl(mRightNode, mLeftNode, mParent, PR_FALSE);
if (NS_SUCCEEDED(result))
{
if (gNoisy) { printf(" left node = %p removed\n", mLeftNode.get()); }
nsCOMPtr<nsIDOMSelection>selection;
mEditor->GetSelection(getter_AddRefs(selection));
if (selection)
{
selection->Collapse(mRightNode, mOffset);
}
}
}
else
{
NS_ASSERTION(PR_FALSE, "2 nodes do not have same parent");
return NS_ERROR_INVALID_ARG;
}
// get the parent node
nsCOMPtr<nsIDOMNode>leftParent;
nsresult result = mLeftNode->GetParentNode(getter_AddRefs(leftParent));
if (NS_FAILED(result)) return result;
if (!leftParent) return NS_ERROR_NULL_POINTER;
// verify that mLeftNode and mRightNode have the same parent
nsCOMPtr<nsIDOMNode>rightParent;
result = mRightNode->GetParentNode(getter_AddRefs(rightParent));
if (NS_FAILED(result)) return result;
if (!rightParent) return NS_ERROR_NULL_POINTER;
if (leftParent==rightParent)
{
mParent= do_QueryInterface(leftParent); // set this instance mParent.
// Other methods will see a non-null mParent and know all is well
nsCOMPtr<nsIDOMNodeList> childNodes;
result = mLeftNode->GetChildNodes(getter_AddRefs(childNodes));
if (NS_FAILED(result)) return result;
if (childNodes) {
childNodes->GetLength(&mOffset);
}
else
{
nsCOMPtr<nsIDOMCharacterData> leftNodeAsText;
leftNodeAsText = do_QueryInterface(mLeftNode);
if (leftNodeAsText) {
leftNodeAsText->GetLength(&mOffset);
}
}
result = nsEditor::JoinNodesImpl(mRightNode, mLeftNode, mParent, PR_FALSE);
if (NS_SUCCEEDED(result))
{
if (gNoisy) { printf(" left node = %p removed\n", mLeftNode.get()); }
nsCOMPtr<nsIDOMSelection>selection;
result = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
selection->Collapse(mRightNode, mOffset);
}
}
else
{
NS_ASSERTION(PR_FALSE, "2 nodes do not have same parent");
return NS_ERROR_INVALID_ARG;
}
return result;
}
@ -110,9 +112,7 @@ NS_IMETHODIMP JoinElementTxn::Undo(void)
{
if (gNoisy) { printf("%p Undo Join, right node = %p\n", this, mRightNode.get()); }
NS_ASSERTION(mRightNode && mLeftNode && mParent, "bad state");
if (!mRightNode || !mLeftNode || !mParent) {
return NS_ERROR_NOT_INITIALIZED;
}
if (!mRightNode || !mLeftNode || !mParent) { return NS_ERROR_NOT_INITIALIZED; }
nsresult result;
nsCOMPtr<nsIDOMNode>resultNode;
// first, massage the existing node so it is in its post-split state

Просмотреть файл

@ -39,6 +39,9 @@ NS_IMETHODIMP SplitElementTxn::Init(nsIEditor *aEditor,
nsIDOMNode *aNode,
PRInt32 aOffset)
{
NS_ASSERTION(aEditor && aNode, "bad args");
if (!aEditor || !aNode) { return NS_ERROR_NOT_INITIALIZED; }
mEditor = aEditor;
mExistingRightNode = do_QueryInterface(aNode);
mOffset = aOffset;
@ -52,36 +55,33 @@ SplitElementTxn::~SplitElementTxn()
NS_IMETHODIMP SplitElementTxn::Do(void)
{
if (gNoisy) { printf("%p Do Split of node %p offset %d\n", this, mExistingRightNode.get(), mOffset); }
NS_ASSERTION(mExistingRightNode, "bad state");
if (!mExistingRightNode) {
return NS_ERROR_NOT_INITIALIZED;
}
NS_ASSERTION(mExistingRightNode && mEditor, "bad state");
if (!mExistingRightNode || !mEditor) { return NS_ERROR_NOT_INITIALIZED; }
// create a new node
nsresult result = mExistingRightNode->CloneNode(PR_FALSE, getter_AddRefs(mNewLeftNode));
NS_ASSERTION(((NS_SUCCEEDED(result)) && (mNewLeftNode)), "could not create element.");
if (NS_FAILED(result)) return result;
if (!mNewLeftNode) return NS_ERROR_NULL_POINTER;
if ((NS_SUCCEEDED(result)) && (mNewLeftNode))
if (gNoisy) { printf(" created left node = %p\n", mNewLeftNode.get()); }
// get the parent node
result = mExistingRightNode->GetParentNode(getter_AddRefs(mParent));
if (NS_FAILED(result)) return result;
if (!mParent) return NS_ERROR_NULL_POINTER;
// insert the new node
result = nsEditor::SplitNodeImpl(mExistingRightNode, mOffset, mNewLeftNode, mParent);
if (NS_SUCCEEDED(result) && mNewLeftNode)
{
if (gNoisy) { printf(" created left node = %p\n", mNewLeftNode.get()); }
// get the parent node
result = mExistingRightNode->GetParentNode(getter_AddRefs(mParent));
// insert the new node
if ((NS_SUCCEEDED(result)) && (mParent))
{
result = nsEditor::SplitNodeImpl(mExistingRightNode, mOffset, mNewLeftNode, mParent);
if (NS_SUCCEEDED(result) && mNewLeftNode)
{
nsCOMPtr<nsIDOMSelection>selection;
mEditor->GetSelection(getter_AddRefs(selection));
if (selection)
{
selection->Collapse(mNewLeftNode, mOffset);
}
}
else {
result = NS_ERROR_NOT_IMPLEMENTED;
}
}
nsCOMPtr<nsIDOMSelection>selection;
mEditor->GetSelection(getter_AddRefs(selection));
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
result = selection->Collapse(mNewLeftNode, mOffset);
}
else {
result = NS_ERROR_NOT_IMPLEMENTED;
}
return result;
}
@ -92,8 +92,8 @@ NS_IMETHODIMP SplitElementTxn::Undo(void)
printf("%p Undo Split of existing node %p and new node %p offset %d\n",
this, mExistingRightNode.get(), mNewLeftNode.get(), mOffset);
}
NS_ASSERTION(mExistingRightNode && mNewLeftNode && mParent, "bad state");
if (!mExistingRightNode || !mNewLeftNode || !mParent) {
NS_ASSERTION(mEditor && mExistingRightNode && mNewLeftNode && mParent, "bad state");
if (!mEditor || !mExistingRightNode || !mNewLeftNode || !mParent) {
return NS_ERROR_NOT_INITIALIZED;
}
@ -110,10 +110,9 @@ NS_IMETHODIMP SplitElementTxn::Undo(void)
if (gNoisy) { printf(" left node = %p removed\n", mNewLeftNode.get()); }
nsCOMPtr<nsIDOMSelection>selection;
mEditor->GetSelection(getter_AddRefs(selection));
if (selection)
{
selection->Collapse(mExistingRightNode, mOffset);
}
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
result = selection->Collapse(mExistingRightNode, mOffset);
}
else {
result = NS_ERROR_NOT_IMPLEMENTED;
@ -126,8 +125,8 @@ NS_IMETHODIMP SplitElementTxn::Undo(void)
*/
NS_IMETHODIMP SplitElementTxn::Redo(void)
{
NS_ASSERTION(mExistingRightNode && mNewLeftNode && mParent, "bad state");
if (!mExistingRightNode || !mNewLeftNode || !mParent) {
NS_ASSERTION(mEditor && mExistingRightNode && mNewLeftNode && mParent, "bad state");
if (!mEditor || !mExistingRightNode || !mNewLeftNode || !mParent) {
return NS_ERROR_NOT_INITIALIZED;
}
if (gNoisy) {
@ -146,7 +145,7 @@ NS_IMETHODIMP SplitElementTxn::Redo(void)
if (gNoisy)
{
printf("** after delete of text in right text node %p offset %d\n", rightNodeAsText.get(), mOffset);
if (gNoisy) {mEditor->DebugDumpContent(); } // DEBUG
mEditor->DebugDumpContent(); // DEBUG
}
}
else

Просмотреть файл

@ -239,6 +239,7 @@ nsEditor::Init(nsIDOMDocument *aDoc, nsIPresShell* aPresShell, PRUint32 aFlags)
// disable links
nsCOMPtr<nsIPresContext> context;
mPresShell->GetPresContext(getter_AddRefs(context));
if (!context) return NS_ERROR_NULL_POINTER;
context->SetLinkHandler(0);
// Set up the DTD
@ -252,9 +253,9 @@ nsEditor::Init(nsIDOMDocument *aDoc, nsIPresShell* aPresShell, PRUint32 aFlags)
if (!mEditProperty) {return NS_ERROR_NULL_POINTER;}
mPresShell->GetViewManager(&mViewManager);
if (mViewManager){
mViewManager->Release(); //we want a weak link
}
if (!mViewManager) {return NS_ERROR_NULL_POINTER;}
mViewManager->Release(); //we want a weak link
mPresShell->SetDisplayNonTextSelection(PR_TRUE);//we want to see all the selection reflected to user
mUpdateCount=0;
InsertTextTxn::ClassInit();
@ -267,22 +268,25 @@ nsEditor::Init(nsIDOMDocument *aDoc, nsIPresShell* aPresShell, PRUint32 aFlags)
mIMEBufferLength = 0;
/* Show the caret */
// XXX: I suppose it's legal to fail to get a caret, so don't propogate the
// result of GetCaret
nsCOMPtr<nsICaret> caret;
if (NS_SUCCEEDED(mPresShell->GetCaret(getter_AddRefs(caret))) && caret)
{
caret->SetCaretVisible(PR_TRUE);
caret->SetCaretReadOnly(PR_FALSE);
}
// NOTE: We don't fail if we can't get prefs or string bundles
// since we could still be used as the text edit widget without prefs
// Get the prefs service (Note: can't use nsCOMPtr for service pointers)
result = nsServiceManager::GetService(kPrefCID,
nsIPref::GetIID(),
(nsISupports**)&mPrefs);
if (NS_FAILED(result) || !mPrefs)
nsresult ignoredResult = nsServiceManager::GetService(kPrefCID,
nsIPref::GetIID(),
(nsISupports**)&mPrefs);
if (NS_FAILED(ignoredResult) || !mPrefs)
{
printf("ERROR: Failed to get Prefs Service instance.\n");
if (gNoisy) { printf("ERROR: Failed to get Prefs Service instance.\n");}
}
// TODO: Cache basic preferences?
@ -290,20 +294,21 @@ nsEditor::Init(nsIDOMDocument *aDoc, nsIPresShell* aPresShell, PRUint32 aFlags)
// respond to while running
nsIStringBundleService* service;
result = nsServiceManager::GetService(kStringBundleServiceCID,
nsIStringBundleService::GetIID(),
(nsISupports**)&service);
ignoredResult = nsServiceManager::GetService(kStringBundleServiceCID,
nsIStringBundleService::GetIID(),
(nsISupports**)&service);
if (NS_SUCCEEDED(result) && service)
if (NS_SUCCEEDED(ignoredResult) && service)
{
nsILocale* locale = nsnull;
result = service->CreateBundle(EDITOR_BUNDLE_URL, locale,
getter_AddRefs(mStringBundle));
ignoredResult = service->CreateBundle(EDITOR_BUNDLE_URL, locale,
getter_AddRefs(mStringBundle));
// We don't need to keep service around once we created the bundle
nsServiceManager::ReleaseService(kStringBundleServiceCID, service);
} else {
printf("ERROR: Failed to get StringBundle Service instance.\n");
if (gNoisy) printf("ERROR: Failed to get StringBundle Service instance.\n");
}
/*
Example of getting a string:
nsString value;
@ -374,8 +379,8 @@ NS_IMETHODIMP nsEditor::SaveDocument(PRBool saveAs, PRBool saveCopy)
// get the document
nsCOMPtr<nsIDOMDocument> doc;
rv = GetDocument(getter_AddRefs(doc));
if (NS_FAILED(rv) || !doc)
return rv;
if (NS_FAILED(rv)) return rv;
if (!doc) return NS_ERROR_NULL_POINTER;
nsCOMPtr<nsIDiskDocument> diskDoc = do_QueryInterface(doc);
if (!diskDoc)
@ -1238,7 +1243,6 @@ nsEditor::SetCompositionString(const nsString& aCompositionString, nsIPrivateTex
return result;
}
#ifdef XP_MAC
#pragma mark -
#pragma mark --- public nsEditor methods ---

Просмотреть файл

@ -387,7 +387,7 @@ public:
* @param aNodeToKeep The node that will remain after the join.
* @param aNodeToJoin The node that will be joined with aNodeToKeep.
* There is no requirement that the two nodes be of the same type.
* @param aParent The parent of aExistingRightNode
* @param aParent The parent of aNodeToKeep
* @param aNodeToKeepIsFirst if PR_TRUE, the contents|children of aNodeToKeep come before the
* contents|children of aNodeToJoin, otherwise their positions are switched.
*/

Просмотреть файл

@ -47,12 +47,18 @@
#include "nsIDragSession.h"
#include "nsITransferable.h"
#include "nsIFormatConverter.h"
#include "nsIContentIterator.h"
#include "nsIContent.h"
#include "nsLayoutCID.h"
// Drag & Drop, Clipboard Support
static NS_DEFINE_CID(kCDragServiceCID, NS_DRAGSERVICE_CID);
static NS_DEFINE_CID(kCTransferableCID, NS_TRANSFERABLE_CID);
static NS_DEFINE_IID(kCDataFlavorCID, NS_DATAFLAVOR_CID);
static NS_DEFINE_IID(kCXIFFormatConverterCID, NS_XIFFORMATCONVERTER_CID);
static NS_DEFINE_IID(kContentIteratorCID, NS_CONTENTITERATOR_CID);
static NS_DEFINE_CID(kCClipboardCID, NS_CLIPBOARD_CID);
static NS_DEFINE_IID(kCXIFConverterCID, NS_XIFFORMATCONVERTER_CID);
/*
@ -116,8 +122,7 @@ nsresult
nsTextEditorKeyListener::KeyDown(nsIDOMEvent* aKeyEvent)
{
PRUint32 keyCode;
PRBool isShift;
PRBool ctrlKey;
PRBool isShift, ctrlKey, altKey, metaKey;
nsCOMPtr<nsIDOMUIEvent>uiEvent;
uiEvent = do_QueryInterface(aKeyEvent);
@ -126,87 +131,87 @@ nsTextEditorKeyListener::KeyDown(nsIDOMEvent* aKeyEvent)
return NS_OK;
}
if (NS_SUCCEEDED(uiEvent->GetKeyCode(&keyCode)) &&
NS_SUCCEEDED(uiEvent->GetShiftKey(&isShift)) &&
NS_SUCCEEDED(uiEvent->GetCtrlKey(&ctrlKey))
) {
PRBool keyProcessed;
ProcessShortCutKeys(aKeyEvent, keyProcessed);
if (PR_FALSE==keyProcessed)
{
switch(keyCode) {
// case nsIDOMUIEvent::VK_BACK:
// mEditor->DeleteSelection(nsIEditor::eDeleteLeft);
// break;
PRBool keyProcessed;
ProcessShortCutKeys(aKeyEvent, keyProcessed);
if (PR_FALSE==keyProcessed)
{
if (NS_SUCCEEDED(uiEvent->GetKeyCode(&keyCode)) &&
NS_SUCCEEDED(uiEvent->GetShiftKey(&isShift)) &&
NS_SUCCEEDED(uiEvent->GetCtrlKey(&ctrlKey)) &&
NS_SUCCEEDED(uiEvent->GetAltKey(&altKey)) &&
NS_SUCCEEDED(uiEvent->GetMetaKey(&metaKey)))
{
{
switch(keyCode) {
// case nsIDOMUIEvent::VK_BACK:
// mEditor->DeleteSelection(nsIEditor::eDeleteLeft);
// break;
case nsIDOMUIEvent::VK_DELETE:
mEditor->DeleteSelection(nsIEditor::eDeleteNext);
break;
case nsIDOMUIEvent::VK_DELETE:
mEditor->DeleteSelection(nsIEditor::eDeleteNext);
break;
// case nsIDOMUIEvent::VK_RETURN:
//case nsIDOMUIEvent::VK_ENTER: // why does this not exist?
// Need to implement creation of either <P> or <BR> nodes.
// mEditor->InsertBreak();
// break;
// case nsIDOMUIEvent::VK_RETURN:
//case nsIDOMUIEvent::VK_ENTER: // why does this not exist?
// Need to implement creation of either <P> or <BR> nodes.
// mEditor->InsertBreak();
// break;
case nsIDOMUIEvent::VK_LEFT:
case nsIDOMUIEvent::VK_RIGHT:
case nsIDOMUIEvent::VK_UP:
case nsIDOMUIEvent::VK_DOWN:
// these have already been handled in nsRangeList. Why are we getting them
// again here (Mac)? In switch to avoid putting in bogus chars.
case nsIDOMUIEvent::VK_LEFT:
case nsIDOMUIEvent::VK_RIGHT:
case nsIDOMUIEvent::VK_UP:
case nsIDOMUIEvent::VK_DOWN:
// these have already been handled in nsRangeList. Why are we getting them
// again here (Mac)? In switch to avoid putting in bogus chars.
//return NS_OK to allow page scrolling.
return NS_OK;
break;
//return NS_OK to allow page scrolling.
return NS_OK;
break;
case nsIDOMUIEvent::VK_HOME:
case nsIDOMUIEvent::VK_END:
// who handles these?
#if DEBUG
printf("Key not handled\n");
#endif
return NS_OK;
break;
case nsIDOMUIEvent::VK_HOME:
case nsIDOMUIEvent::VK_END:
// who handles these?
#if DEBUG
printf("Key not handled\n");
#endif
return NS_OK;
break;
case nsIDOMUIEvent::VK_PAGE_UP:
case nsIDOMUIEvent::VK_PAGE_DOWN:
//return NS_OK to allow page scrolling.
return NS_OK;
break;
case nsIDOMUIEvent::VK_PAGE_UP:
case nsIDOMUIEvent::VK_PAGE_DOWN:
//return NS_OK to allow page scrolling.
return NS_OK;
break;
case nsIDOMUIEvent::VK_TAB:
{
PRUint32 flags=0;
mEditor->GetFlags(&flags);
if (! (flags & nsIHTMLEditor::eEditorSingleLineMask))
{
PRBool ctrlKey, altKey, metaKey;
uiEvent->GetCtrlKey(&ctrlKey);
uiEvent->GetAltKey(&altKey);
uiEvent->GetMetaKey(&metaKey);
if (metaKey || altKey)
return NS_OK; // don't consume
// else we insert the tab straight through
nsAutoString key;
key += keyCode;
case nsIDOMUIEvent::VK_TAB:
{
PRUint32 flags=0;
mEditor->GetFlags(&flags);
if (! (flags & nsIHTMLEditor::eEditorSingleLineMask))
{
if (metaKey || altKey)
return NS_OK; // don't consume
// else we insert the tab straight through
nsAutoString key;
key += keyCode;
nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(mEditor);
if (htmlEditor)
htmlEditor->InsertText(key);
return NS_ERROR_BASE; // this means "I handled the event, don't do default processing"
}
else {
return NS_OK;
}
break;
}
nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(mEditor);
if (htmlEditor)
htmlEditor->InsertText(key);
return NS_ERROR_BASE; // this means "I handled the event, don't do default processing"
}
else {
return NS_OK;
}
break;
}
default:
return NS_OK; // this indicates that we have not handled the keyDown event in any way.
}
}
}
default:
return NS_OK; // this indicates that we have not handled the keyDown event in any way.
}
}
}
}
return NS_ERROR_BASE;
}
@ -238,19 +243,20 @@ nsTextEditorKeyListener::KeyPress(nsIDOMEvent* aKeyEvent)
// eaiser
//
PRBool ctrlKey, altKey, metaKey;
uiEvent->GetCtrlKey(&ctrlKey);
uiEvent->GetAltKey(&altKey);
uiEvent->GetMetaKey(&metaKey);
if (metaKey)
return NS_OK; // don't consume
nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(mEditor);
if (!htmlEditor) return NS_ERROR_NO_INTERFACE;
PRBool ctrlKey, altKey, metaKey;
uiEvent->GetCtrlKey(&ctrlKey);
uiEvent->GetAltKey(&altKey);
uiEvent->GetMetaKey(&metaKey);
if (metaKey)
return NS_OK; // don't consume
nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(mEditor);
if (!htmlEditor) return NS_ERROR_NO_INTERFACE;
if (NS_SUCCEEDED(uiEvent->GetKeyCode(&keyCode)))
{
if (nsIDOMUIEvent::VK_BACK==keyCode) {
if (nsIDOMUIEvent::VK_BACK==keyCode) {
mEditor->DeleteSelection(nsIEditor::eDeletePrevious);
return NS_ERROR_BASE; // consumed
}
@ -261,14 +267,14 @@ nsTextEditorKeyListener::KeyPress(nsIDOMEvent* aKeyEvent)
}
if ((PR_FALSE==altKey) && (PR_FALSE==ctrlKey) &&
(NS_SUCCEEDED(uiEvent->GetCharCode(&character))))
(NS_SUCCEEDED(uiEvent->GetCharCode(&character))))
{
if (nsIDOMUIEvent::VK_TAB==character) {
return NS_OK; // ignore tabs here, they're handled in keyDown if at all
}
if (nsIDOMUIEvent::VK_TAB==character) {
return NS_OK; // ignore tabs here, they're handled in keyDown if at all
}
key += character;
htmlEditor->InsertText(key);
}
}
return NS_ERROR_BASE; // consumed
@ -843,6 +849,62 @@ nsTextEditorMouseListener::HandleEvent(nsIDOMEvent* aEvent)
// XXX: evil global functions, move them to proper support module
nsresult
IsNodeInSelection(nsIDOMNode *aInNode, nsIDOMSelection *aInSelection, PRBool &aOutIsInSel)
{
aOutIsInSel = PR_FALSE; // init out-param
if (!aInNode || !aInSelection) { return NS_ERROR_NULL_POINTER; }
nsCOMPtr<nsIContentIterator>iter;
nsresult result = nsComponentManager::CreateInstance(kContentIteratorCID, nsnull,
nsIContentIterator::GetIID(),
getter_AddRefs(iter));
if (NS_FAILED(result)) { return result; }
if (!iter) { return NS_ERROR_OUT_OF_MEMORY; }
nsCOMPtr<nsIEnumerator> enumerator;
result = aInSelection->GetEnumerator(getter_AddRefs(enumerator));
if (NS_FAILED(result)) { return result; }
if (!enumerator) { return NS_ERROR_OUT_OF_MEMORY; }
for (enumerator->First(); NS_OK!=enumerator->IsDone(); enumerator->Next())
{
nsCOMPtr<nsISupports> currentItem;
result = enumerator->CurrentItem(getter_AddRefs(currentItem));
if ((NS_SUCCEEDED(result)) && (currentItem))
{
nsCOMPtr<nsIDOMRange> range( do_QueryInterface(currentItem) );
iter->Init(range);
nsCOMPtr<nsIContent> currentContent;
iter->CurrentNode(getter_AddRefs(currentContent));
while (NS_COMFALSE == iter->IsDone())
{
nsCOMPtr<nsIDOMNode>currentNode = do_QueryInterface(currentContent);
if (currentNode.get()==aInNode)
{
// if it's a start or end node, need to test whether the (x,y)
// of the event falls within the selection
// talk to Mike
aOutIsInSel = PR_TRUE;
return NS_OK;
}
/* do not check result here, and especially do not return the result code.
* we rely on iter->IsDone to tell us when the iteration is complete
*/
iter->Next();
iter->CurrentNode(getter_AddRefs(currentContent));
}
}
}
return NS_OK;
}
nsresult
nsTextEditorMouseListener::MouseDown(nsIDOMEvent* aMouseEvent)
{
@ -856,49 +918,141 @@ nsTextEditorMouseListener::MouseDown(nsIDOMEvent* aMouseEvent)
return NS_OK;
}
// We only do anything special for middle-mouse click (paste);
// ignore all other events.
nsCOMPtr<nsIEditor> editor (do_QueryInterface(mEditor));
if (!editor) { return NS_OK; }
// get the document
nsCOMPtr<nsIDOMDocument>domDoc;
editor->GetDocument(getter_AddRefs(domDoc));
if (!domDoc) { return NS_OK; }
nsCOMPtr<nsIDocument>doc = do_QueryInterface(domDoc);
if (!doc) { return NS_OK; }
PRUint16 button = 0;
uiEvent->GetButton(&button);
if (button != 2)
return NS_OK;
// left button click might be a drag-start
if (1==button)
{
#ifndef EXPERIMENTAL_DRAG_CODE
return NS_OK;
#else
nsString XIFBuffer;
// get the DOM select here
nsCOMPtr<nsIDOMSelection> sel;
editor->GetSelection(getter_AddRefs(sel));
// convert the DOMselection to XIF
if (sel)
{
// if we are within the selection, start the drag
nsCOMPtr<nsIEditor> editor (do_QueryInterface(mEditor));
if (!editor)
return NS_ERROR_FAILURE;
nsCOMPtr<nsIDOMNode> target;
nsresult rv = aMouseEvent->GetTarget(getter_AddRefs(target));
if (NS_FAILED(rv) || !target) { return NS_OK; }
PRBool isInSel;
rv = IsNodeInSelection(target, sel, isInSel);
if (NS_FAILED(rv) || PR_FALSE==isInSel) { return NS_OK; }
doc->CreateXIF(XIFBuffer, sel);
// Set the selection to the point under the mouse cursor:
nsCOMPtr<nsIDOMNSUIEvent> mouseEvent (do_QueryInterface(aMouseEvent));
// Get the Clipboard
nsIClipboard* clipboard;
rv = nsServiceManager::GetService(kCClipboardCID,
nsIClipboard::GetIID(),
(nsISupports **)&clipboard);
if (NS_OK == rv)
{
// Create a data flavor to tell the transferable
// that it is about to receive XIF
nsAutoString flavor(kXIFMime);
if (!mouseEvent)
return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event".
nsCOMPtr<nsIDOMNode> parent;
if (!NS_SUCCEEDED(mouseEvent->GetRangeParent(getter_AddRefs(parent))))
return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event".
PRInt32 offset = 0;
if (!NS_SUCCEEDED(mouseEvent->GetRangeOffset(&offset)))
return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event".
// Create a transferable for putting data on the Clipboard
nsCOMPtr<nsITransferable> trans;
rv = nsComponentManager::CreateInstance(kCTransferableCID, nsnull,
nsITransferable::GetIID(),
(void**) getter_AddRefs(trans));
if (NS_OK == rv) {
// The data on the clipboard will be in "XIF" format
// so give the clipboard transferable a "XIFConverter" for
// converting from XIF to other formats
nsCOMPtr<nsIFormatConverter> xifConverter;
rv = nsComponentManager::CreateInstance(kCXIFConverterCID, nsnull,
nsIFormatConverter::GetIID(), (void**) getter_AddRefs(xifConverter));
if (NS_OK == rv) {
// Add the XIF DataFlavor to the transferable
// this tells the transferable that it can handle receiving the XIF format
trans->AddDataFlavor(&flavor);
nsCOMPtr<nsIDOMSelection> selection;
if (NS_SUCCEEDED(editor->GetSelection(getter_AddRefs(selection))))
(void)selection->Collapse(parent, offset);
// Add the converter for going from XIF to other formats
trans->SetConverter(xifConverter);
// If the ctrl key is pressed, we'll do paste as quotation.
// Would've used the alt key, but the kde wmgr treats alt-middle specially.
PRBool ctrlKey = PR_FALSE;
uiEvent->GetCtrlKey(&ctrlKey);
// Now add the XIF data to the transferable
// the transferable wants the number bytes for the data and since it is double byte
// we multiply by 2
trans->SetTransferData(&flavor, XIFBuffer.ToNewUnicode(), XIFBuffer.Length()*2);
if (ctrlKey)
{
nsCOMPtr<nsIEditorMailSupport> mailEditor = do_QueryInterface(mEditor);
if (mailEditor)
mailEditor->PasteAsQuotation();
// Now invoke the drag session
nsIDragService* dragService;
nsresult rv = nsServiceManager::GetService(kCDragServiceCID,
nsIDragService::GetIID(),
(nsISupports **)&dragService);
if (NS_OK == rv) {
nsCOMPtr<nsISupportsArray> items;
NS_NewISupportsArray(getter_AddRefs(items));
if ( items ) {
items->AppendElement(trans);
dragService->InvokeDragSession(items, nsnull, nsIDragService::DRAGDROP_ACTION_COPY | nsIDragService::DRAGDROP_ACTION_MOVE);
}
nsServiceManager::ReleaseService(kCDragServiceCID, dragService);
}
}
}
nsServiceManager::ReleaseService(kCClipboardCID, clipboard);
}
return NS_ERROR_BASE; // return that we've handled the event
}
#endif
}
return editor->Paste();
// middle-mouse click (paste);
else if (button == 2)
{
// Set the selection to the point under the mouse cursor:
nsCOMPtr<nsIDOMNSUIEvent> mouseEvent (do_QueryInterface(aMouseEvent));
if (!mouseEvent)
return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event".
nsCOMPtr<nsIDOMNode> parent;
if (!NS_SUCCEEDED(mouseEvent->GetRangeParent(getter_AddRefs(parent))))
return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event".
PRInt32 offset = 0;
if (!NS_SUCCEEDED(mouseEvent->GetRangeOffset(&offset)))
return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event".
nsCOMPtr<nsIDOMSelection> selection;
if (NS_SUCCEEDED(editor->GetSelection(getter_AddRefs(selection))))
(void)selection->Collapse(parent, offset);
// If the ctrl key is pressed, we'll do paste as quotation.
// Would've used the alt key, but the kde wmgr treats alt-middle specially.
PRBool ctrlKey = PR_FALSE;
uiEvent->GetCtrlKey(&ctrlKey);
if (ctrlKey)
{
nsCOMPtr<nsIEditorMailSupport> mailEditor = do_QueryInterface(mEditor);
if (mailEditor)
mailEditor->PasteAsQuotation();
}
editor->Paste();
return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event".
}
return NS_OK; // did not process the event
}
nsresult
nsTextEditorMouseListener::MouseUp(nsIDOMEvent* aMouseEvent)
{
@ -1425,9 +1579,6 @@ nsTextEditorFocusListener::HandleEvent(nsIDOMEvent* aEvent)
nsresult
nsTextEditorFocusListener::Focus(nsIDOMEvent* aEvent)
{
// turn on selection and caret
if (mEditor)
{
// turn on selection and caret
if (mEditor)
{
@ -1474,8 +1625,6 @@ nsTextEditorFocusListener::Focus(nsIDOMEvent* aEvent)
}
return NS_OK;
}
return NS_OK;
}
nsresult
nsTextEditorFocusListener::Blur(nsIDOMEvent* aEvent)

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -192,7 +192,15 @@ protected:
virtual void InitRules();
NS_IMETHOD GetLayoutObject(nsIDOMNode *aNode, nsISupports **aLayoutObject);
/** returns the layout object (nsIFrame in the real world) for aNode
* @param aNode the content to get a frame for
* @param aLayoutObject the "primary frame" for aNode, if one exists. May be null
* @return NS_OK whether a frame is found or not
* an error if some serious error occurs
*/
NS_IMETHOD GetLayoutObject(nsIDOMNode *aInNode, nsISupports **aOutLayoutObject);
NS_IMETHOD CollapseAdjacentTextNodes(nsIDOMSelection *aInSelection);
NS_IMETHOD DeleteSelectionAndPrepareToCreateNode(nsCOMPtr<nsIDOMNode> &parentSelectedNode, PRInt32& offsetOfNewNode);
@ -382,10 +390,10 @@ protected:
const nsString *aAttribute,
const nsString *aValue);
void GetTextSelectionOffsetsForRange(nsIDOMSelection *aSelection,
nsIDOMNode **aParent,
PRInt32 &aStartOffset,
PRInt32 &aEndOffset);
NS_IMETHOD GetTextSelectionOffsetsForRange(nsIDOMSelection *aSelection,
nsIDOMNode **aParent,
PRInt32 &aStartOffset,
PRInt32 &aEndOffset);
void ResetTextSelectionForRange(nsIDOMNode *aParent,
PRInt32 aStartOffset,

Просмотреть файл

@ -104,11 +104,10 @@ nsTextEditRules::SetFlags(PRUint32 aFlags)
// put a "white-space: pre" style on the body
nsCOMPtr<nsIDOMElement> bodyElement;
nsresult res = mEditor->GetBodyElement(getter_AddRefs(bodyElement));
if (NS_SUCCEEDED(res) && bodyElement)
{
// not going through the editor to do this.
bodyElement->SetAttribute("style", "white-space: pre");
}
if (NS_FAILED(res)) return res;
if (!bodyElement) return NS_ERROR_NULL_POINTER;
// not going through the editor to do this.
bodyElement->SetAttribute("style", "white-space: pre");
}
}
mFlags = aFlags;
@ -394,7 +393,9 @@ nsTextEditRules::CreateStyleForInsertText(nsIDOMSelection *aSelection, TypeInSta
// splitting anchor twice sets newTextNode as an empty text node between
// two halves of the original text node
result = mEditor->SplitNode(anchorAsText, offset, getter_AddRefs(newTextNode));
result = mEditor->SplitNode(anchorAsText, 0, getter_AddRefs(newTextNode));
if (NS_SUCCEEDED(result)) {
result = mEditor->SplitNode(anchorAsText, 0, getter_AddRefs(newTextNode));
}
}
}
// now we have the new text node we are going to insert into.
@ -464,37 +465,30 @@ nsTextEditRules::CreateStyleForInsertText(nsIDOMSelection *aSelection, TypeInSta
}
else // we have no selection, so insert a style tag in the body
{
nsCOMPtr<nsIDOMDocument>doc;
mEditor->GetDocument(getter_AddRefs(doc));
nsCOMPtr<nsIDOMNodeList>nodeList;
nsAutoString bodyTag = "body";
result = doc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList));
if ((NS_SUCCEEDED(result)) && nodeList)
{
PRUint32 count;
nodeList->GetLength(&count);
NS_ASSERTION(1==count, "there is not exactly 1 body in the document!");
nsCOMPtr<nsIDOMNode>bodyNode;
result = nodeList->Item(0, getter_AddRefs(bodyNode));
if ((NS_SUCCEEDED(result)) && bodyNode)
{ // now we've got the body tag. insert the style tag
if (aTypeInState.IsSet(NS_TYPEINSTATE_BOLD))
{
if (PR_TRUE==aTypeInState.GetBold()) {
InsertStyleAndNewTextNode(bodyNode, nsIEditProperty::b, aSelection);
}
nsCOMPtr<nsIDOMElement> bodyElement;
nsresult res = mEditor->GetBodyElement(getter_AddRefs(bodyElement));
if (NS_FAILED(res)) return res;
if (!bodyElement) return NS_ERROR_NULL_POINTER;
nsCOMPtr<nsIDOMNode>bodyNode = do_QueryInterface(bodyElement);
if (bodyNode)
{ // now we've got the body tag. insert the style tag
if (aTypeInState.IsSet(NS_TYPEINSTATE_BOLD))
{
if (PR_TRUE==aTypeInState.GetBold()) {
InsertStyleAndNewTextNode(bodyNode, nsIEditProperty::b, aSelection);
}
if (aTypeInState.IsSet(NS_TYPEINSTATE_ITALIC))
{
if (PR_TRUE==aTypeInState.GetItalic()) {
InsertStyleAndNewTextNode(bodyNode, nsIEditProperty::i, aSelection);
}
}
if (aTypeInState.IsSet(NS_TYPEINSTATE_ITALIC))
{
if (PR_TRUE==aTypeInState.GetItalic()) {
InsertStyleAndNewTextNode(bodyNode, nsIEditProperty::i, aSelection);
}
if (aTypeInState.IsSet(NS_TYPEINSTATE_UNDERLINE))
{
if (PR_TRUE==aTypeInState.GetUnderline()) {
InsertStyleAndNewTextNode(bodyNode, nsIEditProperty::u, aSelection);
}
}
if (aTypeInState.IsSet(NS_TYPEINSTATE_UNDERLINE))
{
if (PR_TRUE==aTypeInState.GetUnderline()) {
InsertStyleAndNewTextNode(bodyNode, nsIEditProperty::u, aSelection);
}
}
}
@ -513,12 +507,11 @@ nsTextEditRules::CreateFontStyleForInsertText(nsIDOMNode *aNewTextNode,
if (0!=aValue.Length())
{
result = InsertStyleNode(aNewTextNode, nsIEditProperty::font, aSelection, getter_AddRefs(newStyleNode));
if (NS_SUCCEEDED(result) && newStyleNode)
{
nsCOMPtr<nsIDOMElement>element = do_QueryInterface(newStyleNode);
if (element) {
result = mEditor->SetAttribute(element, aAttr, aValue);
}
if (NS_FAILED(result)) return result;
if (!newStyleNode) return NS_ERROR_NULL_POINTER;
nsCOMPtr<nsIDOMElement>element = do_QueryInterface(newStyleNode);
if (element) {
result = mEditor->SetAttribute(element, aAttr, aValue);
}
}
else
@ -540,21 +533,26 @@ nsTextEditRules::InsertStyleNode(nsIDOMNode *aNode,
nsresult result;
nsCOMPtr<nsIDOMNode>parent;
aNode->GetParentNode(getter_AddRefs(parent));
if (NS_FAILED(result)) return result;
if (!parent) return NS_ERROR_NULL_POINTER;
PRInt32 offsetInParent;
nsEditor::GetChildOffset(aNode, parent, offsetInParent);
result = nsEditor::GetChildOffset(aNode, parent, offsetInParent);
if (NS_FAILED(result)) return result;
nsAutoString tag;
aTag->ToString(tag);
result = mEditor->CreateNode(tag, parent, offsetInParent, aNewNode);
if ((NS_SUCCEEDED(result)) && *aNewNode)
if (NS_FAILED(result)) return result;
if (!aNewNode) return NS_ERROR_NULL_POINTER;
result = mEditor->DeleteNode(aNode);
if (NS_SUCCEEDED(result))
{
result = mEditor->DeleteNode(aNode);
if (NS_SUCCEEDED(result))
{
result = mEditor->InsertNode(aNode, *aNewNode, 0);
if (NS_SUCCEEDED(result)) {
if (aSelection) {
aSelection->Collapse(aNode, 0);
}
result = mEditor->InsertNode(aNode, *aNewNode, 0);
if (NS_SUCCEEDED(result)) {
if (aSelection) {
result = aSelection->Collapse(aNode, 0);
}
}
}
@ -575,16 +573,17 @@ nsTextEditRules::InsertStyleAndNewTextNode(nsIDOMNode *aParentNode, nsIAtom *aTa
nsCOMPtr<nsIDOMNode>anchor;
PRInt32 offset;
result = aSelection->GetAnchorNode(getter_AddRefs(anchor));
if (NS_SUCCEEDED(result) && NS_SUCCEEDED(aSelection->GetAnchorOffset(&offset)) && anchor)
if (NS_FAILED(result)) return result;
if (!anchor) return NS_ERROR_NULL_POINTER;
result = aSelection->GetAnchorOffset(&offset);
if (NS_FAILED(result)) return result;
nsCOMPtr<nsIDOMCharacterData>anchorAsText;
anchorAsText = do_QueryInterface(anchor);
if (anchorAsText)
{
nsCOMPtr<nsIDOMCharacterData>anchorAsText;
anchorAsText = do_QueryInterface(anchor);
if (anchorAsText)
{
nsCOMPtr<nsIDOMNode> newStyleNode;
result = InsertStyleNode(anchor, aTag, aSelection, getter_AddRefs(newStyleNode));
return result;
}
nsCOMPtr<nsIDOMNode> newStyleNode;
result = InsertStyleNode(anchor, aTag, aSelection, getter_AddRefs(newStyleNode));
return result;
}
}
// if we get here, there is no selected text node so we create one.
@ -593,15 +592,15 @@ nsTextEditRules::InsertStyleAndNewTextNode(nsIDOMNode *aParentNode, nsIAtom *aTa
nsCOMPtr<nsIDOMNode>newStyleNode;
nsCOMPtr<nsIDOMNode>newTextNode;
result = mEditor->CreateNode(tag, aParentNode, 0, getter_AddRefs(newStyleNode));
if (NS_SUCCEEDED(result))
{
result = mEditor->CreateNode(nsEditor::GetTextNodeTag(), newStyleNode, 0, getter_AddRefs(newTextNode));
if (NS_SUCCEEDED(result))
{
if (aSelection) {
aSelection->Collapse(newTextNode, 0);
}
}
if (NS_FAILED(result)) return result;
if (!newStyleNode) return NS_ERROR_NULL_POINTER;
result = mEditor->CreateNode(nsEditor::GetTextNodeTag(), newStyleNode, 0, getter_AddRefs(newTextNode));
if (NS_FAILED(result)) return result;
if (!newTextNode) return NS_ERROR_NULL_POINTER;
if (aSelection) {
result = aSelection->Collapse(newTextNode, 0);
}
return result;
}
@ -707,55 +706,63 @@ nsTextEditRules::DidDeleteSelection(nsIDOMSelection *aSelection,
nsCOMPtr<nsIDOMNode>anchor;
PRInt32 offset;
result = aSelection->GetAnchorNode(getter_AddRefs(anchor));
if (NS_SUCCEEDED(result) && NS_SUCCEEDED(aSelection->GetAnchorOffset(&offset)) && anchor)
if (NS_FAILED(result)) return result;
if (!anchor) return NS_ERROR_NULL_POINTER;
result = aSelection->GetAnchorOffset(&offset);
if (NS_FAILED(result)) return result;
nsCOMPtr<nsIDOMNodeList> anchorChildren;
result = anchor->GetChildNodes(getter_AddRefs(anchorChildren));
nsCOMPtr<nsIDOMNode> selectedNode;
if ((NS_SUCCEEDED(result)) && anchorChildren) {
result = anchorChildren->Item(offset, getter_AddRefs(selectedNode));
}
else {
selectedNode = do_QueryInterface(anchor);
}
if ((NS_SUCCEEDED(result)) && selectedNode)
{
nsCOMPtr<nsIDOMNodeList> anchorChildren;
result = anchor->GetChildNodes(getter_AddRefs(anchorChildren));
nsCOMPtr<nsIDOMNode> selectedNode;
if ((NS_SUCCEEDED(result)) && anchorChildren) {
result = anchorChildren->Item(offset, getter_AddRefs(selectedNode));
}
else {
selectedNode = do_QueryInterface(anchor);
}
if ((NS_SUCCEEDED(result)) && selectedNode)
nsCOMPtr<nsIDOMCharacterData>selectedNodeAsText;
selectedNodeAsText = do_QueryInterface(selectedNode);
if (selectedNodeAsText)
{
nsCOMPtr<nsIDOMCharacterData>selectedNodeAsText;
selectedNodeAsText = do_QueryInterface(selectedNode);
if (selectedNodeAsText)
nsCOMPtr<nsIDOMNode> siblingNode;
selectedNode->GetPreviousSibling(getter_AddRefs(siblingNode));
if (siblingNode)
{
nsCOMPtr<nsIDOMNode> siblingNode;
selectedNode->GetPreviousSibling(getter_AddRefs(siblingNode));
if (siblingNode)
nsCOMPtr<nsIDOMCharacterData>siblingNodeAsText;
siblingNodeAsText = do_QueryInterface(siblingNode);
if (siblingNodeAsText)
{
nsCOMPtr<nsIDOMCharacterData>siblingNodeAsText;
siblingNodeAsText = do_QueryInterface(siblingNode);
if (siblingNodeAsText)
{
PRUint32 siblingLength; // the length of siblingNode before the join
siblingNodeAsText->GetLength(&siblingLength);
nsCOMPtr<nsIDOMNode> parentNode;
selectedNode->GetParentNode(getter_AddRefs(parentNode));
result = mEditor->JoinNodes(siblingNode, selectedNode, parentNode);
// selectedNode will remain after the join, siblingNode is removed
}
PRUint32 siblingLength; // the length of siblingNode before the join
siblingNodeAsText->GetLength(&siblingLength);
nsCOMPtr<nsIDOMNode> parentNode;
result = selectedNode->GetParentNode(getter_AddRefs(parentNode));
if (NS_FAILED(result)) return result;
if (!parentNode) return NS_ERROR_NULL_POINTER;
result = mEditor->JoinNodes(siblingNode, selectedNode, parentNode);
// selectedNode will remain after the join, siblingNode is removed
}
selectedNode->GetNextSibling(getter_AddRefs(siblingNode));
if (siblingNode)
}
selectedNode->GetNextSibling(getter_AddRefs(siblingNode));
if (siblingNode)
{
nsCOMPtr<nsIDOMCharacterData>siblingNodeAsText;
siblingNodeAsText = do_QueryInterface(siblingNode);
if (siblingNodeAsText)
{
nsCOMPtr<nsIDOMCharacterData>siblingNodeAsText;
siblingNodeAsText = do_QueryInterface(siblingNode);
if (siblingNodeAsText)
{
PRUint32 selectedNodeLength; // the length of siblingNode before the join
selectedNodeAsText->GetLength(&selectedNodeLength);
nsCOMPtr<nsIDOMNode> parentNode;
selectedNode->GetParentNode(getter_AddRefs(parentNode));
result = mEditor->JoinNodes(selectedNode, siblingNode, parentNode);
// selectedNode will remain after the join, siblingNode is removed
// set selection
aSelection->Collapse(siblingNode, selectedNodeLength);
}
PRUint32 selectedNodeLength; // the length of siblingNode before the join
selectedNodeAsText->GetLength(&selectedNodeLength);
nsCOMPtr<nsIDOMNode> parentNode;
result = selectedNode->GetParentNode(getter_AddRefs(parentNode));
if (NS_FAILED(result)) return result;
if (!parentNode) return NS_ERROR_NULL_POINTER;
result = mEditor->JoinNodes(selectedNode, siblingNode, parentNode);
if (NS_FAILED(result)) return result;
// selectedNode will remain after the join, siblingNode is removed
// set selection
result = aSelection->Collapse(siblingNode, selectedNodeLength);
}
}
}
@ -795,23 +802,24 @@ nsTextEditRules:: DidUndo(nsIDOMSelection *aSelection, nsresult aResult)
nsCOMPtr<nsIDOMNode>node;
PRInt32 offset;
result = aSelection->GetAnchorNode(getter_AddRefs(node));
if (NS_SUCCEEDED(result) && NS_SUCCEEDED(aSelection->GetAnchorOffset(&offset)) && node)
if (NS_FAILED(result)) return result;
if (!node) return NS_ERROR_NULL_POINTER;
result = aSelection->GetAnchorOffset(&offset);
if (NS_FAILED(result)) return result;
nsCOMPtr<nsIDOMElement>element;
element = do_QueryInterface(node);
if (element)
{
nsCOMPtr<nsIDOMElement>element;
element = do_QueryInterface(node);
if (element)
{
nsAutoString att(nsEditor::kMOZEditorBogusNodeAttr);
nsAutoString val;
(void)element->GetAttribute(att, val);
if (val.Equals(nsEditor::kMOZEditorBogusNodeValue)) {
mBogusNode = do_QueryInterface(element);
}
nsAutoString att(nsEditor::kMOZEditorBogusNodeAttr);
nsAutoString val;
(void)element->GetAttribute(att, val);
if (val.Equals(nsEditor::kMOZEditorBogusNodeValue)) {
mBogusNode = do_QueryInterface(element);
}
nsCOMPtr<nsIDOMNode> temp;
result = node->GetParentNode(getter_AddRefs(temp));
node = do_QueryInterface(temp);
}
nsCOMPtr<nsIDOMNode> temp;
result = node->GetParentNode(getter_AddRefs(temp));
node = do_QueryInterface(temp);
}
}
return result;
@ -842,23 +850,24 @@ nsTextEditRules::DidRedo(nsIDOMSelection *aSelection, nsresult aResult)
nsCOMPtr<nsIDOMNode>node;
PRInt32 offset;
result = aSelection->GetAnchorNode(getter_AddRefs(node));
if (NS_SUCCEEDED(result) && NS_SUCCEEDED(aSelection->GetAnchorOffset(&offset)) && node)
if (NS_FAILED(result)) return result;
if (!node) return NS_ERROR_NULL_POINTER;
result = aSelection->GetAnchorOffset(&offset);
if (NS_FAILED(result)) return result;
nsCOMPtr<nsIDOMElement>element;
element = do_QueryInterface(node);
if (element)
{
nsCOMPtr<nsIDOMElement>element;
element = do_QueryInterface(node);
if (element)
{
nsAutoString att(nsEditor::kMOZEditorBogusNodeAttr);
nsAutoString val;
(void)element->GetAttribute(att, val);
if (val.Equals(nsEditor::kMOZEditorBogusNodeValue)) {
mBogusNode = do_QueryInterface(element);
}
nsAutoString att(nsEditor::kMOZEditorBogusNodeAttr);
nsAutoString val;
(void)element->GetAttribute(att, val);
if (val.Equals(nsEditor::kMOZEditorBogusNodeValue)) {
mBogusNode = do_QueryInterface(element);
}
nsCOMPtr<nsIDOMNode> temp;
result = node->GetParentNode(getter_AddRefs(temp));
node = do_QueryInterface(temp);
}
nsCOMPtr<nsIDOMNode> temp;
result = node->GetParentNode(getter_AddRefs(temp));
node = do_QueryInterface(temp);
}
}
return result;
@ -895,86 +904,63 @@ nsTextEditRules::CreateBogusNodeIfNeeded(nsIDOMSelection *aSelection)
{
if (!aSelection) { return NS_ERROR_NULL_POINTER; }
if (!mEditor) { return NS_ERROR_NULL_POINTER; }
nsCOMPtr<nsIDOMDocument>doc;
mEditor->GetDocument(getter_AddRefs(doc));
nsCOMPtr<nsIDOMNodeList>nodeList;
nsAutoString bodyTag = "body";
nsresult result = doc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList));
if ((NS_SUCCEEDED(result)) && nodeList)
nsCOMPtr<nsIDOMElement> bodyElement;
nsresult result = mEditor->GetBodyElement(getter_AddRefs(bodyElement));
if (NS_FAILED(result)) return result;
if (!bodyElement) return NS_ERROR_NULL_POINTER;
nsCOMPtr<nsIDOMNode>bodyNode = do_QueryInterface(bodyElement);
// now we've got the body tag.
// iterate the body tag, looking for editable content
// if no editable content is found, insert the bogus node
PRBool needsBogusContent=PR_TRUE;
nsCOMPtr<nsIDOMNode>bodyChild;
result = bodyNode->GetFirstChild(getter_AddRefs(bodyChild));
while ((NS_SUCCEEDED(result)) && bodyChild)
{
if (PR_TRUE==mEditor->IsEditable(bodyChild))
{
needsBogusContent = PR_FALSE;
break;
}
nsCOMPtr<nsIDOMNode>temp;
bodyChild->GetNextSibling(getter_AddRefs(temp));
bodyChild = do_QueryInterface(temp);
}
if (PR_TRUE==needsBogusContent)
{
PRUint32 count;
nodeList->GetLength(&count);
NS_ASSERTION(1==count, "there is not exactly 1 body in the document!");
nsCOMPtr<nsIDOMNode>bodyNode;
result = nodeList->Item(0, getter_AddRefs(bodyNode));
if ((NS_SUCCEEDED(result)) && bodyNode)
{ // now we've got the body tag.
// iterate the body tag, looking for editable content
// if no editable content is found, insert the bogus node
PRBool needsBogusContent=PR_TRUE;
nsCOMPtr<nsIDOMNode>bodyChild;
result = bodyNode->GetFirstChild(getter_AddRefs(bodyChild));
while ((NS_SUCCEEDED(result)) && bodyChild)
{
if (PR_TRUE==mEditor->IsEditable(bodyChild))
{
needsBogusContent = PR_FALSE;
break;
}
nsCOMPtr<nsIDOMNode>temp;
bodyChild->GetNextSibling(getter_AddRefs(temp));
bodyChild = do_QueryInterface(temp);
}
if (PR_TRUE==needsBogusContent)
{
// set mBogusNode to be the newly created <P>
result = mEditor->CreateNode(nsAutoString("P"), bodyNode, 0,
getter_AddRefs(mBogusNode));
if ((NS_SUCCEEDED(result)) && mBogusNode)
{
nsCOMPtr<nsIDOMNode>newTNode;
result = mEditor->CreateNode(nsEditor::GetTextNodeTag(), mBogusNode, 0,
getter_AddRefs(newTNode));
if ((NS_SUCCEEDED(result)) && newTNode)
{
nsCOMPtr<nsIDOMCharacterData>newNodeAsText;
newNodeAsText = do_QueryInterface(newTNode);
if (newNodeAsText)
{
nsAutoString data;
data += 160;
newNodeAsText->SetData(data);
aSelection->Collapse(newTNode, 0);
}
}
// make sure we know the PNode is bogus
nsCOMPtr<nsIDOMElement>newPElement;
newPElement = do_QueryInterface(mBogusNode);
if (newPElement)
{
nsAutoString att(nsEditor::kMOZEditorBogusNodeAttr);
nsAutoString val(nsEditor::kMOZEditorBogusNodeValue);
newPElement->SetAttribute(att, val);
}
}
}
// set mBogusNode to be the newly created <P>
result = mEditor->CreateNode(nsAutoString("P"), bodyNode, 0,
getter_AddRefs(mBogusNode));
if (NS_FAILED(result)) return result;
if (!mBogusNode) return NS_ERROR_NULL_POINTER;
nsCOMPtr<nsIDOMNode>newTNode;
result = mEditor->CreateNode(nsEditor::GetTextNodeTag(), mBogusNode, 0,
getter_AddRefs(newTNode));
if (NS_FAILED(result)) return result;
if (!newTNode) return NS_ERROR_NULL_POINTER;
nsCOMPtr<nsIDOMCharacterData>newNodeAsText;
newNodeAsText = do_QueryInterface(newTNode);
if (newNodeAsText)
{
nsAutoString data;
data += 160;
newNodeAsText->SetData(data);
aSelection->Collapse(newTNode, 0);
}
// make sure we know the PNode is bogus
nsCOMPtr<nsIDOMElement>newPElement;
newPElement = do_QueryInterface(mBogusNode);
if (newPElement)
{
nsAutoString att(nsEditor::kMOZEditorBogusNodeAttr);
nsAutoString val(nsEditor::kMOZEditorBogusNodeValue);
newPElement->SetAttribute(att, val);
}
}
return result;
}

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -35,23 +35,24 @@ NS_IMETHODIMP ChangeAttributeTxn::Init(nsIEditor *aEditor,
const nsString& aValue,
PRBool aRemoveAttribute)
{
if (nsnull!=aEditor && nsnull!=aElement)
{
mEditor = aEditor;
mElement = do_QueryInterface(aElement);
mAttribute = aAttribute;
mValue = aValue;
mRemoveAttribute = aRemoveAttribute;
mAttributeWasSet=PR_FALSE;
mUndoValue="";
return NS_OK;
}
else
return NS_ERROR_NULL_POINTER;
NS_ASSERTION(aEditor && aElement, "bad arg");
if (!aEditor || !aElement) { return NS_ERROR_NULL_POINTER; }
mEditor = aEditor;
mElement = do_QueryInterface(aElement);
mAttribute = aAttribute;
mValue = aValue;
mRemoveAttribute = aRemoveAttribute;
mAttributeWasSet=PR_FALSE;
mUndoValue="";
return NS_OK;
}
NS_IMETHODIMP ChangeAttributeTxn::Do(void)
{
NS_ASSERTION(mEditor && mElement, "bad state");
if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
// need to get the current value of the attribute and save it, and set mAttributeWasSet
nsresult result = mEditor->GetAttributeValue(mElement, mAttribute, mUndoValue, mAttributeWasSet);
// XXX: hack until attribute-was-set code is implemented
@ -70,6 +71,9 @@ NS_IMETHODIMP ChangeAttributeTxn::Do(void)
NS_IMETHODIMP ChangeAttributeTxn::Undo(void)
{
NS_ASSERTION(mEditor && mElement, "bad state");
if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
nsresult result=NS_OK;
if (PR_TRUE==mAttributeWasSet)
result = mElement->SetAttribute(mAttribute, mUndoValue);
@ -81,6 +85,9 @@ NS_IMETHODIMP ChangeAttributeTxn::Undo(void)
NS_IMETHODIMP ChangeAttributeTxn::Redo(void)
{
NS_ASSERTION(mEditor && mElement, "bad state");
if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
nsresult result;
if (PR_FALSE==mRemoveAttribute)

Просмотреть файл

@ -41,24 +41,21 @@ NS_IMETHODIMP CreateElementTxn::Init(nsIEditor *aEditor,
PRUint32 aOffsetInParent)
{
NS_ASSERTION(aEditor&&aParent, "null args");
if (aEditor && aParent)
{
mEditor = aEditor;
mTag = aTag;
mParent = do_QueryInterface(aParent);
mOffsetInParent = aOffsetInParent;
if (!aEditor || !aParent) { return NS_ERROR_NULL_POINTER; }
mEditor = aEditor;
mTag = aTag;
mParent = do_QueryInterface(aParent);
mOffsetInParent = aOffsetInParent;
#ifdef NS_DEBUG
{
nsCOMPtr<nsIDOMNodeList> testChildNodes;
nsresult testResult = mParent->GetChildNodes(getter_AddRefs(testChildNodes));
NS_ASSERTION(testChildNodes, "bad parent type, can't have children.");
NS_ASSERTION(NS_SUCCEEDED(testResult), "bad result.");
}
#endif
return NS_OK;
{
nsCOMPtr<nsIDOMNodeList> testChildNodes;
nsresult testResult = mParent->GetChildNodes(getter_AddRefs(testChildNodes));
NS_ASSERTION(testChildNodes, "bad parent type, can't have children.");
NS_ASSERTION(NS_SUCCEEDED(testResult), "bad result.");
}
else
return NS_ERROR_NULL_POINTER;
#endif
return NS_OK;
}
@ -70,69 +67,69 @@ NS_IMETHODIMP CreateElementTxn::Do(void)
{
if (gNoisy) { printf("Do Create Element parent = %p, offset = %d\n",
mParent.get(), mOffsetInParent); }
NS_ASSERTION(mEditor, "bad state -- null editor");
nsresult result = NS_ERROR_NULL_POINTER;
if (mEditor)
NS_ASSERTION(mEditor && mParent, "bad state");
if (!mEditor || !mParent) return NS_ERROR_NOT_INITIALIZED;
nsresult result;
// create a new node
nsCOMPtr<nsIDOMDocument>doc;
result = mEditor->GetDocument(getter_AddRefs(doc));
if (NS_FAILED(result)) return result;
if (!doc) return NS_ERROR_NULL_POINTER;
if (nsEditor::GetTextNodeTag() == mTag)
{
// create a new node
nsCOMPtr<nsIDOMDocument>doc;
result = mEditor->GetDocument(getter_AddRefs(doc));
if ((NS_SUCCEEDED(result)) && (doc))
const nsString stringData;
nsCOMPtr<nsIDOMText>newTextNode;
result = doc->CreateTextNode(stringData, getter_AddRefs(newTextNode));
if (NS_FAILED(result)) return result;
if (!newTextNode) return NS_ERROR_NULL_POINTER;
mNewNode = do_QueryInterface(newTextNode);
}
else
{
nsCOMPtr<nsIDOMElement>newElement;
result = doc->CreateElement(mTag, getter_AddRefs(newElement));
if (NS_FAILED(result)) return result;
if (!newElement) return NS_ERROR_NULL_POINTER;
mNewNode = do_QueryInterface(newElement);
}
NS_ASSERTION(((NS_SUCCEEDED(result)) && (mNewNode)), "could not create element.");
if (!mNewNode) return NS_ERROR_NULL_POINTER;
if (gNoisy) { printf(" newNode = %p\n", mNewNode.get()); }
// insert the new node
nsCOMPtr<nsIDOMNode> resultNode;
if (CreateElementTxn::eAppend==(PRInt32)mOffsetInParent)
{
result = mParent->AppendChild(mNewNode, getter_AddRefs(resultNode));
}
else
{
nsCOMPtr<nsIDOMNodeList> childNodes;
result = mParent->GetChildNodes(getter_AddRefs(childNodes));
if ((NS_SUCCEEDED(result)) && (childNodes))
{
if (nsEditor::GetTextNodeTag() == mTag)
PRUint32 count;
childNodes->GetLength(&count);
if (mOffsetInParent>count)
mOffsetInParent = count;
result = childNodes->Item(mOffsetInParent, getter_AddRefs(mRefNode));
if (NS_SUCCEEDED(result)) // note, it's ok for mRefNode to be null. that means append
{
const nsString stringData;
nsCOMPtr<nsIDOMText>newTextNode;
result = doc->CreateTextNode(stringData, getter_AddRefs(newTextNode));
if (NS_SUCCEEDED(result) && newTextNode) {
mNewNode = do_QueryInterface(newTextNode);
}
}
else
{
nsCOMPtr<nsIDOMElement>newElement;
result = doc->CreateElement(mTag, getter_AddRefs(newElement));
if (NS_SUCCEEDED(result) && newElement) {
mNewNode = do_QueryInterface(newElement);
}
}
NS_ASSERTION(((NS_SUCCEEDED(result)) && (mNewNode)), "could not create element.");
if ((NS_SUCCEEDED(result)) && (mNewNode))
{
if (gNoisy) { printf(" newNode = %p\n", mNewNode.get()); }
// insert the new node
nsCOMPtr<nsIDOMNode> resultNode;
if (CreateElementTxn::eAppend==(PRInt32)mOffsetInParent)
result = mParent->InsertBefore(mNewNode, mRefNode, getter_AddRefs(resultNode));
if (NS_SUCCEEDED(result))
{
result = mParent->AppendChild(mNewNode, getter_AddRefs(resultNode));
}
else
{
nsCOMPtr<nsIDOMNodeList> childNodes;
result = mParent->GetChildNodes(getter_AddRefs(childNodes));
if ((NS_SUCCEEDED(result)) && (childNodes))
{
PRUint32 count;
childNodes->GetLength(&count);
if (mOffsetInParent>count)
mOffsetInParent = count;
result = childNodes->Item(mOffsetInParent, getter_AddRefs(mRefNode));
if (NS_SUCCEEDED(result)) // note, it's ok for mRefNode to be null. that means append
{
result = mParent->InsertBefore(mNewNode, mRefNode, getter_AddRefs(resultNode));
if (NS_SUCCEEDED(result))
{
nsCOMPtr<nsIDOMSelection> selection;
nsresult selectionResult = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_SUCCEEDED(selectionResult) && selection) {
PRInt32 offset=0;
nsEditor::GetChildOffset(mNewNode, mParent, offset);
selectionResult = selection->Collapse(mParent, offset+1);
NS_ASSERTION((NS_SUCCEEDED(selectionResult)), "selection could not be collapsed after insert.");
}
}
}
}
nsCOMPtr<nsIDOMSelection> selection;
result = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
PRInt32 offset=0;
result = nsEditor::GetChildOffset(mNewNode, mParent, offset);
if (NS_FAILED(result)) return result;
result = selection->Collapse(mParent, offset+1);
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after insert.");
}
}
}
@ -144,20 +141,24 @@ NS_IMETHODIMP CreateElementTxn::Undo(void)
{
if (gNoisy) { printf("Undo Create Element, mParent = %p, node = %p\n",
mParent.get(), mNewNode.get()); }
NS_ASSERTION(mEditor && mParent, "bad state");
if (!mEditor || !mParent) return NS_ERROR_NOT_INITIALIZED;
nsCOMPtr<nsIDOMNode> resultNode;
nsresult result = mParent->RemoveChild(mNewNode, getter_AddRefs(resultNode));
if (NS_SUCCEEDED(result))
{
nsCOMPtr<nsIDOMSelection> selection;
nsresult selectionResult = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_SUCCEEDED(selectionResult) && selection) {
PRInt32 offset=0;
if (mRefNode) {
nsEditor::GetChildOffset(mRefNode, mParent, offset);
}
selectionResult = selection->Collapse(mParent, offset);
NS_ASSERTION((NS_SUCCEEDED(selectionResult)), "selection could not be collapsed after undo of insert.");
result = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
PRInt32 offset=0;
if (mRefNode) {
nsEditor::GetChildOffset(mRefNode, mParent, offset);
}
result = selection->Collapse(mParent, offset);
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of insert.");
}
return result;
}
@ -165,6 +166,8 @@ NS_IMETHODIMP CreateElementTxn::Undo(void)
NS_IMETHODIMP CreateElementTxn::Redo(void)
{
if (gNoisy) { printf("Redo Create Element\n"); }
NS_ASSERTION(mEditor && mParent, "bad state");
if (!mEditor || !mParent) return NS_ERROR_NOT_INITIALIZED;
// first, reset mNewNode so it has no attributes or content
nsCOMPtr<nsIDOMCharacterData>nodeAsText;
@ -182,12 +185,12 @@ NS_IMETHODIMP CreateElementTxn::Redo(void)
{
nsCOMPtr<nsIDOMSelection> selection;
result = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_SUCCEEDED(result) && selection) {
PRInt32 offset=0;
nsEditor::GetChildOffset(mNewNode, mParent, offset);
nsresult selectionResult = selection->Collapse(mParent, offset);
NS_ASSERTION((NS_SUCCEEDED(selectionResult)), "selection could not be collapsed after undo of insert.");
}
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
PRInt32 offset=0;
nsEditor::GetChildOffset(mNewNode, mParent, offset);
result = selection->Collapse(mParent, offset);
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of insert.");
}
return result;
}

Просмотреть файл

@ -51,16 +51,11 @@ DeleteElementTxn::~DeleteElementTxn()
NS_IMETHODIMP DeleteElementTxn::Do(void)
{
if (gNoisy) { printf("%p Do Delete Element element = %p\n", this, mElement.get()); }
if (!mElement)
return NS_ERROR_NULL_POINTER;
if (!mElement) return NS_ERROR_NOT_INITIALIZED;
nsresult result = mElement->GetParentNode(getter_AddRefs(mParent));
if (NS_FAILED(result)) {
return result;
}
if (!mParent) {
return NS_OK; // this is a no-op, there's no parent to delete mElement from
}
if (NS_FAILED(result)) { return result; }
if (!mParent) { return NS_OK; } // this is a no-op, there's no parent to delete mElement from
#ifdef NS_DEBUG
// begin debug output

Просмотреть файл

@ -46,21 +46,22 @@ DeleteRangeTxn::DeleteRangeTxn()
NS_IMETHODIMP DeleteRangeTxn::Init(nsIEditor *aEditor, nsIDOMRange *aRange)
{
if (aEditor && aRange)
{
mEditor = aEditor;
mRange = do_QueryInterface(aRange);
nsresult result = aRange->GetStartParent(getter_AddRefs(mStartParent));
NS_ASSERTION((NS_SUCCEEDED(result)), "GetStartParent failed.");
result = aRange->GetEndParent(getter_AddRefs(mEndParent));
NS_ASSERTION((NS_SUCCEEDED(result)), "GetEndParent failed.");
result = aRange->GetStartOffset(&mStartOffset);
NS_ASSERTION((NS_SUCCEEDED(result)), "GetStartOffset failed.");
result = aRange->GetEndOffset(&mEndOffset);
NS_ASSERTION((NS_SUCCEEDED(result)), "GetEndOffset failed.");
result = aRange->GetCommonParent(getter_AddRefs(mCommonParent));
NS_ASSERTION((NS_SUCCEEDED(result)), "GetCommonParent failed.");
NS_ASSERTION(aEditor && aRange, "bad state");
if (!aEditor || !aRange) { return NS_ERROR_NOT_INITIALIZED; }
mEditor = aEditor;
mRange = do_QueryInterface(aRange);
nsresult result = aRange->GetStartParent(getter_AddRefs(mStartParent));
NS_ASSERTION((NS_SUCCEEDED(result)), "GetStartParent failed.");
result = aRange->GetEndParent(getter_AddRefs(mEndParent));
NS_ASSERTION((NS_SUCCEEDED(result)), "GetEndParent failed.");
result = aRange->GetStartOffset(&mStartOffset);
NS_ASSERTION((NS_SUCCEEDED(result)), "GetStartOffset failed.");
result = aRange->GetEndOffset(&mEndOffset);
NS_ASSERTION((NS_SUCCEEDED(result)), "GetEndOffset failed.");
result = aRange->GetCommonParent(getter_AddRefs(mCommonParent));
NS_ASSERTION((NS_SUCCEEDED(result)), "GetCommonParent failed.");
#ifdef NS_DEBUG
{
@ -96,10 +97,8 @@ NS_IMETHODIMP DeleteRangeTxn::Init(nsIEditor *aEditor, nsIDOMRange *aRange)
}
}
#endif
return result;
}
else
return NS_ERROR_NULL_POINTER;
return result;
}
DeleteRangeTxn::~DeleteRangeTxn()
@ -109,11 +108,10 @@ DeleteRangeTxn::~DeleteRangeTxn()
NS_IMETHODIMP DeleteRangeTxn::Do(void)
{
if (gNoisy) { printf("Do Delete Range\n"); }
if (!mStartParent || !mEndParent || !mCommonParent)
return NS_ERROR_NULL_POINTER;
if (!mStartParent || !mEndParent || !mCommonParent || !mEditor)
return NS_ERROR_NOT_INITIALIZED;
nsresult result;
// build the child transactions
if (mStartParent==mEndParent)
@ -145,9 +143,9 @@ NS_IMETHODIMP DeleteRangeTxn::Do(void)
// set the resulting selection
nsCOMPtr<nsIDOMSelection> selection;
result = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_SUCCEEDED(result)) {
result = selection->Collapse(mStartParent, mStartOffset);
}
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
result = selection->Collapse(mStartParent, mStartOffset);
}
return result;
@ -156,19 +154,21 @@ NS_IMETHODIMP DeleteRangeTxn::Do(void)
NS_IMETHODIMP DeleteRangeTxn::Undo(void)
{
if (gNoisy) { printf("Undo Delete Range\n"); }
if (!mStartParent || !mEndParent || !mCommonParent)
return NS_ERROR_NULL_POINTER;
if (!mStartParent || !mEndParent || !mCommonParent || !mEditor)
return NS_ERROR_NOT_INITIALIZED;
nsresult result = EditAggregateTxn::Undo();
if (NS_SUCCEEDED(result)) {
if (NS_SUCCEEDED(result))
{
// set the resulting selection
nsCOMPtr<nsIDOMSelection> selection;
result = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_SUCCEEDED(result)) {
selection->Collapse(mStartParent, mStartOffset);
selection->Extend(mEndParent, mEndOffset);
}
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
result = selection->Collapse(mStartParent, mStartOffset);
if (NS_FAILED(result)) return result;
result = selection->Extend(mEndParent, mEndOffset);
}
return result;
@ -177,8 +177,8 @@ NS_IMETHODIMP DeleteRangeTxn::Undo(void)
NS_IMETHODIMP DeleteRangeTxn::Redo(void)
{
if (gNoisy) { printf("Redo Delete Range\n"); }
if (!mStartParent || !mEndParent || !mCommonParent)
return NS_ERROR_NULL_POINTER;
if (!mStartParent || !mEndParent || !mCommonParent || !mEditor)
return NS_ERROR_NOT_INITIALIZED;
nsresult result = EditAggregateTxn::Redo();
@ -186,9 +186,9 @@ NS_IMETHODIMP DeleteRangeTxn::Redo(void)
// set the resulting selection
nsCOMPtr<nsIDOMSelection> selection;
result = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_SUCCEEDED(result)) {
result = selection->Collapse(mStartParent, mStartOffset);
}
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
result = selection->Collapse(mStartParent, mStartOffset);
}
return result;
@ -224,9 +224,10 @@ NS_IMETHODIMP DeleteRangeTxn::GetRedoString(nsString *aString)
return NS_OK;
}
NS_IMETHODIMP DeleteRangeTxn::CreateTxnsToDeleteBetween(nsIDOMNode *aStartParent,
PRUint32 aStartOffset,
PRUint32 aEndOffset)
NS_IMETHODIMP
DeleteRangeTxn::CreateTxnsToDeleteBetween(nsIDOMNode *aStartParent,
PRUint32 aStartOffset,
PRUint32 aEndOffset)
{
nsresult result;
// see what kind of node we have
@ -236,44 +237,42 @@ NS_IMETHODIMP DeleteRangeTxn::CreateTxnsToDeleteBetween(nsIDOMNode *aStartParent
{ // if the node is a text node, then delete text content
DeleteTextTxn *txn;
result = TransactionFactory::GetNewTransaction(DeleteTextTxn::GetCID(), (EditTxn **)&txn);
if (nsnull!=txn)
{
PRInt32 numToDel;
if (aStartOffset==aEndOffset)
numToDel = 1;
else
numToDel = aEndOffset-aStartOffset;
txn->Init(mEditor, textNode, aStartOffset, numToDel);
AppendChild(txn);
}
if (NS_FAILED(result)) return result;
if (!txn) return NS_ERROR_NULL_POINTER;
PRInt32 numToDel;
if (aStartOffset==aEndOffset)
numToDel = 1;
else
numToDel = aEndOffset-aStartOffset;
txn->Init(mEditor, textNode, aStartOffset, numToDel);
AppendChild(txn);
}
else
{
PRUint32 childCount;
nsCOMPtr<nsIDOMNodeList> children;
result = aStartParent->GetChildNodes(getter_AddRefs(children));
if ((NS_SUCCEEDED(result)) && children)
if (NS_FAILED(result)) return result;
if (!children) return NS_ERROR_NULL_POINTER;
children->GetLength(&childCount);
NS_ASSERTION(aEndOffset<=childCount, "bad aEndOffset");
PRUint32 i;
for (i=aStartOffset; i<aEndOffset; i++)
{
children->GetLength(&childCount);
NS_ASSERTION(aEndOffset<=childCount, "bad aEndOffset");
PRUint32 i;
for (i=aStartOffset; i<aEndOffset; i++)
{
nsCOMPtr<nsIDOMNode> child;
result = children->Item(i, getter_AddRefs(child));
if ((NS_SUCCEEDED(result)) && child)
{
DeleteElementTxn *txn;
result = TransactionFactory::GetNewTransaction(DeleteElementTxn::GetCID(), (EditTxn **)&txn);
if (nsnull!=txn)
{
txn->Init(child);
AppendChild(txn);
}
else
return NS_ERROR_NULL_POINTER;
}
}
nsCOMPtr<nsIDOMNode> child;
result = children->Item(i, getter_AddRefs(child));
if (NS_FAILED(result)) return result;
if (!child) return NS_ERROR_NULL_POINTER;
DeleteElementTxn *txn;
result = TransactionFactory::GetNewTransaction(DeleteElementTxn::GetCID(), (EditTxn **)&txn);
if (NS_FAILED(result)) return result;
if (!txn) return NS_ERROR_NULL_POINTER;
txn->Init(child);
AppendChild(txn);
}
}
return result;
@ -306,13 +305,11 @@ NS_IMETHODIMP DeleteRangeTxn::CreateTxnsToDeleteContent(nsIDOMNode *aParent,
{
DeleteTextTxn *txn;
result = TransactionFactory::GetNewTransaction(DeleteTextTxn::GetCID(), (EditTxn **)&txn);
if (nsnull!=txn)
{
txn->Init(mEditor, textNode, start, numToDelete);
AppendChild(txn);
}
else
return NS_ERROR_NULL_POINTER;
if (NS_FAILED(result)) return result;
if (!txn) return NS_ERROR_NULL_POINTER;
txn->Init(mEditor, textNode, start, numToDelete);
AppendChild(txn);
}
}
@ -332,11 +329,11 @@ NS_IMETHODIMP DeleteRangeTxn::CreateTxnsToDeleteNodesBetween()
nsnull,
nsIContentIterator::GetIID(),
getter_AddRefs(iter));
if (!NS_SUCCEEDED(result))
return result;
if (NS_FAILED(result)) return result;
if (!iter) return NS_ERROR_NULL_POINTER;
result = iter->Init(mRange);
if (!NS_SUCCEEDED(result))
return result;
if (NS_FAILED(result)) return result;
while (NS_COMFALSE == iter->IsDone())
{
@ -344,18 +341,16 @@ NS_IMETHODIMP DeleteRangeTxn::CreateTxnsToDeleteNodesBetween()
nsCOMPtr<nsIContent> content;
result = iter->CurrentNode(getter_AddRefs(content));
node = do_QueryInterface(content);
if ((NS_SUCCEEDED(result)) && node)
{
DeleteElementTxn *txn;
result = TransactionFactory::GetNewTransaction(DeleteElementTxn::GetCID(), (EditTxn **)&txn);
if (nsnull!=txn)
{
txn->Init(node);
AppendChild(txn);
}
else
return NS_ERROR_NULL_POINTER;
}
if (NS_FAILED(result)) return result;
if (!node) return NS_ERROR_NULL_POINTER;
DeleteElementTxn *txn;
result = TransactionFactory::GetNewTransaction(DeleteElementTxn::GetCID(), (EditTxn **)&txn);
if (NS_FAILED(result)) return result;
if (!txn) return NS_ERROR_NULL_POINTER;
txn->Init(node);
AppendChild(txn);
iter->Next();
}
return result;

Просмотреть файл

@ -41,6 +41,8 @@ NS_IMETHODIMP DeleteTextTxn::Init(nsIEditor *aEditor,
PRUint32 aNumCharsToDelete)
{
NS_ASSERTION(aEditor&&aElement, "bad arg");
if (!aEditor || !aElement) { return NS_ERROR_NULL_POINTER; }
mEditor = aEditor;
mElement = do_QueryInterface(aElement);
mOffset = aOffset;
@ -57,22 +59,20 @@ NS_IMETHODIMP DeleteTextTxn::Init(nsIEditor *aEditor,
NS_IMETHODIMP DeleteTextTxn::Do(void)
{
if (gNoisy) { printf("Do Delete Text\n"); }
nsresult result = NS_ERROR_NULL_POINTER;
if (mEditor && mElement)
NS_ASSERTION(mEditor && mElement, "bad state");
if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
// get the text that we're about to delete
nsresult result = mElement->SubstringData(mOffset, mNumCharsToDelete, mDeletedText);
NS_ASSERTION(NS_SUCCEEDED(result), "could not get text to delete.");
result = mElement->DeleteData(mOffset, mNumCharsToDelete);
if (NS_SUCCEEDED(result))
{
// get the text that we're about to delete
result = mElement->SubstringData(mOffset, mNumCharsToDelete, mDeletedText);
NS_ASSERTION(NS_SUCCEEDED(result), "could not get text to delete.");
result = mElement->DeleteData(mOffset, mNumCharsToDelete);
if (NS_SUCCEEDED(result))
{
nsCOMPtr<nsIDOMSelection> selection;
nsresult selectionResult = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_SUCCEEDED(selectionResult) && selection) {
selectionResult = selection->Collapse(mElement, mOffset);
NS_ASSERTION((NS_SUCCEEDED(selectionResult)), "selection could not be collapsed after undo of insert.");
}
}
nsCOMPtr<nsIDOMSelection> selection;
result = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
result = selection->Collapse(mElement, mOffset);
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of insert.");
}
return result;
}
@ -82,19 +82,19 @@ NS_IMETHODIMP DeleteTextTxn::Do(void)
NS_IMETHODIMP DeleteTextTxn::Undo(void)
{
if (gNoisy) { printf("Undo Delete Text\n"); }
nsresult result = NS_ERROR_NULL_POINTER;
if (mEditor && mElement)
NS_ASSERTION(mEditor && mElement, "bad state");
if (!mEditor || !mElement) { return NS_ERROR_NOT_INITIALIZED; }
nsresult result;
result = mElement->InsertData(mOffset, mDeletedText);
if (NS_SUCCEEDED(result))
{
result = mElement->InsertData(mOffset, mDeletedText);
if (NS_SUCCEEDED(result))
{
nsCOMPtr<nsIDOMSelection> selection;
nsresult selectionResult = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_SUCCEEDED(selectionResult) && selection) {
selectionResult = selection->Collapse(mElement, mOffset);
NS_ASSERTION((NS_SUCCEEDED(selectionResult)), "selection could not be collapsed after undo of insert.");
}
}
nsCOMPtr<nsIDOMSelection> selection;
nsresult selectionResult = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
result = selection->Collapse(mElement, mOffset);
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of insert.");
}
return result;
}

Просмотреть файл

@ -64,8 +64,7 @@ NS_IMETHODIMP InsertElementTxn::Do(void)
printf("%p Do Insert Element of %p into parent %p at offset %d\n",
this, nodeAsContent.get(), parentAsContent.get(), mOffset);
}
if (!mNode || !mParent)
return NS_ERROR_NULL_POINTER;
if (!mNode || !mParent) return NS_ERROR_NOT_INITIALIZED;
nsresult result;
nsCOMPtr<nsIDOMNode>refNode;
@ -92,16 +91,16 @@ NS_IMETHODIMP InsertElementTxn::Do(void)
nsCOMPtr<nsIDOMNode> resultNode;
result = mParent->InsertBefore(mNode, refNode, getter_AddRefs(resultNode));
if (NS_SUCCEEDED(result) && resultNode)
{
nsCOMPtr<nsIDOMSelection> selection;
result = mEditor->GetSelection(getter_AddRefs(selection));
if ((NS_SUCCEEDED(result)) && selection)
{ // place the selection just after the inserted element
selection->Collapse(mParent, mOffset+1);
//selection->Extend(mParent, mOffset+1);
}
}
if (NS_FAILED(result)) return result;
if (!resultNode) return NS_ERROR_NULL_POINTER;
nsCOMPtr<nsIDOMSelection> selection;
result = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
// place the selection just after the inserted element
selection->Collapse(mParent, mOffset+1);
//selection->Extend(mParent, mOffset+1);
return result;
}
@ -109,8 +108,7 @@ NS_IMETHODIMP InsertElementTxn::Undo(void)
{
if (gNoisy) { printf("%p Undo Insert Element of %p into parent %p at offset %d\n",
this, mNode.get(), mParent.get(), mOffset); }
if (!mNode || !mParent)
return NS_ERROR_NULL_POINTER;
if (!mNode || !mParent) return NS_ERROR_NOT_INITIALIZED;
nsCOMPtr<nsIDOMNode> resultNode;
nsresult result = mParent->RemoveChild(mNode, getter_AddRefs(resultNode));

Просмотреть файл

@ -62,6 +62,9 @@ NS_IMETHODIMP InsertTextTxn::Init(nsIDOMCharacterData *aElement,
printf("\n");
#endif
NS_ASSERTION(aElement&&aPresShell, "bad args");
if (!aElement || !aPresShell) return NS_ERROR_NULL_POINTER;
mElement = do_QueryInterface(aElement);
mOffset = aOffset;
mStringToInsert = aStringToInsert;
@ -72,16 +75,19 @@ NS_IMETHODIMP InsertTextTxn::Init(nsIDOMCharacterData *aElement,
NS_IMETHODIMP InsertTextTxn::Do(void)
{
if (gNoisy) { printf("Do Insert Text element = %p\n", mElement.get()); }
NS_ASSERTION(mElement && mPresShell, "bad state");
if (!mElement || !mPresShell) { return NS_ERROR_NOT_INITIALIZED; }
// advance caret: This requires the presentation shell to get the selection.
nsCOMPtr<nsIDOMSelection> selection;
nsresult result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection));
NS_ASSERTION(selection,"Could not get selection in InsertTextTxn::Do\n");
if (NS_SUCCEEDED(result) && selection) {
result = mElement->InsertData(mOffset, mStringToInsert);
if (NS_SUCCEEDED(result)) {
result = selection->Collapse(mElement, mOffset+mStringToInsert.Length());
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after insert.");
}
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
result = mElement->InsertData(mOffset, mStringToInsert);
if (NS_SUCCEEDED(result))
{
result = selection->Collapse(mElement, mOffset+mStringToInsert.Length());
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after insert.");
}
return result;
}
@ -89,6 +95,9 @@ NS_IMETHODIMP InsertTextTxn::Do(void)
NS_IMETHODIMP InsertTextTxn::Undo(void)
{
if (gNoisy) { printf("Undo Insert Text element = %p\n", mElement.get()); }
NS_ASSERTION(mElement && mPresShell, "bad state");
if (!mElement || !mPresShell) { return NS_ERROR_NOT_INITIALIZED; }
nsresult result;
PRUint32 length = mStringToInsert.Length();
result = mElement->DeleteData(mOffset, length);
@ -96,10 +105,10 @@ NS_IMETHODIMP InsertTextTxn::Undo(void)
{ // set the selection to the insertion point where the string was removed
nsCOMPtr<nsIDOMSelection> selection;
result = mPresShell->GetSelection(SELECTION_NORMAL, getter_AddRefs(selection));
if (NS_SUCCEEDED(result) && selection) {
result = selection->Collapse(mElement, mOffset);
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of insert.");
}
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
result = selection->Collapse(mElement, mOffset);
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after undo of insert.");
}
return result;
}

Просмотреть файл

@ -37,6 +37,8 @@ NS_IMETHODIMP JoinElementTxn::Init(nsIEditor *aEditor,
nsIDOMNode *aLeftNode,
nsIDOMNode *aRightNode)
{
NS_PRECONDITION((aEditor && aLeftNode && aRightNode), "null arg");
if (!aEditor || !aLeftNode || !aRightNode) { return NS_ERROR_NULL_POINTER; }
mEditor = aEditor;
mLeftNode = do_QueryInterface(aLeftNode);
mRightNode = do_QueryInterface(aRightNode);
@ -52,54 +54,54 @@ JoinElementTxn::~JoinElementTxn()
NS_IMETHODIMP JoinElementTxn::Do(void)
{
if (gNoisy) { printf("%p Do Join of %p and %p\n", this, mLeftNode.get(), mRightNode.get()); }
nsresult result;
NS_PRECONDITION((mEditor && mLeftNode && mRightNode), "null arg");
if (!mEditor || !mLeftNode || !mRightNode) { return NS_ERROR_NOT_INITIALIZED; }
if ((mLeftNode) && (mRightNode))
{ // get the parent node
nsCOMPtr<nsIDOMNode>leftParent;
result = mLeftNode->GetParentNode(getter_AddRefs(leftParent));
if ((NS_SUCCEEDED(result)) && (leftParent))
{ // verify that mLeftNode and mRightNode have the same parent
nsCOMPtr<nsIDOMNode>rightParent;
result = mRightNode->GetParentNode(getter_AddRefs(rightParent));
if ((NS_SUCCEEDED(result)) && (rightParent))
{
if (leftParent==rightParent)
{
mParent= do_QueryInterface(leftParent); // set this instance mParent.
// Other methods will see a non-null mParent and know all is well
nsCOMPtr<nsIDOMNodeList> childNodes;
result = mLeftNode->GetChildNodes(getter_AddRefs(childNodes));
if ((NS_SUCCEEDED(result)) && (childNodes)) {
childNodes->GetLength(&mOffset);
}
else
{
nsCOMPtr<nsIDOMCharacterData> leftNodeAsText;
leftNodeAsText = do_QueryInterface(mLeftNode);
if (leftNodeAsText) {
leftNodeAsText->GetLength(&mOffset);
}
}
result = nsEditor::JoinNodesImpl(mRightNode, mLeftNode, mParent, PR_FALSE);
if (NS_SUCCEEDED(result))
{
if (gNoisy) { printf(" left node = %p removed\n", mLeftNode.get()); }
nsCOMPtr<nsIDOMSelection>selection;
mEditor->GetSelection(getter_AddRefs(selection));
if (selection)
{
selection->Collapse(mRightNode, mOffset);
}
}
}
else
{
NS_ASSERTION(PR_FALSE, "2 nodes do not have same parent");
return NS_ERROR_INVALID_ARG;
}
// get the parent node
nsCOMPtr<nsIDOMNode>leftParent;
nsresult result = mLeftNode->GetParentNode(getter_AddRefs(leftParent));
if (NS_FAILED(result)) return result;
if (!leftParent) return NS_ERROR_NULL_POINTER;
// verify that mLeftNode and mRightNode have the same parent
nsCOMPtr<nsIDOMNode>rightParent;
result = mRightNode->GetParentNode(getter_AddRefs(rightParent));
if (NS_FAILED(result)) return result;
if (!rightParent) return NS_ERROR_NULL_POINTER;
if (leftParent==rightParent)
{
mParent= do_QueryInterface(leftParent); // set this instance mParent.
// Other methods will see a non-null mParent and know all is well
nsCOMPtr<nsIDOMNodeList> childNodes;
result = mLeftNode->GetChildNodes(getter_AddRefs(childNodes));
if (NS_FAILED(result)) return result;
if (childNodes) {
childNodes->GetLength(&mOffset);
}
else
{
nsCOMPtr<nsIDOMCharacterData> leftNodeAsText;
leftNodeAsText = do_QueryInterface(mLeftNode);
if (leftNodeAsText) {
leftNodeAsText->GetLength(&mOffset);
}
}
result = nsEditor::JoinNodesImpl(mRightNode, mLeftNode, mParent, PR_FALSE);
if (NS_SUCCEEDED(result))
{
if (gNoisy) { printf(" left node = %p removed\n", mLeftNode.get()); }
nsCOMPtr<nsIDOMSelection>selection;
result = mEditor->GetSelection(getter_AddRefs(selection));
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
selection->Collapse(mRightNode, mOffset);
}
}
else
{
NS_ASSERTION(PR_FALSE, "2 nodes do not have same parent");
return NS_ERROR_INVALID_ARG;
}
return result;
}
@ -110,9 +112,7 @@ NS_IMETHODIMP JoinElementTxn::Undo(void)
{
if (gNoisy) { printf("%p Undo Join, right node = %p\n", this, mRightNode.get()); }
NS_ASSERTION(mRightNode && mLeftNode && mParent, "bad state");
if (!mRightNode || !mLeftNode || !mParent) {
return NS_ERROR_NOT_INITIALIZED;
}
if (!mRightNode || !mLeftNode || !mParent) { return NS_ERROR_NOT_INITIALIZED; }
nsresult result;
nsCOMPtr<nsIDOMNode>resultNode;
// first, massage the existing node so it is in its post-split state

Просмотреть файл

@ -39,6 +39,9 @@ NS_IMETHODIMP SplitElementTxn::Init(nsIEditor *aEditor,
nsIDOMNode *aNode,
PRInt32 aOffset)
{
NS_ASSERTION(aEditor && aNode, "bad args");
if (!aEditor || !aNode) { return NS_ERROR_NOT_INITIALIZED; }
mEditor = aEditor;
mExistingRightNode = do_QueryInterface(aNode);
mOffset = aOffset;
@ -52,36 +55,33 @@ SplitElementTxn::~SplitElementTxn()
NS_IMETHODIMP SplitElementTxn::Do(void)
{
if (gNoisy) { printf("%p Do Split of node %p offset %d\n", this, mExistingRightNode.get(), mOffset); }
NS_ASSERTION(mExistingRightNode, "bad state");
if (!mExistingRightNode) {
return NS_ERROR_NOT_INITIALIZED;
}
NS_ASSERTION(mExistingRightNode && mEditor, "bad state");
if (!mExistingRightNode || !mEditor) { return NS_ERROR_NOT_INITIALIZED; }
// create a new node
nsresult result = mExistingRightNode->CloneNode(PR_FALSE, getter_AddRefs(mNewLeftNode));
NS_ASSERTION(((NS_SUCCEEDED(result)) && (mNewLeftNode)), "could not create element.");
if (NS_FAILED(result)) return result;
if (!mNewLeftNode) return NS_ERROR_NULL_POINTER;
if ((NS_SUCCEEDED(result)) && (mNewLeftNode))
if (gNoisy) { printf(" created left node = %p\n", mNewLeftNode.get()); }
// get the parent node
result = mExistingRightNode->GetParentNode(getter_AddRefs(mParent));
if (NS_FAILED(result)) return result;
if (!mParent) return NS_ERROR_NULL_POINTER;
// insert the new node
result = nsEditor::SplitNodeImpl(mExistingRightNode, mOffset, mNewLeftNode, mParent);
if (NS_SUCCEEDED(result) && mNewLeftNode)
{
if (gNoisy) { printf(" created left node = %p\n", mNewLeftNode.get()); }
// get the parent node
result = mExistingRightNode->GetParentNode(getter_AddRefs(mParent));
// insert the new node
if ((NS_SUCCEEDED(result)) && (mParent))
{
result = nsEditor::SplitNodeImpl(mExistingRightNode, mOffset, mNewLeftNode, mParent);
if (NS_SUCCEEDED(result) && mNewLeftNode)
{
nsCOMPtr<nsIDOMSelection>selection;
mEditor->GetSelection(getter_AddRefs(selection));
if (selection)
{
selection->Collapse(mNewLeftNode, mOffset);
}
}
else {
result = NS_ERROR_NOT_IMPLEMENTED;
}
}
nsCOMPtr<nsIDOMSelection>selection;
mEditor->GetSelection(getter_AddRefs(selection));
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
result = selection->Collapse(mNewLeftNode, mOffset);
}
else {
result = NS_ERROR_NOT_IMPLEMENTED;
}
return result;
}
@ -92,8 +92,8 @@ NS_IMETHODIMP SplitElementTxn::Undo(void)
printf("%p Undo Split of existing node %p and new node %p offset %d\n",
this, mExistingRightNode.get(), mNewLeftNode.get(), mOffset);
}
NS_ASSERTION(mExistingRightNode && mNewLeftNode && mParent, "bad state");
if (!mExistingRightNode || !mNewLeftNode || !mParent) {
NS_ASSERTION(mEditor && mExistingRightNode && mNewLeftNode && mParent, "bad state");
if (!mEditor || !mExistingRightNode || !mNewLeftNode || !mParent) {
return NS_ERROR_NOT_INITIALIZED;
}
@ -110,10 +110,9 @@ NS_IMETHODIMP SplitElementTxn::Undo(void)
if (gNoisy) { printf(" left node = %p removed\n", mNewLeftNode.get()); }
nsCOMPtr<nsIDOMSelection>selection;
mEditor->GetSelection(getter_AddRefs(selection));
if (selection)
{
selection->Collapse(mExistingRightNode, mOffset);
}
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
result = selection->Collapse(mExistingRightNode, mOffset);
}
else {
result = NS_ERROR_NOT_IMPLEMENTED;
@ -126,8 +125,8 @@ NS_IMETHODIMP SplitElementTxn::Undo(void)
*/
NS_IMETHODIMP SplitElementTxn::Redo(void)
{
NS_ASSERTION(mExistingRightNode && mNewLeftNode && mParent, "bad state");
if (!mExistingRightNode || !mNewLeftNode || !mParent) {
NS_ASSERTION(mEditor && mExistingRightNode && mNewLeftNode && mParent, "bad state");
if (!mEditor || !mExistingRightNode || !mNewLeftNode || !mParent) {
return NS_ERROR_NOT_INITIALIZED;
}
if (gNoisy) {
@ -146,7 +145,7 @@ NS_IMETHODIMP SplitElementTxn::Redo(void)
if (gNoisy)
{
printf("** after delete of text in right text node %p offset %d\n", rightNodeAsText.get(), mOffset);
if (gNoisy) {mEditor->DebugDumpContent(); } // DEBUG
mEditor->DebugDumpContent(); // DEBUG
}
}
else

Просмотреть файл

@ -239,6 +239,7 @@ nsEditor::Init(nsIDOMDocument *aDoc, nsIPresShell* aPresShell, PRUint32 aFlags)
// disable links
nsCOMPtr<nsIPresContext> context;
mPresShell->GetPresContext(getter_AddRefs(context));
if (!context) return NS_ERROR_NULL_POINTER;
context->SetLinkHandler(0);
// Set up the DTD
@ -252,9 +253,9 @@ nsEditor::Init(nsIDOMDocument *aDoc, nsIPresShell* aPresShell, PRUint32 aFlags)
if (!mEditProperty) {return NS_ERROR_NULL_POINTER;}
mPresShell->GetViewManager(&mViewManager);
if (mViewManager){
mViewManager->Release(); //we want a weak link
}
if (!mViewManager) {return NS_ERROR_NULL_POINTER;}
mViewManager->Release(); //we want a weak link
mPresShell->SetDisplayNonTextSelection(PR_TRUE);//we want to see all the selection reflected to user
mUpdateCount=0;
InsertTextTxn::ClassInit();
@ -267,22 +268,25 @@ nsEditor::Init(nsIDOMDocument *aDoc, nsIPresShell* aPresShell, PRUint32 aFlags)
mIMEBufferLength = 0;
/* Show the caret */
// XXX: I suppose it's legal to fail to get a caret, so don't propogate the
// result of GetCaret
nsCOMPtr<nsICaret> caret;
if (NS_SUCCEEDED(mPresShell->GetCaret(getter_AddRefs(caret))) && caret)
{
caret->SetCaretVisible(PR_TRUE);
caret->SetCaretReadOnly(PR_FALSE);
}
// NOTE: We don't fail if we can't get prefs or string bundles
// since we could still be used as the text edit widget without prefs
// Get the prefs service (Note: can't use nsCOMPtr for service pointers)
result = nsServiceManager::GetService(kPrefCID,
nsIPref::GetIID(),
(nsISupports**)&mPrefs);
if (NS_FAILED(result) || !mPrefs)
nsresult ignoredResult = nsServiceManager::GetService(kPrefCID,
nsIPref::GetIID(),
(nsISupports**)&mPrefs);
if (NS_FAILED(ignoredResult) || !mPrefs)
{
printf("ERROR: Failed to get Prefs Service instance.\n");
if (gNoisy) { printf("ERROR: Failed to get Prefs Service instance.\n");}
}
// TODO: Cache basic preferences?
@ -290,20 +294,21 @@ nsEditor::Init(nsIDOMDocument *aDoc, nsIPresShell* aPresShell, PRUint32 aFlags)
// respond to while running
nsIStringBundleService* service;
result = nsServiceManager::GetService(kStringBundleServiceCID,
nsIStringBundleService::GetIID(),
(nsISupports**)&service);
ignoredResult = nsServiceManager::GetService(kStringBundleServiceCID,
nsIStringBundleService::GetIID(),
(nsISupports**)&service);
if (NS_SUCCEEDED(result) && service)
if (NS_SUCCEEDED(ignoredResult) && service)
{
nsILocale* locale = nsnull;
result = service->CreateBundle(EDITOR_BUNDLE_URL, locale,
getter_AddRefs(mStringBundle));
ignoredResult = service->CreateBundle(EDITOR_BUNDLE_URL, locale,
getter_AddRefs(mStringBundle));
// We don't need to keep service around once we created the bundle
nsServiceManager::ReleaseService(kStringBundleServiceCID, service);
} else {
printf("ERROR: Failed to get StringBundle Service instance.\n");
if (gNoisy) printf("ERROR: Failed to get StringBundle Service instance.\n");
}
/*
Example of getting a string:
nsString value;
@ -374,8 +379,8 @@ NS_IMETHODIMP nsEditor::SaveDocument(PRBool saveAs, PRBool saveCopy)
// get the document
nsCOMPtr<nsIDOMDocument> doc;
rv = GetDocument(getter_AddRefs(doc));
if (NS_FAILED(rv) || !doc)
return rv;
if (NS_FAILED(rv)) return rv;
if (!doc) return NS_ERROR_NULL_POINTER;
nsCOMPtr<nsIDiskDocument> diskDoc = do_QueryInterface(doc);
if (!diskDoc)
@ -1238,7 +1243,6 @@ nsEditor::SetCompositionString(const nsString& aCompositionString, nsIPrivateTex
return result;
}
#ifdef XP_MAC
#pragma mark -
#pragma mark --- public nsEditor methods ---

Просмотреть файл

@ -387,7 +387,7 @@ public:
* @param aNodeToKeep The node that will remain after the join.
* @param aNodeToJoin The node that will be joined with aNodeToKeep.
* There is no requirement that the two nodes be of the same type.
* @param aParent The parent of aExistingRightNode
* @param aParent The parent of aNodeToKeep
* @param aNodeToKeepIsFirst if PR_TRUE, the contents|children of aNodeToKeep come before the
* contents|children of aNodeToJoin, otherwise their positions are switched.
*/

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -192,7 +192,15 @@ protected:
virtual void InitRules();
NS_IMETHOD GetLayoutObject(nsIDOMNode *aNode, nsISupports **aLayoutObject);
/** returns the layout object (nsIFrame in the real world) for aNode
* @param aNode the content to get a frame for
* @param aLayoutObject the "primary frame" for aNode, if one exists. May be null
* @return NS_OK whether a frame is found or not
* an error if some serious error occurs
*/
NS_IMETHOD GetLayoutObject(nsIDOMNode *aInNode, nsISupports **aOutLayoutObject);
NS_IMETHOD CollapseAdjacentTextNodes(nsIDOMSelection *aInSelection);
NS_IMETHOD DeleteSelectionAndPrepareToCreateNode(nsCOMPtr<nsIDOMNode> &parentSelectedNode, PRInt32& offsetOfNewNode);
@ -382,10 +390,10 @@ protected:
const nsString *aAttribute,
const nsString *aValue);
void GetTextSelectionOffsetsForRange(nsIDOMSelection *aSelection,
nsIDOMNode **aParent,
PRInt32 &aStartOffset,
PRInt32 &aEndOffset);
NS_IMETHOD GetTextSelectionOffsetsForRange(nsIDOMSelection *aSelection,
nsIDOMNode **aParent,
PRInt32 &aStartOffset,
PRInt32 &aEndOffset);
void ResetTextSelectionForRange(nsIDOMNode *aParent,
PRInt32 aStartOffset,

Просмотреть файл

@ -47,12 +47,18 @@
#include "nsIDragSession.h"
#include "nsITransferable.h"
#include "nsIFormatConverter.h"
#include "nsIContentIterator.h"
#include "nsIContent.h"
#include "nsLayoutCID.h"
// Drag & Drop, Clipboard Support
static NS_DEFINE_CID(kCDragServiceCID, NS_DRAGSERVICE_CID);
static NS_DEFINE_CID(kCTransferableCID, NS_TRANSFERABLE_CID);
static NS_DEFINE_IID(kCDataFlavorCID, NS_DATAFLAVOR_CID);
static NS_DEFINE_IID(kCXIFFormatConverterCID, NS_XIFFORMATCONVERTER_CID);
static NS_DEFINE_IID(kContentIteratorCID, NS_CONTENTITERATOR_CID);
static NS_DEFINE_CID(kCClipboardCID, NS_CLIPBOARD_CID);
static NS_DEFINE_IID(kCXIFConverterCID, NS_XIFFORMATCONVERTER_CID);
/*
@ -116,8 +122,7 @@ nsresult
nsTextEditorKeyListener::KeyDown(nsIDOMEvent* aKeyEvent)
{
PRUint32 keyCode;
PRBool isShift;
PRBool ctrlKey;
PRBool isShift, ctrlKey, altKey, metaKey;
nsCOMPtr<nsIDOMUIEvent>uiEvent;
uiEvent = do_QueryInterface(aKeyEvent);
@ -126,87 +131,87 @@ nsTextEditorKeyListener::KeyDown(nsIDOMEvent* aKeyEvent)
return NS_OK;
}
if (NS_SUCCEEDED(uiEvent->GetKeyCode(&keyCode)) &&
NS_SUCCEEDED(uiEvent->GetShiftKey(&isShift)) &&
NS_SUCCEEDED(uiEvent->GetCtrlKey(&ctrlKey))
) {
PRBool keyProcessed;
ProcessShortCutKeys(aKeyEvent, keyProcessed);
if (PR_FALSE==keyProcessed)
{
switch(keyCode) {
// case nsIDOMUIEvent::VK_BACK:
// mEditor->DeleteSelection(nsIEditor::eDeleteLeft);
// break;
PRBool keyProcessed;
ProcessShortCutKeys(aKeyEvent, keyProcessed);
if (PR_FALSE==keyProcessed)
{
if (NS_SUCCEEDED(uiEvent->GetKeyCode(&keyCode)) &&
NS_SUCCEEDED(uiEvent->GetShiftKey(&isShift)) &&
NS_SUCCEEDED(uiEvent->GetCtrlKey(&ctrlKey)) &&
NS_SUCCEEDED(uiEvent->GetAltKey(&altKey)) &&
NS_SUCCEEDED(uiEvent->GetMetaKey(&metaKey)))
{
{
switch(keyCode) {
// case nsIDOMUIEvent::VK_BACK:
// mEditor->DeleteSelection(nsIEditor::eDeleteLeft);
// break;
case nsIDOMUIEvent::VK_DELETE:
mEditor->DeleteSelection(nsIEditor::eDeleteNext);
break;
case nsIDOMUIEvent::VK_DELETE:
mEditor->DeleteSelection(nsIEditor::eDeleteNext);
break;
// case nsIDOMUIEvent::VK_RETURN:
//case nsIDOMUIEvent::VK_ENTER: // why does this not exist?
// Need to implement creation of either <P> or <BR> nodes.
// mEditor->InsertBreak();
// break;
// case nsIDOMUIEvent::VK_RETURN:
//case nsIDOMUIEvent::VK_ENTER: // why does this not exist?
// Need to implement creation of either <P> or <BR> nodes.
// mEditor->InsertBreak();
// break;
case nsIDOMUIEvent::VK_LEFT:
case nsIDOMUIEvent::VK_RIGHT:
case nsIDOMUIEvent::VK_UP:
case nsIDOMUIEvent::VK_DOWN:
// these have already been handled in nsRangeList. Why are we getting them
// again here (Mac)? In switch to avoid putting in bogus chars.
case nsIDOMUIEvent::VK_LEFT:
case nsIDOMUIEvent::VK_RIGHT:
case nsIDOMUIEvent::VK_UP:
case nsIDOMUIEvent::VK_DOWN:
// these have already been handled in nsRangeList. Why are we getting them
// again here (Mac)? In switch to avoid putting in bogus chars.
//return NS_OK to allow page scrolling.
return NS_OK;
break;
//return NS_OK to allow page scrolling.
return NS_OK;
break;
case nsIDOMUIEvent::VK_HOME:
case nsIDOMUIEvent::VK_END:
// who handles these?
#if DEBUG
printf("Key not handled\n");
#endif
return NS_OK;
break;
case nsIDOMUIEvent::VK_HOME:
case nsIDOMUIEvent::VK_END:
// who handles these?
#if DEBUG
printf("Key not handled\n");
#endif
return NS_OK;
break;
case nsIDOMUIEvent::VK_PAGE_UP:
case nsIDOMUIEvent::VK_PAGE_DOWN:
//return NS_OK to allow page scrolling.
return NS_OK;
break;
case nsIDOMUIEvent::VK_PAGE_UP:
case nsIDOMUIEvent::VK_PAGE_DOWN:
//return NS_OK to allow page scrolling.
return NS_OK;
break;
case nsIDOMUIEvent::VK_TAB:
{
PRUint32 flags=0;
mEditor->GetFlags(&flags);
if (! (flags & nsIHTMLEditor::eEditorSingleLineMask))
{
PRBool ctrlKey, altKey, metaKey;
uiEvent->GetCtrlKey(&ctrlKey);
uiEvent->GetAltKey(&altKey);
uiEvent->GetMetaKey(&metaKey);
if (metaKey || altKey)
return NS_OK; // don't consume
// else we insert the tab straight through
nsAutoString key;
key += keyCode;
case nsIDOMUIEvent::VK_TAB:
{
PRUint32 flags=0;
mEditor->GetFlags(&flags);
if (! (flags & nsIHTMLEditor::eEditorSingleLineMask))
{
if (metaKey || altKey)
return NS_OK; // don't consume
// else we insert the tab straight through
nsAutoString key;
key += keyCode;
nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(mEditor);
if (htmlEditor)
htmlEditor->InsertText(key);
return NS_ERROR_BASE; // this means "I handled the event, don't do default processing"
}
else {
return NS_OK;
}
break;
}
nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(mEditor);
if (htmlEditor)
htmlEditor->InsertText(key);
return NS_ERROR_BASE; // this means "I handled the event, don't do default processing"
}
else {
return NS_OK;
}
break;
}
default:
return NS_OK; // this indicates that we have not handled the keyDown event in any way.
}
}
}
default:
return NS_OK; // this indicates that we have not handled the keyDown event in any way.
}
}
}
}
return NS_ERROR_BASE;
}
@ -238,19 +243,20 @@ nsTextEditorKeyListener::KeyPress(nsIDOMEvent* aKeyEvent)
// eaiser
//
PRBool ctrlKey, altKey, metaKey;
uiEvent->GetCtrlKey(&ctrlKey);
uiEvent->GetAltKey(&altKey);
uiEvent->GetMetaKey(&metaKey);
if (metaKey)
return NS_OK; // don't consume
nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(mEditor);
if (!htmlEditor) return NS_ERROR_NO_INTERFACE;
PRBool ctrlKey, altKey, metaKey;
uiEvent->GetCtrlKey(&ctrlKey);
uiEvent->GetAltKey(&altKey);
uiEvent->GetMetaKey(&metaKey);
if (metaKey)
return NS_OK; // don't consume
nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(mEditor);
if (!htmlEditor) return NS_ERROR_NO_INTERFACE;
if (NS_SUCCEEDED(uiEvent->GetKeyCode(&keyCode)))
{
if (nsIDOMUIEvent::VK_BACK==keyCode) {
if (nsIDOMUIEvent::VK_BACK==keyCode) {
mEditor->DeleteSelection(nsIEditor::eDeletePrevious);
return NS_ERROR_BASE; // consumed
}
@ -261,14 +267,14 @@ nsTextEditorKeyListener::KeyPress(nsIDOMEvent* aKeyEvent)
}
if ((PR_FALSE==altKey) && (PR_FALSE==ctrlKey) &&
(NS_SUCCEEDED(uiEvent->GetCharCode(&character))))
(NS_SUCCEEDED(uiEvent->GetCharCode(&character))))
{
if (nsIDOMUIEvent::VK_TAB==character) {
return NS_OK; // ignore tabs here, they're handled in keyDown if at all
}
if (nsIDOMUIEvent::VK_TAB==character) {
return NS_OK; // ignore tabs here, they're handled in keyDown if at all
}
key += character;
htmlEditor->InsertText(key);
}
}
return NS_ERROR_BASE; // consumed
@ -843,6 +849,62 @@ nsTextEditorMouseListener::HandleEvent(nsIDOMEvent* aEvent)
// XXX: evil global functions, move them to proper support module
nsresult
IsNodeInSelection(nsIDOMNode *aInNode, nsIDOMSelection *aInSelection, PRBool &aOutIsInSel)
{
aOutIsInSel = PR_FALSE; // init out-param
if (!aInNode || !aInSelection) { return NS_ERROR_NULL_POINTER; }
nsCOMPtr<nsIContentIterator>iter;
nsresult result = nsComponentManager::CreateInstance(kContentIteratorCID, nsnull,
nsIContentIterator::GetIID(),
getter_AddRefs(iter));
if (NS_FAILED(result)) { return result; }
if (!iter) { return NS_ERROR_OUT_OF_MEMORY; }
nsCOMPtr<nsIEnumerator> enumerator;
result = aInSelection->GetEnumerator(getter_AddRefs(enumerator));
if (NS_FAILED(result)) { return result; }
if (!enumerator) { return NS_ERROR_OUT_OF_MEMORY; }
for (enumerator->First(); NS_OK!=enumerator->IsDone(); enumerator->Next())
{
nsCOMPtr<nsISupports> currentItem;
result = enumerator->CurrentItem(getter_AddRefs(currentItem));
if ((NS_SUCCEEDED(result)) && (currentItem))
{
nsCOMPtr<nsIDOMRange> range( do_QueryInterface(currentItem) );
iter->Init(range);
nsCOMPtr<nsIContent> currentContent;
iter->CurrentNode(getter_AddRefs(currentContent));
while (NS_COMFALSE == iter->IsDone())
{
nsCOMPtr<nsIDOMNode>currentNode = do_QueryInterface(currentContent);
if (currentNode.get()==aInNode)
{
// if it's a start or end node, need to test whether the (x,y)
// of the event falls within the selection
// talk to Mike
aOutIsInSel = PR_TRUE;
return NS_OK;
}
/* do not check result here, and especially do not return the result code.
* we rely on iter->IsDone to tell us when the iteration is complete
*/
iter->Next();
iter->CurrentNode(getter_AddRefs(currentContent));
}
}
}
return NS_OK;
}
nsresult
nsTextEditorMouseListener::MouseDown(nsIDOMEvent* aMouseEvent)
{
@ -856,49 +918,141 @@ nsTextEditorMouseListener::MouseDown(nsIDOMEvent* aMouseEvent)
return NS_OK;
}
// We only do anything special for middle-mouse click (paste);
// ignore all other events.
nsCOMPtr<nsIEditor> editor (do_QueryInterface(mEditor));
if (!editor) { return NS_OK; }
// get the document
nsCOMPtr<nsIDOMDocument>domDoc;
editor->GetDocument(getter_AddRefs(domDoc));
if (!domDoc) { return NS_OK; }
nsCOMPtr<nsIDocument>doc = do_QueryInterface(domDoc);
if (!doc) { return NS_OK; }
PRUint16 button = 0;
uiEvent->GetButton(&button);
if (button != 2)
return NS_OK;
// left button click might be a drag-start
if (1==button)
{
#ifndef EXPERIMENTAL_DRAG_CODE
return NS_OK;
#else
nsString XIFBuffer;
// get the DOM select here
nsCOMPtr<nsIDOMSelection> sel;
editor->GetSelection(getter_AddRefs(sel));
// convert the DOMselection to XIF
if (sel)
{
// if we are within the selection, start the drag
nsCOMPtr<nsIEditor> editor (do_QueryInterface(mEditor));
if (!editor)
return NS_ERROR_FAILURE;
nsCOMPtr<nsIDOMNode> target;
nsresult rv = aMouseEvent->GetTarget(getter_AddRefs(target));
if (NS_FAILED(rv) || !target) { return NS_OK; }
PRBool isInSel;
rv = IsNodeInSelection(target, sel, isInSel);
if (NS_FAILED(rv) || PR_FALSE==isInSel) { return NS_OK; }
doc->CreateXIF(XIFBuffer, sel);
// Set the selection to the point under the mouse cursor:
nsCOMPtr<nsIDOMNSUIEvent> mouseEvent (do_QueryInterface(aMouseEvent));
// Get the Clipboard
nsIClipboard* clipboard;
rv = nsServiceManager::GetService(kCClipboardCID,
nsIClipboard::GetIID(),
(nsISupports **)&clipboard);
if (NS_OK == rv)
{
// Create a data flavor to tell the transferable
// that it is about to receive XIF
nsAutoString flavor(kXIFMime);
if (!mouseEvent)
return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event".
nsCOMPtr<nsIDOMNode> parent;
if (!NS_SUCCEEDED(mouseEvent->GetRangeParent(getter_AddRefs(parent))))
return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event".
PRInt32 offset = 0;
if (!NS_SUCCEEDED(mouseEvent->GetRangeOffset(&offset)))
return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event".
// Create a transferable for putting data on the Clipboard
nsCOMPtr<nsITransferable> trans;
rv = nsComponentManager::CreateInstance(kCTransferableCID, nsnull,
nsITransferable::GetIID(),
(void**) getter_AddRefs(trans));
if (NS_OK == rv) {
// The data on the clipboard will be in "XIF" format
// so give the clipboard transferable a "XIFConverter" for
// converting from XIF to other formats
nsCOMPtr<nsIFormatConverter> xifConverter;
rv = nsComponentManager::CreateInstance(kCXIFConverterCID, nsnull,
nsIFormatConverter::GetIID(), (void**) getter_AddRefs(xifConverter));
if (NS_OK == rv) {
// Add the XIF DataFlavor to the transferable
// this tells the transferable that it can handle receiving the XIF format
trans->AddDataFlavor(&flavor);
nsCOMPtr<nsIDOMSelection> selection;
if (NS_SUCCEEDED(editor->GetSelection(getter_AddRefs(selection))))
(void)selection->Collapse(parent, offset);
// Add the converter for going from XIF to other formats
trans->SetConverter(xifConverter);
// If the ctrl key is pressed, we'll do paste as quotation.
// Would've used the alt key, but the kde wmgr treats alt-middle specially.
PRBool ctrlKey = PR_FALSE;
uiEvent->GetCtrlKey(&ctrlKey);
// Now add the XIF data to the transferable
// the transferable wants the number bytes for the data and since it is double byte
// we multiply by 2
trans->SetTransferData(&flavor, XIFBuffer.ToNewUnicode(), XIFBuffer.Length()*2);
if (ctrlKey)
{
nsCOMPtr<nsIEditorMailSupport> mailEditor = do_QueryInterface(mEditor);
if (mailEditor)
mailEditor->PasteAsQuotation();
// Now invoke the drag session
nsIDragService* dragService;
nsresult rv = nsServiceManager::GetService(kCDragServiceCID,
nsIDragService::GetIID(),
(nsISupports **)&dragService);
if (NS_OK == rv) {
nsCOMPtr<nsISupportsArray> items;
NS_NewISupportsArray(getter_AddRefs(items));
if ( items ) {
items->AppendElement(trans);
dragService->InvokeDragSession(items, nsnull, nsIDragService::DRAGDROP_ACTION_COPY | nsIDragService::DRAGDROP_ACTION_MOVE);
}
nsServiceManager::ReleaseService(kCDragServiceCID, dragService);
}
}
}
nsServiceManager::ReleaseService(kCClipboardCID, clipboard);
}
return NS_ERROR_BASE; // return that we've handled the event
}
#endif
}
return editor->Paste();
// middle-mouse click (paste);
else if (button == 2)
{
// Set the selection to the point under the mouse cursor:
nsCOMPtr<nsIDOMNSUIEvent> mouseEvent (do_QueryInterface(aMouseEvent));
if (!mouseEvent)
return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event".
nsCOMPtr<nsIDOMNode> parent;
if (!NS_SUCCEEDED(mouseEvent->GetRangeParent(getter_AddRefs(parent))))
return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event".
PRInt32 offset = 0;
if (!NS_SUCCEEDED(mouseEvent->GetRangeOffset(&offset)))
return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event".
nsCOMPtr<nsIDOMSelection> selection;
if (NS_SUCCEEDED(editor->GetSelection(getter_AddRefs(selection))))
(void)selection->Collapse(parent, offset);
// If the ctrl key is pressed, we'll do paste as quotation.
// Would've used the alt key, but the kde wmgr treats alt-middle specially.
PRBool ctrlKey = PR_FALSE;
uiEvent->GetCtrlKey(&ctrlKey);
if (ctrlKey)
{
nsCOMPtr<nsIEditorMailSupport> mailEditor = do_QueryInterface(mEditor);
if (mailEditor)
mailEditor->PasteAsQuotation();
}
editor->Paste();
return NS_ERROR_BASE; // NS_ERROR_BASE means "We did process the event".
}
return NS_OK; // did not process the event
}
nsresult
nsTextEditorMouseListener::MouseUp(nsIDOMEvent* aMouseEvent)
{
@ -1425,9 +1579,6 @@ nsTextEditorFocusListener::HandleEvent(nsIDOMEvent* aEvent)
nsresult
nsTextEditorFocusListener::Focus(nsIDOMEvent* aEvent)
{
// turn on selection and caret
if (mEditor)
{
// turn on selection and caret
if (mEditor)
{
@ -1474,8 +1625,6 @@ nsTextEditorFocusListener::Focus(nsIDOMEvent* aEvent)
}
return NS_OK;
}
return NS_OK;
}
nsresult
nsTextEditorFocusListener::Blur(nsIDOMEvent* aEvent)

Просмотреть файл

@ -104,11 +104,10 @@ nsTextEditRules::SetFlags(PRUint32 aFlags)
// put a "white-space: pre" style on the body
nsCOMPtr<nsIDOMElement> bodyElement;
nsresult res = mEditor->GetBodyElement(getter_AddRefs(bodyElement));
if (NS_SUCCEEDED(res) && bodyElement)
{
// not going through the editor to do this.
bodyElement->SetAttribute("style", "white-space: pre");
}
if (NS_FAILED(res)) return res;
if (!bodyElement) return NS_ERROR_NULL_POINTER;
// not going through the editor to do this.
bodyElement->SetAttribute("style", "white-space: pre");
}
}
mFlags = aFlags;
@ -394,7 +393,9 @@ nsTextEditRules::CreateStyleForInsertText(nsIDOMSelection *aSelection, TypeInSta
// splitting anchor twice sets newTextNode as an empty text node between
// two halves of the original text node
result = mEditor->SplitNode(anchorAsText, offset, getter_AddRefs(newTextNode));
result = mEditor->SplitNode(anchorAsText, 0, getter_AddRefs(newTextNode));
if (NS_SUCCEEDED(result)) {
result = mEditor->SplitNode(anchorAsText, 0, getter_AddRefs(newTextNode));
}
}
}
// now we have the new text node we are going to insert into.
@ -464,37 +465,30 @@ nsTextEditRules::CreateStyleForInsertText(nsIDOMSelection *aSelection, TypeInSta
}
else // we have no selection, so insert a style tag in the body
{
nsCOMPtr<nsIDOMDocument>doc;
mEditor->GetDocument(getter_AddRefs(doc));
nsCOMPtr<nsIDOMNodeList>nodeList;
nsAutoString bodyTag = "body";
result = doc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList));
if ((NS_SUCCEEDED(result)) && nodeList)
{
PRUint32 count;
nodeList->GetLength(&count);
NS_ASSERTION(1==count, "there is not exactly 1 body in the document!");
nsCOMPtr<nsIDOMNode>bodyNode;
result = nodeList->Item(0, getter_AddRefs(bodyNode));
if ((NS_SUCCEEDED(result)) && bodyNode)
{ // now we've got the body tag. insert the style tag
if (aTypeInState.IsSet(NS_TYPEINSTATE_BOLD))
{
if (PR_TRUE==aTypeInState.GetBold()) {
InsertStyleAndNewTextNode(bodyNode, nsIEditProperty::b, aSelection);
}
nsCOMPtr<nsIDOMElement> bodyElement;
nsresult res = mEditor->GetBodyElement(getter_AddRefs(bodyElement));
if (NS_FAILED(res)) return res;
if (!bodyElement) return NS_ERROR_NULL_POINTER;
nsCOMPtr<nsIDOMNode>bodyNode = do_QueryInterface(bodyElement);
if (bodyNode)
{ // now we've got the body tag. insert the style tag
if (aTypeInState.IsSet(NS_TYPEINSTATE_BOLD))
{
if (PR_TRUE==aTypeInState.GetBold()) {
InsertStyleAndNewTextNode(bodyNode, nsIEditProperty::b, aSelection);
}
if (aTypeInState.IsSet(NS_TYPEINSTATE_ITALIC))
{
if (PR_TRUE==aTypeInState.GetItalic()) {
InsertStyleAndNewTextNode(bodyNode, nsIEditProperty::i, aSelection);
}
}
if (aTypeInState.IsSet(NS_TYPEINSTATE_ITALIC))
{
if (PR_TRUE==aTypeInState.GetItalic()) {
InsertStyleAndNewTextNode(bodyNode, nsIEditProperty::i, aSelection);
}
if (aTypeInState.IsSet(NS_TYPEINSTATE_UNDERLINE))
{
if (PR_TRUE==aTypeInState.GetUnderline()) {
InsertStyleAndNewTextNode(bodyNode, nsIEditProperty::u, aSelection);
}
}
if (aTypeInState.IsSet(NS_TYPEINSTATE_UNDERLINE))
{
if (PR_TRUE==aTypeInState.GetUnderline()) {
InsertStyleAndNewTextNode(bodyNode, nsIEditProperty::u, aSelection);
}
}
}
@ -513,12 +507,11 @@ nsTextEditRules::CreateFontStyleForInsertText(nsIDOMNode *aNewTextNode,
if (0!=aValue.Length())
{
result = InsertStyleNode(aNewTextNode, nsIEditProperty::font, aSelection, getter_AddRefs(newStyleNode));
if (NS_SUCCEEDED(result) && newStyleNode)
{
nsCOMPtr<nsIDOMElement>element = do_QueryInterface(newStyleNode);
if (element) {
result = mEditor->SetAttribute(element, aAttr, aValue);
}
if (NS_FAILED(result)) return result;
if (!newStyleNode) return NS_ERROR_NULL_POINTER;
nsCOMPtr<nsIDOMElement>element = do_QueryInterface(newStyleNode);
if (element) {
result = mEditor->SetAttribute(element, aAttr, aValue);
}
}
else
@ -540,21 +533,26 @@ nsTextEditRules::InsertStyleNode(nsIDOMNode *aNode,
nsresult result;
nsCOMPtr<nsIDOMNode>parent;
aNode->GetParentNode(getter_AddRefs(parent));
if (NS_FAILED(result)) return result;
if (!parent) return NS_ERROR_NULL_POINTER;
PRInt32 offsetInParent;
nsEditor::GetChildOffset(aNode, parent, offsetInParent);
result = nsEditor::GetChildOffset(aNode, parent, offsetInParent);
if (NS_FAILED(result)) return result;
nsAutoString tag;
aTag->ToString(tag);
result = mEditor->CreateNode(tag, parent, offsetInParent, aNewNode);
if ((NS_SUCCEEDED(result)) && *aNewNode)
if (NS_FAILED(result)) return result;
if (!aNewNode) return NS_ERROR_NULL_POINTER;
result = mEditor->DeleteNode(aNode);
if (NS_SUCCEEDED(result))
{
result = mEditor->DeleteNode(aNode);
if (NS_SUCCEEDED(result))
{
result = mEditor->InsertNode(aNode, *aNewNode, 0);
if (NS_SUCCEEDED(result)) {
if (aSelection) {
aSelection->Collapse(aNode, 0);
}
result = mEditor->InsertNode(aNode, *aNewNode, 0);
if (NS_SUCCEEDED(result)) {
if (aSelection) {
result = aSelection->Collapse(aNode, 0);
}
}
}
@ -575,16 +573,17 @@ nsTextEditRules::InsertStyleAndNewTextNode(nsIDOMNode *aParentNode, nsIAtom *aTa
nsCOMPtr<nsIDOMNode>anchor;
PRInt32 offset;
result = aSelection->GetAnchorNode(getter_AddRefs(anchor));
if (NS_SUCCEEDED(result) && NS_SUCCEEDED(aSelection->GetAnchorOffset(&offset)) && anchor)
if (NS_FAILED(result)) return result;
if (!anchor) return NS_ERROR_NULL_POINTER;
result = aSelection->GetAnchorOffset(&offset);
if (NS_FAILED(result)) return result;
nsCOMPtr<nsIDOMCharacterData>anchorAsText;
anchorAsText = do_QueryInterface(anchor);
if (anchorAsText)
{
nsCOMPtr<nsIDOMCharacterData>anchorAsText;
anchorAsText = do_QueryInterface(anchor);
if (anchorAsText)
{
nsCOMPtr<nsIDOMNode> newStyleNode;
result = InsertStyleNode(anchor, aTag, aSelection, getter_AddRefs(newStyleNode));
return result;
}
nsCOMPtr<nsIDOMNode> newStyleNode;
result = InsertStyleNode(anchor, aTag, aSelection, getter_AddRefs(newStyleNode));
return result;
}
}
// if we get here, there is no selected text node so we create one.
@ -593,15 +592,15 @@ nsTextEditRules::InsertStyleAndNewTextNode(nsIDOMNode *aParentNode, nsIAtom *aTa
nsCOMPtr<nsIDOMNode>newStyleNode;
nsCOMPtr<nsIDOMNode>newTextNode;
result = mEditor->CreateNode(tag, aParentNode, 0, getter_AddRefs(newStyleNode));
if (NS_SUCCEEDED(result))
{
result = mEditor->CreateNode(nsEditor::GetTextNodeTag(), newStyleNode, 0, getter_AddRefs(newTextNode));
if (NS_SUCCEEDED(result))
{
if (aSelection) {
aSelection->Collapse(newTextNode, 0);
}
}
if (NS_FAILED(result)) return result;
if (!newStyleNode) return NS_ERROR_NULL_POINTER;
result = mEditor->CreateNode(nsEditor::GetTextNodeTag(), newStyleNode, 0, getter_AddRefs(newTextNode));
if (NS_FAILED(result)) return result;
if (!newTextNode) return NS_ERROR_NULL_POINTER;
if (aSelection) {
result = aSelection->Collapse(newTextNode, 0);
}
return result;
}
@ -707,55 +706,63 @@ nsTextEditRules::DidDeleteSelection(nsIDOMSelection *aSelection,
nsCOMPtr<nsIDOMNode>anchor;
PRInt32 offset;
result = aSelection->GetAnchorNode(getter_AddRefs(anchor));
if (NS_SUCCEEDED(result) && NS_SUCCEEDED(aSelection->GetAnchorOffset(&offset)) && anchor)
if (NS_FAILED(result)) return result;
if (!anchor) return NS_ERROR_NULL_POINTER;
result = aSelection->GetAnchorOffset(&offset);
if (NS_FAILED(result)) return result;
nsCOMPtr<nsIDOMNodeList> anchorChildren;
result = anchor->GetChildNodes(getter_AddRefs(anchorChildren));
nsCOMPtr<nsIDOMNode> selectedNode;
if ((NS_SUCCEEDED(result)) && anchorChildren) {
result = anchorChildren->Item(offset, getter_AddRefs(selectedNode));
}
else {
selectedNode = do_QueryInterface(anchor);
}
if ((NS_SUCCEEDED(result)) && selectedNode)
{
nsCOMPtr<nsIDOMNodeList> anchorChildren;
result = anchor->GetChildNodes(getter_AddRefs(anchorChildren));
nsCOMPtr<nsIDOMNode> selectedNode;
if ((NS_SUCCEEDED(result)) && anchorChildren) {
result = anchorChildren->Item(offset, getter_AddRefs(selectedNode));
}
else {
selectedNode = do_QueryInterface(anchor);
}
if ((NS_SUCCEEDED(result)) && selectedNode)
nsCOMPtr<nsIDOMCharacterData>selectedNodeAsText;
selectedNodeAsText = do_QueryInterface(selectedNode);
if (selectedNodeAsText)
{
nsCOMPtr<nsIDOMCharacterData>selectedNodeAsText;
selectedNodeAsText = do_QueryInterface(selectedNode);
if (selectedNodeAsText)
nsCOMPtr<nsIDOMNode> siblingNode;
selectedNode->GetPreviousSibling(getter_AddRefs(siblingNode));
if (siblingNode)
{
nsCOMPtr<nsIDOMNode> siblingNode;
selectedNode->GetPreviousSibling(getter_AddRefs(siblingNode));
if (siblingNode)
nsCOMPtr<nsIDOMCharacterData>siblingNodeAsText;
siblingNodeAsText = do_QueryInterface(siblingNode);
if (siblingNodeAsText)
{
nsCOMPtr<nsIDOMCharacterData>siblingNodeAsText;
siblingNodeAsText = do_QueryInterface(siblingNode);
if (siblingNodeAsText)
{
PRUint32 siblingLength; // the length of siblingNode before the join
siblingNodeAsText->GetLength(&siblingLength);
nsCOMPtr<nsIDOMNode> parentNode;
selectedNode->GetParentNode(getter_AddRefs(parentNode));
result = mEditor->JoinNodes(siblingNode, selectedNode, parentNode);
// selectedNode will remain after the join, siblingNode is removed
}
PRUint32 siblingLength; // the length of siblingNode before the join
siblingNodeAsText->GetLength(&siblingLength);
nsCOMPtr<nsIDOMNode> parentNode;
result = selectedNode->GetParentNode(getter_AddRefs(parentNode));
if (NS_FAILED(result)) return result;
if (!parentNode) return NS_ERROR_NULL_POINTER;
result = mEditor->JoinNodes(siblingNode, selectedNode, parentNode);
// selectedNode will remain after the join, siblingNode is removed
}
selectedNode->GetNextSibling(getter_AddRefs(siblingNode));
if (siblingNode)
}
selectedNode->GetNextSibling(getter_AddRefs(siblingNode));
if (siblingNode)
{
nsCOMPtr<nsIDOMCharacterData>siblingNodeAsText;
siblingNodeAsText = do_QueryInterface(siblingNode);
if (siblingNodeAsText)
{
nsCOMPtr<nsIDOMCharacterData>siblingNodeAsText;
siblingNodeAsText = do_QueryInterface(siblingNode);
if (siblingNodeAsText)
{
PRUint32 selectedNodeLength; // the length of siblingNode before the join
selectedNodeAsText->GetLength(&selectedNodeLength);
nsCOMPtr<nsIDOMNode> parentNode;
selectedNode->GetParentNode(getter_AddRefs(parentNode));
result = mEditor->JoinNodes(selectedNode, siblingNode, parentNode);
// selectedNode will remain after the join, siblingNode is removed
// set selection
aSelection->Collapse(siblingNode, selectedNodeLength);
}
PRUint32 selectedNodeLength; // the length of siblingNode before the join
selectedNodeAsText->GetLength(&selectedNodeLength);
nsCOMPtr<nsIDOMNode> parentNode;
result = selectedNode->GetParentNode(getter_AddRefs(parentNode));
if (NS_FAILED(result)) return result;
if (!parentNode) return NS_ERROR_NULL_POINTER;
result = mEditor->JoinNodes(selectedNode, siblingNode, parentNode);
if (NS_FAILED(result)) return result;
// selectedNode will remain after the join, siblingNode is removed
// set selection
result = aSelection->Collapse(siblingNode, selectedNodeLength);
}
}
}
@ -795,23 +802,24 @@ nsTextEditRules:: DidUndo(nsIDOMSelection *aSelection, nsresult aResult)
nsCOMPtr<nsIDOMNode>node;
PRInt32 offset;
result = aSelection->GetAnchorNode(getter_AddRefs(node));
if (NS_SUCCEEDED(result) && NS_SUCCEEDED(aSelection->GetAnchorOffset(&offset)) && node)
if (NS_FAILED(result)) return result;
if (!node) return NS_ERROR_NULL_POINTER;
result = aSelection->GetAnchorOffset(&offset);
if (NS_FAILED(result)) return result;
nsCOMPtr<nsIDOMElement>element;
element = do_QueryInterface(node);
if (element)
{
nsCOMPtr<nsIDOMElement>element;
element = do_QueryInterface(node);
if (element)
{
nsAutoString att(nsEditor::kMOZEditorBogusNodeAttr);
nsAutoString val;
(void)element->GetAttribute(att, val);
if (val.Equals(nsEditor::kMOZEditorBogusNodeValue)) {
mBogusNode = do_QueryInterface(element);
}
nsAutoString att(nsEditor::kMOZEditorBogusNodeAttr);
nsAutoString val;
(void)element->GetAttribute(att, val);
if (val.Equals(nsEditor::kMOZEditorBogusNodeValue)) {
mBogusNode = do_QueryInterface(element);
}
nsCOMPtr<nsIDOMNode> temp;
result = node->GetParentNode(getter_AddRefs(temp));
node = do_QueryInterface(temp);
}
nsCOMPtr<nsIDOMNode> temp;
result = node->GetParentNode(getter_AddRefs(temp));
node = do_QueryInterface(temp);
}
}
return result;
@ -842,23 +850,24 @@ nsTextEditRules::DidRedo(nsIDOMSelection *aSelection, nsresult aResult)
nsCOMPtr<nsIDOMNode>node;
PRInt32 offset;
result = aSelection->GetAnchorNode(getter_AddRefs(node));
if (NS_SUCCEEDED(result) && NS_SUCCEEDED(aSelection->GetAnchorOffset(&offset)) && node)
if (NS_FAILED(result)) return result;
if (!node) return NS_ERROR_NULL_POINTER;
result = aSelection->GetAnchorOffset(&offset);
if (NS_FAILED(result)) return result;
nsCOMPtr<nsIDOMElement>element;
element = do_QueryInterface(node);
if (element)
{
nsCOMPtr<nsIDOMElement>element;
element = do_QueryInterface(node);
if (element)
{
nsAutoString att(nsEditor::kMOZEditorBogusNodeAttr);
nsAutoString val;
(void)element->GetAttribute(att, val);
if (val.Equals(nsEditor::kMOZEditorBogusNodeValue)) {
mBogusNode = do_QueryInterface(element);
}
nsAutoString att(nsEditor::kMOZEditorBogusNodeAttr);
nsAutoString val;
(void)element->GetAttribute(att, val);
if (val.Equals(nsEditor::kMOZEditorBogusNodeValue)) {
mBogusNode = do_QueryInterface(element);
}
nsCOMPtr<nsIDOMNode> temp;
result = node->GetParentNode(getter_AddRefs(temp));
node = do_QueryInterface(temp);
}
nsCOMPtr<nsIDOMNode> temp;
result = node->GetParentNode(getter_AddRefs(temp));
node = do_QueryInterface(temp);
}
}
return result;
@ -895,86 +904,63 @@ nsTextEditRules::CreateBogusNodeIfNeeded(nsIDOMSelection *aSelection)
{
if (!aSelection) { return NS_ERROR_NULL_POINTER; }
if (!mEditor) { return NS_ERROR_NULL_POINTER; }
nsCOMPtr<nsIDOMDocument>doc;
mEditor->GetDocument(getter_AddRefs(doc));
nsCOMPtr<nsIDOMNodeList>nodeList;
nsAutoString bodyTag = "body";
nsresult result = doc->GetElementsByTagName(bodyTag, getter_AddRefs(nodeList));
if ((NS_SUCCEEDED(result)) && nodeList)
nsCOMPtr<nsIDOMElement> bodyElement;
nsresult result = mEditor->GetBodyElement(getter_AddRefs(bodyElement));
if (NS_FAILED(result)) return result;
if (!bodyElement) return NS_ERROR_NULL_POINTER;
nsCOMPtr<nsIDOMNode>bodyNode = do_QueryInterface(bodyElement);
// now we've got the body tag.
// iterate the body tag, looking for editable content
// if no editable content is found, insert the bogus node
PRBool needsBogusContent=PR_TRUE;
nsCOMPtr<nsIDOMNode>bodyChild;
result = bodyNode->GetFirstChild(getter_AddRefs(bodyChild));
while ((NS_SUCCEEDED(result)) && bodyChild)
{
if (PR_TRUE==mEditor->IsEditable(bodyChild))
{
needsBogusContent = PR_FALSE;
break;
}
nsCOMPtr<nsIDOMNode>temp;
bodyChild->GetNextSibling(getter_AddRefs(temp));
bodyChild = do_QueryInterface(temp);
}
if (PR_TRUE==needsBogusContent)
{
PRUint32 count;
nodeList->GetLength(&count);
NS_ASSERTION(1==count, "there is not exactly 1 body in the document!");
nsCOMPtr<nsIDOMNode>bodyNode;
result = nodeList->Item(0, getter_AddRefs(bodyNode));
if ((NS_SUCCEEDED(result)) && bodyNode)
{ // now we've got the body tag.
// iterate the body tag, looking for editable content
// if no editable content is found, insert the bogus node
PRBool needsBogusContent=PR_TRUE;
nsCOMPtr<nsIDOMNode>bodyChild;
result = bodyNode->GetFirstChild(getter_AddRefs(bodyChild));
while ((NS_SUCCEEDED(result)) && bodyChild)
{
if (PR_TRUE==mEditor->IsEditable(bodyChild))
{
needsBogusContent = PR_FALSE;
break;
}
nsCOMPtr<nsIDOMNode>temp;
bodyChild->GetNextSibling(getter_AddRefs(temp));
bodyChild = do_QueryInterface(temp);
}
if (PR_TRUE==needsBogusContent)
{
// set mBogusNode to be the newly created <P>
result = mEditor->CreateNode(nsAutoString("P"), bodyNode, 0,
getter_AddRefs(mBogusNode));
if ((NS_SUCCEEDED(result)) && mBogusNode)
{
nsCOMPtr<nsIDOMNode>newTNode;
result = mEditor->CreateNode(nsEditor::GetTextNodeTag(), mBogusNode, 0,
getter_AddRefs(newTNode));
if ((NS_SUCCEEDED(result)) && newTNode)
{
nsCOMPtr<nsIDOMCharacterData>newNodeAsText;
newNodeAsText = do_QueryInterface(newTNode);
if (newNodeAsText)
{
nsAutoString data;
data += 160;
newNodeAsText->SetData(data);
aSelection->Collapse(newTNode, 0);
}
}
// make sure we know the PNode is bogus
nsCOMPtr<nsIDOMElement>newPElement;
newPElement = do_QueryInterface(mBogusNode);
if (newPElement)
{
nsAutoString att(nsEditor::kMOZEditorBogusNodeAttr);
nsAutoString val(nsEditor::kMOZEditorBogusNodeValue);
newPElement->SetAttribute(att, val);
}
}
}
// set mBogusNode to be the newly created <P>
result = mEditor->CreateNode(nsAutoString("P"), bodyNode, 0,
getter_AddRefs(mBogusNode));
if (NS_FAILED(result)) return result;
if (!mBogusNode) return NS_ERROR_NULL_POINTER;
nsCOMPtr<nsIDOMNode>newTNode;
result = mEditor->CreateNode(nsEditor::GetTextNodeTag(), mBogusNode, 0,
getter_AddRefs(newTNode));
if (NS_FAILED(result)) return result;
if (!newTNode) return NS_ERROR_NULL_POINTER;
nsCOMPtr<nsIDOMCharacterData>newNodeAsText;
newNodeAsText = do_QueryInterface(newTNode);
if (newNodeAsText)
{
nsAutoString data;
data += 160;
newNodeAsText->SetData(data);
aSelection->Collapse(newTNode, 0);
}
// make sure we know the PNode is bogus
nsCOMPtr<nsIDOMElement>newPElement;
newPElement = do_QueryInterface(mBogusNode);
if (newPElement)
{
nsAutoString att(nsEditor::kMOZEditorBogusNodeAttr);
nsAutoString val(nsEditor::kMOZEditorBogusNodeValue);
newPElement->SetAttribute(att, val);
}
}
return result;
}