From c59396c61b6a15fff5f5679e9b4404defa95ecbc Mon Sep 17 00:00:00 2001 From: "buster%netscape.com" Date: Wed, 5 May 1999 04:51:54 +0000 Subject: [PATCH] added a param to GetPriorNode and GetNextNode to tell these methods whether to use or skip non-editable content. added some comments, turned off some debugging flags. git-svn-id: svn://10.0.0.236/trunk@30373 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/editor/base/nsEditor.cpp | 83 ++++++++++++++++--- mozilla/editor/base/nsEditor.h | 36 +++++--- mozilla/editor/base/nsHTMLEditor.cpp | 6 +- mozilla/editor/base/nsTextEditor.cpp | 4 +- mozilla/editor/libeditor/base/nsEditor.cpp | 83 ++++++++++++++++--- mozilla/editor/libeditor/base/nsEditor.h | 36 +++++--- .../editor/libeditor/html/nsHTMLEditor.cpp | 6 +- 7 files changed, 200 insertions(+), 54 deletions(-) diff --git a/mozilla/editor/base/nsEditor.cpp b/mozilla/editor/base/nsEditor.cpp index 70baa157ba1..5a0eff8f462 100644 --- a/mozilla/editor/base/nsEditor.cpp +++ b/mozilla/editor/base/nsEditor.cpp @@ -150,7 +150,7 @@ const char* nsEditor::kMOZEditorBogusNodeAttr="MOZ_EDITOR_BOGUS_NODE"; const char* nsEditor::kMOZEditorBogusNodeValue="TRUE"; #ifdef NS_DEBUG_EDITOR -static PRBool gNoisy = PR_TRUE; +static PRBool gNoisy = PR_FALSE; #else static const PRBool gNoisy = PR_FALSE; #endif @@ -1622,7 +1622,7 @@ nsEditor::CreateTxnForDeleteInsertionPoint(nsIDOMRange *aRange, if ((nsIEditor::eRTL==aDir) && (PR_TRUE==isFirst)) { // we're backspacing from the beginning of the node. Delete the first thing to our left nsCOMPtr priorNode; - result = GetPriorNode(node, getter_AddRefs(priorNode)); + result = GetPriorNode(node, PR_TRUE, getter_AddRefs(priorNode)); if ((NS_SUCCEEDED(result)) && priorNode) { // there is a priorNode, so delete it's last child (if text content, delete the last char.) // if it has no children, delete it @@ -1659,7 +1659,7 @@ nsEditor::CreateTxnForDeleteInsertionPoint(nsIDOMRange *aRange, else if ((nsIEditor::eLTR==aDir) && (PR_TRUE==isLast)) { // we're deleting from the end of the node. Delete the first thing to our right nsCOMPtr nextNode; - result = GetNextNode(node, getter_AddRefs(nextNode)); + result = GetNextNode(node, PR_TRUE, getter_AddRefs(nextNode)); if ((NS_SUCCEEDED(result)) && nextNode) { // there is a priorNode, so delete it's last child (if text content, delete the last char.) // if it has no children, delete it @@ -2343,19 +2343,37 @@ nsEditor::IntermediateNodesAreInline(nsIDOMRange *aRange, nsresult -nsEditor::GetPriorNode(nsIDOMNode *aCurrentNode, nsIDOMNode **aResultNode) +nsEditor::GetPriorNode(nsIDOMNode *aCurrentNode, + PRBool aEditableNode, + nsIDOMNode **aResultNode) { nsresult result; - *aResultNode = nsnull; + if (!aCurrentNode || !aResultNode) { return NS_ERROR_NULL_POINTER; } + + *aResultNode = nsnull; // init out-param + // if aCurrentNode has a left sibling, return that sibling's rightmost child (or itself if it has no children) result = aCurrentNode->GetPreviousSibling(aResultNode); if ((NS_SUCCEEDED(result)) && *aResultNode) - return GetRightmostChild(*aResultNode, aResultNode); + { + result = GetRightmostChild(*aResultNode, aResultNode); + if (NS_FAILED(result)) { return result; } + if (PR_FALSE==aEditableNode) { + return result; + } + if (PR_TRUE==IsEditable(*aResultNode)) { + return result; + } + else + { // restart the search from the non-editable node we just found + nsCOMPtr notEditableNode = do_QueryInterface(*aResultNode); + return GetPriorNode(notEditableNode, aEditableNode, aResultNode); + } + } // otherwise, walk up the parent change until there is a child that comes before // the ancestor of aCurrentNode. Then return that node's rightmost child - - nsCOMPtr parent(do_QueryInterface(aCurrentNode)); + nsCOMPtr parent = do_QueryInterface(aCurrentNode); do { nsCOMPtr node(parent); result = node->GetParentNode(getter_AddRefs(parent)); @@ -2364,8 +2382,19 @@ nsEditor::GetPriorNode(nsIDOMNode *aCurrentNode, nsIDOMNode **aResultNode) result = parent->GetPreviousSibling(getter_AddRefs(node)); if ((NS_SUCCEEDED(result)) && node) { - - return GetRightmostChild(node, aResultNode); + result = GetRightmostChild(node, aResultNode); + if (NS_FAILED(result)) { return result; } + if (PR_FALSE==aEditableNode) { + return result; + } + if (PR_TRUE==IsEditable(*aResultNode)) { + return result; + } + else + { // restart the search from the non-editable node we just found + nsCOMPtr notEditableNode = do_QueryInterface(*aResultNode); + return GetPriorNode(notEditableNode, aEditableNode, aResultNode); + } } } } while ((NS_SUCCEEDED(result)) && parent); @@ -2374,14 +2403,30 @@ nsEditor::GetPriorNode(nsIDOMNode *aCurrentNode, nsIDOMNode **aResultNode) } nsresult -nsEditor::GetNextNode(nsIDOMNode *aCurrentNode, nsIDOMNode **aResultNode) +nsEditor::GetNextNode(nsIDOMNode *aCurrentNode, + PRBool aEditableNode, + nsIDOMNode **aResultNode) { nsresult result; *aResultNode = nsnull; // if aCurrentNode has a right sibling, return that sibling's leftmost child (or itself if it has no children) result = aCurrentNode->GetNextSibling(aResultNode); if ((NS_SUCCEEDED(result)) && *aResultNode) - return GetLeftmostChild(*aResultNode, aResultNode); + { + result = GetLeftmostChild(*aResultNode, aResultNode); + if (NS_FAILED(result)) { return result; } + if (PR_FALSE==aEditableNode) { + return result; + } + if (PR_TRUE==IsEditable(*aResultNode)) { + return result; + } + else + { // restart the search from the non-editable node we just found + nsCOMPtr notEditableNode = do_QueryInterface(*aResultNode); + return GetNextNode(notEditableNode, aEditableNode, aResultNode); + } + } // otherwise, walk up the parent change until there is a child that comes before // the ancestor of aCurrentNode. Then return that node's rightmost child @@ -2395,7 +2440,19 @@ nsEditor::GetNextNode(nsIDOMNode *aCurrentNode, nsIDOMNode **aResultNode) result = parent->GetNextSibling(getter_AddRefs(node)); if ((NS_SUCCEEDED(result)) && node) { - return GetLeftmostChild(node, aResultNode); + result = GetLeftmostChild(node, aResultNode); + if (NS_FAILED(result)) { return result; } + if (PR_FALSE==aEditableNode) { + return result; + } + if (PR_TRUE==IsEditable(*aResultNode)) { + return result; + } + else + { // restart the search from the non-editable node we just found + nsCOMPtr notEditableNode = do_QueryInterface(*aResultNode); + return GetNextNode(notEditableNode, aEditableNode, aResultNode); + } } } } while ((NS_SUCCEEDED(result)) && parent); diff --git a/mozilla/editor/base/nsEditor.h b/mozilla/editor/base/nsEditor.h index 86d42bbfb37..375bc818459 100644 --- a/mozilla/editor/base/nsEditor.h +++ b/mozilla/editor/base/nsEditor.h @@ -384,20 +384,36 @@ public: */ static nsresult GetLengthOfDOMNode(nsIDOMNode *aNode, PRUint32 &aCount); - /** - */ - static nsresult GetPriorNode(nsIDOMNode *aCurrentNode, nsIDOMNode **aResultNode); + /** get the node immediately prior to aCurrentNode + * @param aCurrentNode the node from which we start the search + * @param aEditableNode if PR_TRUE, only return an editable node + * @param aResultNode [OUT] the node that occurs before aCurrentNode in the tree, + * skipping non-editable nodes if aEditableNode is PR_TRUE. + * If there is no prior node, aResultNode will be nsnull. + */ + static nsresult GetPriorNode(nsIDOMNode *aCurrentNode, + PRBool aEditableNode, + nsIDOMNode **aResultNode); - /** - */ - static nsresult GetNextNode(nsIDOMNode *aCurrentNode, nsIDOMNode **aResultNode); + /** get the node immediately after to aCurrentNode + * @param aCurrentNode the node from which we start the search + * @param aEditableNode if PR_TRUE, only return an editable node + * @param aResultNode [OUT] the node that occurs after aCurrentNode in the tree, + * skipping non-editable nodes if aEditableNode is PR_TRUE. + * If there is no prior node, aResultNode will be nsnull. + */ + static nsresult GetNextNode(nsIDOMNode *aCurrentNode, + PRBool aEditableNode, + nsIDOMNode **aResultNode); - /** - */ + /** Get the rightmost child of aCurrentNode, and return it in aResultNode + * aResultNode is set to nsnull if aCurrentNode has no children. + */ static nsresult GetRightmostChild(nsIDOMNode *aCurrentNode, nsIDOMNode **aResultNode); - /** - */ + /** Get the leftmost child of aCurrentNode, and return it in aResultNode + * aResultNode is set to nsnull if aCurrentNode has no children. + */ static nsresult GetLeftmostChild(nsIDOMNode *aCurrentNode, nsIDOMNode **aResultNode); /** GetFirstTextNode ADDREFFS and will get the next available text node from the passed diff --git a/mozilla/editor/base/nsHTMLEditor.cpp b/mozilla/editor/base/nsHTMLEditor.cpp index 22ce3578337..d684641b61c 100644 --- a/mozilla/editor/base/nsHTMLEditor.cpp +++ b/mozilla/editor/base/nsHTMLEditor.cpp @@ -58,7 +58,7 @@ static NS_DEFINE_CID(kCContentIteratorCID, NS_CONTENTITERATOR_CID); static NS_DEFINE_IID(kIContentIteratorIID, NS_ICONTENTITERTOR_IID); #ifdef NS_DEBUG -static PRBool gNoisy = PR_TRUE; +static PRBool gNoisy = PR_FALSE; #else static const PRBool gNoisy = PR_FALSE; #endif @@ -771,7 +771,7 @@ nsHTMLEditor::ReParentBlockContent(nsIDOMNode *aNode, { // if the prior node is a
and we did something to change vertical whitespacing, delete the
nsCOMPtr brNode; - result = GetPriorNode(leftNode, getter_AddRefs(brNode)); + result = GetPriorNode(leftNode, PR_TRUE, getter_AddRefs(brNode)); if (NS_SUCCEEDED(result) && brNode) { nsCOMPtr brContent = do_QueryInterface(brNode); @@ -788,7 +788,7 @@ nsHTMLEditor::ReParentBlockContent(nsIDOMNode *aNode, // if the next node is a
and we did something to change vertical whitespacing, delete the
if (NS_SUCCEEDED(result)) { - result = GetNextNode(rightNode, getter_AddRefs(brNode)); + result = GetNextNode(rightNode, PR_TRUE, getter_AddRefs(brNode)); if (NS_SUCCEEDED(result) && brNode) { nsCOMPtr brContent = do_QueryInterface(brNode); diff --git a/mozilla/editor/base/nsTextEditor.cpp b/mozilla/editor/base/nsTextEditor.cpp index e24119d2ef1..2090f2b5e08 100644 --- a/mozilla/editor/base/nsTextEditor.cpp +++ b/mozilla/editor/base/nsTextEditor.cpp @@ -95,7 +95,7 @@ static NS_DEFINE_IID(kIInputStreamIID, NS_IINPUTSTREAM_IID); static NS_DEFINE_IID(kIOutputStreamIID, NS_IOUTPUTSTREAM_IID); #ifdef NS_DEBUG -static PRBool gNoisy = PR_TRUE; +static PRBool gNoisy = PR_FALSE; #else static const PRBool gNoisy = PR_FALSE; #endif @@ -1910,7 +1910,7 @@ nsTextEditor::RemoveTextPropertiesForNodeWithDifferentParents(nsIDOMNode *aStar // compute the start node nsCOMPtrstartNode = do_QueryInterface(aStartNode); if (PR_TRUE==skippedStartNode) { - nsEditor::GetNextNode(aStartNode, getter_AddRefs(startNode)); + nsEditor::GetNextNode(aStartNode, PR_TRUE, getter_AddRefs(startNode)); } range->SetStart(startNode, rangeStartOffset); range->SetEnd(aEndNode, rangeEndOffset); diff --git a/mozilla/editor/libeditor/base/nsEditor.cpp b/mozilla/editor/libeditor/base/nsEditor.cpp index 70baa157ba1..5a0eff8f462 100644 --- a/mozilla/editor/libeditor/base/nsEditor.cpp +++ b/mozilla/editor/libeditor/base/nsEditor.cpp @@ -150,7 +150,7 @@ const char* nsEditor::kMOZEditorBogusNodeAttr="MOZ_EDITOR_BOGUS_NODE"; const char* nsEditor::kMOZEditorBogusNodeValue="TRUE"; #ifdef NS_DEBUG_EDITOR -static PRBool gNoisy = PR_TRUE; +static PRBool gNoisy = PR_FALSE; #else static const PRBool gNoisy = PR_FALSE; #endif @@ -1622,7 +1622,7 @@ nsEditor::CreateTxnForDeleteInsertionPoint(nsIDOMRange *aRange, if ((nsIEditor::eRTL==aDir) && (PR_TRUE==isFirst)) { // we're backspacing from the beginning of the node. Delete the first thing to our left nsCOMPtr priorNode; - result = GetPriorNode(node, getter_AddRefs(priorNode)); + result = GetPriorNode(node, PR_TRUE, getter_AddRefs(priorNode)); if ((NS_SUCCEEDED(result)) && priorNode) { // there is a priorNode, so delete it's last child (if text content, delete the last char.) // if it has no children, delete it @@ -1659,7 +1659,7 @@ nsEditor::CreateTxnForDeleteInsertionPoint(nsIDOMRange *aRange, else if ((nsIEditor::eLTR==aDir) && (PR_TRUE==isLast)) { // we're deleting from the end of the node. Delete the first thing to our right nsCOMPtr nextNode; - result = GetNextNode(node, getter_AddRefs(nextNode)); + result = GetNextNode(node, PR_TRUE, getter_AddRefs(nextNode)); if ((NS_SUCCEEDED(result)) && nextNode) { // there is a priorNode, so delete it's last child (if text content, delete the last char.) // if it has no children, delete it @@ -2343,19 +2343,37 @@ nsEditor::IntermediateNodesAreInline(nsIDOMRange *aRange, nsresult -nsEditor::GetPriorNode(nsIDOMNode *aCurrentNode, nsIDOMNode **aResultNode) +nsEditor::GetPriorNode(nsIDOMNode *aCurrentNode, + PRBool aEditableNode, + nsIDOMNode **aResultNode) { nsresult result; - *aResultNode = nsnull; + if (!aCurrentNode || !aResultNode) { return NS_ERROR_NULL_POINTER; } + + *aResultNode = nsnull; // init out-param + // if aCurrentNode has a left sibling, return that sibling's rightmost child (or itself if it has no children) result = aCurrentNode->GetPreviousSibling(aResultNode); if ((NS_SUCCEEDED(result)) && *aResultNode) - return GetRightmostChild(*aResultNode, aResultNode); + { + result = GetRightmostChild(*aResultNode, aResultNode); + if (NS_FAILED(result)) { return result; } + if (PR_FALSE==aEditableNode) { + return result; + } + if (PR_TRUE==IsEditable(*aResultNode)) { + return result; + } + else + { // restart the search from the non-editable node we just found + nsCOMPtr notEditableNode = do_QueryInterface(*aResultNode); + return GetPriorNode(notEditableNode, aEditableNode, aResultNode); + } + } // otherwise, walk up the parent change until there is a child that comes before // the ancestor of aCurrentNode. Then return that node's rightmost child - - nsCOMPtr parent(do_QueryInterface(aCurrentNode)); + nsCOMPtr parent = do_QueryInterface(aCurrentNode); do { nsCOMPtr node(parent); result = node->GetParentNode(getter_AddRefs(parent)); @@ -2364,8 +2382,19 @@ nsEditor::GetPriorNode(nsIDOMNode *aCurrentNode, nsIDOMNode **aResultNode) result = parent->GetPreviousSibling(getter_AddRefs(node)); if ((NS_SUCCEEDED(result)) && node) { - - return GetRightmostChild(node, aResultNode); + result = GetRightmostChild(node, aResultNode); + if (NS_FAILED(result)) { return result; } + if (PR_FALSE==aEditableNode) { + return result; + } + if (PR_TRUE==IsEditable(*aResultNode)) { + return result; + } + else + { // restart the search from the non-editable node we just found + nsCOMPtr notEditableNode = do_QueryInterface(*aResultNode); + return GetPriorNode(notEditableNode, aEditableNode, aResultNode); + } } } } while ((NS_SUCCEEDED(result)) && parent); @@ -2374,14 +2403,30 @@ nsEditor::GetPriorNode(nsIDOMNode *aCurrentNode, nsIDOMNode **aResultNode) } nsresult -nsEditor::GetNextNode(nsIDOMNode *aCurrentNode, nsIDOMNode **aResultNode) +nsEditor::GetNextNode(nsIDOMNode *aCurrentNode, + PRBool aEditableNode, + nsIDOMNode **aResultNode) { nsresult result; *aResultNode = nsnull; // if aCurrentNode has a right sibling, return that sibling's leftmost child (or itself if it has no children) result = aCurrentNode->GetNextSibling(aResultNode); if ((NS_SUCCEEDED(result)) && *aResultNode) - return GetLeftmostChild(*aResultNode, aResultNode); + { + result = GetLeftmostChild(*aResultNode, aResultNode); + if (NS_FAILED(result)) { return result; } + if (PR_FALSE==aEditableNode) { + return result; + } + if (PR_TRUE==IsEditable(*aResultNode)) { + return result; + } + else + { // restart the search from the non-editable node we just found + nsCOMPtr notEditableNode = do_QueryInterface(*aResultNode); + return GetNextNode(notEditableNode, aEditableNode, aResultNode); + } + } // otherwise, walk up the parent change until there is a child that comes before // the ancestor of aCurrentNode. Then return that node's rightmost child @@ -2395,7 +2440,19 @@ nsEditor::GetNextNode(nsIDOMNode *aCurrentNode, nsIDOMNode **aResultNode) result = parent->GetNextSibling(getter_AddRefs(node)); if ((NS_SUCCEEDED(result)) && node) { - return GetLeftmostChild(node, aResultNode); + result = GetLeftmostChild(node, aResultNode); + if (NS_FAILED(result)) { return result; } + if (PR_FALSE==aEditableNode) { + return result; + } + if (PR_TRUE==IsEditable(*aResultNode)) { + return result; + } + else + { // restart the search from the non-editable node we just found + nsCOMPtr notEditableNode = do_QueryInterface(*aResultNode); + return GetNextNode(notEditableNode, aEditableNode, aResultNode); + } } } } while ((NS_SUCCEEDED(result)) && parent); diff --git a/mozilla/editor/libeditor/base/nsEditor.h b/mozilla/editor/libeditor/base/nsEditor.h index 86d42bbfb37..375bc818459 100644 --- a/mozilla/editor/libeditor/base/nsEditor.h +++ b/mozilla/editor/libeditor/base/nsEditor.h @@ -384,20 +384,36 @@ public: */ static nsresult GetLengthOfDOMNode(nsIDOMNode *aNode, PRUint32 &aCount); - /** - */ - static nsresult GetPriorNode(nsIDOMNode *aCurrentNode, nsIDOMNode **aResultNode); + /** get the node immediately prior to aCurrentNode + * @param aCurrentNode the node from which we start the search + * @param aEditableNode if PR_TRUE, only return an editable node + * @param aResultNode [OUT] the node that occurs before aCurrentNode in the tree, + * skipping non-editable nodes if aEditableNode is PR_TRUE. + * If there is no prior node, aResultNode will be nsnull. + */ + static nsresult GetPriorNode(nsIDOMNode *aCurrentNode, + PRBool aEditableNode, + nsIDOMNode **aResultNode); - /** - */ - static nsresult GetNextNode(nsIDOMNode *aCurrentNode, nsIDOMNode **aResultNode); + /** get the node immediately after to aCurrentNode + * @param aCurrentNode the node from which we start the search + * @param aEditableNode if PR_TRUE, only return an editable node + * @param aResultNode [OUT] the node that occurs after aCurrentNode in the tree, + * skipping non-editable nodes if aEditableNode is PR_TRUE. + * If there is no prior node, aResultNode will be nsnull. + */ + static nsresult GetNextNode(nsIDOMNode *aCurrentNode, + PRBool aEditableNode, + nsIDOMNode **aResultNode); - /** - */ + /** Get the rightmost child of aCurrentNode, and return it in aResultNode + * aResultNode is set to nsnull if aCurrentNode has no children. + */ static nsresult GetRightmostChild(nsIDOMNode *aCurrentNode, nsIDOMNode **aResultNode); - /** - */ + /** Get the leftmost child of aCurrentNode, and return it in aResultNode + * aResultNode is set to nsnull if aCurrentNode has no children. + */ static nsresult GetLeftmostChild(nsIDOMNode *aCurrentNode, nsIDOMNode **aResultNode); /** GetFirstTextNode ADDREFFS and will get the next available text node from the passed diff --git a/mozilla/editor/libeditor/html/nsHTMLEditor.cpp b/mozilla/editor/libeditor/html/nsHTMLEditor.cpp index 22ce3578337..d684641b61c 100644 --- a/mozilla/editor/libeditor/html/nsHTMLEditor.cpp +++ b/mozilla/editor/libeditor/html/nsHTMLEditor.cpp @@ -58,7 +58,7 @@ static NS_DEFINE_CID(kCContentIteratorCID, NS_CONTENTITERATOR_CID); static NS_DEFINE_IID(kIContentIteratorIID, NS_ICONTENTITERTOR_IID); #ifdef NS_DEBUG -static PRBool gNoisy = PR_TRUE; +static PRBool gNoisy = PR_FALSE; #else static const PRBool gNoisy = PR_FALSE; #endif @@ -771,7 +771,7 @@ nsHTMLEditor::ReParentBlockContent(nsIDOMNode *aNode, { // if the prior node is a
and we did something to change vertical whitespacing, delete the
nsCOMPtr brNode; - result = GetPriorNode(leftNode, getter_AddRefs(brNode)); + result = GetPriorNode(leftNode, PR_TRUE, getter_AddRefs(brNode)); if (NS_SUCCEEDED(result) && brNode) { nsCOMPtr brContent = do_QueryInterface(brNode); @@ -788,7 +788,7 @@ nsHTMLEditor::ReParentBlockContent(nsIDOMNode *aNode, // if the next node is a
and we did something to change vertical whitespacing, delete the
if (NS_SUCCEEDED(result)) { - result = GetNextNode(rightNode, getter_AddRefs(brNode)); + result = GetNextNode(rightNode, PR_TRUE, getter_AddRefs(brNode)); if (NS_SUCCEEDED(result) && brNode) { nsCOMPtr brContent = do_QueryInterface(brNode);