From c02f965115e231753bafde361c01771ca047d43f Mon Sep 17 00:00:00 2001 From: "akkana%netscape.com" Date: Fri, 10 Sep 1999 18:54:13 +0000 Subject: [PATCH] 490: infrastructure for inserting formatting whitespace. Not turned on yet except for DEBUG_akkana git-svn-id: svn://10.0.0.236/trunk@46804 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/editor/base/SplitElementTxn.cpp | 5 ++ mozilla/editor/base/nsEditor.cpp | 85 +++++++++++++++++++ mozilla/editor/base/nsEditor.h | 6 ++ mozilla/editor/base/nsHTMLEditor.cpp | 4 +- .../editor/libeditor/base/SplitElementTxn.cpp | 5 ++ mozilla/editor/libeditor/base/nsEditor.cpp | 85 +++++++++++++++++++ mozilla/editor/libeditor/base/nsEditor.h | 6 ++ .../editor/libeditor/html/nsHTMLEditor.cpp | 4 +- mozilla/editor/public/nsIEditor.h | 15 ++++ 9 files changed, 209 insertions(+), 6 deletions(-) diff --git a/mozilla/editor/base/SplitElementTxn.cpp b/mozilla/editor/base/SplitElementTxn.cpp index 8ee8bec4628..1d5b9c83e0a 100644 --- a/mozilla/editor/base/SplitElementTxn.cpp +++ b/mozilla/editor/base/SplitElementTxn.cpp @@ -74,6 +74,11 @@ NS_IMETHODIMP SplitElementTxn::Do(void) result = nsEditor::SplitNodeImpl(mExistingRightNode, mOffset, mNewLeftNode, mParent); if (NS_SUCCEEDED(result) && mNewLeftNode) { +#ifdef DEBUG_akkana + // Insert formatting whitespace for the new node: + mEditor->InsertFormattingForNode(mExistingRightNode); +#endif + nsCOMPtrselection; mEditor->GetSelection(getter_AddRefs(selection)); if (NS_FAILED(result)) return result; diff --git a/mozilla/editor/base/nsEditor.cpp b/mozilla/editor/base/nsEditor.cpp index 1644b1950d9..99ceb3983c1 100644 --- a/mozilla/editor/base/nsEditor.cpp +++ b/mozilla/editor/base/nsEditor.cpp @@ -862,6 +862,91 @@ nsEditor::SplitNode(nsIDOMNode * aNode, } +// +// Insert a noneditable text node, e.g. formatting whitespace +// +nsresult +nsEditor::InsertNoneditableTextNode(nsIDOMNode* parent, PRInt32 offset, + nsString& aStr) +{ + nsAutoString textNodeTag; + nsresult res = GetTextNodeTag(textNodeTag); + if (NS_FAILED(res)) + return res; + + // Can't call CreateNode, because that will call us recursively. + // So duplicate what it does: + CreateElementTxn *txn; + res = CreateTxnForCreateElement(textNodeTag, parent, offset, &txn); + if (NS_FAILED(res)) + return res; + + res = Do(txn); + if (NS_FAILED(res)) + return res; + + // Now get the pointer to the node we just created ... + nsCOMPtr newNode; + res = txn->GetNewNode(getter_AddRefs(newNode)); + if (NS_FAILED(res)) + return res; + nsCOMPtr newTextNode; + newTextNode = do_QueryInterface(newNode); + if (!newTextNode) + return NS_ERROR_UNEXPECTED; + + // ... and set its text. + return newTextNode->SetData(aStr); +} + +// +// Figure out what formatting needs to go with this node, and insert it. +// +NS_IMETHODIMP +nsEditor::InsertFormattingForNode(nsIDOMNode* aNode) +{ + nsresult res; + + // Don't insert any formatting unless it's an element node + PRUint16 nodeType; + res = aNode->GetNodeType(&nodeType); + if (NS_FAILED(res)) + return res; + if (nodeType != nsIDOMNode::ELEMENT_NODE) + return NS_OK; + + nsCOMPtr parent; + res = aNode->GetParentNode(getter_AddRefs(parent)); + if (NS_FAILED(res)) + return res; + PRInt32 offset = GetIndexOf(parent, aNode); + +#ifdef DEBUG_akkana + DumpContentTree(); + nsString namestr; + aNode->GetNodeName(namestr); + char* nodename = namestr.ToNewCString(); + printf("Inserting formatting for node <%s> at offset %d\n", + nodename, offset); + delete[] nodename; +#endif /* DEBUG_akkana */ + + // + // XXX We don't yet have a real formatter. As a cheap stopgap, + // XXX just insert a newline before and after each newly inserted tag. + // + + nsAutoString str (NS_LINEBREAK); + + // After the close tag + //res = InsertNoneditableTextNode(parent, offset+1, str); + + // Before the open tag + res = InsertNoneditableTextNode(parent, offset, str); + + return res; +} + NS_IMETHODIMP nsEditor::JoinNodes(nsIDOMNode * aLeftNode, nsIDOMNode * aRightNode, diff --git a/mozilla/editor/base/nsEditor.h b/mozilla/editor/base/nsEditor.h index 9a66efcf106..783fffe30b0 100644 --- a/mozilla/editor/base/nsEditor.h +++ b/mozilla/editor/base/nsEditor.h @@ -172,6 +172,12 @@ public: NS_IMETHOD DeleteNode(nsIDOMNode * aChild); + /* formatting within the dom tree */ + NS_IMETHOD InsertNoneditableTextNode(nsIDOMNode* aParent, + PRInt32 aOffset, + nsString& aStr); + NS_IMETHOD InsertFormattingForNode(nsIDOMNode* aNode); + /* output */ NS_IMETHOD OutputToString(nsString& aOutputString, diff --git a/mozilla/editor/base/nsHTMLEditor.cpp b/mozilla/editor/base/nsHTMLEditor.cpp index 3effc5a9d03..d966e4e4e4e 100644 --- a/mozilla/editor/base/nsHTMLEditor.cpp +++ b/mozilla/editor/base/nsHTMLEditor.cpp @@ -2594,9 +2594,6 @@ NS_IMETHODIMP nsHTMLEditor::GetBodyWrapWidth(PRInt32 *aWrapColumn) // NS_IMETHODIMP nsHTMLEditor::SetBodyWrapWidth(PRInt32 aWrapColumn) { -#ifdef DEBUG_akkana - printf("SetBodyWrapWidth(%d)\n", aWrapColumn); -#endif nsresult res; // Ought to set a style sheet here ... @@ -5877,3 +5874,4 @@ nsHTMLEditor::CollapseAdjacentTextNodes(nsIDOMSelection *aInSelection) return result; } + diff --git a/mozilla/editor/libeditor/base/SplitElementTxn.cpp b/mozilla/editor/libeditor/base/SplitElementTxn.cpp index 8ee8bec4628..1d5b9c83e0a 100644 --- a/mozilla/editor/libeditor/base/SplitElementTxn.cpp +++ b/mozilla/editor/libeditor/base/SplitElementTxn.cpp @@ -74,6 +74,11 @@ NS_IMETHODIMP SplitElementTxn::Do(void) result = nsEditor::SplitNodeImpl(mExistingRightNode, mOffset, mNewLeftNode, mParent); if (NS_SUCCEEDED(result) && mNewLeftNode) { +#ifdef DEBUG_akkana + // Insert formatting whitespace for the new node: + mEditor->InsertFormattingForNode(mExistingRightNode); +#endif + nsCOMPtrselection; mEditor->GetSelection(getter_AddRefs(selection)); if (NS_FAILED(result)) return result; diff --git a/mozilla/editor/libeditor/base/nsEditor.cpp b/mozilla/editor/libeditor/base/nsEditor.cpp index 1644b1950d9..99ceb3983c1 100644 --- a/mozilla/editor/libeditor/base/nsEditor.cpp +++ b/mozilla/editor/libeditor/base/nsEditor.cpp @@ -862,6 +862,91 @@ nsEditor::SplitNode(nsIDOMNode * aNode, } +// +// Insert a noneditable text node, e.g. formatting whitespace +// +nsresult +nsEditor::InsertNoneditableTextNode(nsIDOMNode* parent, PRInt32 offset, + nsString& aStr) +{ + nsAutoString textNodeTag; + nsresult res = GetTextNodeTag(textNodeTag); + if (NS_FAILED(res)) + return res; + + // Can't call CreateNode, because that will call us recursively. + // So duplicate what it does: + CreateElementTxn *txn; + res = CreateTxnForCreateElement(textNodeTag, parent, offset, &txn); + if (NS_FAILED(res)) + return res; + + res = Do(txn); + if (NS_FAILED(res)) + return res; + + // Now get the pointer to the node we just created ... + nsCOMPtr newNode; + res = txn->GetNewNode(getter_AddRefs(newNode)); + if (NS_FAILED(res)) + return res; + nsCOMPtr newTextNode; + newTextNode = do_QueryInterface(newNode); + if (!newTextNode) + return NS_ERROR_UNEXPECTED; + + // ... and set its text. + return newTextNode->SetData(aStr); +} + +// +// Figure out what formatting needs to go with this node, and insert it. +// +NS_IMETHODIMP +nsEditor::InsertFormattingForNode(nsIDOMNode* aNode) +{ + nsresult res; + + // Don't insert any formatting unless it's an element node + PRUint16 nodeType; + res = aNode->GetNodeType(&nodeType); + if (NS_FAILED(res)) + return res; + if (nodeType != nsIDOMNode::ELEMENT_NODE) + return NS_OK; + + nsCOMPtr parent; + res = aNode->GetParentNode(getter_AddRefs(parent)); + if (NS_FAILED(res)) + return res; + PRInt32 offset = GetIndexOf(parent, aNode); + +#ifdef DEBUG_akkana + DumpContentTree(); + nsString namestr; + aNode->GetNodeName(namestr); + char* nodename = namestr.ToNewCString(); + printf("Inserting formatting for node <%s> at offset %d\n", + nodename, offset); + delete[] nodename; +#endif /* DEBUG_akkana */ + + // + // XXX We don't yet have a real formatter. As a cheap stopgap, + // XXX just insert a newline before and after each newly inserted tag. + // + + nsAutoString str (NS_LINEBREAK); + + // After the close tag + //res = InsertNoneditableTextNode(parent, offset+1, str); + + // Before the open tag + res = InsertNoneditableTextNode(parent, offset, str); + + return res; +} + NS_IMETHODIMP nsEditor::JoinNodes(nsIDOMNode * aLeftNode, nsIDOMNode * aRightNode, diff --git a/mozilla/editor/libeditor/base/nsEditor.h b/mozilla/editor/libeditor/base/nsEditor.h index 9a66efcf106..783fffe30b0 100644 --- a/mozilla/editor/libeditor/base/nsEditor.h +++ b/mozilla/editor/libeditor/base/nsEditor.h @@ -172,6 +172,12 @@ public: NS_IMETHOD DeleteNode(nsIDOMNode * aChild); + /* formatting within the dom tree */ + NS_IMETHOD InsertNoneditableTextNode(nsIDOMNode* aParent, + PRInt32 aOffset, + nsString& aStr); + NS_IMETHOD InsertFormattingForNode(nsIDOMNode* aNode); + /* output */ NS_IMETHOD OutputToString(nsString& aOutputString, diff --git a/mozilla/editor/libeditor/html/nsHTMLEditor.cpp b/mozilla/editor/libeditor/html/nsHTMLEditor.cpp index 3effc5a9d03..d966e4e4e4e 100644 --- a/mozilla/editor/libeditor/html/nsHTMLEditor.cpp +++ b/mozilla/editor/libeditor/html/nsHTMLEditor.cpp @@ -2594,9 +2594,6 @@ NS_IMETHODIMP nsHTMLEditor::GetBodyWrapWidth(PRInt32 *aWrapColumn) // NS_IMETHODIMP nsHTMLEditor::SetBodyWrapWidth(PRInt32 aWrapColumn) { -#ifdef DEBUG_akkana - printf("SetBodyWrapWidth(%d)\n", aWrapColumn); -#endif nsresult res; // Ought to set a style sheet here ... @@ -5877,3 +5874,4 @@ nsHTMLEditor::CollapseAdjacentTextNodes(nsIDOMSelection *aInSelection) return result; } + diff --git a/mozilla/editor/public/nsIEditor.h b/mozilla/editor/public/nsIEditor.h index 9ee836678cf..9693fcf1498 100644 --- a/mozilla/editor/public/nsIEditor.h +++ b/mozilla/editor/public/nsIEditor.h @@ -352,6 +352,21 @@ public: */ NS_IMETHOD DeleteNode(nsIDOMNode * aChild)=0; + /** + * InsertNoneditableTextNode() inserts a noneditable text node, e.g. for formatting. + * @param aParent The parent of the newly created node + * @param aOffset The offset in the parent for the new node + * @param aStr The string contents to be placed in the node + */ + NS_IMETHOD InsertNoneditableTextNode(nsIDOMNode* aParent, PRInt32 aOffset, + nsString& aStr) = 0; + /** + * InsertFormattingForNode() inserts formatting before and/or after a node. + * Usually this will be called immediately after creating a new node. + * @param aNode The node for which to insert formatting. + */ + NS_IMETHOD InsertFormattingForNode(nsIDOMNode* aNode) = 0; + /* ------------ Output methods -------------- */ /**