From cbaf88c3e7db3565a965ddb8f0b7e41b962560ed Mon Sep 17 00:00:00 2001 From: "buster%netscape.com" Date: Tue, 20 Apr 1999 22:02:02 +0000 Subject: [PATCH] more factoring git-svn-id: svn://10.0.0.236/trunk@28331 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/editor/base/JoinElementTxn.cpp | 27 ----- mozilla/editor/base/TextEditorTest.cpp | 17 ++- mozilla/editor/base/nsTextEditor.cpp | 109 +++++++++++------- mozilla/editor/base/nsTextEditor.h | 35 +++++- .../editor/libeditor/base/JoinElementTxn.cpp | 27 ----- .../editor/libeditor/html/TextEditorTest.cpp | 17 ++- 6 files changed, 128 insertions(+), 104 deletions(-) diff --git a/mozilla/editor/base/JoinElementTxn.cpp b/mozilla/editor/base/JoinElementTxn.cpp index 46ba27dbe7c..ed076f1d533 100644 --- a/mozilla/editor/base/JoinElementTxn.cpp +++ b/mozilla/editor/base/JoinElementTxn.cpp @@ -150,33 +150,6 @@ NS_IMETHODIMP JoinElementTxn::Undo(void) } -/* -NS_IMETHODIMP JoinElementTxn::Redo(void) -{ - if (gNoisy) { printf("Redo Join\n"); } - nsresult result; - nsCOMPtr editor; - result = mEditor->QueryInterface(kIEditorSupportIID, getter_AddRefs(editor)); - if (NS_SUCCEEDED(result) && editor) - { - result = editor->JoinNodesImpl(mRightNode, mLeftNode, mParent, PR_FALSE); - if (NS_SUCCEEDED(result)) - { - nsCOMPtrselection; - mEditor->GetSelection(getter_AddRefs(selection)); - if (selection) - { - selection->Collapse(mRightNode, mOffset); - } - } - } - else { - result = NS_ERROR_NOT_IMPLEMENTED; - } - return result; -} -*/ - NS_IMETHODIMP JoinElementTxn::GetIsTransient(PRBool *aIsTransient) { if (nsnull!=aIsTransient) diff --git a/mozilla/editor/base/TextEditorTest.cpp b/mozilla/editor/base/TextEditorTest.cpp index f07e8e7bd93..3d4d8641125 100644 --- a/mozilla/editor/base/TextEditorTest.cpp +++ b/mozilla/editor/base/TextEditorTest.cpp @@ -63,7 +63,7 @@ nsresult TextEditorTest::RunUnitTest() TEST_RESULT(result); // insert some more text - nsString docContent2("Moreover, I am cognizant of the interrelatedness of all communities and states. I cannot sit idly by in Atlanta and not be concerned about what happens in Birmingham. Injustice anywhere is a threat to justice everywhere."); + nsString docContent2("Moreover, I am cognizant of the interrelatedness of all communities and states. I cannot sit idly by in Atlanta and not be concerned about what happens in Birmingham. Injustice anywhere is a threat to justice everywhere"); result = mTextEditor->InsertText(docContent2); TEST_RESULT(result); @@ -194,8 +194,20 @@ nsresult TextEditorTest::TestTextProperties() NS_ASSERTION(PR_TRUE==any, "any should be true"); NS_ASSERTION(PR_TRUE==all, "all should be true"); mEditor->DebugDumpContent(); - /* I need a way of setting the selection I want here. Maybe use Kin's text service stuff? + // make all the text underlined, except for the first 2 and last 2 characters + result = doc->GetElementsByTagName(textTag, getter_AddRefs(nodeList)); + TEST_RESULT(result); + TEST_POINTER(nodeList.get()); + nodeList->GetLength(&count); + NS_ASSERTION(0!=count, "there are no text nodes in the document!"); + result = nodeList->Item(1, getter_AddRefs(textNode)); + TEST_RESULT(result); + TEST_POINTER(textNode.get()); + textData = do_QueryInterface(textNode); + textData->GetLength(&length); + selection->Collapse(textNode, 1); + selection->Extend(textNode, length-2); result = mTextEditor->SetTextProperty(nsIEditProperty::u, nsnull, nsnull); TEST_RESULT(result); @@ -205,7 +217,6 @@ nsresult TextEditorTest::TestTextProperties() NS_ASSERTION(PR_TRUE==any, "any should be true"); NS_ASSERTION(PR_TRUE==all, "all should be true"); mEditor->DebugDumpContent(); - */ return result; } diff --git a/mozilla/editor/base/nsTextEditor.cpp b/mozilla/editor/base/nsTextEditor.cpp index f74023663fd..bfbdf8fa5b9 100644 --- a/mozilla/editor/base/nsTextEditor.cpp +++ b/mozilla/editor/base/nsTextEditor.cpp @@ -1136,7 +1136,7 @@ NS_IMETHODIMP nsTextEditor::SetTextPropertiesForNode(nsIDOMNode *aNode, result = nsEditor::CreateNode(tag, aParent, 0, getter_AddRefs(newStyleNode)); if (NS_SUCCEEDED(result) && newStyleNode) { - result = MoveContentIntoNewParent(aNode, newStyleNode, aStartOffset, aEndOffset); + result = MoveContentOfNodeIntoNewParent(aNode, newStyleNode, aStartOffset, aEndOffset); if (NS_SUCCEEDED(result) && newStyleNode) { if (aAttribute) @@ -1158,13 +1158,13 @@ NS_IMETHODIMP nsTextEditor::SetTextPropertiesForNode(nsIDOMNode *aNode, return result; } -NS_IMETHODIMP nsTextEditor::MoveContentIntoNewParent(nsIDOMNode *aNode, - nsIDOMNode *aNewParentNode, - PRInt32 aStartOffset, - PRInt32 aEndOffset) +NS_IMETHODIMP nsTextEditor::MoveContentOfNodeIntoNewParent(nsIDOMNode *aNode, + nsIDOMNode *aNewParentNode, + PRInt32 aStartOffset, + PRInt32 aEndOffset) { if (!aNode || !aNewParentNode) { return NS_ERROR_NULL_POINTER; } - if (gNoisy) { printf("nsTextEditor::MoveContentIntoNewParent\n"); } + if (gNoisy) { printf("nsTextEditor::MoveContentOfNodeIntoNewParent\n"); } nsresult result=NS_OK; PRUint32 count; @@ -1491,43 +1491,73 @@ nsTextEditor::SetTextPropertiesForNodesWithSameParent(nsIDOMNode *aStartNode, IsTextPropertySetByContent(aStartNode, aPropName, aAttribute, aValue, textPropertySet, getter_AddRefs(resultNode)); if (PR_FALSE==textPropertySet) { - nsCOMPtrnewLeftTextNode; // this will be the middle text node - if (0!=aStartOffset) { - result = nsEditor::SplitNode(aStartNode, aStartOffset, getter_AddRefs(newLeftTextNode)); + nsAutoString tag; + aPropName->ToString(tag); + // create the new style node, which will be the new parent for the selected nodes + nsCOMPtrnewStyleNode; + result = nsEditor::CreateNode(tag, aParent, 0, getter_AddRefs(newStyleNode)); + if (NS_SUCCEEDED(result) && newStyleNode) + { + result = MoveContiguousContentIntoNewParent(aStartNode, aStartOffset, aEndNode, aEndOffset, aParent, newStyleNode); + if (NS_SUCCEEDED(result) && aAttribute) + { + nsCOMPtr newStyleElement; + newStyleElement = do_QueryInterface(newStyleNode); + nsAutoString value; + if (aValue) { + value = *aValue; + } + result = newStyleElement->SetAttribute(*aAttribute, value); + } + } + } + return result; +} + +NS_IMETHODIMP nsTextEditor::MoveContiguousContentIntoNewParent(nsIDOMNode *aStartNode, + PRInt32 aStartOffset, + nsIDOMNode *aEndNode, + PRInt32 aEndOffset, + nsIDOMNode *aGrandParentNode, + nsIDOMNode *aNewParentNode) +{ + if (!aStartNode || !aEndNode || !aNewParentNode) { return NS_ERROR_NULL_POINTER; } + if (gNoisy) { printf("nsTextEditor::MoveContiguousContentIntoNewParent\n"); } + + nsresult result = NS_OK; + nsCOMPtrnewLeftNode; // this will be the middle text node + if (0!=aStartOffset) { + result = nsEditor::SplitNode(aStartNode, aStartOffset, getter_AddRefs(newLeftNode)); + } + if (NS_SUCCEEDED(result)) + { + PRUint32 count; + GetLengthOfDOMNode(aEndNode, count); + nsCOMPtrnewRightNode; // this will be the middle text node + if ((PRInt32)count!=aEndOffset) { + result = nsEditor::SplitNode(aEndNode, aEndOffset, getter_AddRefs(newRightNode)); + } + else { + newRightNode = do_QueryInterface(aEndNode); } if (NS_SUCCEEDED(result)) { - nsCOMPtrendNodeAsChar; - endNodeAsChar = do_QueryInterface(aEndNode); - if (!endNodeAsChar) - return NS_ERROR_FAILURE; - PRUint32 count; - endNodeAsChar->GetLength(&count); - nsCOMPtrnewRightTextNode; // this will be the middle text node - if ((PRInt32)count!=aEndOffset) { - result = nsEditor::SplitNode(aEndNode, aEndOffset, getter_AddRefs(newRightTextNode)); + PRInt32 offsetInParent; + if (newLeftNode) { + result = nsIEditorSupport::GetChildOffset(newLeftNode, aGrandParentNode, offsetInParent); } else { - newRightTextNode = do_QueryInterface(aEndNode); + offsetInParent = -1; // relies on +1 below in call to CreateNode } if (NS_SUCCEEDED(result)) { - PRInt32 offsetInParent; - if (newLeftTextNode) { - result = nsIEditorSupport::GetChildOffset(newLeftTextNode, aParent, offsetInParent); - } - else { - offsetInParent = -1; // relies on +1 below in call to CreateNode - } + // wherever aNewParentNode is, delete it and insert it into aGrandParentNode + result = nsEditor::DeleteNode(aNewParentNode); if (NS_SUCCEEDED(result)) - { - nsAutoString tag; - aPropName->ToString(tag); - // create the new style node, which will be the new parent for the selected nodes - nsCOMPtrnewStyleNode; - result = nsEditor::CreateNode(tag, aParent, offsetInParent+1, getter_AddRefs(newStyleNode)); + { + result = nsEditor::InsertNode(aNewParentNode, aGrandParentNode, offsetInParent); if (NS_SUCCEEDED(result)) - { // move the right half of the start node into the new style node + { // move the right half of the start node into the new parent node nsCOMPtrintermediateNode; result = aStartNode->GetNextSibling(getter_AddRefs(intermediateNode)); if (NS_SUCCEEDED(result)) @@ -1536,10 +1566,10 @@ nsTextEditor::SetTextPropertiesForNodesWithSameParent(nsIDOMNode *aStartNode, if (NS_SUCCEEDED(result)) { PRInt32 childIndex=0; - result = nsEditor::InsertNode(aStartNode, newStyleNode, childIndex); + result = nsEditor::InsertNode(aStartNode, aNewParentNode, childIndex); childIndex++; if (NS_SUCCEEDED(result)) - { // move all the intermediate nodes into the new style node + { // move all the intermediate nodes into the new parent node nsCOMPtrnextSibling; while (intermediateNode.get() != aEndNode) { @@ -1552,17 +1582,17 @@ nsTextEditor::SetTextPropertiesForNodesWithSameParent(nsIDOMNode *aStartNode, intermediateNode->GetNextSibling(getter_AddRefs(nextSibling)); result = nsEditor::DeleteNode(intermediateNode); if (NS_SUCCEEDED(result)) { - result = nsEditor::InsertNode(intermediateNode, newStyleNode, childIndex); + result = nsEditor::InsertNode(intermediateNode, aNewParentNode, childIndex); childIndex++; } intermediateNode = do_QueryInterface(nextSibling); } if (NS_SUCCEEDED(result)) - { // move the left half of the end node into the new style node - result = nsEditor::DeleteNode(newRightTextNode); + { // move the left half of the end node into the new parent node + result = nsEditor::DeleteNode(newRightNode); if (NS_SUCCEEDED(result)) { - result = nsEditor::InsertNode(newRightTextNode, newStyleNode, childIndex); + result = nsEditor::InsertNode(newRightNode, aNewParentNode, childIndex); } } } @@ -1576,6 +1606,7 @@ nsTextEditor::SetTextPropertiesForNodesWithSameParent(nsIDOMNode *aStartNode, return result; } + /* this wraps every selected text node in a new inline style node if needed the text nodes are treated as being unique -- each needs it's own style node if the style is not already present. diff --git a/mozilla/editor/base/nsTextEditor.h b/mozilla/editor/base/nsTextEditor.h index 396f8ab2150..5136d225a44 100644 --- a/mozilla/editor/base/nsTextEditor.h +++ b/mozilla/editor/base/nsTextEditor.h @@ -170,14 +170,39 @@ protected: * position of all leaf content. * @param aNode The node whose content we're repositioning. * aNode can be either a text node or a container node. - * @param aNewParentNode The node that will be the repositioned contents' parent + * @param aNewParentNode The node that will be the repositioned contents' parent. + * The caller is responsible for allocating aNewParentNode * @param aStartOffset The start offset of the content of aNode * @param aEndOffset The end offset of the content of aNode. */ - NS_IMETHOD MoveContentIntoNewParent(nsIDOMNode *aNode, - nsIDOMNode *aNewParentNode, - PRInt32 aStartOffset, - PRInt32 aEndOffset); + NS_IMETHOD MoveContentOfNodeIntoNewParent(nsIDOMNode *aNode, + nsIDOMNode *aNewParentNode, + PRInt32 aStartOffset, + PRInt32 aEndOffset); + + /** Moves the content between (aStartNode, aStartOffset) and (aEndNode, aEndOffset) + * into aNewParentNode, splitting aStartNode and aEndNode as necessary to maintain + * the relative position of all leaf content. + * The content between the two endpoints MUST be "contiguous" in the sense that + * it is all in the same block. Another way of saying this is all content nodes + * between aStartNode and aEndNode must be inline. + * @see IntermediateNodesAreInline + * + * @param aStartNode The left node, can be either a text node or a container node. + * @param aStartOffset The start offset in the content of aStartNode + * @param aEndNode The right node, can be either a text node or a container node. + * @param aEndOffset The end offset in the content of aEndNode. + * @param aGrandParentNode The common ancestor of aStartNode and aEndNode. + * aGrandParentNode will be the parent of aNewParentNode. + * @param aNewParentNode The node that will be the repositioned contents' parent. + * The caller is responsible for allocating aNewParentNode + */ + NS_IMETHOD MoveContiguousContentIntoNewParent(nsIDOMNode *aStartNode, + PRInt32 aStartOffset, + nsIDOMNode *aEndNode, + PRInt32 aEndOffset, + nsIDOMNode *aGrandParentNode, + nsIDOMNode *aNewParentNode); NS_IMETHOD SetTextPropertiesForNode(nsIDOMNode *aNode, diff --git a/mozilla/editor/libeditor/base/JoinElementTxn.cpp b/mozilla/editor/libeditor/base/JoinElementTxn.cpp index 46ba27dbe7c..ed076f1d533 100644 --- a/mozilla/editor/libeditor/base/JoinElementTxn.cpp +++ b/mozilla/editor/libeditor/base/JoinElementTxn.cpp @@ -150,33 +150,6 @@ NS_IMETHODIMP JoinElementTxn::Undo(void) } -/* -NS_IMETHODIMP JoinElementTxn::Redo(void) -{ - if (gNoisy) { printf("Redo Join\n"); } - nsresult result; - nsCOMPtr editor; - result = mEditor->QueryInterface(kIEditorSupportIID, getter_AddRefs(editor)); - if (NS_SUCCEEDED(result) && editor) - { - result = editor->JoinNodesImpl(mRightNode, mLeftNode, mParent, PR_FALSE); - if (NS_SUCCEEDED(result)) - { - nsCOMPtrselection; - mEditor->GetSelection(getter_AddRefs(selection)); - if (selection) - { - selection->Collapse(mRightNode, mOffset); - } - } - } - else { - result = NS_ERROR_NOT_IMPLEMENTED; - } - return result; -} -*/ - NS_IMETHODIMP JoinElementTxn::GetIsTransient(PRBool *aIsTransient) { if (nsnull!=aIsTransient) diff --git a/mozilla/editor/libeditor/html/TextEditorTest.cpp b/mozilla/editor/libeditor/html/TextEditorTest.cpp index f07e8e7bd93..3d4d8641125 100644 --- a/mozilla/editor/libeditor/html/TextEditorTest.cpp +++ b/mozilla/editor/libeditor/html/TextEditorTest.cpp @@ -63,7 +63,7 @@ nsresult TextEditorTest::RunUnitTest() TEST_RESULT(result); // insert some more text - nsString docContent2("Moreover, I am cognizant of the interrelatedness of all communities and states. I cannot sit idly by in Atlanta and not be concerned about what happens in Birmingham. Injustice anywhere is a threat to justice everywhere."); + nsString docContent2("Moreover, I am cognizant of the interrelatedness of all communities and states. I cannot sit idly by in Atlanta and not be concerned about what happens in Birmingham. Injustice anywhere is a threat to justice everywhere"); result = mTextEditor->InsertText(docContent2); TEST_RESULT(result); @@ -194,8 +194,20 @@ nsresult TextEditorTest::TestTextProperties() NS_ASSERTION(PR_TRUE==any, "any should be true"); NS_ASSERTION(PR_TRUE==all, "all should be true"); mEditor->DebugDumpContent(); - /* I need a way of setting the selection I want here. Maybe use Kin's text service stuff? + // make all the text underlined, except for the first 2 and last 2 characters + result = doc->GetElementsByTagName(textTag, getter_AddRefs(nodeList)); + TEST_RESULT(result); + TEST_POINTER(nodeList.get()); + nodeList->GetLength(&count); + NS_ASSERTION(0!=count, "there are no text nodes in the document!"); + result = nodeList->Item(1, getter_AddRefs(textNode)); + TEST_RESULT(result); + TEST_POINTER(textNode.get()); + textData = do_QueryInterface(textNode); + textData->GetLength(&length); + selection->Collapse(textNode, 1); + selection->Extend(textNode, length-2); result = mTextEditor->SetTextProperty(nsIEditProperty::u, nsnull, nsnull); TEST_RESULT(result); @@ -205,7 +217,6 @@ nsresult TextEditorTest::TestTextProperties() NS_ASSERTION(PR_TRUE==any, "any should be true"); NS_ASSERTION(PR_TRUE==all, "all should be true"); mEditor->DebugDumpContent(); - */ return result; }