From faba49aa9b8b160f3077dc05da5fec0cd13b9a09 Mon Sep 17 00:00:00 2001 From: "cmanske%netscape.com" Date: Thu, 12 Apr 2001 03:41:06 +0000 Subject: [PATCH] Fixed reset selection when inserting under . Also removed compile warning in nsTableEditor.cpp, b=71883, r=jfrancis,kin sr=sfraser git-svn-id: svn://10.0.0.236/trunk@92060 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/editor/base/nsEditor.cpp | 15 ++++++++------- mozilla/editor/base/nsEditor.h | 1 - mozilla/editor/base/nsEditorShell.cpp | 12 ++++++++++-- mozilla/editor/base/nsHTMLEditor.cpp | 3 +++ mozilla/editor/base/nsTableEditor.cpp | 1 - mozilla/editor/composer/src/nsEditorShell.cpp | 12 ++++++++++-- mozilla/editor/idl/nsIEditor.idl | 9 +++++++++ mozilla/editor/idl/nsIEditorShell.idl | 15 +++++++++------ mozilla/editor/libeditor/base/nsEditor.cpp | 15 ++++++++------- mozilla/editor/libeditor/base/nsEditor.h | 1 - mozilla/editor/libeditor/html/nsHTMLEditor.cpp | 3 +++ mozilla/editor/libeditor/html/nsTableEditor.cpp | 1 - .../editor/ui/dialogs/content/EdDialogCommon.js | 3 ++- 13 files changed, 62 insertions(+), 29 deletions(-) diff --git a/mozilla/editor/base/nsEditor.cpp b/mozilla/editor/base/nsEditor.cpp index 019d3ef4e8a..b015fd40575 100644 --- a/mozilla/editor/base/nsEditor.cpp +++ b/mozilla/editor/base/nsEditor.cpp @@ -742,13 +742,20 @@ nsEditor::EndPlaceHolderTransaction() return NS_OK; } -NS_IMETHODIMP nsEditor::ShouldTxnSetSelection(PRBool *aResult) +NS_IMETHODIMP +nsEditor::ShouldTxnSetSelection(PRBool *aResult) { if (!aResult) return NS_ERROR_NULL_POINTER; *aResult = mShouldTxnSetSelection; return NS_OK; } +NS_IMETHODIMP +nsEditor::SetShouldTxnSetSelection(PRBool aShould) +{ + mShouldTxnSetSelection = aShould; + return NS_OK; +} NS_IMETHODIMP nsEditor::GetDocumentIsEmpty(PRBool *aDocumentIsEmpty) @@ -4316,12 +4323,6 @@ nsEditor::GetShouldTxnSetSelection() } -void -nsEditor::SetShouldTxnSetSelection(PRBool aShould) -{ - mShouldTxnSetSelection = aShould; -} - #ifdef XP_MAC #pragma mark - #pragma mark protected nsEditor methods diff --git a/mozilla/editor/base/nsEditor.h b/mozilla/editor/base/nsEditor.h index ddf0db75bab..d8fc94cd693 100644 --- a/mozilla/editor/base/nsEditor.h +++ b/mozilla/editor/base/nsEditor.h @@ -516,7 +516,6 @@ public: nsresult EndUpdateViewBatch(void); PRBool GetShouldTxnSetSelection(); - void SetShouldTxnSetSelection(PRBool aShould); public: // Argh! These transaction names are used by PlaceholderTxn and diff --git a/mozilla/editor/base/nsEditorShell.cpp b/mozilla/editor/base/nsEditorShell.cpp index da9491f4bf5..f3b90c07628 100644 --- a/mozilla/editor/base/nsEditorShell.cpp +++ b/mozilla/editor/base/nsEditorShell.cpp @@ -3536,19 +3536,27 @@ nsEditorShell::DeleteElement(nsIDOMElement *element) } NS_IMETHODIMP -nsEditorShell::InsertElement(nsIDOMElement *element, nsIDOMElement *parent, PRInt32 position) +nsEditorShell::InsertElement(nsIDOMElement *element, nsIDOMElement *parent, PRInt32 position, PRBool dontChangeSelection) { if (!element || !parent) return NS_ERROR_NULL_POINTER; nsresult result = NS_NOINTERFACE; nsCOMPtr editor = do_QueryInterface(mEditor); - if (editor) { + if (editor) + { + // Set flag so InsertElementTxn doesn't change the selection + if (dontChangeSelection) + editor->SetShouldTxnSetSelection(PR_FALSE); + // The nsIEditor::InsertNode() wants nodes as params, // but it actually requires that they are elements! nsCOMPtr node = do_QueryInterface(element); nsCOMPtr parentNode = do_QueryInterface(parent); result = editor->InsertNode(node, parentNode, position); + + if (dontChangeSelection) + editor->SetShouldTxnSetSelection(PR_TRUE); } return result; } diff --git a/mozilla/editor/base/nsHTMLEditor.cpp b/mozilla/editor/base/nsHTMLEditor.cpp index 2fd35c6157a..530b415c3bf 100644 --- a/mozilla/editor/base/nsHTMLEditor.cpp +++ b/mozilla/editor/base/nsHTMLEditor.cpp @@ -615,6 +615,9 @@ nsHTMLEditor::SetDocumentTitle(const nsAReadableString &aTitle) if (NS_SUCCEEDED(result)) { + //Don't let Rules System change the selection + nsAutoTxnsConserveSelection dontChangeSelection(this); + result = nsEditor::Do(txn); } // The transaction system (if any) has taken ownwership of txn diff --git a/mozilla/editor/base/nsTableEditor.cpp b/mozilla/editor/base/nsTableEditor.cpp index 804b61d162a..5fa326d6f24 100644 --- a/mozilla/editor/base/nsTableEditor.cpp +++ b/mozilla/editor/base/nsTableEditor.cpp @@ -366,7 +366,6 @@ nsHTMLEditor::GetNextRow(nsIDOMNode* aCurrentRowNode, nsIDOMNode **aRowNode) // We can encounter "__moz_text" nodes here -- must find a row while (nextRow && !IsRowNode(nextRow)) { - nsCOMPtr nextNode; res = nextRow->GetNextSibling(getter_AddRefs(nextNode)); if (NS_FAILED(res)) return res; diff --git a/mozilla/editor/composer/src/nsEditorShell.cpp b/mozilla/editor/composer/src/nsEditorShell.cpp index da9491f4bf5..f3b90c07628 100644 --- a/mozilla/editor/composer/src/nsEditorShell.cpp +++ b/mozilla/editor/composer/src/nsEditorShell.cpp @@ -3536,19 +3536,27 @@ nsEditorShell::DeleteElement(nsIDOMElement *element) } NS_IMETHODIMP -nsEditorShell::InsertElement(nsIDOMElement *element, nsIDOMElement *parent, PRInt32 position) +nsEditorShell::InsertElement(nsIDOMElement *element, nsIDOMElement *parent, PRInt32 position, PRBool dontChangeSelection) { if (!element || !parent) return NS_ERROR_NULL_POINTER; nsresult result = NS_NOINTERFACE; nsCOMPtr editor = do_QueryInterface(mEditor); - if (editor) { + if (editor) + { + // Set flag so InsertElementTxn doesn't change the selection + if (dontChangeSelection) + editor->SetShouldTxnSetSelection(PR_FALSE); + // The nsIEditor::InsertNode() wants nodes as params, // but it actually requires that they are elements! nsCOMPtr node = do_QueryInterface(element); nsCOMPtr parentNode = do_QueryInterface(parent); result = editor->InsertNode(node, parentNode, position); + + if (dontChangeSelection) + editor->SetShouldTxnSetSelection(PR_TRUE); } return result; } diff --git a/mozilla/editor/idl/nsIEditor.idl b/mozilla/editor/idl/nsIEditor.idl index ae8d726f5e6..3e5d25a0ec6 100644 --- a/mozilla/editor/idl/nsIEditor.idl +++ b/mozilla/editor/idl/nsIEditor.idl @@ -255,6 +255,15 @@ interface nsIEditor : nsISupports void EndPlaceHolderTransaction(); void ShouldTxnSetSelection(out boolean result); + /** Set the flag that prevents InsertElementTxn from changing the selection + * @param should Set false to suppress changing the selection; + * i.e., before using InsertElement() to insert under element + * WARNING: You must be very careful to reset back to PR_TRUE after + * setting PR_FALSE, else selection/caret is trashed + * for further editing + */ + void SetShouldTxnSetSelection(in boolean should); + /* ------------ Clipboard methods -------------- */ /** cut the currently selected text, putting it into the OS clipboard diff --git a/mozilla/editor/idl/nsIEditorShell.idl b/mozilla/editor/idl/nsIEditorShell.idl index 8cba8ae5ec6..af0158bf097 100644 --- a/mozilla/editor/idl/nsIEditorShell.idl +++ b/mozilla/editor/idl/nsIEditorShell.idl @@ -318,13 +318,16 @@ interface nsIEditorShell : nsISupports * Insert an element under parent at position. * No checking is done to verify the legality of the insertion. * That is the responsibility of the caller. - * element: The DOM element to insert. - * parent: The element to insert the new object under - * position: The place in parent to insert the new element - * 0=first child, 1=second child, etc. - * any number > number of current children = last child + * element: The DOM element to insert. + * parent: The element to insert the new object under + * position: The place in parent to insert the new element + * 0=first child, 1=second child, etc. + * any number > number of current children = last child + * dontChangeSelection If false, selection is set to just after new element + * If true, preserve existing selection + * (use when inserting under element) */ - void InsertElement(in nsIDOMElement element, in nsIDOMElement parent, in PRInt32 position); + void InsertElement(in nsIDOMElement element, in nsIDOMElement parent, in PRInt32 position, in PRBool dontChangeSelection); /** * DeleteNode removes element from the document diff --git a/mozilla/editor/libeditor/base/nsEditor.cpp b/mozilla/editor/libeditor/base/nsEditor.cpp index 019d3ef4e8a..b015fd40575 100644 --- a/mozilla/editor/libeditor/base/nsEditor.cpp +++ b/mozilla/editor/libeditor/base/nsEditor.cpp @@ -742,13 +742,20 @@ nsEditor::EndPlaceHolderTransaction() return NS_OK; } -NS_IMETHODIMP nsEditor::ShouldTxnSetSelection(PRBool *aResult) +NS_IMETHODIMP +nsEditor::ShouldTxnSetSelection(PRBool *aResult) { if (!aResult) return NS_ERROR_NULL_POINTER; *aResult = mShouldTxnSetSelection; return NS_OK; } +NS_IMETHODIMP +nsEditor::SetShouldTxnSetSelection(PRBool aShould) +{ + mShouldTxnSetSelection = aShould; + return NS_OK; +} NS_IMETHODIMP nsEditor::GetDocumentIsEmpty(PRBool *aDocumentIsEmpty) @@ -4316,12 +4323,6 @@ nsEditor::GetShouldTxnSetSelection() } -void -nsEditor::SetShouldTxnSetSelection(PRBool aShould) -{ - mShouldTxnSetSelection = aShould; -} - #ifdef XP_MAC #pragma mark - #pragma mark protected nsEditor methods diff --git a/mozilla/editor/libeditor/base/nsEditor.h b/mozilla/editor/libeditor/base/nsEditor.h index ddf0db75bab..d8fc94cd693 100644 --- a/mozilla/editor/libeditor/base/nsEditor.h +++ b/mozilla/editor/libeditor/base/nsEditor.h @@ -516,7 +516,6 @@ public: nsresult EndUpdateViewBatch(void); PRBool GetShouldTxnSetSelection(); - void SetShouldTxnSetSelection(PRBool aShould); public: // Argh! These transaction names are used by PlaceholderTxn and diff --git a/mozilla/editor/libeditor/html/nsHTMLEditor.cpp b/mozilla/editor/libeditor/html/nsHTMLEditor.cpp index 2fd35c6157a..530b415c3bf 100644 --- a/mozilla/editor/libeditor/html/nsHTMLEditor.cpp +++ b/mozilla/editor/libeditor/html/nsHTMLEditor.cpp @@ -615,6 +615,9 @@ nsHTMLEditor::SetDocumentTitle(const nsAReadableString &aTitle) if (NS_SUCCEEDED(result)) { + //Don't let Rules System change the selection + nsAutoTxnsConserveSelection dontChangeSelection(this); + result = nsEditor::Do(txn); } // The transaction system (if any) has taken ownwership of txn diff --git a/mozilla/editor/libeditor/html/nsTableEditor.cpp b/mozilla/editor/libeditor/html/nsTableEditor.cpp index 804b61d162a..5fa326d6f24 100644 --- a/mozilla/editor/libeditor/html/nsTableEditor.cpp +++ b/mozilla/editor/libeditor/html/nsTableEditor.cpp @@ -366,7 +366,6 @@ nsHTMLEditor::GetNextRow(nsIDOMNode* aCurrentRowNode, nsIDOMNode **aRowNode) // We can encounter "__moz_text" nodes here -- must find a row while (nextRow && !IsRowNode(nextRow)) { - nsCOMPtr nextNode; res = nextRow->GetNextSibling(getter_AddRefs(nextNode)); if (NS_FAILED(res)) return res; diff --git a/mozilla/editor/ui/dialogs/content/EdDialogCommon.js b/mozilla/editor/ui/dialogs/content/EdDialogCommon.js index 3ae80151705..1d585bc1ead 100644 --- a/mozilla/editor/ui/dialogs/content/EdDialogCommon.js +++ b/mozilla/editor/ui/dialogs/content/EdDialogCommon.js @@ -1049,7 +1049,8 @@ function AppendHeadElement(element) position = head.childNodes.length; // Use editor's undoable transaction - editorShell.InsertElement(element, head, position); + // Last param "true" says "don't change the selection" + editorShell.InsertElement(element, head, position, true); } }