1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
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 code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * L. David Baron <dbaron@dbaron.org>
24 * Daniel Glazman <glazman@netscape.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or 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 MPL, 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 MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 /* tokenization of CSS style sheets */
42 #ifndef nsCSSScanner_h___
43 #define nsCSSScanner_h___
47 class nsIUnicharInputStream
;
49 // XXX turn this off for minimo builds
50 #define CSS_REPORT_PARSE_ERRORS
52 #define CSS_BUFFER_SIZE 256
54 // for #ifdef CSS_REPORT_PARSE_ERRORS
55 #include "nsXPIDLString.h"
60 // A css identifier (e.g. foo)
61 eCSSToken_Ident
, // mIdent
63 // A css at keyword (e.g. @foo)
64 eCSSToken_AtKeyword
, // mIdent
66 // A css number without a percentage or dimension; with percentage;
67 // without percentage but with a dimension
68 eCSSToken_Number
, // mNumber
69 eCSSToken_Percentage
, // mNumber
70 eCSSToken_Dimension
, // mNumber + mIdent
72 // A css string (e.g. "foo" or 'foo')
73 eCSSToken_String
, // mSymbol + mIdent + mSymbol
75 // Whitespace (e.g. " " or "/* abc */")
76 eCSSToken_WhiteSpace
, // mIdent
78 // A css symbol (e.g. ':', ';', '+', etc.)
79 eCSSToken_Symbol
, // mSymbol
81 // A css1 id (e.g. #foo3)
82 eCSSToken_ID
, // mIdent
83 // Just like eCSSToken_ID, except the part following the '#' is not
84 // a valid CSS identifier (eg. starts with a digit, is the empty
86 eCSSToken_Ref
, // mIdent
88 eCSSToken_Function
, // mIdent
90 eCSSToken_URL
, // mIdent
91 eCSSToken_InvalidURL
, // doesn't matter
93 eCSSToken_HTMLComment
, // "<!--" or "-->"
95 eCSSToken_Includes
, // "~="
96 eCSSToken_Dashmatch
, // "|="
97 eCSSToken_Beginsmatch
, // "^="
98 eCSSToken_Endsmatch
, // "$="
99 eCSSToken_Containsmatch
, // "*="
101 // A special token indicating that there was an error in tokenization.
102 // It's always an unterminated string.
103 eCSSToken_Error
// mSymbol + mIdent
107 nsCSSTokenType mType
;
108 PRPackedBool mIntegerValid
; // for number and dimension
109 PRPackedBool mHasSign
; // for number, percentage, and dimension
110 nsAutoString mIdent NS_OKONHEAP
;
117 PRBool
IsDimension() {
118 return PRBool((eCSSToken_Dimension
== mType
) ||
119 ((eCSSToken_Number
== mType
) && (mNumber
== 0.0f
)));
122 PRBool
IsSymbol(PRUnichar aSymbol
) {
123 return PRBool((eCSSToken_Symbol
== mType
) && (mSymbol
== aSymbol
));
126 void AppendToString(nsString
& aBuffer
);
129 // CSS Scanner API. Used to tokenize an input stream using the CSS
130 // forward compatible tokenization rules. This implementation is
131 // private to this package and is only used internally by the css
139 // |aLineNumber == 1| is the beginning of a file, use |aLineNumber == 0|
140 // when the line number is unknown.
141 // Either aInput or (aBuffer and aCount) must be set.
142 void Init(nsIUnicharInputStream
* aInput
,
143 const PRUnichar
*aBuffer
, PRUint32 aCount
,
144 nsIURI
* aURI
, PRUint32 aLineNumber
);
147 static PRBool
InitGlobals();
148 static void ReleaseGlobals();
151 // Set whether or not we are processing SVG
152 void SetSVGMode(PRBool aSVGMode
) {
153 NS_ASSERTION(aSVGMode
== PR_TRUE
|| aSVGMode
== PR_FALSE
,
157 PRBool
IsSVGMode() const {
162 #ifdef CSS_REPORT_PARSE_ERRORS
163 NS_HIDDEN_(void) AddToError(const nsSubstring
& aErrorText
);
164 NS_HIDDEN_(void) OutputError();
165 NS_HIDDEN_(void) ClearError();
167 // aMessage must take no parameters
168 NS_HIDDEN_(void) ReportUnexpected(const char* aMessage
);
169 NS_HIDDEN_(void) ReportUnexpectedParams(const char* aMessage
,
170 const PRUnichar
**aParams
,
171 PRUint32 aParamsLength
);
172 // aLookingFor is a plain string, not a format string
173 NS_HIDDEN_(void) ReportUnexpectedEOF(const char* aLookingFor
);
174 // aLookingFor is a single character
175 NS_HIDDEN_(void) ReportUnexpectedEOF(PRUnichar aLookingFor
);
176 // aMessage must take 1 parameter (for the string representation of the
178 NS_HIDDEN_(void) ReportUnexpectedToken(nsCSSToken
& tok
,
179 const char *aMessage
);
180 // aParams's first entry must be null, and we'll fill in the token
181 NS_HIDDEN_(void) ReportUnexpectedTokenParams(nsCSSToken
& tok
,
182 const char* aMessage
,
183 const PRUnichar
**aParams
,
184 PRUint32 aParamsLength
);
187 PRUint32
GetLineNumber() { return mLineNumber
; }
189 // Get the next token. Return PR_FALSE on EOF. aTokenResult
190 // is filled in with the data for the token.
191 PRBool
Next(nsCSSToken
& aTokenResult
);
193 // Get the next token that may be a string or unquoted URL or whitespace
194 PRBool
NextURL(nsCSSToken
& aTokenResult
);
196 // It's really ugly that we have to expose this, but it's the easiest
197 // way to do :nth-child() parsing sanely. (In particular, in
198 // :nth-child(2n-1), "2n-1" is a dimension, and we need to push the
199 // "-1" back so we can read it again as a number.)
200 void Pushback(PRUnichar aChar
);
202 // Reports operating-system level errors, e.g. read failures and
204 nsresult
GetLowLevelError();
206 // sometimes the parser wants to make note of a low-level error
207 void SetLowLevelError(nsresult aErrorCode
);
213 PRBool
LookAhead(PRUnichar aChar
);
214 PRBool
EatWhiteSpace();
217 void ParseAndAppendEscape(nsString
& aOutput
);
218 PRBool
ParseIdent(PRInt32 aChar
, nsCSSToken
& aResult
);
219 PRBool
ParseAtKeyword(PRInt32 aChar
, nsCSSToken
& aResult
);
220 PRBool
ParseNumber(PRInt32 aChar
, nsCSSToken
& aResult
);
221 PRBool
ParseRef(PRInt32 aChar
, nsCSSToken
& aResult
);
222 PRBool
ParseString(PRInt32 aChar
, nsCSSToken
& aResult
);
224 PRBool
ParseCComment(nsCSSToken
& aResult
);
225 PRBool
ParseEOLComment(nsCSSToken
& aResult
);
227 PRBool
SkipCComment();
229 PRBool
GatherIdent(PRInt32 aChar
, nsString
& aIdent
);
231 // Only used when input is a stream
232 nsCOMPtr
<nsIUnicharInputStream
> mInputStream
;
233 PRUnichar mBuffer
[CSS_BUFFER_SIZE
];
235 const PRUnichar
*mReadPointer
;
238 PRUnichar
* mPushback
;
239 PRInt32 mPushbackCount
;
240 PRInt32 mPushbackSize
;
241 PRUnichar mLocalPushback
[4];
242 nsresult mLowLevelError
;
244 PRUint32 mLineNumber
;
246 // True if we are in SVG mode; false in "normal" CSS
247 PRPackedBool mSVGMode
;
249 #ifdef CSS_REPORT_PARSE_ERRORS
250 nsXPIDLCString mFileName
;
251 nsCOMPtr
<nsIURI
> mURI
; // Cached so we know to not refetch mFileName
252 PRUint32 mErrorLineNumber
, mColNumber
, mErrorColNumber
;
253 nsFixedString mError
;
254 PRUnichar mErrorBuf
[200];
258 #endif /* nsCSSScanner_h___ */