Back to index

lightning-sunbird  0.9+nobinonly
nsPlainTextSerializer.h
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  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef nsPlainTextSerializer_h__
00039 #define nsPlainTextSerializer_h__
00040 
00041 #include "nsIContentSerializer.h"
00042 #include "nsIHTMLContentSink.h"
00043 #include "nsHTMLTags.h"
00044 #include "nsCOMPtr.h"
00045 #include "nsString.h"
00046 #include "nsILineBreaker.h"
00047 #include "nsIContent.h"
00048 #include "nsIAtom.h"
00049 #include "nsIHTMLToTextSink.h"
00050 #include "nsIDocumentEncoder.h"
00051 #include "nsVoidArray.h"
00052 
00053 
00054 class nsPlainTextSerializer : public nsIContentSerializer,
00055                               public nsIHTMLContentSink,
00056                               public nsIHTMLToTextSink
00057 {
00058 public:
00059   nsPlainTextSerializer();
00060   virtual ~nsPlainTextSerializer();
00061 
00062   NS_DECL_ISUPPORTS
00063 
00064   // nsIContentSerializer
00065   NS_IMETHOD Init(PRUint32 flags, PRUint32 aWrapColumn,
00066                   const char* aCharSet, PRBool aIsCopying);
00067 
00068   NS_IMETHOD AppendText(nsIDOMText* aText, PRInt32 aStartOffset,
00069                         PRInt32 aEndOffset, nsAString& aStr);
00070   NS_IMETHOD AppendCDATASection(nsIDOMCDATASection* aCDATASection,
00071                                 PRInt32 aStartOffset, PRInt32 aEndOffset,
00072                                 nsAString& aStr);
00073   NS_IMETHOD AppendProcessingInstruction(nsIDOMProcessingInstruction* aPI,
00074                                          PRInt32 aStartOffset,
00075                                          PRInt32 aEndOffset,
00076                                          nsAString& aStr)  { return NS_OK; }
00077   NS_IMETHOD AppendComment(nsIDOMComment* aComment, PRInt32 aStartOffset,
00078                            PRInt32 aEndOffset, nsAString& aStr)  { return NS_OK; }
00079   NS_IMETHOD AppendDoctype(nsIDOMDocumentType *aDoctype,
00080                            nsAString& aStr)  { return NS_OK; }
00081   NS_IMETHOD AppendElementStart(nsIDOMElement *aElement,
00082                                 PRBool aHasChildren,
00083                                 nsAString& aStr); 
00084   NS_IMETHOD AppendElementEnd(nsIDOMElement *aElement,
00085                               nsAString& aStr);
00086   NS_IMETHOD Flush(nsAString& aStr);
00087 
00088   NS_IMETHOD AppendDocumentStart(nsIDOMDocument *aDocument,
00089                                  nsAString& aStr);
00090 
00091   // nsIContentSink
00092   NS_IMETHOD WillBuildModel(void) { return NS_OK; }
00093   NS_IMETHOD DidBuildModel(void) { return NS_OK; }
00094   NS_IMETHOD WillInterrupt(void) { return NS_OK; }
00095   NS_IMETHOD WillResume(void) { return NS_OK; }
00096   NS_IMETHOD SetParser(nsIParser* aParser) { return NS_OK; }
00097   NS_IMETHOD OpenContainer(const nsIParserNode& aNode);
00098   NS_IMETHOD CloseContainer(const nsHTMLTag aTag);
00099   NS_IMETHOD AddHeadContent(const nsIParserNode& aNode);
00100   NS_IMETHOD AddLeaf(const nsIParserNode& aNode);
00101   NS_IMETHOD AddComment(const nsIParserNode& aNode) { return NS_OK; }
00102   NS_IMETHOD AddProcessingInstruction(const nsIParserNode& aNode) { return NS_OK; }
00103   NS_IMETHOD AddDocTypeDecl(const nsIParserNode& aNode) { return NS_OK; }
00104   virtual void FlushPendingNotifications(mozFlushType aType) { }
00105   NS_IMETHOD SetDocumentCharset(nsACString& aCharset) { return NS_OK; }
00106   virtual nsISupports *GetTarget() { return nsnull; }
00107 
00108   // nsIHTMLContentSink
00109   NS_IMETHOD SetTitle(const nsString& aValue) { return NS_OK; }
00110   NS_IMETHOD OpenHTML(const nsIParserNode& aNode);
00111   NS_IMETHOD CloseHTML();
00112   NS_IMETHOD OpenHead(const nsIParserNode& aNode);
00113   NS_IMETHOD CloseHead();
00114   NS_IMETHOD OpenBody(const nsIParserNode& aNode);
00115   NS_IMETHOD CloseBody();
00116   NS_IMETHOD OpenForm(const nsIParserNode& aNode);
00117   NS_IMETHOD CloseForm();
00118   NS_IMETHOD OpenMap(const nsIParserNode& aNode);
00119   NS_IMETHOD CloseMap();
00120   NS_IMETHOD OpenFrameset(const nsIParserNode& aNode);
00121   NS_IMETHOD CloseFrameset();
00122   NS_IMETHOD IsEnabled(PRInt32 aTag, PRBool* aReturn);
00123   NS_IMETHOD NotifyTagObservers(nsIParserNode* aNode) { return NS_OK; }
00124   NS_IMETHOD_(PRBool) IsFormOnStack() { return PR_FALSE; }
00125 
00126   NS_IMETHOD BeginContext(PRInt32 aPosition) { return NS_OK; }
00127   NS_IMETHOD EndContext(PRInt32 aPosition) { return NS_OK; }
00128   NS_IMETHOD WillProcessTokens(void) { return NS_OK; }
00129   NS_IMETHOD DidProcessTokens(void) { return NS_OK; }
00130   NS_IMETHOD WillProcessAToken(void) { return NS_OK; }
00131   NS_IMETHOD DidProcessAToken(void) { return NS_OK; }
00132 
00133   // nsIHTMLToTextSink
00134   NS_IMETHOD Initialize(nsAString* aOutString,
00135                         PRUint32 aFlags, PRUint32 aWrapCol);
00136 
00137 protected:
00138   nsresult GetAttributeValue(const nsIParserNode* node, nsIAtom* aName, nsString& aValueRet);
00139   void AddToLine(const PRUnichar* aStringToAdd, PRInt32 aLength);
00140   void EndLine(PRBool softlinebreak);
00141   void EnsureVerticalSpace(PRInt32 noOfRows);
00142   void FlushLine();
00143   void OutputQuotesAndIndent(PRBool stripTrailingSpaces=PR_FALSE);
00144   void Output(nsString& aString);
00145   void Write(const nsAString& aString);
00146   PRBool IsBlockLevel(PRInt32 aId);
00147   PRBool IsContainer(PRInt32 aId);
00148   PRBool IsInPre();
00149   PRBool IsInOL();
00150   PRBool IsCurrentNodeConverted(const nsIParserNode* aNode);
00151   static PRInt32 GetIdForContent(nsIContent* aContent);
00152   nsresult DoOpenContainer(const nsIParserNode* aNode, PRInt32 aTag);
00153   nsresult DoCloseContainer(PRInt32 aTag);
00154   nsresult DoAddLeaf(const nsIParserNode* aNode,
00155                      PRInt32 aTag,
00156                      const nsAString& aText);
00157 
00158   // Inlined functions
00159   inline PRBool MayWrap()
00160   {
00161     return mWrapColumn &&
00162       ((mFlags & nsIDocumentEncoder::OutputFormatted) ||
00163        (mFlags & nsIDocumentEncoder::OutputWrap));
00164   }
00165 
00166   inline PRBool DoOutput()
00167   {
00168     return !mInHead;
00169   }
00170 
00171   // Stack handling functions
00172   PRBool GetLastBool(const nsVoidArray& aStack);
00173   void SetLastBool(nsVoidArray& aStack, PRBool aValue);
00174   void PushBool(nsVoidArray& aStack, PRBool aValue);
00175   PRBool PopBool(nsVoidArray& aStack);
00176   
00177 protected:
00178   nsString         mCurrentLine;
00179 
00180   PRPackedBool     mInHead;
00181   PRPackedBool     mAtFirstColumn;
00182 
00183   // Handling of quoted text (for mail):
00184   // Quotes need to be wrapped differently from non-quoted text,
00185   // because quoted text has a few extra characters (e.g. ">> ")
00186   // which makes the line length longer.
00187   // Mail can represent quotes in different ways: it can wrap
00188   // quotes in a <pre> (if editor.quotesPreformatted is set),
00189   // or not wrapped in any special tag (if mail.compose.wrap_to_window_width)
00190   // or in a <span> (if neither of the above are set).
00191   PRPackedBool     mQuotesPreformatted; // expect quotes wrapped in <pre>
00192   PRPackedBool     mDontWrapAnyQuotes;  // no special quote markers
00193 
00194   PRPackedBool     mStructs;            // Output structs (pref)
00195 
00196   // If we've just written out a cite blockquote, we need to remember it
00197   // so we don't duplicate spaces before a <pre wrap> (which mail uses to quote
00198   // old messages).
00199   PRPackedBool     mHasWrittenCiteBlockquote;
00200 
00201   PRInt32          mIndent;
00202   // mInIndentString keeps a header that has to be written in the indent.
00203   // That could be, for instance, the bullet in a bulleted list.
00204   nsString         mInIndentString;
00205   PRInt32          mCiteQuoteLevel;
00206   PRInt32          mFlags;
00207   PRInt32          mFloatingLines; // To store the number of lazy line breaks
00208 
00209   // The wrap column is how many standard sized chars (western languages)
00210   // should be allowed on a line. There could be less chars if the chars
00211   // are wider than latin chars of more if the chars are more narrow.
00212   PRUint32         mWrapColumn;
00213 
00214   // The width of the line as it will appear on the screen (approx.) 
00215   PRUint32         mCurrentLineWidth; 
00216 
00217   // Treat quoted text as though it's preformatted -- don't wrap it.
00218   // Having it on a pref is a temporary measure, See bug 69638.
00219   PRInt32          mSpanLevel;
00220 
00221 
00222   PRInt32          mEmptyLines; // Will be the number of empty lines before
00223                                 // the current. 0 if we are starting a new
00224                                 // line and -1 if we are in a line.
00225 
00226   PRPackedBool     mInWhitespace;
00227   PRPackedBool     mPreFormatted;
00228   PRPackedBool     mStartedOutput; // we've produced at least a character
00229 
00230   // While handling a new tag, this variable should remind if any line break
00231   // is due because of a closing tag. Setting it to "TRUE" while closing the tags.
00232   // Hence opening tags are guaranteed to start with appropriate line breaks.
00233   PRPackedBool     mLineBreakDue; 
00234 
00235   nsString         mURL;
00236   PRInt32          mHeaderStrategy;    /* Header strategy (pref)
00237                                           0 = no indention
00238                                           1 = indention, increased with
00239                                               header level (default)
00240                                           2 = numbering and slight indention */
00241   PRInt32          mHeaderCounter[7];  /* For header-numbering:
00242                                           Number of previous headers of
00243                                           the same depth and in the same
00244                                           section.
00245                                           mHeaderCounter[1] for <h1> etc. */
00246 
00247   nsCOMPtr<nsIContent> mContent;
00248 
00249   // For handling table rows
00250   nsAutoVoidArray mHasWrittenCellsForRow; // really an array of bools
00251   
00252   // Values gotten in OpenContainer that is (also) needed in CloseContainer
00253   nsAutoVoidArray     mCurrentNodeIsConverted; // really an array of bools
00254   nsAutoVoidArray     mIsInCiteBlockquote; // really an array of bools
00255 
00256   // The output data
00257   nsAString*            mOutputString;
00258 
00259   // The tag stack: the stack of tags we're operating on, so we can nest:
00260   nsHTMLTag       *mTagStack;
00261   PRUint32         mTagStackIndex;
00262 
00263   // Content in the stack above this index should be ignored:
00264   PRUint32          mIgnoreAboveIndex;
00265 
00266   // The stack for ordered lists:
00267   PRInt32         *mOLStack;
00268   PRUint32         mOLStackIndex;
00269 
00270   PRUint32         mULCount;
00271 
00272   nsString                     mLineBreak;
00273   nsCOMPtr<nsILineBreaker>     mLineBreaker;
00274 
00275   // Conveniance constant. It would be nice to have it as a const static
00276   // variable, but that causes issues with OpenBSD and module unloading.
00277   const nsString          kSpace;
00278 };
00279 
00280 nsresult
00281 NS_NewPlainTextSerializer(nsIContentSerializer** aSerializer);
00282 
00283 #endif