From b799c9f3b9ee25b32fe1ffde428f6d17473a3320 Mon Sep 17 00:00:00 2001 From: "akkana%netscape.com" Date: Thu, 18 Nov 1999 19:43:14 +0000 Subject: [PATCH] Improve the problem of excessive whitespace around
 quotes in plaintext mail (r=mcafee). Also, 19237: an initial fix for html
 quoting (html source being inserted outside the blockquote).

---
 editor/base/nsHTMLEditor.cpp           | 73 ++++++++++++++++++++++++++
 editor/base/nsHTMLEditor.h             |  3 ++
 editor/libeditor/html/nsHTMLEditor.cpp | 73 ++++++++++++++++++++++++++
 editor/libeditor/html/nsHTMLEditor.h   |  3 ++
 4 files changed, 152 insertions(+)

diff --git a/editor/base/nsHTMLEditor.cpp b/editor/base/nsHTMLEditor.cpp
index bc208a85fcd..4ab818ef514 100644
--- a/editor/base/nsHTMLEditor.cpp
+++ b/editor/base/nsHTMLEditor.cpp
@@ -3262,6 +3262,73 @@ nsHTMLEditor::GetEmbeddedObjects(nsISupportsArray** aNodeList)
 #pragma mark -
 #endif
 
+//
+// Figure out what formatting needs to go with this node, and insert it.
+//
+NS_IMETHODIMP
+nsHTMLEditor::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;
+
+  // Insert formatting only for block nodes
+  // (would it be better to insert for any non-inline node?)
+  PRBool block;
+  res = IsNodeBlock(aNode, block);
+  if (NS_FAILED(res))
+    return res;
+  if (!block)
+    return NS_OK;
+
+  nsCOMPtr parent;
+  res = aNode->GetParentNode(getter_AddRefs(parent));
+  if (NS_FAILED(res))
+    return res;
+  PRInt32 offset = GetIndexOf(parent, aNode);
+
+  nsString namestr;
+  aNode->GetNodeName(namestr);
+
+  // Don't insert formatting for pre or br if we're a plaintext editor.
+  // The newlines get considered to be part of the text, yet the
+  // edit rules also insert breaks, so we end up with too much
+  // vertical whitespace any time the user hits return.
+  // This, of course, makes the html look lousy, but we're
+  // expecting that no one will look at the html from a plaintext editor.
+  if ((mFlags & nsHTMLEditor::eEditorPlaintextMask)
+      && (namestr.Equals("pre", PR_TRUE) || namestr.Equals("br", PR_TRUE)))
+    return NS_OK;
+
+#ifdef DEBUG_akkana
+  //DumpContentTree();
+  char* nodename = namestr.ToNewCString();
+  printf("Inserting formatting for node <%s> at offset %d\n",
+         nodename, offset);
+  Recycle(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 ("\n");
+
+  // After the close tag
+  //res = InsertNoneditableTextNode(parent, offset+1, str);
+
+  // Before the open tag
+  res = InsertNoneditableTextNode(parent, offset, str);
+
+  return res;
+}
 
 NS_IMETHODIMP 
 nsHTMLEditor::Undo(PRUint32 aCount)
@@ -3648,6 +3715,12 @@ NS_IMETHODIMP nsHTMLEditor::InsertAsCitedQuotation(const nsString& aQuotedText,
 
     if (aCitation.Length() > 0)
       newElement->SetAttribute(cite, aCitation);
+
+    // Set the selection inside the blockquote so aQuotedText will go there:
+    nsCOMPtr selection;
+    res = GetSelection(getter_AddRefs(selection));
+    if (NS_SUCCEEDED(res) && selection)
+      selection->Collapse(newNode, 0);
   }
 
   res = InsertHTML(aQuotedText);
diff --git a/editor/base/nsHTMLEditor.h b/editor/base/nsHTMLEditor.h
index c0f4fefba46..6807af8be06 100644
--- a/editor/base/nsHTMLEditor.h
+++ b/editor/base/nsHTMLEditor.h
@@ -200,6 +200,9 @@ public:
   NS_IMETHOD GetFlags(PRUint32 *aFlags);
   NS_IMETHOD SetFlags(PRUint32 aFlags);
 
