Back to index

lightning-sunbird  0.9+nobinonly
nsDocumentEncoder.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsIDocumentEncoder.h"
00040 
00041 #include "nscore.h"
00042 #include "nsIFactory.h"
00043 #include "nsISupports.h"
00044 #include "nsIComponentManager.h" 
00045 #include "nsIServiceManager.h"
00046 #include "nsIDocument.h"
00047 #include "nsIHTMLDocument.h"
00048 #include "nsISelection.h"
00049 #include "nsCOMPtr.h"
00050 #include "nsIContentSerializer.h"
00051 #include "nsIUnicodeEncoder.h"
00052 #include "nsIOutputStream.h"
00053 #include "nsIDOMElement.h"
00054 #include "nsIDOMText.h"
00055 #include "nsIDOMCDATASection.h"
00056 #include "nsIDOMComment.h"
00057 #include "nsIDOMProcessingInstruction.h"
00058 #include "nsIDOMDocumentType.h"
00059 #include "nsIDOMNodeList.h"
00060 #include "nsIDOMRange.h"
00061 #include "nsIDOMDocument.h"
00062 #include "nsICharsetConverterManager.h"
00063 #include "nsHTMLAtoms.h"
00064 #include "nsITextContent.h"
00065 #include "nsIEnumerator.h"
00066 #include "nsISelectionPrivate.h"
00067 #include "nsIFrameSelection.h"
00068 #include "nsISupportsArray.h"
00069 #include "nsIParserService.h"
00070 #include "nsIScriptContext.h"
00071 #include "nsIScriptGlobalObject.h"
00072 #include "nsIScriptSecurityManager.h"
00073 #include "nsContentUtils.h"
00074 #include "nsUnicharUtils.h"
00075 #include "nsReadableUtils.h"
00076 
00077 nsresult NS_NewDomSelection(nsISelection **aDomSelection);
00078 
00079 enum nsRangeIterationDirection {
00080   kDirectionOut = -1,
00081   kDirectionIn = 1
00082 };
00083 
00084 #ifdef XP_MAC
00085 #pragma mark -
00086 #pragma mark  nsDocumentEncoder declaration 
00087 #pragma mark -
00088 #endif
00089 
00090 class nsDocumentEncoder : public nsIDocumentEncoder
00091 {
00092 public:
00093   NS_DEFINE_STATIC_IID_ACCESSOR(NS_IDOCUMENT_ENCODER_IID)
00094 
00095   nsDocumentEncoder();
00096   virtual ~nsDocumentEncoder();
00097 
00098   NS_IMETHOD Init(nsIDocument* aDocument, const nsAString& aMimeType, PRUint32 aFlags);
00099 
00100   /* Interfaces for addref and release and queryinterface */
00101   NS_DECL_ISUPPORTS
00102 
00103   // Inherited methods from nsIDocumentEncoder
00104   NS_IMETHOD SetSelection(nsISelection* aSelection);
00105   NS_IMETHOD SetRange(nsIDOMRange* aRange);
00106   NS_IMETHOD SetNode(nsIDOMNode* aNode);
00107   NS_IMETHOD SetWrapColumn(PRUint32 aWC);
00108   NS_IMETHOD SetCharset(const nsACString& aCharset);
00109   NS_IMETHOD GetMimeType(nsAString& aMimeType);
00110   NS_IMETHOD EncodeToStream(nsIOutputStream* aStream);
00111   NS_IMETHOD EncodeToString(nsAString& aOutputString);
00112   NS_IMETHOD EncodeToStringWithContext(nsAString& aEncodedString, 
00113                                        nsAString& aContextString,
00114                                        nsAString& aInfoString);
00115   NS_IMETHOD SetNodeFixup(nsIDocumentEncoderNodeFixup *aFixup);
00116                                        
00117 protected:
00118   void Initialize();
00119   nsresult SerializeNodeStart(nsIDOMNode* aNode, PRInt32 aStartOffset,
00120                               PRInt32 aEndOffset, nsAString& aStr);
00121   nsresult SerializeToStringRecursive(nsIDOMNode* aNode,
00122                                       nsAString& aStr);
00123   nsresult SerializeNodeEnd(nsIDOMNode* aNode, nsAString& aStr);
00124   nsresult SerializeRangeToString(nsIDOMRange *aRange,
00125                                   nsAString& aOutputString);
00126   nsresult SerializeRangeNodes(nsIDOMRange* aRange, 
00127                                nsIDOMNode* aNode, 
00128                                nsAString& aString,
00129                                PRInt32 aDepth);
00130   nsresult SerializeRangeContextStart(const nsVoidArray& aAncestorArray,
00131                                       nsAString& aString);
00132   nsresult SerializeRangeContextEnd(const nsVoidArray& aAncestorArray,
00133                                     nsAString& aString);
00134 
00135   nsresult FlushText(nsAString& aString, PRBool aForce);
00136 
00137   static PRBool IsTag(nsIDOMNode* aNode, nsIAtom* aAtom);
00138   
00139   virtual PRBool IncludeInContext(nsIDOMNode *aNode);
00140 
00141   nsCOMPtr<nsIDocument>          mDocument;
00142   nsCOMPtr<nsISelection>         mSelection;
00143   nsCOMPtr<nsIDOMRange>          mRange;
00144   nsCOMPtr<nsIDOMNode>           mNode;
00145   nsCOMPtr<nsIOutputStream>      mStream;
00146   nsCOMPtr<nsIContentSerializer> mSerializer;
00147   nsCOMPtr<nsIUnicodeEncoder>    mUnicodeEncoder;
00148   nsCOMPtr<nsIDOMNode>           mCommonParent;
00149   nsCOMPtr<nsIDocumentEncoderNodeFixup> mNodeFixup;
00150   nsCOMPtr<nsICharsetConverterManager> mCharsetConverterManager;
00151 
00152   nsString          mMimeType;
00153   nsCString         mCharset;
00154   PRUint32          mFlags;
00155   PRUint32          mWrapColumn;
00156   PRUint32          mStartDepth;
00157   PRUint32          mEndDepth;
00158   PRInt32           mStartRootIndex;
00159   PRInt32           mEndRootIndex;
00160   nsAutoVoidArray   mCommonAncestors;
00161   nsAutoVoidArray   mStartNodes;
00162   nsAutoVoidArray   mStartOffsets;
00163   nsAutoVoidArray   mEndNodes;
00164   nsAutoVoidArray   mEndOffsets;
00165   PRPackedBool      mHaltRangeHint;  
00166   PRPackedBool      mIsCopying;  // Set to PR_TRUE only while copying
00167 };
00168 
00169 #ifdef XP_MAC
00170 #pragma mark  nsDocumentEncoder implementation 
00171 #pragma mark -
00172 #endif
00173 
00174 NS_IMPL_ADDREF(nsDocumentEncoder)
00175 NS_IMPL_RELEASE(nsDocumentEncoder)
00176 
00177 NS_INTERFACE_MAP_BEGIN(nsDocumentEncoder)
00178    NS_INTERFACE_MAP_ENTRY(nsIDocumentEncoder)
00179    NS_INTERFACE_MAP_ENTRY(nsISupports)
00180 NS_INTERFACE_MAP_END
00181 
00182 nsDocumentEncoder::nsDocumentEncoder()
00183 {
00184   Initialize();
00185   mMimeType.AssignLiteral("text/plain");
00186 
00187 }
00188 
00189 void nsDocumentEncoder::Initialize()
00190 {
00191   mFlags = 0;
00192   mWrapColumn = 72;
00193   mStartDepth = 0;
00194   mEndDepth = 0;
00195   mStartRootIndex = 0;
00196   mEndRootIndex = 0;
00197   mHaltRangeHint = PR_FALSE;
00198 }
00199 
00200 nsDocumentEncoder::~nsDocumentEncoder()
00201 {
00202 }
00203 
00204 NS_IMETHODIMP
00205 nsDocumentEncoder::Init(nsIDocument* aDocument,
00206                         const nsAString& aMimeType,
00207                         PRUint32 aFlags)
00208 {
00209   if (!aDocument)
00210     return NS_ERROR_INVALID_ARG;
00211 
00212   Initialize();
00213 
00214   mDocument = aDocument;
00215 
00216   mMimeType = aMimeType;
00217 
00218   mFlags = aFlags;
00219   mIsCopying = PR_FALSE;
00220 
00221   return NS_OK;
00222 }
00223 
00224 NS_IMETHODIMP
00225 nsDocumentEncoder::SetWrapColumn(PRUint32 aWC)
00226 {
00227   mWrapColumn = aWC;
00228   return NS_OK;
00229 }
00230 
00231 NS_IMETHODIMP
00232 nsDocumentEncoder::SetSelection(nsISelection* aSelection)
00233 {
00234   mSelection = aSelection;
00235   return NS_OK;
00236 }
00237 
00238 NS_IMETHODIMP
00239 nsDocumentEncoder::SetRange(nsIDOMRange* aRange)
00240 {
00241   mRange = aRange;
00242   return NS_OK;
00243 }
00244 
00245 NS_IMETHODIMP
00246 nsDocumentEncoder::SetNode(nsIDOMNode* aNode)
00247 {
00248   mNode = aNode;
00249   return NS_OK;
00250 }
00251 
00252 NS_IMETHODIMP
00253 nsDocumentEncoder::SetCharset(const nsACString& aCharset)
00254 {
00255   mCharset = aCharset;
00256   return NS_OK;
00257 }
00258 
00259 NS_IMETHODIMP
00260 nsDocumentEncoder::GetMimeType(nsAString& aMimeType)
00261 {
00262   aMimeType = mMimeType;
00263   return NS_OK;
00264 }
00265 
00266 
00267 PRBool
00268 nsDocumentEncoder::IncludeInContext(nsIDOMNode *aNode)
00269 {
00270   return PR_FALSE;
00271 }
00272 
00273 nsresult
00274 nsDocumentEncoder::SerializeNodeStart(nsIDOMNode* aNode, PRInt32 aStartOffset,
00275                                       PRInt32 aEndOffset,
00276                                       nsAString& aStr)
00277 {
00278   PRUint16 type;
00279 
00280   nsCOMPtr<nsIDOMNode> node;
00281   if (mNodeFixup)
00282   {
00283     mNodeFixup->FixupNode(aNode, getter_AddRefs(node));
00284   }
00285   if (!node)
00286   {
00287     node = do_QueryInterface(aNode);
00288   }
00289 
00290   node->GetNodeType(&type);
00291   switch (type) {
00292     case nsIDOMNode::ELEMENT_NODE:
00293     {
00294       nsCOMPtr<nsIDOMElement> element = do_QueryInterface(node);
00295       // Because FixupNode() may have done a shallow copy of aNode
00296       // we need to tell the serializer if the original had children.
00297       // Some serializers (notably XML) need this information 
00298       // in order to handle empty tags properly.
00299       PRBool hasChildren;
00300       mSerializer->AppendElementStart(element, 
00301                                       NS_SUCCEEDED(aNode->HasChildNodes(&hasChildren)) && hasChildren,
00302                                       aStr);
00303       break;
00304     }
00305     case nsIDOMNode::TEXT_NODE:
00306     {
00307       nsCOMPtr<nsIDOMText> text = do_QueryInterface(node);
00308       mSerializer->AppendText(text, aStartOffset, aEndOffset, aStr);
00309       break;
00310     }
00311     case nsIDOMNode::CDATA_SECTION_NODE:
00312     {
00313       nsCOMPtr<nsIDOMCDATASection> cdata = do_QueryInterface(node);
00314       mSerializer->AppendCDATASection(cdata, aStartOffset, aEndOffset, aStr);
00315       break;
00316     }
00317     case nsIDOMNode::PROCESSING_INSTRUCTION_NODE:
00318     {
00319       nsCOMPtr<nsIDOMProcessingInstruction> pi = do_QueryInterface(node);
00320       mSerializer->AppendProcessingInstruction(pi, aStartOffset, aEndOffset,
00321                                                aStr);
00322       break;
00323     }
00324     case nsIDOMNode::COMMENT_NODE:
00325     {
00326       nsCOMPtr<nsIDOMComment> comment = do_QueryInterface(node);
00327       mSerializer->AppendComment(comment, aStartOffset, aEndOffset, aStr);
00328       break;
00329     }
00330     case nsIDOMNode::DOCUMENT_TYPE_NODE:
00331     {
00332       nsCOMPtr<nsIDOMDocumentType> doctype = do_QueryInterface(node);
00333       mSerializer->AppendDoctype(doctype, aStr);
00334       break;
00335     }
00336   }
00337   
00338   return NS_OK;
00339 }
00340 
00341 nsresult
00342 nsDocumentEncoder::SerializeNodeEnd(nsIDOMNode* aNode,
00343                                     nsAString& aStr)
00344 {
00345   PRUint16 type;
00346 
00347   aNode->GetNodeType(&type);
00348   switch (type) {
00349     case nsIDOMNode::ELEMENT_NODE:
00350     {
00351       nsCOMPtr<nsIDOMElement> element = do_QueryInterface(aNode);
00352       mSerializer->AppendElementEnd(element, aStr);
00353       break;
00354     }
00355   }
00356 
00357   return NS_OK;
00358 }
00359 
00360 nsresult
00361 nsDocumentEncoder::SerializeToStringRecursive(nsIDOMNode* aNode,
00362                                               nsAString& aStr)
00363 {
00364   nsresult rv = SerializeNodeStart(aNode, 0, -1, aStr);
00365   NS_ENSURE_SUCCESS(rv, rv);
00366 
00367   PRBool hasChildren = PR_FALSE;
00368 
00369   aNode->HasChildNodes(&hasChildren);
00370 
00371   if (hasChildren) {
00372     nsCOMPtr<nsIDOMNodeList> childNodes;
00373     rv = aNode->GetChildNodes(getter_AddRefs(childNodes));
00374     NS_ENSURE_TRUE(childNodes, NS_SUCCEEDED(rv) ? NS_ERROR_FAILURE : rv);
00375 
00376     PRInt32 index, count;
00377 
00378     childNodes->GetLength((PRUint32*)&count);
00379     for (index = 0; index < count; index++) {
00380       nsCOMPtr<nsIDOMNode> child;
00381 
00382       rv = childNodes->Item(index, getter_AddRefs(child));
00383       NS_ENSURE_SUCCESS(rv, rv);
00384 
00385       rv = SerializeToStringRecursive(child, aStr);
00386       NS_ENSURE_SUCCESS(rv, rv);     
00387     }
00388   }
00389 
00390   rv = SerializeNodeEnd(aNode, aStr);
00391   NS_ENSURE_SUCCESS(rv, rv);
00392 
00393   return FlushText(aStr, PR_FALSE);
00394 }
00395 
00396 PRBool 
00397 nsDocumentEncoder::IsTag(nsIDOMNode* aNode, nsIAtom* aAtom)
00398 {
00399   nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
00400   return content && content->Tag() == aAtom;
00401 }
00402 
00403 static nsresult
00404 ConvertAndWrite(const nsAString& aString,
00405                 nsIOutputStream* aStream,
00406                 nsIUnicodeEncoder* aEncoder)
00407 {
00408   NS_ENSURE_ARG_POINTER(aStream);
00409   NS_ENSURE_ARG_POINTER(aEncoder);
00410   nsresult rv;
00411   PRInt32 charLength, startCharLength;
00412   const nsPromiseFlatString& flat = PromiseFlatString(aString);
00413   const PRUnichar* unicodeBuf = flat.get();
00414   PRInt32 unicodeLength = aString.Length();
00415   PRInt32 startLength = unicodeLength;
00416 
00417   rv = aEncoder->GetMaxLength(unicodeBuf, unicodeLength, &charLength);
00418   startCharLength = charLength;
00419   NS_ENSURE_SUCCESS(rv, rv);
00420 
00421   nsCAutoString charXferString;
00422   if (!EnsureStringLength(charXferString, charLength))
00423     return NS_ERROR_OUT_OF_MEMORY;
00424 
00425   char* charXferBuf = charXferString.BeginWriting();
00426   nsresult convert_rv = NS_OK;
00427 
00428   do {
00429     unicodeLength = startLength;
00430     charLength = startCharLength;
00431 
00432     convert_rv = aEncoder->Convert(unicodeBuf, &unicodeLength, charXferBuf, &charLength);
00433     NS_ENSURE_SUCCESS(convert_rv, convert_rv);
00434 
00435     // Make sure charXferBuf is null-terminated before we call
00436     // Write().
00437 
00438     charXferBuf[charLength] = '\0';
00439 
00440     PRUint32 written;
00441     rv = aStream->Write(charXferBuf, charLength, &written);
00442     NS_ENSURE_SUCCESS(rv, rv);
00443 
00444     // If the converter couldn't convert a chraacer we replace the
00445     // character with a characre entity.
00446     if (convert_rv == NS_ERROR_UENC_NOMAPPING) {
00447       // Finishes the conversion. 
00448       // The converter has the possibility to write some extra data and flush its final state.
00449       char finish_buf[33];
00450       charLength = sizeof(finish_buf) - 1;
00451       rv = aEncoder->Finish(finish_buf, &charLength);
00452       NS_ENSURE_SUCCESS(rv, rv);
00453 
00454       // Make sure finish_buf is null-terminated before we call
00455       // Write().
00456 
00457       finish_buf[charLength] = '\0';
00458 
00459       rv = aStream->Write(finish_buf, charLength, &written);
00460       NS_ENSURE_SUCCESS(rv, rv);
00461 
00462       nsCAutoString entString("&#");
00463       if (IS_HIGH_SURROGATE(unicodeBuf[unicodeLength - 1]) && 
00464           unicodeLength < startLength && IS_LOW_SURROGATE(unicodeBuf[unicodeLength]))  {
00465         entString.AppendInt(SURROGATE_TO_UCS4(unicodeBuf[unicodeLength - 1],
00466                                               unicodeBuf[unicodeLength]));
00467         unicodeLength += 1;
00468       }
00469       else
00470         entString.AppendInt(unicodeBuf[unicodeLength - 1]);
00471       entString.Append(';');
00472 
00473       // Since entString is an nsCAutoString we know entString.get()
00474       // returns a null-terminated string, so no need for extra
00475       // null-termination before calling Write() here.
00476 
00477       rv = aStream->Write(entString.get(), entString.Length(), &written);
00478       NS_ENSURE_SUCCESS(rv, rv);
00479 
00480       unicodeBuf += unicodeLength;
00481       startLength -= unicodeLength;
00482     }
00483   } while (convert_rv == NS_ERROR_UENC_NOMAPPING);
00484 
00485   return rv;
00486 }
00487 
00488 nsresult
00489 nsDocumentEncoder::FlushText(nsAString& aString, PRBool aForce)
00490 {
00491   if (!mStream)
00492     return NS_OK;
00493 
00494   nsresult rv = NS_OK;
00495 
00496   if (aString.Length() > 1024 || aForce) {
00497     rv = ConvertAndWrite(aString, mStream, mUnicodeEncoder);
00498 
00499     aString.Truncate();
00500   }
00501 
00502   return rv;
00503 }
00504 
00505 #if 0 // This code is really fast at serializing a range, but unfortunately
00506       // there are problems with it so we don't use it now, maybe later...
00507 static nsresult ChildAt(nsIDOMNode* aNode, PRInt32 aIndex, nsIDOMNode*& aChild)
00508 {
00509   nsCOMPtr<nsIContent> content(do_QueryInterface(aNode));
00510 
00511   aChild = nsnull;
00512 
00513   NS_ENSURE_TRUE(content, NS_ERROR_FAILURE);
00514 
00515   nsIContent *child = content->GetChildAt(aIndex);
00516 
00517   if (child)
00518     return CallQueryInterface(child, &aChild);
00519 
00520   return NS_OK;
00521 }
00522 
00523 static PRInt32 IndexOf(nsIDOMNode* aParent, nsIDOMNode* aChild)
00524 {
00525   nsCOMPtr<nsIContent> parent(do_QueryInterface(aParent));
00526   nsCOMPtr<nsIContent> child(do_QueryInterface(aChild));
00527 
00528   if (!parent)
00529     return -1;
00530 
00531   return parent->IndexOf(child);
00532 }
00533 
00534 static inline PRInt32 GetIndex(nsVoidArray& aIndexArray)
00535 {
00536   PRInt32 count = aIndexArray.Count();
00537 
00538   if (count) {
00539     return (PRInt32)aIndexArray.ElementAt(count - 1);
00540   }
00541 
00542   return 0;
00543 }
00544 
00545 static nsresult GetNextNode(nsIDOMNode* aNode, nsVoidArray& aIndexArray,
00546                             nsIDOMNode*& aNextNode,
00547                             nsRangeIterationDirection& aDirection)
00548 {
00549   PRBool hasChildren;
00550 
00551   aNextNode = nsnull;
00552 
00553   aNode->HasChildNodes(&hasChildren);
00554 
00555   if (hasChildren && aDirection == kDirectionIn) {
00556     ChildAt(aNode, 0, aNextNode);
00557     NS_ENSURE_TRUE(aNextNode, NS_ERROR_FAILURE);
00558 
00559     aIndexArray.AppendElement((void *)0);
00560 
00561     aDirection = kDirectionIn;
00562   } else if (aDirection == kDirectionIn) {
00563     aNextNode = aNode;
00564 
00565     NS_ADDREF(aNextNode);
00566 
00567     aDirection = kDirectionOut;
00568   } else {
00569     nsCOMPtr<nsIDOMNode> parent;
00570 
00571     aNode->GetParentNode(getter_AddRefs(parent));
00572     NS_ENSURE_TRUE(parent, NS_ERROR_FAILURE);
00573 
00574     PRInt32 count = aIndexArray.Count();
00575 
00576     if (count) {
00577       PRInt32 indx = (PRInt32)aIndexArray.ElementAt(count - 1);
00578 
00579       ChildAt(parent, indx + 1, aNextNode);
00580 
00581       if (aNextNode)
00582         aIndexArray.ReplaceElementAt((void *)(indx + 1), count - 1);
00583       else
00584         aIndexArray.RemoveElementAt(count - 1);
00585     } else {
00586       PRInt32 indx = IndexOf(parent, aNode);
00587 
00588       if (indx >= 0) {
00589         ChildAt(parent, indx + 1, aNextNode);
00590 
00591         if (aNextNode)
00592           aIndexArray.AppendElement((void *)(indx + 1));
00593       }
00594     }
00595 
00596     if (aNextNode) {
00597       aDirection = kDirectionIn;
00598     } else {
00599       aDirection = kDirectionOut;
00600 
00601       aNextNode = parent;
00602 
00603       NS_ADDREF(aNextNode);
00604     }
00605   }
00606 
00607   return NS_OK;
00608 }
00609 #endif
00610 
00611 static PRBool IsTextNode(nsIDOMNode *aNode)
00612 {
00613   if (!aNode) return PR_FALSE;
00614   PRUint16 nodeType;
00615   aNode->GetNodeType(&nodeType);
00616   if (nodeType == nsIDOMNode::TEXT_NODE ||
00617       nodeType == nsIDOMNode::CDATA_SECTION_NODE)
00618     return PR_TRUE;
00619   return PR_FALSE;
00620 }
00621 
00622 static nsresult GetLengthOfDOMNode(nsIDOMNode *aNode, PRUint32 &aCount) 
00623 {
00624   aCount = 0;
00625   if (!aNode) { return NS_ERROR_NULL_POINTER; }
00626   nsresult result=NS_OK;
00627   nsCOMPtr<nsIDOMCharacterData>nodeAsChar;
00628   nodeAsChar = do_QueryInterface(aNode);
00629   if (nodeAsChar) {
00630     nodeAsChar->GetLength(&aCount);
00631   }
00632   else
00633   {
00634     PRBool hasChildNodes;
00635     aNode->HasChildNodes(&hasChildNodes);
00636     if (PR_TRUE==hasChildNodes)
00637     {
00638       nsCOMPtr<nsIDOMNodeList>nodeList;
00639       result = aNode->GetChildNodes(getter_AddRefs(nodeList));
00640       if (NS_SUCCEEDED(result) && nodeList) {
00641         nodeList->GetLength(&aCount);
00642       }
00643     }
00644   }
00645   return result;
00646 }
00647 
00648 nsresult
00649 nsDocumentEncoder::SerializeRangeNodes(nsIDOMRange* aRange, 
00650                                        nsIDOMNode* aNode, 
00651                                        nsAString& aString,
00652                                        PRInt32 aDepth)
00653 {
00654   nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
00655   NS_ENSURE_TRUE(content, NS_ERROR_FAILURE);
00656 
00657   nsresult rv=NS_OK;
00658   
00659   // get start and end nodes for this recursion level
00660   nsCOMPtr<nsIContent> startNode, endNode;
00661   PRInt32 start = mStartRootIndex - aDepth;
00662   if (start >= 0 && start <= mStartNodes.Count())
00663     startNode = NS_STATIC_CAST(nsIContent *, mStartNodes[start]);
00664 
00665   PRInt32 end = mEndRootIndex - aDepth;
00666   if (end >= 0 && end <= mEndNodes.Count())
00667     endNode = NS_STATIC_CAST(nsIContent *, mEndNodes[end]);
00668 
00669   if ((startNode != content) && (endNode != content))
00670   {
00671     // node is completely contained in range.  Serialize the whole subtree
00672     // rooted by this node.
00673     rv = SerializeToStringRecursive(aNode, aString);
00674     NS_ENSURE_SUCCESS(rv, rv);
00675   }
00676   else
00677   {
00678     // due to implementation it is impossible for text node to be both start and end of 
00679     // range.  We would have handled that case without getting here.
00680     if (IsTextNode(aNode))
00681     {
00682       if (startNode == content)
00683       {
00684         PRInt32 startOffset;
00685         aRange->GetStartOffset(&startOffset);
00686         rv = SerializeNodeStart(aNode, startOffset, -1, aString);
00687         NS_ENSURE_SUCCESS(rv, rv);
00688       }
00689       else
00690       {
00691         PRInt32 endOffset;
00692         aRange->GetEndOffset(&endOffset);
00693         rv = SerializeNodeStart(aNode, 0, endOffset, aString);
00694         NS_ENSURE_SUCCESS(rv, rv);
00695       }
00696     }
00697     else
00698     {
00699       if (aNode != mCommonParent)
00700       {
00701         if (IncludeInContext(aNode))
00702         {
00703           // halt the incrementing of mStartDepth/mEndDepth.  This is
00704           // so paste client will include this node in paste.
00705           mHaltRangeHint = PR_TRUE;
00706         }
00707         if ((startNode == content) && !mHaltRangeHint) mStartDepth++;
00708         if ((endNode == content) && !mHaltRangeHint) mEndDepth++;
00709       
00710         // serialize the start of this node
00711         rv = SerializeNodeStart(aNode, 0, -1, aString);
00712         NS_ENSURE_SUCCESS(rv, rv);
00713       }
00714       
00715       // do some calculations that will tell us which children of this
00716       // node are in the range.
00717       nsCOMPtr<nsIDOMNode> childAsNode;
00718       PRInt32 startOffset = 0, endOffset = -1;
00719       if (startNode == content && mStartRootIndex >= aDepth)
00720         startOffset = NS_PTR_TO_INT32(mStartOffsets[mStartRootIndex - aDepth]);
00721       if (endNode == content && mEndRootIndex >= aDepth)
00722         endOffset = NS_PTR_TO_INT32(mEndOffsets[mEndRootIndex - aDepth]) ;
00723       // generated content will cause offset values of -1 to be returned.  
00724       PRInt32 j;
00725       PRUint32 childCount = content->GetChildCount();
00726 
00727       if (startOffset == -1) startOffset = 0;
00728       if (endOffset == -1) endOffset = childCount;
00729       else
00730       {
00731         // if we are at the "tip" of the selection, endOffset is fine.
00732         // otherwise, we need to add one.  This is because of the semantics
00733         // of the offset list created by GetAncestorsAndOffsets().  The
00734         // intermediate points on the list use the endOffset of the 
00735         // location of the ancestor, rather than just past it.  So we need
00736         // to add one here in order to include it in the children we serialize.
00737         nsCOMPtr<nsIDOMNode> endParent;
00738         aRange->GetEndContainer(getter_AddRefs(endParent));
00739         if (aNode != endParent)
00740         {
00741           endOffset++;
00742         }
00743       }
00744       // serialize the children of this node that are in the range
00745       for (j=startOffset; j<endOffset; j++)
00746       {
00747         childAsNode = do_QueryInterface(content->GetChildAt(j));
00748 
00749         if ((j==startOffset) || (j==endOffset-1))
00750           rv = SerializeRangeNodes(aRange, childAsNode, aString, aDepth+1);
00751         else
00752           rv = SerializeToStringRecursive(childAsNode, aString);
00753 
00754         NS_ENSURE_SUCCESS(rv, rv);
00755       }
00756 
00757       // serialize the end of this node
00758       if (aNode != mCommonParent)
00759       {
00760         rv = SerializeNodeEnd(aNode, aString);
00761         NS_ENSURE_SUCCESS(rv, rv); 
00762       }
00763     }
00764   }
00765   return NS_OK;
00766 }
00767 
00768 nsresult
00769 nsDocumentEncoder::SerializeRangeContextStart(const nsVoidArray& aAncestorArray,
00770                                               nsAString& aString)
00771 {
00772   PRInt32 i = aAncestorArray.Count();
00773   nsresult rv = NS_OK;
00774 
00775   while (i > 0) {
00776     nsIDOMNode *node = (nsIDOMNode *)aAncestorArray.ElementAt(--i);
00777 
00778     if (!node)
00779       break;
00780 
00781     if (IncludeInContext(node)) {
00782       rv = SerializeNodeStart(node, 0, -1, aString);
00783 
00784       if (NS_FAILED(rv))
00785         break;
00786     }
00787   }
00788 
00789   return rv;
00790 }
00791 
00792 nsresult
00793 nsDocumentEncoder::SerializeRangeContextEnd(const nsVoidArray& aAncestorArray,
00794                                             nsAString& aString)
00795 {
00796   PRInt32 i = 0;
00797   PRInt32 count = aAncestorArray.Count();
00798   nsresult rv = NS_OK;
00799 
00800   while (i < count) {
00801     nsIDOMNode *node = (nsIDOMNode *)aAncestorArray.ElementAt(i++);
00802 
00803     if (!node)
00804       break;
00805 
00806     if (IncludeInContext(node)) {
00807       rv = SerializeNodeEnd(node, aString);
00808 
00809       if (NS_FAILED(rv))
00810         break;
00811     }
00812   }
00813 
00814   return rv;
00815 }
00816 
00817 nsresult
00818 nsDocumentEncoder::SerializeRangeToString(nsIDOMRange *aRange,
00819                                           nsAString& aOutputString)
00820 {
00821   if (!aRange)
00822     return NS_OK;
00823 
00824   PRBool collapsed;
00825 
00826   aRange->GetCollapsed(&collapsed);
00827 
00828   if (collapsed)
00829     return NS_OK;
00830 
00831   nsCOMPtr<nsIDOMNode> startParent, endParent;
00832   PRInt32 startOffset, endOffset;
00833   
00834   aRange->GetCommonAncestorContainer(getter_AddRefs(mCommonParent));
00835 
00836   if (!mCommonParent)
00837     return NS_OK;
00838   
00839   aRange->GetStartContainer(getter_AddRefs(startParent));
00840   NS_ENSURE_TRUE(startParent, NS_ERROR_FAILURE);
00841   aRange->GetStartOffset(&startOffset);
00842 
00843   aRange->GetEndContainer(getter_AddRefs(endParent));
00844   NS_ENSURE_TRUE(endParent, NS_ERROR_FAILURE);
00845   aRange->GetEndOffset(&endOffset);
00846 
00847   mCommonAncestors.Clear();
00848   mStartNodes.Clear();
00849   mStartOffsets.Clear();
00850   mEndNodes.Clear();
00851   mEndOffsets.Clear();
00852 
00853   nsContentUtils::GetAncestors(mCommonParent, &mCommonAncestors);
00854   nsContentUtils::GetAncestorsAndOffsets(startParent, startOffset,
00855                                          &mStartNodes, &mStartOffsets);
00856   nsContentUtils::GetAncestorsAndOffsets(endParent, endOffset,
00857                                          &mEndNodes, &mEndOffsets);
00858 
00859   nsCOMPtr<nsIContent> commonContent = do_QueryInterface(mCommonParent);
00860   mStartRootIndex = mStartNodes.IndexOf(commonContent);
00861   mEndRootIndex = mEndNodes.IndexOf(commonContent);
00862   
00863   nsresult rv = NS_OK;
00864 
00865   rv = SerializeRangeContextStart(mCommonAncestors, aOutputString);
00866   NS_ENSURE_SUCCESS(rv, rv);
00867 
00868   if ((startParent == endParent) && IsTextNode(startParent))
00869   {
00870     rv = SerializeNodeStart(startParent, startOffset, endOffset, aOutputString);
00871     NS_ENSURE_SUCCESS(rv, rv);
00872   }
00873   else
00874   {
00875     rv = SerializeRangeNodes(aRange, mCommonParent, aOutputString, 0);
00876     NS_ENSURE_SUCCESS(rv, rv);
00877   }
00878   rv = SerializeRangeContextEnd(mCommonAncestors, aOutputString);
00879   NS_ENSURE_SUCCESS(rv, rv);
00880 
00881   return rv;
00882 }
00883 
00884 NS_IMETHODIMP
00885 nsDocumentEncoder::EncodeToString(nsAString& aOutputString)
00886 {
00887   if (!mDocument)
00888     return NS_ERROR_NOT_INITIALIZED;
00889 
00890   aOutputString.Truncate();
00891 
00892   nsCAutoString progId(NS_CONTENTSERIALIZER_CONTRACTID_PREFIX);
00893   AppendUTF16toUTF8(mMimeType, progId);
00894 
00895   mSerializer = do_CreateInstance(progId.get());
00896   NS_ENSURE_TRUE(mSerializer, NS_ERROR_NOT_IMPLEMENTED);
00897 
00898   nsresult rv = NS_OK;
00899 
00900   nsCOMPtr<nsIAtom> charsetAtom;
00901   if (!mCharset.IsEmpty()) {
00902     if (!mCharsetConverterManager) {
00903       mCharsetConverterManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
00904       NS_ENSURE_SUCCESS(rv, rv);
00905     }
00906   }
00907   mSerializer->Init(mFlags, mWrapColumn, mCharset.get(), mIsCopying);
00908 
00909   if (mSelection) {
00910     nsCOMPtr<nsIDOMRange> range;
00911     PRInt32 i, count = 0;
00912 
00913     rv = mSelection->GetRangeCount(&count);
00914     NS_ENSURE_SUCCESS(rv, rv);
00915 
00916     for (i = 0; i < count; i++) {
00917       mSelection->GetRangeAt(i, getter_AddRefs(range));
00918 
00919       rv = SerializeRangeToString(range, aOutputString);
00920       NS_ENSURE_SUCCESS(rv, rv);
00921     }
00922 
00923     mSelection = nsnull;
00924   } else if (mRange) {
00925       rv = SerializeRangeToString(mRange, aOutputString);
00926 
00927       mRange = nsnull;
00928   } else if (mNode) {
00929     rv = SerializeToStringRecursive(mNode, aOutputString);
00930     mNode = nsnull;
00931   } else {
00932     nsCOMPtr<nsIDOMDocument> domdoc(do_QueryInterface(mDocument));
00933     rv = mSerializer->AppendDocumentStart(domdoc, aOutputString);
00934 
00935     if (NS_SUCCEEDED(rv)) {
00936       nsCOMPtr<nsIDOMNode> doc(do_QueryInterface(mDocument));
00937 
00938       rv = SerializeToStringRecursive(doc, aOutputString);
00939     }
00940   }
00941 
00942   NS_ENSURE_SUCCESS(rv, rv);
00943   rv = mSerializer->Flush(aOutputString);
00944 
00945   return rv;
00946 }
00947 
00948 NS_IMETHODIMP
00949 nsDocumentEncoder::EncodeToStream(nsIOutputStream* aStream)
00950 {
00951   nsresult rv = NS_OK;
00952 
00953   if (!mDocument)
00954     return NS_ERROR_NOT_INITIALIZED;
00955 
00956   if (!mCharsetConverterManager) {
00957     mCharsetConverterManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
00958     NS_ENSURE_SUCCESS(rv, rv);
00959   }
00960 
00961   rv = mCharsetConverterManager->GetUnicodeEncoder(mCharset.get(),
00962                                                    getter_AddRefs(mUnicodeEncoder));
00963   NS_ENSURE_SUCCESS(rv, rv);
00964 
00965   if (mMimeType.LowerCaseEqualsLiteral("text/plain")) {
00966     rv = mUnicodeEncoder->SetOutputErrorBehavior(nsIUnicodeEncoder::kOnError_Replace, nsnull, '?');
00967     NS_ENSURE_SUCCESS(rv, rv);
00968   }
00969 
00970   mStream = aStream;
00971 
00972   nsAutoString buf;
00973 
00974   rv = EncodeToString(buf);
00975 
00976   // Force a flush of the last chunk of data.
00977   FlushText(buf, PR_TRUE);
00978 
00979   mStream = nsnull;
00980   mUnicodeEncoder = nsnull;
00981 
00982   return rv;
00983 }
00984 
00985 NS_IMETHODIMP
00986 nsDocumentEncoder::EncodeToStringWithContext(nsAString& aEncodedString, 
00987                                              nsAString& aContextString,
00988                                              nsAString& aInfoString)
00989 {
00990   return NS_ERROR_NOT_IMPLEMENTED;
00991 }
00992 
00993 NS_IMETHODIMP
00994 nsDocumentEncoder::SetNodeFixup(nsIDocumentEncoderNodeFixup *aFixup)
00995 {
00996   mNodeFixup = aFixup;
00997   return NS_OK;
00998 }
00999 
01000 
01001 nsresult NS_NewTextEncoder(nsIDocumentEncoder** aResult); // make mac compiler happy
01002 
01003 nsresult
01004 NS_NewTextEncoder(nsIDocumentEncoder** aResult)
01005 {
01006   *aResult = new nsDocumentEncoder;
01007   if (!*aResult)
01008     return NS_ERROR_OUT_OF_MEMORY;
01009  NS_ADDREF(*aResult);
01010  return NS_OK;
01011 }
01012 
01013 
01014 #ifdef XP_MAC
01015 #pragma mark -
01016 #pragma mark  nsDocumentEncoder declaration 
01017 #pragma mark -
01018 #endif
01019 
01020 class nsHTMLCopyEncoder : public nsDocumentEncoder
01021 {
01022 public:
01023 
01024   nsHTMLCopyEncoder();
01025   virtual ~nsHTMLCopyEncoder();
01026 
01027   NS_IMETHOD Init(nsIDocument* aDocument, const nsAString& aMimeType, PRUint32 aFlags);
01028 
01029   // overridden methods from nsDocumentEncoder
01030   NS_IMETHOD SetSelection(nsISelection* aSelection);
01031   NS_IMETHOD EncodeToStringWithContext(nsAString& aEncodedString, 
01032                                        nsAString& aContextString,
01033                                        nsAString& aInfoString);
01034 
01035 protected:
01036 
01037   enum Endpoint
01038   {
01039     kStart,
01040     kEnd
01041   };
01042   
01043   nsresult PromoteRange(nsIDOMRange *inRange);
01044   nsresult PromoteAncestorChain(nsCOMPtr<nsIDOMNode> *ioNode, 
01045                                 PRInt32 *ioStartOffset, 
01046                                 PRInt32 *ioEndOffset);
01047   nsresult GetPromotedPoint(Endpoint aWhere, nsIDOMNode *aNode, PRInt32 aOffset, 
01048                             nsCOMPtr<nsIDOMNode> *outNode, PRInt32 *outOffset, nsIDOMNode *aCommon);
01049   nsCOMPtr<nsIDOMNode> GetChildAt(nsIDOMNode *aParent, PRInt32 aOffset);
01050   PRBool IsMozBR(nsIDOMNode* aNode);
01051   nsresult GetNodeLocation(nsIDOMNode *inChild, nsCOMPtr<nsIDOMNode> *outParent, PRInt32 *outOffset);
01052   PRBool IsRoot(nsIDOMNode* aNode);
01053   PRBool IsFirstNode(nsIDOMNode *aNode);
01054   PRBool IsLastNode(nsIDOMNode *aNode);
01055   PRBool IsEmptyTextContent(nsIDOMNode* aNode);
01056   virtual PRBool IncludeInContext(nsIDOMNode *aNode);
01057 
01058   PRBool mIsTextWidget;
01059 };
01060 
01061 #ifdef XP_MAC
01062 #pragma mark  nsDocumentEncoder implementation 
01063 #pragma mark -
01064 #endif
01065 
01066 nsHTMLCopyEncoder::nsHTMLCopyEncoder()
01067 {
01068   mIsTextWidget = PR_FALSE;
01069 }
01070 
01071 nsHTMLCopyEncoder::~nsHTMLCopyEncoder()
01072 {
01073 }
01074 
01075 NS_IMETHODIMP
01076 nsHTMLCopyEncoder::Init(nsIDocument* aDocument,
01077                         const nsAString& aMimetype,
01078                         PRUint32 aFlags)
01079 {
01080   if (!aDocument)
01081     return NS_ERROR_INVALID_ARG;
01082 
01083   mIsTextWidget = PR_FALSE;
01084   Initialize();
01085 
01086   mIsCopying = PR_TRUE;
01087   mDocument = aDocument;
01088 
01089 
01090   mMimeType.AssignLiteral("text/html");
01091   
01092   // Make all links absolute when copying
01093   // (see related bugs #57296, #41924, #58646, #32768)
01094   mFlags = aFlags | OutputAbsoluteLinks;
01095 
01096   if (!mDocument->IsScriptEnabled())
01097     mFlags |= OutputNoScriptContent;
01098 
01099   return NS_OK;
01100 }
01101 
01102 NS_IMETHODIMP
01103 nsHTMLCopyEncoder::SetSelection(nsISelection* aSelection)
01104 {
01105   // check for text widgets: we need to recognize these so that
01106   // we don't tweak the selection to be outside of the magic
01107   // div that ender-lite text widgets are embedded in.
01108   
01109   if (!aSelection) 
01110     return NS_ERROR_NULL_POINTER;
01111   
01112   nsCOMPtr<nsIDOMRange> range;
01113   nsCOMPtr<nsIDOMNode> commonParent;
01114   PRInt32 count = 0;
01115 
01116   nsresult rv = aSelection->GetRangeCount(&count);
01117   NS_ENSURE_SUCCESS(rv, rv);
01118 
01119   // if selection is uninitialized return
01120   if (!count)
01121     return NS_ERROR_FAILURE;
01122   
01123   // we'll just use the common parent of the first range.  Implicit assumption
01124   // here that multi-range selections are table cell selections, in which case
01125   // the common parent is somewhere in the table and we don't really care where.
01126   rv = aSelection->GetRangeAt(0, getter_AddRefs(range));
01127   NS_ENSURE_SUCCESS(rv, rv);
01128   if (!range)
01129     return NS_ERROR_NULL_POINTER;
01130   range->GetCommonAncestorContainer(getter_AddRefs(commonParent));
01131 
01132   for (nsCOMPtr<nsIContent> selContent(do_QueryInterface(commonParent));
01133        selContent;
01134        selContent = selContent->GetParent())
01135   {
01136     // checking for selection inside a plaintext form widget
01137     nsIAtom *atom = selContent->Tag();
01138     if (atom == nsHTMLAtoms::input ||
01139         atom == nsHTMLAtoms::textarea)
01140     {
01141       mIsTextWidget = PR_TRUE;
01142       break;
01143     }
01144     else if (atom == nsHTMLAtoms::body)
01145     {
01146       // check for moz prewrap style on body.  If it's there we are 
01147       // in a plaintext editor.  This is pretty cheezy but I haven't 
01148       // found a good way to tell if we are in a plaintext editor.
01149       nsCOMPtr<nsIDOMElement> bodyElem = do_QueryInterface(selContent);
01150       nsAutoString wsVal;
01151       rv = bodyElem->GetAttribute(NS_LITERAL_STRING("style"), wsVal);
01152       if (NS_SUCCEEDED(rv) && (kNotFound != wsVal.Find(NS_LITERAL_STRING("-moz-pre-wrap"))))
01153       {
01154         mIsTextWidget = PR_TRUE;
01155         break;
01156       }
01157     }
01158   }
01159   
01160   // also consider ourselves in a text widget if we can't find an html document
01161   nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(mDocument);
01162   if (!htmlDoc || mDocument->IsCaseSensitive())
01163     mIsTextWidget = PR_TRUE;
01164   
01165   // normalize selection if we are not in a widget
01166   if (mIsTextWidget) 
01167   {
01168     mSelection = aSelection;
01169     mMimeType.AssignLiteral("text/plain");
01170     return NS_OK;
01171   }
01172   
01173   // there's no Clone() for selection! fix...
01174   //nsresult rv = aSelection->Clone(getter_AddRefs(mSelection);
01175   //NS_ENSURE_SUCCESS(rv, rv);
01176   NS_NewDomSelection(getter_AddRefs(mSelection));
01177   NS_ENSURE_TRUE(mSelection, NS_ERROR_FAILURE);
01178   nsCOMPtr<nsISelectionPrivate> privSelection( do_QueryInterface(aSelection) );
01179   NS_ENSURE_TRUE(privSelection, NS_ERROR_FAILURE);
01180   
01181   // get selection range enumerator
01182   nsCOMPtr<nsIEnumerator> enumerator;
01183   rv = privSelection->GetEnumerator(getter_AddRefs(enumerator));
01184   NS_ENSURE_SUCCESS(rv, rv);
01185   NS_ENSURE_TRUE(enumerator, NS_ERROR_FAILURE);
01186 
01187   // loop thru the ranges in the selection
01188   enumerator->First(); 
01189   nsCOMPtr<nsISupports> currentItem;
01190   while ((NS_ENUMERATOR_FALSE == enumerator->IsDone()))
01191   {
01192     rv = enumerator->CurrentItem(getter_AddRefs(currentItem));
01193     NS_ENSURE_SUCCESS(rv, rv);
01194     NS_ENSURE_TRUE(currentItem, NS_ERROR_FAILURE);
01195     
01196     range = do_QueryInterface(currentItem);
01197     NS_ENSURE_TRUE(range, NS_ERROR_FAILURE);
01198     nsCOMPtr<nsIDOMRange> myRange;
01199     range->CloneRange(getter_AddRefs(myRange));
01200     NS_ENSURE_TRUE(myRange, NS_ERROR_FAILURE);
01201 
01202     // adjust range to include any ancestors who's children are entirely selected
01203     rv = PromoteRange(myRange);
01204     NS_ENSURE_SUCCESS(rv, rv);
01205     
01206     rv = mSelection->AddRange(myRange);
01207     NS_ENSURE_SUCCESS(rv, rv);
01208 
01209     enumerator->Next();
01210   }
01211 
01212   return NS_OK;
01213 }
01214 
01215 NS_IMETHODIMP
01216 nsHTMLCopyEncoder::EncodeToStringWithContext(nsAString& aEncodedString, 
01217                                              nsAString& aContextString,
01218                                              nsAString& aInfoString)
01219 {
01220   nsresult rv = EncodeToString(aEncodedString);
01221   NS_ENSURE_SUCCESS(rv, rv);
01222 
01223   // do not encode any context info or range hints if we are in a text widget.
01224   if (mIsTextWidget) return NS_OK;
01225 
01226   // now encode common ancestors into aContextString.  Note that the common ancestors
01227   // will be for the last range in the selection in the case of multirange selections.
01228   // encoding ancestors every range in a multirange selection in a way that could be 
01229   // understood by the paste code would be a lot more work to do.  As a practical matter,
01230   // selections are single range, and the ones that aren't are table cell selections
01231   // where all the cells are in the same table.
01232 
01233   // leaf of ancestors might be text node.  If so discard it.
01234   PRInt32 count = mCommonAncestors.Count();
01235   PRInt32 i;
01236   nsCOMPtr<nsIDOMNode> node;
01237   if (count > 0)
01238     node = NS_STATIC_CAST(nsIDOMNode *, mCommonAncestors.ElementAt(0));
01239 
01240   if (node && IsTextNode(node)) 
01241   {
01242     mCommonAncestors.RemoveElementAt(0);
01243     // don't forget to adjust range depth info
01244     if (mStartDepth) mStartDepth--;
01245     if (mEndDepth) mEndDepth--;
01246     // and the count
01247     count--;
01248   }
01249   
01250   i = count;
01251   while (i > 0)
01252   {
01253     node = NS_STATIC_CAST(nsIDOMNode *, mCommonAncestors.ElementAt(--i));
01254     SerializeNodeStart(node, 0, -1, aContextString);
01255   }
01256   //i = 0; guaranteed by above
01257   while (i < count)
01258   {
01259     node = NS_STATIC_CAST(nsIDOMNode *, mCommonAncestors.ElementAt(i++));
01260     SerializeNodeEnd(node, aContextString);
01261   }
01262 
01263   // encode range info : the start and end depth of the selection, where the depth is 
01264   // distance down in the parent hierarchy.  Later we will need to add leading/trailing
01265   // whitespace info to this.
01266   nsAutoString infoString;
01267   infoString.AppendInt(mStartDepth);
01268   infoString.Append(PRUnichar(','));
01269   infoString.AppendInt(mEndDepth);
01270   aInfoString = infoString;
01271   
01272   return NS_OK;
01273 }
01274 
01275 
01276 PRBool
01277 nsHTMLCopyEncoder::IncludeInContext(nsIDOMNode *aNode)
01278 {
01279   nsCOMPtr<nsIContent> content(do_QueryInterface(aNode));
01280 
01281   if (!content)
01282     return PR_FALSE;
01283 
01284   nsIAtom *tag = content->Tag();
01285 
01286   return (tag == nsHTMLAtoms::b        ||
01287           tag == nsHTMLAtoms::i        ||
01288           tag == nsHTMLAtoms::u        ||
01289           tag == nsHTMLAtoms::a        ||
01290           tag == nsHTMLAtoms::tt       ||
01291           tag == nsHTMLAtoms::s        ||
01292           tag == nsHTMLAtoms::big      ||
01293           tag == nsHTMLAtoms::small    ||
01294           tag == nsHTMLAtoms::strike   ||
01295           tag == nsHTMLAtoms::em       ||
01296           tag == nsHTMLAtoms::strong   ||
01297           tag == nsHTMLAtoms::dfn      ||
01298           tag == nsHTMLAtoms::code     ||
01299           tag == nsHTMLAtoms::cite     ||
01300           tag == nsHTMLAtoms::variable ||
01301           tag == nsHTMLAtoms::abbr     ||
01302           tag == nsHTMLAtoms::font     ||
01303           tag == nsHTMLAtoms::script   ||
01304           tag == nsHTMLAtoms::span     ||
01305           tag == nsHTMLAtoms::pre      ||
01306           tag == nsHTMLAtoms::h1       ||
01307           tag == nsHTMLAtoms::h2       ||
01308           tag == nsHTMLAtoms::h3       ||
01309           tag == nsHTMLAtoms::h4       ||
01310           tag == nsHTMLAtoms::h5       ||
01311           tag == nsHTMLAtoms::h6);
01312 }
01313 
01314 
01315 nsresult 
01316 nsHTMLCopyEncoder::PromoteRange(nsIDOMRange *inRange)
01317 {
01318   if (!inRange) return NS_ERROR_NULL_POINTER;
01319   nsresult rv;
01320   nsCOMPtr<nsIDOMNode> startNode, endNode, common;
01321   PRInt32 startOffset, endOffset;
01322   
01323   rv = inRange->GetCommonAncestorContainer(getter_AddRefs(common));
01324   NS_ENSURE_SUCCESS(rv, rv);
01325   rv = inRange->GetStartContainer(getter_AddRefs(startNode));
01326   NS_ENSURE_SUCCESS(rv, rv);
01327   rv = inRange->GetStartOffset(&startOffset);
01328   NS_ENSURE_SUCCESS(rv, rv);
01329   rv = inRange->GetEndContainer(getter_AddRefs(endNode));
01330   NS_ENSURE_SUCCESS(rv, rv);
01331   rv = inRange->GetEndOffset(&endOffset);
01332   NS_ENSURE_SUCCESS(rv, rv);
01333   
01334   nsCOMPtr<nsIDOMNode> opStartNode;
01335   nsCOMPtr<nsIDOMNode> opEndNode;
01336   PRInt32 opStartOffset, opEndOffset;
01337   nsCOMPtr<nsIDOMRange> opRange;
01338   
01339   // examine range endpoints.  
01340   rv = GetPromotedPoint( kStart, startNode, startOffset, address_of(opStartNode), &opStartOffset, common);
01341   NS_ENSURE_SUCCESS(rv, rv);
01342   rv = GetPromotedPoint( kEnd, endNode, endOffset, address_of(opEndNode), &opEndOffset, common);
01343   NS_ENSURE_SUCCESS(rv, rv);
01344   
01345   // if both range endpoints are at the common ancestor, check for possible inclusion of ancestors
01346   if ( (opStartNode == common) && (opEndNode == common) )
01347   {
01348     rv = PromoteAncestorChain(address_of(opStartNode), &opStartOffset, &opEndOffset);
01349     NS_ENSURE_SUCCESS(rv, rv);
01350     opEndNode = opStartNode;
01351   }
01352   
01353   // set the range to the new values
01354   rv = inRange->SetStart(opStartNode, opStartOffset);
01355   NS_ENSURE_SUCCESS(rv, rv);
01356   rv = inRange->SetEnd(opEndNode, opEndOffset);
01357   return rv;
01358 } 
01359 
01360 
01361 // PromoteAncestorChain will promote a range represented by [{*ioNode,*ioStartOffset} , {*ioNode,*ioEndOffset}]
01362 // The promotion is different from that found in getPromotedPoint: it will only promote one endpoint if it can
01363 // promote the other.  Thus, instead of having a startnode/endNode, there is just the one ioNode.
01364 nsresult
01365 nsHTMLCopyEncoder::PromoteAncestorChain(nsCOMPtr<nsIDOMNode> *ioNode, 
01366                                         PRInt32 *ioStartOffset, 
01367                                         PRInt32 *ioEndOffset) 
01368 {
01369   if (!ioNode || !ioStartOffset || !ioEndOffset) return NS_ERROR_NULL_POINTER;
01370 
01371   nsresult rv = NS_OK;
01372   PRBool done = PR_FALSE;
01373 
01374   nsCOMPtr<nsIDOMNode> frontNode, endNode, parent;
01375   PRInt32 frontOffset, endOffset;
01376   
01377   // loop for as long as we can promote both endpoints
01378   while (!done)
01379   {
01380     rv = (*ioNode)->GetParentNode(getter_AddRefs(parent));
01381     if ((NS_FAILED(rv)) || !parent)
01382       done = PR_TRUE;
01383     else
01384     {
01385       // passing parent as last param to GetPromotedPoint() allows it to promote only one level
01386       // up the heirarchy.
01387       rv = GetPromotedPoint( kStart, *ioNode, *ioStartOffset, address_of(frontNode), &frontOffset, parent);
01388       NS_ENSURE_SUCCESS(rv, rv);
01389       // then we make the same attempt with the endpoint
01390       rv = GetPromotedPoint( kEnd, *ioNode, *ioEndOffset, address_of(endNode), &endOffset, parent);
01391       NS_ENSURE_SUCCESS(rv, rv);
01392       // if both endpoints were promoted one level, keep looping - otherwise we are done.
01393       if ( (frontNode != parent) || (endNode != parent) )
01394         done = PR_TRUE;
01395       else
01396       {
01397         *ioNode = frontNode;  
01398         *ioStartOffset = frontOffset;
01399         *ioEndOffset = endOffset;
01400       }
01401     }
01402   }
01403   return rv;
01404 }
01405 
01406 nsresult
01407 nsHTMLCopyEncoder::GetPromotedPoint(Endpoint aWhere, nsIDOMNode *aNode, PRInt32 aOffset, 
01408                                   nsCOMPtr<nsIDOMNode> *outNode, PRInt32 *outOffset, nsIDOMNode *common)
01409 {
01410   nsresult rv = NS_OK;
01411   nsCOMPtr<nsIDOMNode> node = aNode;
01412   nsCOMPtr<nsIDOMNode> parent = aNode;
01413   PRInt32 offset = aOffset;
01414   PRBool  bResetPromotion = PR_FALSE;
01415   
01416   // default values
01417   *outNode = node;
01418   *outOffset = offset;
01419 
01420   if (common == node) 
01421     return NS_OK;
01422     
01423   if (aWhere == kStart)
01424   {
01425     // some special casing for text nodes
01426     if (IsTextNode(aNode))  
01427     {
01428       // if not at beginning of text node, we are done
01429       if (offset >  0) 
01430       {
01431         // unless everything before us in just whitespace.  NOTE: we need a more
01432         // general solution that truly detects all cases of non-significant
01433         // whitesace with no false alarms.
01434         nsCOMPtr<nsIDOMCharacterData> nodeAsText = do_QueryInterface(aNode);
01435         nsAutoString text;
01436         nodeAsText->SubstringData(0, offset, text);
01437         text.CompressWhitespace();
01438         if (!text.IsEmpty())
01439           return NS_OK;
01440         bResetPromotion = PR_TRUE;
01441       }
01442       // else
01443       rv = GetNodeLocation(aNode, address_of(parent), &offset);
01444       NS_ENSURE_SUCCESS(rv, rv);
01445     }
01446     else
01447     {
01448       node = GetChildAt(parent,offset);
01449     }
01450     if (!node) node = parent;
01451 
01452     // finding the real start for this point.  look up the tree for as long as we are the 
01453     // first node in the container, and as long as we haven't hit the body node.
01454     if (!IsRoot(node) && (parent != common))
01455     {
01456       rv = GetNodeLocation(node, address_of(parent), &offset);
01457       NS_ENSURE_SUCCESS(rv, rv);
01458       if (offset == -1) return NS_OK; // we hit generated content; STOP
01459       nsIParserService *parserService =
01460         nsContentUtils::GetParserServiceWeakRef();
01461       if (!parserService)
01462         return NS_ERROR_OUT_OF_MEMORY;
01463       while ((IsFirstNode(node)) && (!IsRoot(parent)) && (parent != common))
01464       {
01465         if (bResetPromotion)
01466         {
01467           nsCOMPtr<nsIContent> content = do_QueryInterface(parent);
01468           if (content)
01469           {
01470             PRBool isBlock = PR_FALSE;
01471             parserService->IsBlock(parserService->HTMLAtomTagToId(content->Tag()), isBlock);
01472             if (isBlock)
01473             {
01474               bResetPromotion = PR_FALSE;
01475             }
01476           }   
01477         }
01478          
01479         node = parent;
01480         rv = GetNodeLocation(node, address_of(parent), &offset);
01481         NS_ENSURE_SUCCESS(rv, rv);
01482         if (offset == -1)  // we hit generated content; STOP
01483         {
01484           // back up a bit
01485           parent = node;
01486           offset = 0;
01487           break;
01488         }
01489       } 
01490       if (bResetPromotion)
01491       {
01492         *outNode = aNode;
01493         *outOffset = aOffset;
01494       }
01495       else
01496       {
01497         *outNode = parent;
01498         *outOffset = offset;
01499       }
01500       return rv;
01501     }
01502   }
01503   
01504   if (aWhere == kEnd)
01505   {
01506     // some special casing for text nodes
01507     if (IsTextNode(aNode))  
01508     {
01509       // if not at end of text node, we are done
01510       PRUint32 len;
01511       GetLengthOfDOMNode(aNode, len);
01512       if (offset < (PRInt32)len)
01513       {
01514         // unless everything after us in just whitespace.  NOTE: we need a more
01515         // general solution that truly detects all cases of non-significant
01516         // whitesace with no false alarms.
01517         nsCOMPtr<nsIDOMCharacterData> nodeAsText = do_QueryInterface(aNode);
01518         nsAutoString text;
01519         nodeAsText->SubstringData(offset, len-offset, text);
01520         text.CompressWhitespace();
01521         if (!text.IsEmpty())
01522           return NS_OK;
01523         bResetPromotion = PR_TRUE;
01524       }
01525       rv = GetNodeLocation(aNode, address_of(parent), &offset);
01526       NS_ENSURE_SUCCESS(rv, rv);
01527     }
01528     else
01529     {
01530       if (offset) offset--; // we want node _before_ offset
01531       node = GetChildAt(parent,offset);
01532     }
01533     if (!node) node = parent;
01534     
01535     // finding the real end for this point.  look up the tree for as long as we are the 
01536     // last node in the container, and as long as we haven't hit the body node.
01537     if (!IsRoot(node) && (parent != common))
01538     {
01539       rv = GetNodeLocation(node, address_of(parent), &offset);
01540       NS_ENSURE_SUCCESS(rv, rv);
01541       if (offset == -1) return NS_OK; // we hit generated content; STOP
01542       nsIParserService *parserService =
01543         nsContentUtils::GetParserServiceWeakRef();
01544       if (!parserService)
01545         return NS_ERROR_OUT_OF_MEMORY;
01546       while ((IsLastNode(node)) && (!IsRoot(parent)) && (parent != common))
01547       {
01548         if (bResetPromotion)
01549         {
01550           nsCOMPtr<nsIContent> content = do_QueryInterface(parent);
01551           if (content)
01552           {
01553             PRBool isBlock = PR_FALSE;
01554             parserService->IsBlock(parserService->HTMLAtomTagToId(content->Tag()), isBlock);
01555             if (isBlock)
01556             {
01557               bResetPromotion = PR_FALSE;
01558             }
01559           }   
01560         }
01561           
01562         node = parent;
01563         rv = GetNodeLocation(node, address_of(parent), &offset);
01564         NS_ENSURE_SUCCESS(rv, rv);
01565         if (offset == -1)  // we hit generated content; STOP
01566         {
01567           // back up a bit
01568           parent = node;
01569           offset = 0;
01570           break;
01571         }
01572       } 
01573       if (bResetPromotion)
01574       {
01575         *outNode = aNode;
01576         *outOffset = aOffset;
01577       }
01578       else
01579       {
01580         *outNode = parent;
01581         offset++;  // add one since this in an endpoint - want to be AFTER node.
01582         *outOffset = offset;
01583       }
01584       return rv;
01585     }
01586   }
01587   
01588   return rv;
01589 }
01590 
01591 nsCOMPtr<nsIDOMNode> 
01592 nsHTMLCopyEncoder::GetChildAt(nsIDOMNode *aParent, PRInt32 aOffset)
01593 {
01594   nsCOMPtr<nsIDOMNode> resultNode;
01595   
01596   if (!aParent) 
01597     return resultNode;
01598   
01599   nsCOMPtr<nsIContent> content = do_QueryInterface(aParent);
01600   NS_PRECONDITION(content, "null content in nsHTMLCopyEncoder::GetChildAt");
01601 
01602   resultNode = do_QueryInterface(content->GetChildAt(aOffset));
01603 
01604   return resultNode;
01605 }
01606 
01607 PRBool 
01608 nsHTMLCopyEncoder::IsMozBR(nsIDOMNode* aNode)
01609 {
01610   if (IsTag(aNode, nsHTMLAtoms::br))
01611   {
01612     nsCOMPtr<nsIDOMElement> elem = do_QueryInterface(aNode);
01613     if (elem)
01614     {
01615       nsAutoString typeAttrName(NS_LITERAL_STRING("type"));
01616       nsAutoString typeAttrVal;
01617       nsresult rv = elem->GetAttribute(typeAttrName, typeAttrVal);
01618       ToLowerCase(typeAttrVal);
01619       if (NS_SUCCEEDED(rv) && (typeAttrVal.EqualsLiteral("_moz")))
01620         return PR_TRUE;
01621     }
01622     return PR_FALSE;
01623   }
01624   return PR_FALSE;
01625 }
01626 
01627 nsresult 
01628 nsHTMLCopyEncoder::GetNodeLocation(nsIDOMNode *inChild,
01629                                    nsCOMPtr<nsIDOMNode> *outParent,
01630                                    PRInt32 *outOffset)
01631 {
01632   NS_ASSERTION((inChild && outParent && outOffset), "bad args");
01633   nsresult result = NS_ERROR_NULL_POINTER;
01634   if (inChild && outParent && outOffset)
01635   {
01636     result = inChild->GetParentNode(getter_AddRefs(*outParent));
01637     if ((NS_SUCCEEDED(result)) && (*outParent))
01638     {
01639       nsCOMPtr<nsIContent> content = do_QueryInterface(*outParent);
01640       nsCOMPtr<nsIContent> cChild = do_QueryInterface(inChild);
01641       if (!cChild || !content)
01642         return NS_ERROR_NULL_POINTER;
01643 
01644       *outOffset = content->IndexOf(cChild);
01645     }
01646   }
01647   return result;
01648 }
01649 
01650 PRBool
01651 nsHTMLCopyEncoder::IsRoot(nsIDOMNode* aNode)
01652 {
01653   if (aNode)
01654   {
01655     if (mIsTextWidget) 
01656       return (IsTag(aNode, nsHTMLAtoms::div));
01657     else
01658       return (IsTag(aNode, nsHTMLAtoms::body) || 
01659               IsTag(aNode, nsHTMLAtoms::td)   ||
01660               IsTag(aNode, nsHTMLAtoms::th));
01661   }
01662   return PR_FALSE;
01663 }
01664 
01665 PRBool
01666 nsHTMLCopyEncoder::IsFirstNode(nsIDOMNode *aNode)
01667 {
01668   nsCOMPtr<nsIDOMNode> parent;
01669   PRInt32 offset, j=0;
01670   nsresult rv = GetNodeLocation(aNode, address_of(parent), &offset);
01671   if (NS_FAILED(rv)) 
01672   {
01673     NS_NOTREACHED("failure in IsFirstNode");
01674     return PR_FALSE;
01675   }
01676   if (offset == 0)  // easy case, we are first dom child
01677     return PR_TRUE;
01678   if (!parent)  
01679     return PR_TRUE;
01680   
01681   // need to check if any nodes before us are really visible.
01682   // Mike wrote something for me along these lines in nsSelectionController,
01683   // but I don't think it's ready for use yet - revisit.
01684   // HACK: for now, simply consider all whitespace text nodes to be 
01685   // invisible formatting nodes.
01686   nsCOMPtr<nsIDOMNodeList> childList;
01687   nsCOMPtr<nsIDOMNode> child;
01688 
01689   rv = parent->GetChildNodes(getter_AddRefs(childList));
01690   if (NS_FAILED(rv) || !childList) 
01691   {
01692     NS_NOTREACHED("failure in IsFirstNode");
01693     return PR_TRUE;
01694   }
01695   while (j < offset)
01696   {
01697     childList->Item(j, getter_AddRefs(child));
01698     if (!IsEmptyTextContent(child)) 
01699       return PR_FALSE;
01700     j++;
01701   }
01702   return PR_TRUE;
01703 }
01704 
01705 
01706 PRBool
01707 nsHTMLCopyEncoder::IsLastNode(nsIDOMNode *aNode)
01708 {
01709   nsCOMPtr<nsIDOMNode> parent;
01710   PRInt32 offset,j;
01711   PRUint32 numChildren;
01712   nsresult rv = GetNodeLocation(aNode, address_of(parent), &offset);
01713   if (NS_FAILED(rv)) 
01714   {
01715     NS_NOTREACHED("failure in IsLastNode");
01716     return PR_FALSE;
01717   }
01718   GetLengthOfDOMNode(parent, numChildren); 
01719   if (offset+1 == (PRInt32)numChildren) // easy case, we are last dom child
01720     return PR_TRUE;
01721   if (!parent)
01722     return PR_TRUE;
01723   // need to check if any nodes after us are really visible.
01724   // Mike wrote something for me along these lines in nsSelectionController,
01725   // but I don't think it's ready for use yet - revisit.
01726   // HACK: for now, simply consider all whitespace text nodes to be 
01727   // invisible formatting nodes.
01728   j = (PRInt32)numChildren-1;
01729   nsCOMPtr<nsIDOMNodeList>childList;
01730   nsCOMPtr<nsIDOMNode> child;
01731   rv = parent->GetChildNodes(getter_AddRefs(childList));
01732   if (NS_FAILED(rv) || !childList) 
01733   {
01734     NS_NOTREACHED("failure in IsLastNode");
01735     return PR_TRUE;
01736   }
01737   while (j > offset)
01738   {
01739     childList->Item(j, getter_AddRefs(child));
01740     j--;
01741     if (IsMozBR(child))  // we ignore trailing moz BRs.  
01742       continue;
01743     if (!IsEmptyTextContent(child)) 
01744       return PR_FALSE;
01745   }
01746   return PR_TRUE;
01747 }
01748 
01749 PRBool
01750 nsHTMLCopyEncoder::IsEmptyTextContent(nsIDOMNode* aNode)
01751 {
01752   PRBool result = PR_FALSE;
01753   nsCOMPtr<nsITextContent> tc(do_QueryInterface(aNode));
01754   if (tc) {
01755     result = tc->IsOnlyWhitespace();
01756   }
01757   return result;
01758 }
01759 
01760 nsresult NS_NewHTMLCopyTextEncoder(nsIDocumentEncoder** aResult); // make mac compiler happy
01761 
01762 nsresult
01763 NS_NewHTMLCopyTextEncoder(nsIDocumentEncoder** aResult)
01764 {
01765   *aResult = new nsHTMLCopyEncoder;
01766   if (!*aResult)
01767     return NS_ERROR_OUT_OF_MEMORY;
01768  NS_ADDREF(*aResult);
01769  return NS_OK;
01770 }