From f574b5218f408673dfa95826729c475bce25b11e Mon Sep 17 00:00:00 2001 From: "jfrancis%netscape.com" Date: Wed, 1 Sep 1999 21:23:47 +0000 Subject: [PATCH] more deletion work... --- editor/base/nsHTMLEditRules.cpp | 112 +++++++++++++--------- editor/base/nsHTMLEditRules.h | 5 + editor/libeditor/html/nsHTMLEditRules.cpp | 112 +++++++++++++--------- editor/libeditor/html/nsHTMLEditRules.h | 5 + 4 files changed, 146 insertions(+), 88 deletions(-) diff --git a/editor/base/nsHTMLEditRules.cpp b/editor/base/nsHTMLEditRules.cpp index 5f70ff0e5923..7d03dc6c95aa 100644 --- a/editor/base/nsHTMLEditRules.cpp +++ b/editor/base/nsHTMLEditRules.cpp @@ -352,28 +352,12 @@ nsHTMLEditRules::WillDeleteSelection(nsIDOMSelection *aSelection, nsIEditor::ESe nsCOMPtr topParent; leftParent->GetParentNode(getter_AddRefs(topParent)); - if (IsParagraph(leftParent)) - { - // join para's, insert break - *aCancel = PR_TRUE; - res = mEditor->JoinNodeDeep(leftParent,rightParent,&selNode,&selOffset); - if (NS_FAILED(res)) return res; - // fix up selection - res = aSelection->Collapse(selNode,selOffset); - if (NS_FAILED(res)) return res; - res = mEditor->InsertBreak(); - return res; - } - if (IsListItem(leftParent) || IsHeader(leftParent)) - { - // join blocks - *aCancel = PR_TRUE; - res = mEditor->JoinNodeDeep(leftParent,rightParent,&selNode,&selOffset); - if (NS_FAILED(res)) return res; - // fix up selection - res = aSelection->Collapse(selNode,selOffset); - return res; - } + *aCancel = PR_TRUE; + res = JoinNodesSmart(leftParent,rightParent,&selNode,&selOffset); + if (NS_FAILED(res)) return res; + // fix up selection + res = aSelection->Collapse(selNode,selOffset); + return res; } // else blocks not same type, bail to default @@ -411,28 +395,12 @@ nsHTMLEditRules::WillDeleteSelection(nsIDOMSelection *aSelection, nsIEditor::ESe nsCOMPtr topParent; leftParent->GetParentNode(getter_AddRefs(topParent)); - if (IsParagraph(leftParent)) - { - // join para's, insert break - *aCancel = PR_TRUE; - res = mEditor->JoinNodeDeep(leftParent,rightParent,&selNode,&selOffset); - if (NS_FAILED(res)) return res; - // fix up selection - res = aSelection->Collapse(selNode,selOffset); - if (NS_FAILED(res)) return res; - res = mEditor->InsertBreak(); - return res; - } - if (IsListItem(leftParent) || IsHeader(leftParent)) - { - // join blocks - *aCancel = PR_TRUE; - res = mEditor->JoinNodeDeep(leftParent,rightParent,&selNode,&selOffset); - if (NS_FAILED(res)) return res; - // fix up selection - res = aSelection->Collapse(selNode,selOffset); - return res; - } + *aCancel = PR_TRUE; + res = JoinNodesSmart(leftParent,rightParent,&selNode,&selOffset); + if (NS_FAILED(res)) return res; + // fix up selection + res = aSelection->Collapse(selNode,selOffset); + return res; } // else blocks not same type, bail to default @@ -2568,3 +2536,59 @@ nsHTMLEditRules::IsLastEditableChild( nsIDOMNode *aNode, PRBool *aOutIsLast) *aOutIsLast = (child.get() == aNode); return res; } + + +nsresult +nsHTMLEditRules::JoinNodesSmart( nsIDOMNode *aNodeLeft, + nsIDOMNode *aNodeRight, + nsCOMPtr *aOutMergeParent, + PRInt32 *aOutMergeOffset) +{ + // check parms + if (!aNodeLeft || + !aNodeRight || + !aOutMergeParent || + !aOutMergeOffset) + return NS_ERROR_NULL_POINTER; + + nsresult res = NS_OK; + // caller responsible for: + // left & right node are smae type + // left & right node have smae parent + + nsCOMPtr parent; + aNodeLeft->GetParentNode(getter_AddRefs(parent)); + + // defaults for outParams + *aOutMergeParent = aNodeRight; + res = mEditor->GetLengthOfDOMNode(aNodeLeft, *((PRUint32*)aOutMergeOffset)); + if (NS_FAILED(res)) return res; + + // seperate join rules for differing blocks + if (IsParagraph(aNodeLeft)) + { + // for para's, merge deep & add a
after merging + res = mEditor->JoinNodeDeep(aNodeLeft, aNodeRight, aOutMergeParent, aOutMergeOffset); + if (NS_FAILED(res)) return res; + nsAutoString brType("br"); + nsCOMPtr brNode; + res = mEditor->CreateNode(brType, *aOutMergeParent, *aOutMergeOffset, getter_AddRefs(brNode)); + // out offset _after_
+ *aOutMergeOffset++; + return res; + } + else if (IsList(aNodeLeft) || mEditor->IsTextNode(aNodeLeft)) + { + // for list's, merge shallow (wouldn't want to combine list items) + res = mEditor->JoinNodes(aNodeLeft, aNodeRight, parent); + if (NS_FAILED(res)) return res; + return res; + } + else + { + // for list items, divs, etc, merge smart + res = JoinNodesSmart(aNodeLeft, aNodeRight, aOutMergeParent, aOutMergeOffset); + if (NS_FAILED(res)) return res; + return res; + } +} \ No newline at end of file diff --git a/editor/base/nsHTMLEditRules.h b/editor/base/nsHTMLEditRules.h index 283f214292d6..2d09f56881aa 100644 --- a/editor/base/nsHTMLEditRules.h +++ b/editor/base/nsHTMLEditRules.h @@ -111,6 +111,11 @@ protected: nsresult IsFirstEditableChild( nsIDOMNode *aNode, PRBool *aOutIsFirst); nsresult IsLastEditableChild( nsIDOMNode *aNode, PRBool *aOutIsLast); + nsresult JoinNodesSmart( nsIDOMNode *aNodeLeft, + nsIDOMNode *aNodeRight, + nsCOMPtr *aOutMergeParent, + PRInt32 *aOutMergeOffset); + }; #endif //nsHTMLEditRules_h__ diff --git a/editor/libeditor/html/nsHTMLEditRules.cpp b/editor/libeditor/html/nsHTMLEditRules.cpp index 5f70ff0e5923..7d03dc6c95aa 100644 --- a/editor/libeditor/html/nsHTMLEditRules.cpp +++ b/editor/libeditor/html/nsHTMLEditRules.cpp @@ -352,28 +352,12 @@ nsHTMLEditRules::WillDeleteSelection(nsIDOMSelection *aSelection, nsIEditor::ESe nsCOMPtr topParent; leftParent->GetParentNode(getter_AddRefs(topParent)); - if (IsParagraph(leftParent)) - { - // join para's, insert break - *aCancel = PR_TRUE; - res = mEditor->JoinNodeDeep(leftParent,rightParent,&selNode,&selOffset); - if (NS_FAILED(res)) return res; - // fix up selection - res = aSelection->Collapse(selNode,selOffset); - if (NS_FAILED(res)) return res; - res = mEditor->InsertBreak(); - return res; - } - if (IsListItem(leftParent) || IsHeader(leftParent)) - { - // join blocks - *aCancel = PR_TRUE; - res = mEditor->JoinNodeDeep(leftParent,rightParent,&selNode,&selOffset); - if (NS_FAILED(res)) return res; - // fix up selection - res = aSelection->Collapse(selNode,selOffset); - return res; - } + *aCancel = PR_TRUE; + res = JoinNodesSmart(leftParent,rightParent,&selNode,&selOffset); + if (NS_FAILED(res)) return res; + // fix up selection + res = aSelection->Collapse(selNode,selOffset); + return res; } // else blocks not same type, bail to default @@ -411,28 +395,12 @@ nsHTMLEditRules::WillDeleteSelection(nsIDOMSelection *aSelection, nsIEditor::ESe nsCOMPtr topParent; leftParent->GetParentNode(getter_AddRefs(topParent)); - if (IsParagraph(leftParent)) - { - // join para's, insert break - *aCancel = PR_TRUE; - res = mEditor->JoinNodeDeep(leftParent,rightParent,&selNode,&selOffset); - if (NS_FAILED(res)) return res; - // fix up selection - res = aSelection->Collapse(selNode,selOffset); - if (NS_FAILED(res)) return res; - res = mEditor->InsertBreak(); - return res; - } - if (IsListItem(leftParent) || IsHeader(leftParent)) - { - // join blocks - *aCancel = PR_TRUE; - res = mEditor->JoinNodeDeep(leftParent,rightParent,&selNode,&selOffset); - if (NS_FAILED(res)) return res; - // fix up selection - res = aSelection->Collapse(selNode,selOffset); - return res; - } + *aCancel = PR_TRUE; + res = JoinNodesSmart(leftParent,rightParent,&selNode,&selOffset); + if (NS_FAILED(res)) return res; + // fix up selection + res = aSelection->Collapse(selNode,selOffset); + return res; } // else blocks not same type, bail to default @@ -2568,3 +2536,59 @@ nsHTMLEditRules::IsLastEditableChild( nsIDOMNode *aNode, PRBool *aOutIsLast) *aOutIsLast = (child.get() == aNode); return res; } + + +nsresult +nsHTMLEditRules::JoinNodesSmart( nsIDOMNode *aNodeLeft, + nsIDOMNode *aNodeRight, + nsCOMPtr *aOutMergeParent, + PRInt32 *aOutMergeOffset) +{ + // check parms + if (!aNodeLeft || + !aNodeRight || + !aOutMergeParent || + !aOutMergeOffset) + return NS_ERROR_NULL_POINTER; + + nsresult res = NS_OK; + // caller responsible for: + // left & right node are smae type + // left & right node have smae parent + + nsCOMPtr parent; + aNodeLeft->GetParentNode(getter_AddRefs(parent)); + + // defaults for outParams + *aOutMergeParent = aNodeRight; + res = mEditor->GetLengthOfDOMNode(aNodeLeft, *((PRUint32*)aOutMergeOffset)); + if (NS_FAILED(res)) return res; + + // seperate join rules for differing blocks + if (IsParagraph(aNodeLeft)) + { + // for para's, merge deep & add a
after merging + res = mEditor->JoinNodeDeep(aNodeLeft, aNodeRight, aOutMergeParent, aOutMergeOffset); + if (NS_FAILED(res)) return res; + nsAutoString brType("br"); + nsCOMPtr brNode; + res = mEditor->CreateNode(brType, *aOutMergeParent, *aOutMergeOffset, getter_AddRefs(brNode)); + // out offset _after_
+ *aOutMergeOffset++; + return res; + } + else if (IsList(aNodeLeft) || mEditor->IsTextNode(aNodeLeft)) + { + // for list's, merge shallow (wouldn't want to combine list items) + res = mEditor->JoinNodes(aNodeLeft, aNodeRight, parent); + if (NS_FAILED(res)) return res; + return res; + } + else + { + // for list items, divs, etc, merge smart + res = JoinNodesSmart(aNodeLeft, aNodeRight, aOutMergeParent, aOutMergeOffset); + if (NS_FAILED(res)) return res; + return res; + } +} \ No newline at end of file diff --git a/editor/libeditor/html/nsHTMLEditRules.h b/editor/libeditor/html/nsHTMLEditRules.h index 283f214292d6..2d09f56881aa 100644 --- a/editor/libeditor/html/nsHTMLEditRules.h +++ b/editor/libeditor/html/nsHTMLEditRules.h @@ -111,6 +111,11 @@ protected: nsresult IsFirstEditableChild( nsIDOMNode *aNode, PRBool *aOutIsFirst); nsresult IsLastEditableChild( nsIDOMNode *aNode, PRBool *aOutIsLast); + nsresult JoinNodesSmart( nsIDOMNode *aNodeLeft, + nsIDOMNode *aNodeRight, + nsCOMPtr *aOutMergeParent, + PRInt32 *aOutMergeOffset); + }; #endif //nsHTMLEditRules_h__