From f20389fad6a08f60f7090852856418a6b0a80ec4 Mon Sep 17 00:00:00 2001 From: "jfrancis%netscape.com" Date: Fri, 5 May 2000 20:42:36 +0000 Subject: [PATCH] fixing bugs with paste; making paste not inherit local inline stlyes --- editor/base/nsEditor.cpp | 16 +++++++++++-- editor/base/nsHTMLEditRules.cpp | 29 +++++++++++++++++------ editor/base/nsHTMLEditRules.h | 2 ++ editor/base/nsHTMLEditor.cpp | 22 +++++++++++++++++ editor/libeditor/base/nsEditor.cpp | 16 +++++++++++-- editor/libeditor/html/nsHTMLEditRules.cpp | 29 +++++++++++++++++------ editor/libeditor/html/nsHTMLEditRules.h | 2 ++ editor/libeditor/html/nsHTMLEditor.cpp | 22 +++++++++++++++++ 8 files changed, 120 insertions(+), 18 deletions(-) diff --git a/editor/base/nsEditor.cpp b/editor/base/nsEditor.cpp index 76ccc136132..46508dfea12 100644 --- a/editor/base/nsEditor.cpp +++ b/editor/base/nsEditor.cpp @@ -3200,8 +3200,10 @@ nsEditor::JoinNodesImpl(nsIDOMNode * aNodeToKeep, { // and adjust the selection if needed // HACK: this is overly simplified - multi-range selections need more work than this + PRBool bNeedToAdjust = PR_FALSE; if (selStartNode.get() == aNodeToJoin) { + bNeedToAdjust = PR_TRUE; selStartNode = aNodeToKeep; if (aNodeToKeepIsFirst) { @@ -3210,10 +3212,18 @@ nsEditor::JoinNodesImpl(nsIDOMNode * aNodeToKeep, } else if ((selStartNode.get() == aNodeToKeep) && !aNodeToKeepIsFirst) { + bNeedToAdjust = PR_TRUE; selStartOffset += firstNodeLength; } + + if (bNeedToAdjust) + selection->Collapse(selStartNode,selStartOffset); + + bNeedToAdjust = PR_FALSE; + if (selEndNode.get() == aNodeToJoin) { + bNeedToAdjust = PR_TRUE; selEndNode = aNodeToKeep; if (aNodeToKeepIsFirst) { @@ -3222,10 +3232,12 @@ nsEditor::JoinNodesImpl(nsIDOMNode * aNodeToKeep, } else if ((selEndNode.get() == aNodeToKeep) && !aNodeToKeepIsFirst) { + bNeedToAdjust = PR_TRUE; selEndOffset += firstNodeLength; } - selection->Collapse(selStartNode,selStartOffset); - selection->Extend(selEndNode,selEndOffset); + + if (bNeedToAdjust) + selection->Extend(selEndNode,selEndOffset); } } } diff --git a/editor/base/nsHTMLEditRules.cpp b/editor/base/nsHTMLEditRules.cpp index eac6d0173de..7c6cfdfea5f 100644 --- a/editor/base/nsHTMLEditRules.cpp +++ b/editor/base/nsHTMLEditRules.cpp @@ -397,6 +397,28 @@ nsHTMLEditRules::GetIndentState(PRBool &aCanIndent, PRBool &aCanOutdent) * Protected rules methods ********************************************************/ +nsresult +nsHTMLEditRules::WillInsert(nsIDOMSelection *aSelection, PRBool *aCancel) +{ + nsresult res = nsTextEditRules::WillInsert(aSelection, aCancel); + if (NS_FAILED(res)) return res; + + // we need to get the doc + nsCOMPtrdoc; + res = mEditor->GetDocument(getter_AddRefs(doc)); + if (NS_FAILED(res)) return res; + if (!doc) return NS_ERROR_NULL_POINTER; + + // for every property that is set, insert a new inline style node + res = CreateStyleForInsertText(aSelection, doc); + return res; +} + +nsresult +nsHTMLEditRules::DidInsert(nsIDOMSelection *aSelection, nsresult aResult) +{ + return nsTextEditRules::DidInsert(aSelection, aResult); +} nsresult nsHTMLEditRules::WillInsertText(PRInt32 aAction, @@ -459,13 +481,6 @@ nsHTMLEditRules::WillInsertText(PRInt32 aAction, if (NS_FAILED(res)) return res; if (!doc) return NS_ERROR_NULL_POINTER; - // for every property that is set, insert a new inline style node - res = CreateStyleForInsertText(aSelection, doc); - if (NS_FAILED(res)) return res; - // refresh the (collapsed) selection location - res = mEditor->GetStartNodeAndOffset(aSelection, &selNode, &selOffset); - if (NS_FAILED(res)) return res; - if (aAction == kInsertTextIME) { res = mEditor->InsertTextImpl(*inString, &selNode, &selOffset, doc); diff --git a/editor/base/nsHTMLEditRules.h b/editor/base/nsHTMLEditRules.h index cd9292d9c01..a1250f6739f 100644 --- a/editor/base/nsHTMLEditRules.h +++ b/editor/base/nsHTMLEditRules.h @@ -85,6 +85,8 @@ protected: // nsHTMLEditRules implementation methods + nsresult WillInsert(nsIDOMSelection *aSelection, PRBool *aCancel); + nsresult DidInsert(nsIDOMSelection *aSelection, nsresult aResult); nsresult WillInsertText( PRInt32 aAction, nsIDOMSelection *aSelection, PRBool *aCancel, diff --git a/editor/base/nsHTMLEditor.cpp b/editor/base/nsHTMLEditor.cpp index bfd1a1db638..a5e2f397dae 100644 --- a/editor/base/nsHTMLEditor.cpp +++ b/editor/base/nsHTMLEditor.cpp @@ -2351,6 +2351,9 @@ NS_IMETHODIMP nsHTMLEditor::InsertHTMLWithCharset(const nsString& aInputString, res = DeleteSelectionAndPrepareToCreateNode(parentNode, offsetOfNewNode); if (NS_FAILED(res)) return res; + // pasting does not inherit local inline styles + RemoveAllInlineProperties(); + res = GetSelection(getter_AddRefs(selection)); if (NS_FAILED(res)) return res; if (!selection) return NS_ERROR_NULL_POINTER; @@ -2363,6 +2366,22 @@ NS_IMETHODIMP nsHTMLEditor::InsertHTMLWithCharset(const nsString& aInputString, if (cancel) return NS_OK; // rules canceled the operation if (!handled) { + // The rules code (WillDoAction above) might have changed the selection. + // refresh our memory... + res = GetStartNodeAndOffset(selection, &parentNode, &offsetOfNewNode); + if (!parentNode) res = NS_ERROR_FAILURE; + if (NS_FAILED(res)) return res; + + // are we in a text node? If so, split it. + if (IsTextNode(parentNode)) + { + nsCOMPtr temp; + res = SplitNode(parentNode, offsetOfNewNode, getter_AddRefs(temp)); + if (NS_FAILED(res)) return res; + res = GetNodeLocation(parentNode, &temp, &offsetOfNewNode); + if (NS_FAILED(res)) return res; + parentNode = temp; + } // Get the first range in the selection, for context: nsCOMPtr range; res = selection->GetRangeAt(0, getter_AddRefs(range)); @@ -4352,6 +4371,7 @@ NS_IMETHODIMP nsHTMLEditor::InsertFromTransferable(nsITransferable *transferable PRUint32 len = 0; if ( NS_SUCCEEDED(transferable->GetAnyTransferData(&bestFlavor, getter_AddRefs(genericDataObj), &len)) ) { + nsAutoTxnsConserveSelection dontSpazMySelection(this); nsAutoString stuffToPaste; nsAutoString flavor; flavor.AssignWithConversion( bestFlavor ); // just so we can use flavor.Equals() @@ -4379,6 +4399,8 @@ NS_IMETHODIMP nsHTMLEditor::InsertFromTransferable(nsITransferable *transferable textDataObj->ToString ( &text ); stuffToPaste.Assign ( text, len / 2 ); nsAutoEditBatch beginBatching(this); + // pasting does not inherit local inline styles + RemoveAllInlineProperties(); rv = InsertText(stuffToPaste); } } diff --git a/editor/libeditor/base/nsEditor.cpp b/editor/libeditor/base/nsEditor.cpp index 76ccc136132..46508dfea12 100644 --- a/editor/libeditor/base/nsEditor.cpp +++ b/editor/libeditor/base/nsEditor.cpp @@ -3200,8 +3200,10 @@ nsEditor::JoinNodesImpl(nsIDOMNode * aNodeToKeep, { // and adjust the selection if needed // HACK: this is overly simplified - multi-range selections need more work than this + PRBool bNeedToAdjust = PR_FALSE; if (selStartNode.get() == aNodeToJoin) { + bNeedToAdjust = PR_TRUE; selStartNode = aNodeToKeep; if (aNodeToKeepIsFirst) { @@ -3210,10 +3212,18 @@ nsEditor::JoinNodesImpl(nsIDOMNode * aNodeToKeep, } else if ((selStartNode.get() == aNodeToKeep) && !aNodeToKeepIsFirst) { + bNeedToAdjust = PR_TRUE; selStartOffset += firstNodeLength; } + + if (bNeedToAdjust) + selection->Collapse(selStartNode,selStartOffset); + + bNeedToAdjust = PR_FALSE; + if (selEndNode.get() == aNodeToJoin) { + bNeedToAdjust = PR_TRUE; selEndNode = aNodeToKeep; if (aNodeToKeepIsFirst) { @@ -3222,10 +3232,12 @@ nsEditor::JoinNodesImpl(nsIDOMNode * aNodeToKeep, } else if ((selEndNode.get() == aNodeToKeep) && !aNodeToKeepIsFirst) { + bNeedToAdjust = PR_TRUE; selEndOffset += firstNodeLength; } - selection->Collapse(selStartNode,selStartOffset); - selection->Extend(selEndNode,selEndOffset); + + if (bNeedToAdjust) + selection->Extend(selEndNode,selEndOffset); } } } diff --git a/editor/libeditor/html/nsHTMLEditRules.cpp b/editor/libeditor/html/nsHTMLEditRules.cpp index eac6d0173de..7c6cfdfea5f 100644 --- a/editor/libeditor/html/nsHTMLEditRules.cpp +++ b/editor/libeditor/html/nsHTMLEditRules.cpp @@ -397,6 +397,28 @@ nsHTMLEditRules::GetIndentState(PRBool &aCanIndent, PRBool &aCanOutdent) * Protected rules methods ********************************************************/ +nsresult +nsHTMLEditRules::WillInsert(nsIDOMSelection *aSelection, PRBool *aCancel) +{ + nsresult res = nsTextEditRules::WillInsert(aSelection, aCancel); + if (NS_FAILED(res)) return res; + + // we need to get the doc + nsCOMPtrdoc; + res = mEditor->GetDocument(getter_AddRefs(doc)); + if (NS_FAILED(res)) return res; + if (!doc) return NS_ERROR_NULL_POINTER; + + // for every property that is set, insert a new inline style node + res = CreateStyleForInsertText(aSelection, doc); + return res; +} + +nsresult +nsHTMLEditRules::DidInsert(nsIDOMSelection *aSelection, nsresult aResult) +{ + return nsTextEditRules::DidInsert(aSelection, aResult); +} nsresult nsHTMLEditRules::WillInsertText(PRInt32 aAction, @@ -459,13 +481,6 @@ nsHTMLEditRules::WillInsertText(PRInt32 aAction, if (NS_FAILED(res)) return res; if (!doc) return NS_ERROR_NULL_POINTER; - // for every property that is set, insert a new inline style node - res = CreateStyleForInsertText(aSelection, doc); - if (NS_FAILED(res)) return res; - // refresh the (collapsed) selection location - res = mEditor->GetStartNodeAndOffset(aSelection, &selNode, &selOffset); - if (NS_FAILED(res)) return res; - if (aAction == kInsertTextIME) { res = mEditor->InsertTextImpl(*inString, &selNode, &selOffset, doc); diff --git a/editor/libeditor/html/nsHTMLEditRules.h b/editor/libeditor/html/nsHTMLEditRules.h index cd9292d9c01..a1250f6739f 100644 --- a/editor/libeditor/html/nsHTMLEditRules.h +++ b/editor/libeditor/html/nsHTMLEditRules.h @@ -85,6 +85,8 @@ protected: // nsHTMLEditRules implementation methods + nsresult WillInsert(nsIDOMSelection *aSelection, PRBool *aCancel); + nsresult DidInsert(nsIDOMSelection *aSelection, nsresult aResult); nsresult WillInsertText( PRInt32 aAction, nsIDOMSelection *aSelection, PRBool *aCancel, diff --git a/editor/libeditor/html/nsHTMLEditor.cpp b/editor/libeditor/html/nsHTMLEditor.cpp index bfd1a1db638..a5e2f397dae 100644 --- a/editor/libeditor/html/nsHTMLEditor.cpp +++ b/editor/libeditor/html/nsHTMLEditor.cpp @@ -2351,6 +2351,9 @@ NS_IMETHODIMP nsHTMLEditor::InsertHTMLWithCharset(const nsString& aInputString, res = DeleteSelectionAndPrepareToCreateNode(parentNode, offsetOfNewNode); if (NS_FAILED(res)) return res; + // pasting does not inherit local inline styles + RemoveAllInlineProperties(); + res = GetSelection(getter_AddRefs(selection)); if (NS_FAILED(res)) return res; if (!selection) return NS_ERROR_NULL_POINTER; @@ -2363,6 +2366,22 @@ NS_IMETHODIMP nsHTMLEditor::InsertHTMLWithCharset(const nsString& aInputString, if (cancel) return NS_OK; // rules canceled the operation if (!handled) { + // The rules code (WillDoAction above) might have changed the selection. + // refresh our memory... + res = GetStartNodeAndOffset(selection, &parentNode, &offsetOfNewNode); + if (!parentNode) res = NS_ERROR_FAILURE; + if (NS_FAILED(res)) return res; + + // are we in a text node? If so, split it. + if (IsTextNode(parentNode)) + { + nsCOMPtr temp; + res = SplitNode(parentNode, offsetOfNewNode, getter_AddRefs(temp)); + if (NS_FAILED(res)) return res; + res = GetNodeLocation(parentNode, &temp, &offsetOfNewNode); + if (NS_FAILED(res)) return res; + parentNode = temp; + } // Get the first range in the selection, for context: nsCOMPtr range; res = selection->GetRangeAt(0, getter_AddRefs(range)); @@ -4352,6 +4371,7 @@ NS_IMETHODIMP nsHTMLEditor::InsertFromTransferable(nsITransferable *transferable PRUint32 len = 0; if ( NS_SUCCEEDED(transferable->GetAnyTransferData(&bestFlavor, getter_AddRefs(genericDataObj), &len)) ) { + nsAutoTxnsConserveSelection dontSpazMySelection(this); nsAutoString stuffToPaste; nsAutoString flavor; flavor.AssignWithConversion( bestFlavor ); // just so we can use flavor.Equals() @@ -4379,6 +4399,8 @@ NS_IMETHODIMP nsHTMLEditor::InsertFromTransferable(nsITransferable *transferable textDataObj->ToString ( &text ); stuffToPaste.Assign ( text, len / 2 ); nsAutoEditBatch beginBatching(this); + // pasting does not inherit local inline styles + RemoveAllInlineProperties(); rv = InsertText(stuffToPaste); } }