Back to index

lightning-sunbird  0.9+nobinonly
nsIParser.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 #ifndef NS_IPARSER___
00038 #define NS_IPARSER___
00039 
00040 
00050 #include "nsISupports.h"
00051 #include "nsIStreamListener.h"
00052 #include "nsIDTD.h"
00053 #include "nsIInputStream.h"
00054 #include "nsHashtable.h"
00055 #include "nsVoidArray.h"
00056 
00057 // {22039D29-2798-4412-9EA6-A11B41BA27D0}
00058 #define NS_IPARSER_IID      \
00059 { 0x22039d29, 0x2798, 0x4412, \
00060 { 0x9e, 0xa6, 0xa1, 0x1b, 0x41, 0xba, 0x27, 0xd0 } }
00061 
00062 // {41421C60-310A-11d4-816F-000064657374}
00063 #define NS_IDEBUG_DUMP_CONTENT_IID \
00064 { 0x41421c60, 0x310a, 0x11d4, \
00065 { 0x81, 0x6f, 0x0, 0x0, 0x64, 0x65, 0x73, 0x74 } }
00066 
00067 class nsIContentSink;
00068 class nsIRequestObserver;
00069 class nsIParserFilter;
00070 class nsString;
00071 class nsIURI;
00072 class nsIChannel;
00073 
00074 enum eParserCommands {
00075   eViewNormal,
00076   eViewSource,
00077   eViewFragment,
00078   eViewErrors
00079 };
00080 
00081 enum eCRCQuality {
00082   eCRCGood = 0,
00083   eCRCFair,
00084   eCRCPoor
00085 };
00086 
00087 
00088 enum eParserDocType {
00089   ePlainText = 0,
00090   eXML,
00091   eHTML_Quirks,
00092   eHTML3_Quirks, // separate, for editor output, since HTML pre-4.0 lacks tbody
00093   eHTML_Strict
00094 };
00095 
00096 
00097 // define Charset source constants
00098 // note: the value order defines the priority; higher numbers take priority
00099 #define kCharsetUninitialized           0
00100 #define kCharsetFromWeakDocTypeDefault  1
00101 #define kCharsetFromUserDefault         2
00102 #define kCharsetFromDocTypeDefault      3
00103 #define kCharsetFromCache               4
00104 #define kCharsetFromParentFrame         5
00105 #define kCharsetFromBookmarks           6
00106 #define kCharsetFromAutoDetection       7
00107 #define kCharsetFromHintPrevDoc         8
00108 #define kCharsetFromMetaTag             9
00109 #define kCharsetFromByteOrderMark      10
00110 #define kCharsetFromChannel            11 
00111 #define kCharsetFromOtherComponent     12
00112 // Levels below here will be forced onto childframes too
00113 #define kCharsetFromParentForced       13
00114 #define kCharsetFromUserForced         14
00115 #define kCharsetFromPreviousLoading    15
00116 
00117 enum eStreamState {eNone,eOnStart,eOnDataAvail,eOnStop};
00118 
00128 class nsIDebugDumpContent : public nsISupports {
00129 public:
00130   NS_DEFINE_STATIC_IID_ACCESSOR(NS_IDEBUG_DUMP_CONTENT_IID)
00131   NS_IMETHOD DumpContentModel()=0;
00132 };
00133 
00140 class nsIParser : public nsISupports {
00141   public:
00142 
00143     NS_DEFINE_STATIC_IID_ACCESSOR(NS_IPARSER_IID)
00144 
00145     
00152     NS_IMETHOD RegisterDTD(nsIDTD* aDTD)=0;
00153 
00154 
00161     NS_IMETHOD_(void) SetContentSink(nsIContentSink* aSink)=0;
00162 
00163 
00169     NS_IMETHOD_(nsIContentSink*) GetContentSink(void)=0;
00170 
00180     NS_IMETHOD_(void) GetCommand(nsCString& aCommand)=0;
00181     NS_IMETHOD_(void) SetCommand(const char* aCommand)=0;
00182     NS_IMETHOD_(void) SetCommand(eParserCommands aParserCommand)=0;
00183 
00193     NS_IMETHOD_(void) SetDocumentCharset(const nsACString& aCharset, PRInt32 aSource)=0;
00194     NS_IMETHOD_(void) GetDocumentCharset(nsACString& oCharset, PRInt32& oSource)=0;
00195 
00196     NS_IMETHOD_(void) SetParserFilter(nsIParserFilter* aFilter) = 0;
00197 
00204     NS_IMETHOD GetChannel(nsIChannel** aChannel) = 0;
00205 
00212     NS_IMETHOD GetDTD(nsIDTD** aDTD) = 0;
00213 
00214     /******************************************************************************************
00215      *  Parse methods always begin with an input source, and perform conversions 
00216      *  until you wind up being emitted to the given contentsink (which may or may not
00217      *  be a proxy for the NGLayout content model).
00218      ******************************************************************************************/
00219     
00220     // Call this method to resume the parser from the blocked state.
00221     NS_IMETHOD ContinueParsing() = 0;
00222 
00223     // Call this method to resume the parser from an unblocked state.
00224     // This can happen, for example, if parsing was interrupted and then the
00225     // consumer needed to restart the parser without waiting for more data.
00226     // This also happens after loading scripts, which unblock the parser in
00227     // order to process the output of document.write() and then need to
00228     // continue on with the page load on an enabled parser.
00229     NS_IMETHOD ContinueInterruptedParsing() = 0;
00230     
00231     // Stops parsing temporarily.
00232     NS_IMETHOD_(void) BlockParser() = 0;
00233     
00234     // Open up the parser for tokenization, building up content 
00235     // model..etc. However, this method does not resume parsing 
00236     // automatically. It's the callers' responsibility to restart
00237     // the parsing engine.
00238     NS_IMETHOD_(void) UnblockParser() = 0;
00239 
00240     NS_IMETHOD_(PRBool) IsParserEnabled() = 0;
00241     NS_IMETHOD_(PRBool) IsComplete() = 0;
00242     
00243     NS_IMETHOD Parse(nsIURI* aURL,
00244                      nsIRequestObserver* aListener = nsnull,
00245                      PRBool aEnableVerify = PR_FALSE,
00246                      void* aKey = 0,
00247                      nsDTDMode aMode = eDTDMode_autodetect) = 0;
00248     NS_IMETHOD Parse(nsIInputStream* aStream,
00249                      const nsACString& aMimeType,
00250                      PRBool aEnableVerify = PR_FALSE,
00251                      void* aKey = 0,
00252                      nsDTDMode aMode = eDTDMode_autodetect) = 0;
00253     NS_IMETHOD Parse(const nsAString& aSourceBuffer,
00254                      void* aKey,
00255                      const nsACString& aMimeType,
00256                      PRBool aEnableVerify,
00257                      PRBool aLastCall,
00258                      nsDTDMode aMode = eDTDMode_autodetect) = 0;
00259     
00260     NS_IMETHOD Terminate(void) = 0;
00261 
00275     NS_IMETHOD ParseFragment(const nsAString& aSourceBuffer,
00276                              void* aKey,
00277                              nsVoidArray& aTagStack,
00278                              PRBool aXMLMode,
00279                              const nsACString& aContentType,
00280                              nsDTDMode aMode = eDTDMode_autodetect) = 0;
00281 
00288     NS_IMETHOD BuildModel(void) = 0;
00289 
00290 
00297     NS_IMETHOD_(nsDTDMode) GetParseMode(void) = 0;
00298 
00309     NS_IMETHOD CancelParsingEvents() = 0;
00310 };
00311 
00312 /* ===========================================================*
00313   Some useful constants...
00314  * ===========================================================*/
00315 
00316 #include "prtypes.h"
00317 #include "nsError.h"
00318 
00319 #define NS_ERROR_HTMLPARSER_EOF                            NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1000)
00320 #define NS_ERROR_HTMLPARSER_UNKNOWN                        NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1001)
00321 #define NS_ERROR_HTMLPARSER_CANTPROPAGATE                  NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1002)
00322 #define NS_ERROR_HTMLPARSER_CONTEXTMISMATCH                NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1003)
00323 #define NS_ERROR_HTMLPARSER_BADFILENAME                    NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1004)
00324 #define NS_ERROR_HTMLPARSER_BADURL                         NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1005)
00325 #define NS_ERROR_HTMLPARSER_INVALIDPARSERCONTEXT           NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1006)
00326 #define NS_ERROR_HTMLPARSER_INTERRUPTED                    NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1007)
00327 #define NS_ERROR_HTMLPARSER_BLOCK                          NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1008)
00328 #define NS_ERROR_HTMLPARSER_BADTOKENIZER                   NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1009)
00329 #define NS_ERROR_HTMLPARSER_BADATTRIBUTE                   NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1010)
00330 #define NS_ERROR_HTMLPARSER_UNRESOLVEDDTD                  NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1011)
00331 #define NS_ERROR_HTMLPARSER_MISPLACEDTABLECONTENT          NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1012)
00332 #define NS_ERROR_HTMLPARSER_BADDTD                         NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1013)
00333 #define NS_ERROR_HTMLPARSER_BADCONTEXT                     NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1014)
00334 #define NS_ERROR_HTMLPARSER_STOPPARSING                    NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1015)
00335 #define NS_ERROR_HTMLPARSER_UNTERMINATEDSTRINGLITERAL      NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1016)
00336 #define NS_ERROR_HTMLPARSER_HIERARCHYTOODEEP               NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1017)
00337 #define NS_ERROR_HTMLPARSER_FAKE_ENDTAG                    NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1018)
00338 #define NS_ERROR_HTMLPARSER_INVALID_COMMENT                NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_HTMLPARSER,1019)
00339 
00340 #define NS_ERROR_HTMLPARSER_CONTINUE              NS_OK
00341 
00342 
00343 const PRUint32  kEOF              = NS_ERROR_HTMLPARSER_EOF;
00344 const PRUint32  kUnknownError     = NS_ERROR_HTMLPARSER_UNKNOWN;
00345 const PRUint32  kCantPropagate    = NS_ERROR_HTMLPARSER_CANTPROPAGATE;
00346 const PRUint32  kContextMismatch  = NS_ERROR_HTMLPARSER_CONTEXTMISMATCH;
00347 const PRUint32  kBadFilename      = NS_ERROR_HTMLPARSER_BADFILENAME;
00348 const PRUint32  kBadURL           = NS_ERROR_HTMLPARSER_BADURL;
00349 const PRUint32  kInvalidParserContext = NS_ERROR_HTMLPARSER_INVALIDPARSERCONTEXT;
00350 const PRUint32  kBlocked          = NS_ERROR_HTMLPARSER_BLOCK;
00351 const PRUint32  kBadStringLiteral = NS_ERROR_HTMLPARSER_UNTERMINATEDSTRINGLITERAL;
00352 const PRUint32  kHierarchyTooDeep = NS_ERROR_HTMLPARSER_HIERARCHYTOODEEP;
00353 const PRUint32  kFakeEndTag       = NS_ERROR_HTMLPARSER_FAKE_ENDTAG;
00354 const PRUint32  kNotAComment      = NS_ERROR_HTMLPARSER_INVALID_COMMENT;
00355 
00356 const PRUnichar  kNewLine          = '\n';
00357 const PRUnichar  kCR               = '\r';
00358 const PRUnichar  kLF               = '\n';
00359 const PRUnichar  kTab              = '\t';
00360 const PRUnichar  kSpace            = ' ';
00361 const PRUnichar  kQuote            = '"';
00362 const PRUnichar  kApostrophe       = '\'';
00363 const PRUnichar  kLessThan         = '<';
00364 const PRUnichar  kGreaterThan      = '>';
00365 const PRUnichar  kAmpersand        = '&';
00366 const PRUnichar  kForwardSlash     = '/';
00367 const PRUnichar  kBackSlash        = '\\';
00368 const PRUnichar  kEqual            = '=';
00369 const PRUnichar  kMinus            = '-';
00370 const PRUnichar  kPlus             = '+';
00371 const PRUnichar  kExclamation      = '!';
00372 const PRUnichar  kSemicolon        = ';';
00373 const PRUnichar  kHashsign         = '#';
00374 const PRUnichar  kAsterisk         = '*';
00375 const PRUnichar  kUnderbar         = '_';
00376 const PRUnichar  kComma            = ',';
00377 const PRUnichar  kLeftParen        = '(';
00378 const PRUnichar  kRightParen       = ')';
00379 const PRUnichar  kLeftBrace        = '{';
00380 const PRUnichar  kRightBrace       = '}';
00381 const PRUnichar  kQuestionMark     = '?';
00382 const PRUnichar  kLeftSquareBracket  = '[';
00383 const PRUnichar  kRightSquareBracket = ']';
00384 const PRUnichar kNullCh           = '\0';
00385 
00386 // XXXbz these type defines should really just go away....  Until they
00387 // do, changes here should be reflected in nsContentDLF.cpp
00388 #define kHTMLTextContentType  "text/html"
00389 #define kXMLTextContentType   "text/xml"
00390 #define kXMLApplicationContentType "application/xml"
00391 #define kXHTMLApplicationContentType "application/xhtml+xml"
00392 #define kXULTextContentType   "application/vnd.mozilla.xul+xml"
00393 #define kRDFTextContentType   "text/rdf"
00394 #define kRDFApplicationContentType "application/rdf+xml"
00395 #define kXIFTextContentType   "text/xif"
00396 #define kPlainTextContentType "text/plain"
00397 #define kViewSourceCommand    "view-source"
00398 #define kViewFragmentCommand  "view-fragment"
00399 #define kTextCSSContentType   "text/css"
00400 #define kApplicationJSContentType "application/javascript"
00401 #define kApplicationXJSContentType "application/x-javascript"
00402 #define kTextECMAScriptContentType "text/ecmascript"
00403 #define kApplicationECMAScriptContentType "application/ecmascript"
00404 #define kTextJSContentType    "text/javascript"
00405 #define kSGMLTextContentType   "text/sgml"
00406 #define kSVGTextContentType   "image/svg+xml"
00407 
00408 #define NS_IPARSER_FLAG_UNKNOWN_MODE         0x00000000
00409 #define NS_IPARSER_FLAG_QUIRKS_MODE          0x00000002
00410 #define NS_IPARSER_FLAG_STRICT_MODE          0x00000004
00411 #define NS_IPARSER_FLAG_AUTO_DETECT_MODE     0x00000010
00412 #define NS_IPARSER_FLAG_VIEW_NORMAL          0x00000020
00413 #define NS_IPARSER_FLAG_VIEW_SOURCE          0x00000040
00414 #define NS_IPARSER_FLAG_VIEW_ERRORS          0x00000080
00415 #define NS_IPARSER_FLAG_PLAIN_TEXT           0x00000100
00416 #define NS_IPARSER_FLAG_XML                  0x00000200
00417 #define NS_IPARSER_FLAG_HTML                 0x00000400
00418 #define NS_IPARSER_FLAG_SCRIPT_ENABLED       0x00000800
00419 #define NS_IPARSER_FLAG_FRAMES_ENABLED       0x00001000
00420 
00421 #endif