Back to index

lightning-sunbird  0.9+nobinonly
mozSanitizingSerializer.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 HTML Sanitizer code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Ben Bucksch <mozilla@bucksch.org>.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Netscape
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 #ifndef mozSanitizingSerializer_h__
00040 #define mozSanitizingSerializer_h__
00041 
00042 #include "mozISanitizingSerializer.h"
00043 #include "nsIContentSerializer.h"
00044 #include "nsIHTMLContentSink.h"
00045 #include "nsHTMLTags.h"
00046 #include "nsCOMPtr.h"
00047 #include "nsIParserService.h"
00048 #include "nsIContent.h"
00049 #include "nsIAtom.h"
00050 #include "nsIDocumentEncoder.h"
00051 #include "nsString.h"
00052 #include "nsIParser.h"
00053 
00054 class mozSanitizingHTMLSerializer : public nsIContentSerializer,
00055                                     public nsIHTMLContentSink,
00056                                     public mozISanitizingHTMLSerializer
00057 {
00058 public:
00059   mozSanitizingHTMLSerializer();
00060   virtual ~mozSanitizingHTMLSerializer();
00061   static PRBool PR_CALLBACK ReleaseProperties(nsHashKey* key, void* data,
00062                                               void* closure);
00063 
00064   NS_DECL_ISUPPORTS
00065 
00066   // nsIContentSerializer
00067   NS_IMETHOD Init(PRUint32 flags, PRUint32 dummy, const char* aCharSet, 
00068                   PRBool aIsCopying);
00069 
00070   NS_IMETHOD AppendText(nsIDOMText* aText, PRInt32 aStartOffset,
00071                         PRInt32 aEndOffset, nsAString& aStr);
00072   NS_IMETHOD AppendCDATASection(nsIDOMCDATASection* aCDATASection,
00073                                 PRInt32 aStartOffset, PRInt32 aEndOffset,
00074                                 nsAString& aStr)
00075                       { return NS_OK; }
00076   NS_IMETHOD AppendProcessingInstruction(nsIDOMProcessingInstruction* aPI,
00077                                          PRInt32 aStartOffset,
00078                                          PRInt32 aEndOffset,
00079                                          nsAString& aStr)
00080                       { return NS_OK; }
00081   NS_IMETHOD AppendComment(nsIDOMComment* aComment, PRInt32 aStartOffset,
00082                            PRInt32 aEndOffset, nsAString& aStr)
00083                       { return NS_OK; }
00084   NS_IMETHOD AppendDoctype(nsIDOMDocumentType *aDoctype, nsAString& aStr)
00085                       { return NS_OK; }
00086   NS_IMETHOD AppendElementStart(nsIDOMElement *aElement, PRBool aHasChildren,
00087                                 nsAString& aStr); 
00088   NS_IMETHOD AppendElementEnd(nsIDOMElement *aElement, nsAString& aStr);
00089   NS_IMETHOD Flush(nsAString& aStr);
00090 
00091   NS_IMETHOD AppendDocumentStart(nsIDOMDocument *aDocument,
00092                                  nsAString& aStr);
00093 
00094   // nsIContentSink
00095   NS_IMETHOD WillBuildModel(void) { return NS_OK; }
00096   NS_IMETHOD DidBuildModel(void)
00097   { nsCOMPtr<nsIParser> temp(mParser); mParser = nsnull; return NS_OK; }
00098   NS_IMETHOD WillInterrupt(void) { return NS_OK; }
00099   NS_IMETHOD WillResume(void) { return NS_OK; }
00100   NS_IMETHOD SetParser(nsIParser* aParser) { mParser = aParser; return NS_OK; }
00101   NS_IMETHOD OpenContainer(const nsIParserNode& aNode);
00102   NS_IMETHOD CloseContainer(const nsHTMLTag aTag);
00103   NS_IMETHOD AddHeadContent(const nsIParserNode& aNode);
00104   NS_IMETHOD AddLeaf(const nsIParserNode& aNode);
00105   NS_IMETHOD AddComment(const nsIParserNode& aNode) { return NS_OK; }
00106   NS_IMETHOD AddProcessingInstruction(const nsIParserNode& aNode)
00107                                                     { return NS_OK; }
00108   NS_IMETHOD AddDocTypeDecl(const nsIParserNode& aNode);
00109   virtual void FlushPendingNotifications(mozFlushType aType) { }
00110   NS_IMETHOD SetDocumentCharset(nsACString& aCharset);
00111   virtual nsISupports *GetTarget() { return nsnull; }
00112 
00113   // nsIHTMLContentSink
00114   NS_IMETHOD OpenHTML(const nsIParserNode& aNode);
00115   NS_IMETHOD CloseHTML();
00116   NS_IMETHOD OpenHead(const nsIParserNode& aNode);
00117   NS_IMETHOD CloseHead();
00118   NS_IMETHOD SetTitle(const nsString& aValue);
00119   NS_IMETHOD OpenBody(const nsIParserNode& aNode);
00120   NS_IMETHOD CloseBody();
00121   NS_IMETHOD OpenForm(const nsIParserNode& aNode);
00122   NS_IMETHOD CloseForm();
00123   NS_IMETHOD OpenMap(const nsIParserNode& aNode);
00124   NS_IMETHOD CloseMap();
00125   NS_IMETHOD OpenFrameset(const nsIParserNode& aNode);
00126   NS_IMETHOD CloseFrameset();
00127   NS_IMETHOD IsEnabled(PRInt32 aTag, PRBool* aReturn);
00128   NS_IMETHOD NotifyTagObservers(nsIParserNode* aNode) { return NS_OK; }
00129   NS_IMETHOD_(PRBool) IsFormOnStack() { return PR_FALSE; }
00130   NS_IMETHOD BeginContext(PRInt32 aPosition) { return NS_OK; }
00131   NS_IMETHOD EndContext(PRInt32 aPosition) { return NS_OK; }
00132   NS_IMETHOD WillProcessTokens(void) { return NS_OK; }
00133   NS_IMETHOD DidProcessTokens(void) { return NS_OK; }
00134   NS_IMETHOD WillProcessAToken(void) { return NS_OK; }
00135   NS_IMETHOD DidProcessAToken(void) { return NS_OK; }
00136 
00137   // nsISanitizingHTMLSerializer
00138   NS_IMETHOD Initialize(nsAString* aOutString,
00139                         PRUint32 aFlags, const nsAString& allowedTags);
00140 
00141 protected:
00142   nsresult ParsePrefs(const nsAString& aPref);
00143   nsresult ParseTagPref(const nsCAutoString& tagpref);
00144   PRBool IsAllowedTag(nsHTMLTag aTag);
00145   PRBool IsAllowedAttribute(nsHTMLTag aTag, const nsAString& anAttributeName);
00146   nsresult SanitizeAttrValue(nsHTMLTag aTag, const nsAString& attr_name,
00147                              nsString& value /*inout*/);
00148   nsresult SanitizeTextNode(nsString& value /*inout*/);
00149   PRBool IsContainer(PRInt32 aId);
00150   static PRInt32 GetIdForContent(nsIContent* aContent);
00151   nsresult GetParserService(nsIParserService** aParserService);
00152   nsresult DoOpenContainer(PRInt32 aTag);
00153   nsresult DoCloseContainer(PRInt32 aTag);
00154   nsresult DoAddLeaf(PRInt32 aTag, const nsAString& aText);
00155   void Write(const nsAString& aString);
00156 
00157 protected:
00158   PRInt32                      mFlags;
00159   nsHashtable                  mAllowedTags;
00160 
00161   nsCOMPtr<nsIContent>         mContent;
00162   nsAString*                   mOutputString;
00163   nsIParserNode*               mParserNode;
00164   nsCOMPtr<nsIParserService>   mParserService;
00165   nsCOMPtr<nsIParser>          mParser;
00166 };
00167 
00168 nsresult
00169 NS_NewSanitizingHTMLSerializer(nsIContentSerializer** aSerializer);
00170 
00171 #endif