Back to index

lightning-sunbird  0.9+nobinonly
nsHTMLTokens.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 
00057 #ifndef HTMLTOKENS_H
00058 #define HTMLTOKENS_H
00059 
00060 #include "nsToken.h"
00061 #include "nsHTMLTags.h"
00062 #include "nsString.h"
00063 #include "nsScannerString.h"
00064 
00065 class nsScanner;
00066 
00067   /*******************************************************************
00068    * This enum defines the set of token types that we currently support.
00069    *******************************************************************/
00070 
00071 enum eHTMLTokenTypes {
00072   eToken_unknown=0,
00073   eToken_start=1,      eToken_end,          eToken_comment,         eToken_entity,
00074   eToken_whitespace,   eToken_newline,      eToken_text,            eToken_attribute,
00075   eToken_instruction,  eToken_cdatasection, eToken_doctypeDecl,     eToken_markupDecl,
00076   eToken_last //make sure this stays the last token...
00077 };
00078 
00079 enum eHTMLCategory {
00080   eHTMLCategory_unknown=0,
00081   eHTMLCategory_inline,
00082   eHTMLCategory_block,
00083   eHTMLCategory_blockAndInline,
00084   eHTMLCategory_list,
00085   eHTMLCategory_table,
00086   eHTMLCategory_tablepart,
00087   eHTMLCategory_tablerow,
00088   eHTMLCategory_tabledata,
00089   eHTMLCategory_head,
00090   eHTMLCategory_html,
00091   eHTMLCategory_body,
00092   eHTMLCategory_form,
00093   eHTMLCategory_options,
00094   eHTMLCategory_frameset,
00095   eHTMLCategory_text
00096 };
00097 
00098 
00099 nsresult      ConsumeQuotedString(PRUnichar aChar,nsString& aString,nsScanner& aScanner);
00100 nsresult      ConsumeAttributeText(PRUnichar aChar,nsString& aString,nsScanner& aScanner);
00101 const PRUnichar* GetTagName(PRInt32 aTag);
00102 //PRInt32     FindEntityIndex(nsString& aString,PRInt32 aCount=-1);
00103 
00104 
00105 
00110 class CHTMLToken : public CToken {
00111 public:
00112   virtual ~CHTMLToken();
00113   CHTMLToken(eHTMLTags aTag);
00114 
00115   virtual eContainerInfo GetContainerInfo(void) const {return eFormUnknown;}
00116   virtual void SetContainerInfo(eContainerInfo aInfo) { }
00117 
00118 protected:
00119 };
00120 
00127 class CStartToken: public CHTMLToken {
00128   CTOKEN_IMPL_SIZEOF
00129 
00130 public:
00131   CStartToken(eHTMLTags aTag=eHTMLTag_unknown);
00132   CStartToken(const nsAString& aString);
00133   CStartToken(const nsAString& aName,eHTMLTags aTag);
00134 
00135   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
00136   virtual PRInt32 GetTypeID(void);
00137   virtual PRInt32 GetTokenType(void);
00138 
00139   virtual PRBool IsEmpty(void);
00140   virtual void SetEmpty(PRBool aValue);
00141 
00142   virtual const nsSubstring& GetStringValue();
00143   virtual void GetSource(nsString& anOutputString);
00144   virtual void AppendSourceTo(nsAString& anOutputString);
00145 
00146   // the following info is used to set well-formedness state on start tags...
00147   virtual eContainerInfo GetContainerInfo(void) const {return mContainerInfo;}
00148   virtual void SetContainerInfo(eContainerInfo aContainerInfo) {
00149     if (eFormUnknown==mContainerInfo) {
00150       mContainerInfo=aContainerInfo;
00151     }
00152   }
00153   virtual PRBool IsWellFormed(void) const {
00154     return eWellFormed == mContainerInfo;
00155   }
00156 
00157   nsString mTextValue;
00158 protected:
00159   eContainerInfo mContainerInfo;
00160   PRPackedBool mEmpty;
00161 #ifdef DEBUG
00162   PRPackedBool mAttributed;
00163 #endif
00164 };
00165 
00166 
00174 class CEndToken: public CHTMLToken {
00175   CTOKEN_IMPL_SIZEOF
00176 
00177 public:
00178   CEndToken(eHTMLTags aTag);
00179   CEndToken(const nsAString& aString);
00180   CEndToken(const nsAString& aName,eHTMLTags aTag);
00181   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
00182   virtual PRInt32 GetTypeID(void);
00183   virtual PRInt32 GetTokenType(void);
00184 
00185   virtual const nsSubstring& GetStringValue();
00186   virtual void GetSource(nsString& anOutputString);
00187   virtual void AppendSourceTo(nsAString& anOutputString);
00188 
00189 protected:
00190   nsString mTextValue;
00191 };
00192 
00193 
00202 class CCommentToken: public CHTMLToken {
00203   CTOKEN_IMPL_SIZEOF
00204 
00205 public:
00206   CCommentToken();
00207   CCommentToken(const nsAString& aString);
00208   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
00209   virtual PRInt32 GetTokenType(void);
00210   virtual const nsSubstring& GetStringValue(void);
00211   virtual void AppendSourceTo(nsAString& anOutputString);
00212 
00213   nsresult ConsumeStrictComment(nsScanner& aScanner);
00214   nsresult ConsumeQuirksComment(nsScanner& aScanner);
00215 
00216 protected:
00217   nsScannerSubstring mComment; // does not include MDO & MDC
00218   nsScannerSubstring mCommentDecl; // includes MDO & MDC
00219 };
00220 
00221 
00229 class CEntityToken : public CHTMLToken {
00230   CTOKEN_IMPL_SIZEOF
00231 
00232 public:
00233   CEntityToken();
00234   CEntityToken(const nsAString& aString);
00235   virtual PRInt32 GetTokenType(void);
00236   PRInt32 TranslateToUnicodeStr(nsString& aString);
00237   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
00238   static nsresult ConsumeEntity(PRUnichar aChar, nsString& aString,
00239                                 nsScanner& aScanner);
00240   static PRInt32 TranslateToUnicodeStr(PRInt32 aValue,nsString& aString);
00241 
00242   virtual const nsSubstring& GetStringValue(void);
00243   virtual void GetSource(nsString& anOutputString);
00244   virtual void AppendSourceTo(nsAString& anOutputString);
00245 
00246 protected:
00247   nsString mTextValue;
00248 };
00249 
00250 
00258 class CWhitespaceToken: public CHTMLToken {
00259   CTOKEN_IMPL_SIZEOF
00260 
00261 public:
00262   CWhitespaceToken();
00263   CWhitespaceToken(const nsAString& aString);
00264   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
00265   virtual PRInt32 GetTokenType(void);
00266   virtual const nsSubstring& GetStringValue(void);
00267 
00268 protected:
00269   nsScannerSharedSubstring mTextValue;
00270 };
00271 
00279 class CTextToken: public CHTMLToken {
00280   CTOKEN_IMPL_SIZEOF
00281 
00282 public:
00283   CTextToken();
00284   CTextToken(const nsAString& aString);
00285   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
00286   virtual PRInt32 GetTokenType(void);
00287   virtual PRInt32 GetTextLength(void);
00288   virtual void CopyTo(nsAString& aStr);
00289   virtual const nsSubstring& GetStringValue(void);
00290   virtual void Bind(nsScanner* aScanner, nsScannerIterator& aStart,
00291                     nsScannerIterator& aEnd);
00292   virtual void Bind(const nsAString& aStr);
00293 
00294   nsresult ConsumeCharacterData(PRBool aConservativeConsume,
00295                                 PRBool aIgnoreComments,
00296                                 nsScanner& aScanner,
00297                                 const nsAString& aEndTagName,
00298                                 PRInt32 aFlag,
00299                                 PRBool& aFlushTokens);
00300 
00301   nsresult ConsumeParsedCharacterData(PRBool aDiscardFirstNewline,
00302                                       PRBool aConservativeConsume,
00303                                       nsScanner& aScanner,
00304                                       const nsAString& aEndTagName,
00305                                       PRInt32 aFlag,
00306                                       PRBool& aFound);
00307 
00308 protected:
00309   nsScannerSubstring mTextValue;
00310 };
00311 
00312 
00320 class CCDATASectionToken : public CHTMLToken {
00321   CTOKEN_IMPL_SIZEOF
00322 
00323 public:
00324   CCDATASectionToken(eHTMLTags aTag = eHTMLTag_unknown);
00325   CCDATASectionToken(const nsAString& aString);
00326   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
00327   virtual PRInt32 GetTokenType(void);
00328   virtual const nsSubstring& GetStringValue(void);
00329 
00330 protected:
00331   nsString mTextValue;
00332 };
00333 
00334 
00341 class CMarkupDeclToken : public CHTMLToken {
00342   CTOKEN_IMPL_SIZEOF
00343 
00344 public:
00345   CMarkupDeclToken();
00346   CMarkupDeclToken(const nsAString& aString);
00347   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
00348   virtual PRInt32 GetTokenType(void);
00349   virtual const nsSubstring& GetStringValue(void);
00350 
00351 protected:
00352   nsScannerSubstring  mTextValue;
00353 };
00354 
00355 
00364 class CAttributeToken: public CHTMLToken {
00365   CTOKEN_IMPL_SIZEOF
00366 
00367 public:
00368   CAttributeToken();
00369   CAttributeToken(const nsAString& aString);
00370   CAttributeToken(const nsAString& aKey, const nsAString& aString);
00371   ~CAttributeToken() {}
00372   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
00373   virtual PRInt32 GetTokenType(void);
00374   const nsSubstring&     GetKey(void) { return mTextKey.AsString(); }
00375   virtual void SetKey(const nsAString& aKey);
00376   virtual void BindKey(nsScanner* aScanner, nsScannerIterator& aStart,
00377                        nsScannerIterator& aEnd);
00378   const nsSubstring& GetValue(void) {return mTextValue.str();}
00379   virtual const nsSubstring& GetStringValue(void);
00380   virtual void GetSource(nsString& anOutputString);
00381   virtual void AppendSourceTo(nsAString& anOutputString);
00382 
00383   PRPackedBool mHasEqualWithoutValue;
00384 protected:
00385 #ifdef DEBUG
00386   PRPackedBool mLastAttribute;
00387 #endif
00388   nsScannerSharedSubstring mTextValue;
00389   nsScannerSubstring mTextKey;
00390 };
00391 
00392 
00399 class CNewlineToken: public CHTMLToken {
00400   CTOKEN_IMPL_SIZEOF
00401 
00402 public:
00403   CNewlineToken();
00404   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
00405   virtual PRInt32 GetTokenType(void);
00406   virtual const nsSubstring& GetStringValue(void);
00407 
00408   static void AllocNewline();
00409   static void FreeNewline();
00410 };
00411 
00412 
00420 class CInstructionToken: public CHTMLToken {
00421   CTOKEN_IMPL_SIZEOF
00422 
00423 public:
00424   CInstructionToken();
00425   CInstructionToken(const nsAString& aString);
00426   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
00427   virtual PRInt32 GetTokenType(void);
00428   virtual const nsSubstring& GetStringValue(void);
00429 
00430 protected:
00431   nsString mTextValue;
00432 };
00433 
00434 
00441 class CDoctypeDeclToken: public CHTMLToken {
00442   CTOKEN_IMPL_SIZEOF
00443 
00444 public:
00445   CDoctypeDeclToken(eHTMLTags aTag=eHTMLTag_unknown);
00446   CDoctypeDeclToken(const nsAString& aString,eHTMLTags aTag=eHTMLTag_unknown);
00447   virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
00448   virtual PRInt32 GetTokenType(void);
00449   virtual const nsSubstring& GetStringValue(void);
00450   virtual void SetStringValue(const nsAString& aStr);
00451 
00452 protected:
00453   nsString mTextValue;
00454 };
00455 
00456 #endif