1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 sw=2 et tw=78: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
45 * This class defines the iparser interface. This XPCOM
46 * inteface is all that parser clients ever need to see.
50 #include "nsISupports.h"
51 #include "nsIStreamListener.h"
53 #include "nsStringGlue.h"
56 // {506527cc-d832-420b-ba3a-80c05aa105f4}
57 #define NS_IPARSER_IID \
58 { 0x506527cc, 0xd832, 0x420b, \
59 { 0xba, 0x3a, 0x80, 0xc0, 0x5a, 0xa1, 0x05, 0xf4 } }
62 // {41421C60-310A-11d4-816F-000064657374}
63 #define NS_IDEBUG_DUMP_CONTENT_IID \
64 { 0x41421c60, 0x310a, 0x11d4, \
65 { 0x81, 0x6f, 0x0, 0x0, 0x64, 0x65, 0x73, 0x74 } }
68 class nsIRequestObserver
;
69 class nsIParserFilter
;
74 enum eParserCommands
{
85 eHTML3_Quirks
, // separate, for editor output, since HTML pre-4.0 lacks tbody
90 // define Charset source constants
91 // note: the value order defines the priority; higher numbers take priority
92 #define kCharsetUninitialized 0
93 #define kCharsetFromWeakDocTypeDefault 1
94 #define kCharsetFromUserDefault 2
95 #define kCharsetFromDocTypeDefault 3
96 #define kCharsetFromCache 4
97 #define kCharsetFromParentFrame 5
98 #define kCharsetFromBookmarks 6
99 #define kCharsetFromAutoDetection 7
100 #define kCharsetFromHintPrevDoc 8
101 #define kCharsetFromMetaTag 9
102 #define kCharsetFromByteOrderMark 10
103 #define kCharsetFromChannel 11
104 #define kCharsetFromOtherComponent 12
105 // Levels below here will be forced onto childframes too
106 #define kCharsetFromParentForced 13
107 #define kCharsetFromUserForced 14
108 #define kCharsetFromPreviousLoading 15
110 enum eStreamState
{eNone
,eOnStart
,eOnDataAvail
,eOnStop
};
113 * FOR DEBUG PURPOSE ONLY
115 * Use this interface to query objects that contain content information.
116 * Ex. Parser can trigger dump content by querying the sink that has
117 * access to the content.
119 * @update harishd 05/25/00
121 class nsIDebugDumpContent
: public nsISupports
{
123 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IDEBUG_DUMP_CONTENT_IID
)
124 NS_IMETHOD
DumpContentModel()=0;
127 NS_DEFINE_STATIC_IID_ACCESSOR(nsIDebugDumpContent
, NS_IDEBUG_DUMP_CONTENT_IID
)
130 * This class defines the iparser interface. This XPCOM
131 * inteface is all that parser clients ever need to see.
133 class nsIParser
: public nsISupports
{
136 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IPARSER_IID
)
139 * Select given content sink into parser for parser output
140 * @update gess5/11/98
141 * @param aSink is the new sink to be used by parser
144 NS_IMETHOD_(void) SetContentSink(nsIContentSink
* aSink
)=0;
148 * retrieve the sink set into the parser
149 * @update gess5/11/98
150 * @return current sink
152 NS_IMETHOD_(nsIContentSink
*) GetContentSink(void)=0;
155 * Call this method once you've created a parser, and want to instruct it
156 * about the command which caused the parser to be constructed. For example,
157 * this allows us to select a DTD which can do, say, view-source.
159 * @update gess 3/25/98
160 * @param aCommand -- ptrs to string that contains command
163 NS_IMETHOD_(void) GetCommand(nsCString
& aCommand
)=0;
164 NS_IMETHOD_(void) SetCommand(const char* aCommand
)=0;
165 NS_IMETHOD_(void) SetCommand(eParserCommands aParserCommand
)=0;
168 * Call this method once you've created a parser, and want to instruct it
169 * about what charset to load
171 * @update ftang 4/23/99
172 * @param aCharset- the charest of a document
173 * @param aCharsetSource- the soure of the chares
176 NS_IMETHOD_(void) SetDocumentCharset(const nsACString
& aCharset
, PRInt32 aSource
)=0;
177 NS_IMETHOD_(void) GetDocumentCharset(nsACString
& oCharset
, PRInt32
& oSource
)=0;
179 NS_IMETHOD_(void) SetParserFilter(nsIParserFilter
* aFilter
) = 0;
182 * Get the channel associated with this parser
183 * @update harishd,gagan 07/17/01
184 * @param aChannel out param that will contain the result
185 * @return NS_OK if successful
187 NS_IMETHOD
GetChannel(nsIChannel
** aChannel
) = 0;
190 * Get the DTD associated with this parser
191 * @update vidur 9/29/99
192 * @param aDTD out param that will contain the result
193 * @return NS_OK if successful, NS_ERROR_FAILURE for runtime error
195 NS_IMETHOD
GetDTD(nsIDTD
** aDTD
) = 0;
197 /**************************************************************************
198 * Parse methods always begin with an input source, and perform
199 * conversions until you wind up being emitted to the given contentsink
200 * (which may or may not be a proxy for the NGLayout content model).
201 ************************************************************************/
203 // Call this method to resume the parser from the blocked state.
204 NS_IMETHOD
ContinueParsing() = 0;
206 // Call this method to resume the parser from an unblocked state.
207 // This can happen, for example, if parsing was interrupted and then the
208 // consumer needed to restart the parser without waiting for more data.
209 // This also happens after loading scripts, which unblock the parser in
210 // order to process the output of document.write() and then need to
211 // continue on with the page load on an enabled parser.
212 NS_IMETHOD
ContinueInterruptedParsing() = 0;
214 // Stops parsing temporarily.
215 NS_IMETHOD_(void) BlockParser() = 0;
217 // Open up the parser for tokenization, building up content
218 // model..etc. However, this method does not resume parsing
219 // automatically. It's the callers' responsibility to restart
220 // the parsing engine.
221 NS_IMETHOD_(void) UnblockParser() = 0;
223 NS_IMETHOD_(PRBool
) IsParserEnabled() = 0;
224 NS_IMETHOD_(PRBool
) IsComplete() = 0;
226 NS_IMETHOD
Parse(nsIURI
* aURL
,
227 nsIRequestObserver
* aListener
= nsnull
,
229 nsDTDMode aMode
= eDTDMode_autodetect
) = 0;
230 NS_IMETHOD
Parse(const nsAString
& aSourceBuffer
,
232 const nsACString
& aMimeType
,
234 nsDTDMode aMode
= eDTDMode_autodetect
) = 0;
236 // Return a key, suitable for passing into one of the Parse methods above,
237 // that will cause this parser to use the root context.
238 NS_IMETHOD_(void *) GetRootContextKey() = 0;
240 NS_IMETHOD
Terminate(void) = 0;
243 * This method gets called when you want to parse a fragment of HTML or XML
244 * surrounded by the context |aTagStack|. It requires that the parser have
245 * been given a fragment content sink.
247 * @param aSourceBuffer The XML or HTML that hasn't been parsed yet.
248 * @param aKey The key used by the parser.
249 * @param aTagStack The context of the source buffer.
250 * @param aXMLMode Whether this is XML or HTML
251 * @param aContentType The content-type of this document.
252 * @param aMode The DTDMode that the parser should parse this fragment in.
253 * @return Success or failure.
255 NS_IMETHOD
ParseFragment(const nsAString
& aSourceBuffer
,
257 nsTArray
<nsString
>& aTagStack
,
259 const nsACString
& aContentType
,
260 nsDTDMode aMode
= eDTDMode_autodetect
) = 0;
263 * This method gets called when the tokens have been consumed, and it's time
264 * to build the model via the content sink.
265 * @update gess5/11/98
266 * @return error code -- 0 if model building went well .
268 NS_IMETHOD
BuildModel(void) = 0;
272 * Retrieve the parse mode from the parser...
274 * @update gess 6/9/98
275 * @return ptr to scanner
277 NS_IMETHOD_(nsDTDMode
) GetParseMode(void) = 0;
280 * Call this method to cancel any pending parsing events.
281 * Parsing events may be pending if all of the document's content
282 * has been passed to the parser but the parser has been interrupted
283 * because processing the tokens took too long.
285 * @update kmcclusk 05/18/01
286 * @return NS_OK if succeeded else ERROR.
289 NS_IMETHOD
CancelParsingEvents() = 0;
291 virtual void Reset() = 0;
294 NS_DEFINE_STATIC_IID_ACCESSOR(nsIParser
, NS_IPARSER_IID
)
296 /* ===========================================================*
297 Some useful constants...
298 * ===========================================================*/
303 #define NS_ERROR_HTMLPARSER_EOF NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1000)
304 #define NS_ERROR_HTMLPARSER_UNKNOWN NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1001)
305 #define NS_ERROR_HTMLPARSER_CANTPROPAGATE NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1002)
306 #define NS_ERROR_HTMLPARSER_CONTEXTMISMATCH NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1003)
307 #define NS_ERROR_HTMLPARSER_BADFILENAME NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1004)
308 #define NS_ERROR_HTMLPARSER_BADURL NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1005)
309 #define NS_ERROR_HTMLPARSER_INVALIDPARSERCONTEXT NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1006)
310 #define NS_ERROR_HTMLPARSER_INTERRUPTED NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1007)
311 #define NS_ERROR_HTMLPARSER_BLOCK NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1008)
312 #define NS_ERROR_HTMLPARSER_BADTOKENIZER NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1009)
313 #define NS_ERROR_HTMLPARSER_BADATTRIBUTE NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1010)
314 #define NS_ERROR_HTMLPARSER_UNRESOLVEDDTD NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1011)
315 #define NS_ERROR_HTMLPARSER_MISPLACEDTABLECONTENT NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1012)
316 #define NS_ERROR_HTMLPARSER_BADDTD NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1013)
317 #define NS_ERROR_HTMLPARSER_BADCONTEXT NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1014)
318 #define NS_ERROR_HTMLPARSER_STOPPARSING NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1015)
319 #define NS_ERROR_HTMLPARSER_UNTERMINATEDSTRINGLITERAL NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1016)
320 #define NS_ERROR_HTMLPARSER_HIERARCHYTOODEEP NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1017)
321 #define NS_ERROR_HTMLPARSER_FAKE_ENDTAG NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1018)
322 #define NS_ERROR_HTMLPARSER_INVALID_COMMENT NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1019)
324 #define NS_ERROR_HTMLPARSER_CONTINUE NS_OK
327 const PRUint32 kEOF
= NS_ERROR_HTMLPARSER_EOF
;
328 const PRUint32 kUnknownError
= NS_ERROR_HTMLPARSER_UNKNOWN
;
329 const PRUint32 kCantPropagate
= NS_ERROR_HTMLPARSER_CANTPROPAGATE
;
330 const PRUint32 kContextMismatch
= NS_ERROR_HTMLPARSER_CONTEXTMISMATCH
;
331 const PRUint32 kBadFilename
= NS_ERROR_HTMLPARSER_BADFILENAME
;
332 const PRUint32 kBadURL
= NS_ERROR_HTMLPARSER_BADURL
;
333 const PRUint32 kInvalidParserContext
= NS_ERROR_HTMLPARSER_INVALIDPARSERCONTEXT
;
334 const PRUint32 kBlocked
= NS_ERROR_HTMLPARSER_BLOCK
;
335 const PRUint32 kBadStringLiteral
= NS_ERROR_HTMLPARSER_UNTERMINATEDSTRINGLITERAL
;
336 const PRUint32 kHierarchyTooDeep
= NS_ERROR_HTMLPARSER_HIERARCHYTOODEEP
;
337 const PRUint32 kFakeEndTag
= NS_ERROR_HTMLPARSER_FAKE_ENDTAG
;
338 const PRUint32 kNotAComment
= NS_ERROR_HTMLPARSER_INVALID_COMMENT
;
340 const PRUnichar kNewLine
= '\n';
341 const PRUnichar kCR
= '\r';
342 const PRUnichar kLF
= '\n';
343 const PRUnichar kTab
= '\t';
344 const PRUnichar kSpace
= ' ';
345 const PRUnichar kQuote
= '"';
346 const PRUnichar kApostrophe
= '\'';
347 const PRUnichar kLessThan
= '<';
348 const PRUnichar kGreaterThan
= '>';
349 const PRUnichar kAmpersand
= '&';
350 const PRUnichar kForwardSlash
= '/';
351 const PRUnichar kBackSlash
= '\\';
352 const PRUnichar kEqual
= '=';
353 const PRUnichar kMinus
= '-';
354 const PRUnichar kPlus
= '+';
355 const PRUnichar kExclamation
= '!';
356 const PRUnichar kSemicolon
= ';';
357 const PRUnichar kHashsign
= '#';
358 const PRUnichar kAsterisk
= '*';
359 const PRUnichar kUnderbar
= '_';
360 const PRUnichar kComma
= ',';
361 const PRUnichar kLeftParen
= '(';
362 const PRUnichar kRightParen
= ')';
363 const PRUnichar kLeftBrace
= '{';
364 const PRUnichar kRightBrace
= '}';
365 const PRUnichar kQuestionMark
= '?';
366 const PRUnichar kLeftSquareBracket
= '[';
367 const PRUnichar kRightSquareBracket
= ']';
368 const PRUnichar kNullCh
= '\0';
370 // XXXbz these type defines should really just go away.... Until they
371 // do, changes here should be reflected in nsContentDLF.cpp
372 #define kHTMLTextContentType "text/html"
373 #define kXMLTextContentType "text/xml"
374 #define kXMLApplicationContentType "application/xml"
375 #define kXHTMLApplicationContentType "application/xhtml+xml"
376 #define kXULTextContentType "application/vnd.mozilla.xul+xml"
377 #define kRDFTextContentType "text/rdf"
378 #define kRDFApplicationContentType "application/rdf+xml"
379 #define kXIFTextContentType "text/xif"
380 #define kPlainTextContentType "text/plain"
381 #define kViewSourceCommand "view-source"
382 #define kViewFragmentCommand "view-fragment"
383 #define kTextCSSContentType "text/css"
384 #define kApplicationJSContentType "application/javascript"
385 #define kApplicationXJSContentType "application/x-javascript"
386 #define kTextECMAScriptContentType "text/ecmascript"
387 #define kApplicationECMAScriptContentType "application/ecmascript"
388 #define kTextJSContentType "text/javascript"
389 #define kSGMLTextContentType "text/sgml"
390 #define kSVGTextContentType "image/svg+xml"
392 #define NS_IPARSER_FLAG_UNKNOWN_MODE 0x00000000
393 #define NS_IPARSER_FLAG_QUIRKS_MODE 0x00000002
394 #define NS_IPARSER_FLAG_STRICT_MODE 0x00000004
395 #define NS_IPARSER_FLAG_AUTO_DETECT_MODE 0x00000010
396 #define NS_IPARSER_FLAG_VIEW_NORMAL 0x00000020
397 #define NS_IPARSER_FLAG_VIEW_SOURCE 0x00000040
398 #define NS_IPARSER_FLAG_VIEW_ERRORS 0x00000080
399 #define NS_IPARSER_FLAG_PLAIN_TEXT 0x00000100
400 #define NS_IPARSER_FLAG_XML 0x00000200
401 #define NS_IPARSER_FLAG_HTML 0x00000400
402 #define NS_IPARSER_FLAG_SCRIPT_ENABLED 0x00000800
403 #define NS_IPARSER_FLAG_FRAMES_ENABLED 0x00001000