Back to index

lightning-sunbird  0.9+nobinonly
nsHTMLContentSerializer.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 nsHTMLContentSerializer_h__
00039 #define nsHTMLContentSerializer_h__
00040 
00041 #include "nsXMLContentSerializer.h"
00042 #include "nsIEntityConverter.h"
00043 #include "nsString.h"
00044 #include "nsILineBreaker.h"
00045 
00046 class nsIContent;
00047 class nsIAtom;
00048 
00049 class nsHTMLContentSerializer : public nsXMLContentSerializer {
00050  public:
00051   nsHTMLContentSerializer();
00052   virtual ~nsHTMLContentSerializer();
00053 
00054   NS_IMETHOD Init(PRUint32 flags, PRUint32 aWrapColumn,
00055                   const char* aCharSet, PRBool aIsCopying);
00056 
00057   NS_IMETHOD AppendText(nsIDOMText* aText, 
00058                         PRInt32 aStartOffset,
00059                         PRInt32 aEndOffset,
00060                         nsAString& aStr);
00061   NS_IMETHOD AppendElementStart(nsIDOMElement *aElement,
00062                                 PRBool aHasChildren,
00063                                 nsAString& aStr);
00064   
00065   NS_IMETHOD AppendElementEnd(nsIDOMElement *aElement,
00066                               nsAString& aStr);
00067 
00068   NS_IMETHOD AppendDocumentStart(nsIDOMDocument *aDocument,
00069                                  nsAString& aStr);
00070  protected:
00071   PRBool LineBreakBeforeOpen(nsIAtom* aName, PRBool aHasDirtyAttr);
00072   PRBool LineBreakAfterOpen(nsIAtom* aName, PRBool aHasDirtyAttr);
00073   PRBool LineBreakBeforeClose(nsIAtom* aName, PRBool aHasDirtyAttr);
00074   PRBool LineBreakAfterClose(nsIAtom* aName, PRBool aHasDirtyAttr);
00075   PRBool IsFirstChildOfOL(nsIDOMElement* aElement);
00076   void StartIndentation(nsIAtom* aName, 
00077                         PRBool aHasDirtyAttr,
00078                         nsAString& aStr);
00079   void EndIndentation(nsIAtom* aName, 
00080                       PRBool aHasDirtyAttr,
00081                       nsAString& aStr);
00082   nsresult GetEntityConverter(nsIEntityConverter** aConverter);
00083   void SerializeAttributes(nsIContent* aContent,
00084                            nsIAtom* aTagName,
00085                            nsAString& aStr);
00086   void SerializeLIValueAttribute(nsIDOMElement* aElement,
00087                                  nsAString& aStr);
00088   virtual void AppendToString(const PRUnichar* aStr,
00089                               PRInt32 aLength,
00090                               nsAString& aOutputStr);
00091   virtual void AppendToString(const PRUnichar aChar,
00092                               nsAString& aOutputStr);
00093   virtual void AppendToString(const nsAString& aStr,
00094                               nsAString& aOutputStr,
00095                               PRBool aTranslateEntities = PR_FALSE,
00096                               PRBool aIncrColumn = PR_TRUE);
00097   virtual void AppendToStringConvertLF(const nsAString& aStr,
00098                                        nsAString& aOutputStr);
00099   void AppendWrapped_WhitespaceSequence(
00100           nsASingleFragmentString::const_char_iterator &aPos,
00101           const nsASingleFragmentString::const_char_iterator aEnd,
00102           const nsASingleFragmentString::const_char_iterator aSequenceStart,
00103           PRBool &aMayIgnoreStartOfLineWhitespaceSequence,
00104           nsAString &aOutputStr);
00105   void AppendWrapped_NonWhitespaceSequence(
00106           nsASingleFragmentString::const_char_iterator &aPos,
00107           const nsASingleFragmentString::const_char_iterator aEnd,
00108           const nsASingleFragmentString::const_char_iterator aSequenceStart,
00109           PRBool &aMayIgnoreStartOfLineWhitespaceSequence,
00110           nsAString &aOutputStr);
00111   virtual void AppendToStringWrapped(const nsASingleFragmentString& aStr,
00112                                      nsAString& aOutputStr,
00113                                      PRBool aTranslateEntities);
00114   PRBool HasLongLines(const nsString& text, PRInt32& aLastNewlineOffset);
00115   nsresult EscapeURI(const nsAString& aURI, nsAString& aEscapedURI);
00116   PRBool IsJavaScript(nsIAtom* aAttrNameAtom, const nsAString& aAttrValueString);
00117 
00118   nsCOMPtr<nsIEntityConverter> mEntityConverter;
00119 
00120   PRInt32   mIndent;
00121   PRInt32   mColPos;
00122   PRUint32  mFlags;
00123   PRPackedBool  mInBody;
00124 
00125   PRPackedBool  mDoFormat;
00126   PRPackedBool  mDoHeader;
00127   PRPackedBool  mBodyOnly;
00128   PRPackedBool  mIsCopying; // Set to PR_TRUE only while copying
00129 
00130   // Indicates that a space will be added if and only if content is
00131   // continued on the same line while serializing source.  Otherwise,
00132   // the newline character acts as the whitespace and no space is needed.
00133   PRPackedBool  mAddSpace;
00134   PRPackedBool  mMayIgnoreLineBreakSequence;
00135 
00136   // To keep track of First LI child of OL in selected range 
00137   PRPackedBool  mIsFirstChildOfOL;
00138   PRInt32       mPreLevel;
00139 
00140   /*
00141    * mInCDATA is set to PR_TRUE while the serializer is serializing
00142    * the content of a element whose content is considerd CDATA by the
00143    * serializer (such elements are 'script', 'style', 'noscript' and
00144    * possibly others) This doesn't have anything to do with if the
00145    * element is defined as CDATA in the DTD, it simply means we'll
00146    * output the content of the element without doing any entity encoding
00147    * what so ever.
00148    */
00149   PRPackedBool mInCDATA;
00150   PRPackedBool mNeedLineBreaker;
00151 
00152   nsCOMPtr<nsILineBreaker> mLineBreaker;
00153 
00154   PRInt32   mMaxColumn;
00155   nsString  mLineBreak;
00156 
00157   nsCString mCharSet;
00158 
00159  // To keep track of startvalue of OL and first list item for nested lists
00160   struct olState {
00161     olState(PRInt32 aStart, PRBool aIsFirst):startVal(aStart),isFirstListItem(aIsFirst)
00162     {
00163     }
00164     PRInt32 startVal;
00165     PRBool isFirstListItem;
00166   };
00167 
00168   nsAutoVoidArray   mOLStateStack;// Stack to store one olState struct per <OL>.
00169 };
00170 
00171 nsresult
00172 NS_NewHTMLContentSerializer(nsIContentSerializer** aSerializer);
00173 
00174 #endif