1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org HTML Sanitizer code.
17
* The Initial Developer of the Original Code is
18
* Ben Bucksch <mozilla@bucksch.org>.
19
* Portions created by the Initial Developer are Copyright (C) 2002
20
* the Initial Developer. All Rights Reserved.
26
* Alternatively, the contents of this file may be used under the terms of
27
* either the GNU General Public License Version 2 or later (the "GPL"), or
28
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29
* in which case the provisions of the GPL or the LGPL are applicable instead
30
* of those above. If you wish to allow use of your version of this file only
31
* under the terms of either the GPL or the LGPL, and not to allow others to
32
* use your version of this file under the terms of the NPL, indicate your
33
* decision by deleting the provisions above and replace them with the notice
34
* and other provisions required by the GPL or the LGPL. If you do not delete
35
* the provisions above, a recipient may use your version of this file under
36
* the terms of any one of the NPL, the GPL or the LGPL.
38
* ***** END LICENSE BLOCK ***** */
40
#ifndef mozSanitizingSerializer_h__
41
#define mozSanitizingSerializer_h__
43
#include "mozISanitizingSerializer.h"
44
#include "nsIContentSerializer.h"
45
#include "nsIHTMLContentSink.h"
46
#include "nsHTMLTags.h"
48
#include "nsIParserService.h"
49
#include "nsIContent.h"
51
#include "nsIDocumentEncoder.h"
55
class mozSanitizingHTMLSerializer : public nsIContentSerializer,
56
public nsIHTMLContentSink,
57
public mozISanitizingHTMLSerializer
60
mozSanitizingHTMLSerializer();
61
virtual ~mozSanitizingHTMLSerializer();
62
static PRBool PR_CALLBACK ReleaseProperties(nsHashKey* key, void* data,
67
// nsIContentSerializer
68
NS_IMETHOD Init(PRUint32 flags, PRUint32 dummy, const char* aCharSet,
71
NS_IMETHOD AppendText(nsIDOMText* aText, PRInt32 aStartOffset,
72
PRInt32 aEndOffset, nsAString& aStr);
73
NS_IMETHOD AppendCDATASection(nsIDOMCDATASection* aCDATASection,
74
PRInt32 aStartOffset, PRInt32 aEndOffset,
77
NS_IMETHOD AppendProcessingInstruction(nsIDOMProcessingInstruction* aPI,
82
NS_IMETHOD AppendComment(nsIDOMComment* aComment, PRInt32 aStartOffset,
83
PRInt32 aEndOffset, nsAString& aStr)
85
NS_IMETHOD AppendDoctype(nsIDOMDocumentType *aDoctype, nsAString& aStr)
87
NS_IMETHOD AppendElementStart(nsIDOMElement *aElement, PRBool aHasChildren,
89
NS_IMETHOD AppendElementEnd(nsIDOMElement *aElement, nsAString& aStr);
90
NS_IMETHOD Flush(nsAString& aStr);
92
NS_IMETHOD AppendDocumentStart(nsIDOMDocument *aDocument,
95
NS_IMETHOD PreserveSelection(nsIDOMNode * aStartContainer,
97
nsIDOMNode * aEndContainer,
101
NS_IMETHOD WillBuildModel(void) { return NS_OK; }
102
NS_IMETHOD DidBuildModel(void) { return NS_OK; }
103
NS_IMETHOD WillInterrupt(void) { return NS_OK; }
104
NS_IMETHOD WillResume(void) { return NS_OK; }
105
NS_IMETHOD SetParser(nsIParser* aParser) { return NS_OK; }
106
NS_IMETHOD OpenContainer(const nsIParserNode& aNode);
107
NS_IMETHOD CloseContainer(const nsHTMLTag aTag);
108
NS_IMETHOD AddHeadContent(const nsIParserNode& aNode);
109
NS_IMETHOD AddLeaf(const nsIParserNode& aNode);
110
NS_IMETHOD AddComment(const nsIParserNode& aNode) { return NS_OK; }
111
NS_IMETHOD AddProcessingInstruction(const nsIParserNode& aNode)
113
NS_IMETHOD AddDocTypeDecl(const nsIParserNode& aNode);
114
NS_IMETHOD FlushPendingNotifications() { return NS_OK; }
115
NS_IMETHOD SetDocumentCharset(nsACString& aCharset);
116
virtual nsISupports *GetTarget() { return nsnull; }
118
// nsIHTMLContentSink
119
NS_IMETHOD OpenHTML(const nsIParserNode& aNode);
120
NS_IMETHOD CloseHTML();
121
NS_IMETHOD OpenHead(const nsIParserNode& aNode);
122
NS_IMETHOD CloseHead();
123
NS_IMETHOD SetTitle(const nsString& aValue);
124
NS_IMETHOD OpenBody(const nsIParserNode& aNode);
125
NS_IMETHOD CloseBody();
126
NS_IMETHOD OpenForm(const nsIParserNode& aNode);
127
NS_IMETHOD CloseForm();
128
NS_IMETHOD OpenMap(const nsIParserNode& aNode);
129
NS_IMETHOD CloseMap();
130
NS_IMETHOD OpenFrameset(const nsIParserNode& aNode);
131
NS_IMETHOD CloseFrameset();
132
NS_IMETHOD IsEnabled(PRInt32 aTag, PRBool* aReturn);
133
NS_IMETHOD NotifyTagObservers(nsIParserNode* aNode) { return NS_OK; }
134
NS_IMETHOD_(PRBool) IsFormOnStack() { return PR_FALSE; }
135
NS_IMETHOD BeginContext(PRInt32 aPosition) { return NS_OK; }
136
NS_IMETHOD EndContext(PRInt32 aPosition) { return NS_OK; }
137
NS_IMETHOD WillProcessTokens(void) { return NS_OK; }
138
NS_IMETHOD DidProcessTokens(void) { return NS_OK; }
139
NS_IMETHOD WillProcessAToken(void) { return NS_OK; }
140
NS_IMETHOD DidProcessAToken(void) { return NS_OK; }
142
// nsISanitizingHTMLSerializer
143
NS_IMETHOD Initialize(nsAString* aOutString,
144
PRUint32 aFlags, const nsAString& allowedTags);
147
nsresult ParsePrefs(const nsAString& aPref);
148
nsresult ParseTagPref(const nsCAutoString& tagpref);
149
PRBool IsAllowedTag(nsHTMLTag aTag);
150
PRBool IsAllowedAttribute(nsHTMLTag aTag, const nsAString& anAttributeName);
151
nsresult SanitizeAttrValue(nsHTMLTag aTag, const nsAString& attr_name,
152
nsString& value /*inout*/);
153
nsresult SanitizeTextNode(nsString& value /*inout*/);
154
PRBool IsContainer(PRInt32 aId);
155
static PRInt32 GetIdForContent(nsIContent* aContent);
156
nsresult GetParserService(nsIParserService** aParserService);
157
nsresult DoOpenContainer(PRInt32 aTag);
158
nsresult DoCloseContainer(PRInt32 aTag);
159
nsresult DoAddLeaf(PRInt32 aTag, const nsAString& aText);
160
void Write(const nsAString& aString);
164
nsHashtable mAllowedTags;
166
nsCOMPtr<nsIContent> mContent;
167
nsAString* mOutputString;
168
nsIParserNode* mParserNode;
169
nsCOMPtr<nsIParserService> mParserService;
173
NS_NewSanitizingHTMLSerializer(nsIContentSerializer** aSerializer);