+  /** Inherited from nsEditor with special cases for some html nodes **/
+  NS_IMETHOD InsertFormattingForNode(nsIDOMNode* aNode);
+
   NS_IMETHOD Undo(PRUint32 aCount);
   NS_IMETHOD Redo(PRUint32 aCount);
 
diff --git a/editor/libeditor/html/nsHTMLEditor.cpp b/editor/libeditor/html/nsHTMLEditor.cpp
index bc208a85fcd..4ab818ef514 100644
--- a/editor/libeditor/html/nsHTMLEditor.cpp
+++ b/editor/libeditor/html/nsHTMLEditor.cpp
@@ -3262,6 +3262,73 @@ nsHTMLEditor::GetEmbeddedObjects(nsISupportsArray** aNodeList)
 #pragma mark -
 #endif
 
+//
+// Figure out what formatting needs to go with this node, and insert it.
+//
+NS_IMETHODIMP
+nsHTMLEditor::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;
+
+  // Insert formatting only for block nodes
+  // (would it be better to insert for any non-inline node?)
+  PRBool block;
+  res = IsNodeBlock(aNode, block);
+  if (NS_FAILED(res))
+    return res;
+  if (!block)
+    return NS_OK;
+
+  nsCOMPtr parent;
+  res = aNode->GetParentNode(getter_AddRefs(parent));
+  if (NS_FAILED(res))
+    return res;
+  PRInt32 offset = GetIndexOf(parent, aNode);
+
+  nsString namestr;
+  aNode->GetNodeName(namestr);
+
+  // Don't insert formatting for pre or br if we're a plaintext editor.
+  // The newlines get considered to be part of the text, yet the
+  // edit rules also insert breaks, so we end up with too much
+  // vertical whitespace any time the user hits return.
+  // This, of course, makes the html look lousy, but we're
+  // expecting that no one will look at the html from a plaintext editor.
+  if ((mFlags & nsHTMLEditor::eEditorPlaintextMask)
+      && (namestr.Equals("pre", PR_TRUE) || namestr.Equals("br", PR_TRUE)))
+    return NS_OK;
+
+#ifdef DEBUG_akkana
+  //DumpContentTree();
+  char* nodename = namestr.ToNewCString();
+  printf("Inserting formatting for node <%s> at offset %d\n",
+         nodename, offset);
+  Recycle(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 ("\n");
+
+  // After the close tag
+  //res = InsertNoneditableTextNode(parent, offset+1, str);
+
+  // Before the open tag
+  res = InsertNoneditableTextNode(parent, offset, str);
+
+  return res;
+}
 
 NS_IMETHODIMP 
 nsHTMLEditor::Undo(PRUint32 aCount)
@@ -3648,6 +3715,12 @@ NS_IMETHODIMP nsHTMLEditor::InsertAsCitedQuotation(const nsString& aQuotedText,
 
     if (aCitation.Length() > 0)
       newElement->SetAttribute(cite, aCitation);
+
+    // Set the selection inside the blockquote so aQuotedText will go there:
+    nsCOMPtr selection;
+    res = GetSelection(getter_AddRefs(selection));
+    if (NS_SUCCEEDED(res) && selection)
+      selection->Collapse(newNode, 0);
   }
 
   res = InsertHTML(aQuotedText);
diff --git a/editor/libeditor/html/nsHTMLEditor.h b/editor/libeditor/html/nsHTMLEditor.h
index c0f4fefba46..6807af8be06 100644
--- a/editor/libeditor/html/nsHTMLEditor.h
+++ b/editor/libeditor/html/nsHTMLEditor.h
@@ -200,6 +200,9 @@ public:
   NS_IMETHOD GetFlags(PRUint32 *aFlags);
   NS_IMETHOD SetFlags(PRUint32 aFlags);
 
+  /** Inherited from nsEditor with special cases for some html nodes **/
+  NS_IMETHOD InsertFormattingForNode(nsIDOMNode* aNode);
+
   NS_IMETHOD Undo(PRUint32 aCount);
   NS_IMETHOD Redo(PRUint32 aCount);