Bug 596094 - fix a race condition w/ipc disabled in focus related event handling...
[mozilla-central.git] / layout / style / nsCSSScanner.h
blobf9dae6f239c9c61a632447b3d38a778a4bfd425b
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
13 * License.
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.
22 * Contributor(s):
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___
45 #include "nsString.h"
46 #include "nsCOMPtr.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"
56 class nsIURI;
58 // Token types
59 enum nsCSSTokenType {
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
85 // string, etc).
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 eCSSToken_URange, // Low in mInteger, high in mInteger2;
102 // mIntegerValid is true if the token is a
103 // valid range; mIdent preserves the textual
104 // form of the token for error reporting
106 // A special token indicating that there was an error in tokenization.
107 // It's always an unterminated string.
108 eCSSToken_Error // mSymbol + mIdent
111 struct nsCSSToken {
112 nsAutoString mIdent NS_OKONHEAP;
113 float mNumber;
114 PRInt32 mInteger;
115 PRInt32 mInteger2;
116 nsCSSTokenType mType;
117 PRUnichar mSymbol;
118 PRPackedBool mIntegerValid; // for number, dimension, urange
119 PRPackedBool mHasSign; // for number, percentage, and dimension
121 nsCSSToken();
123 PRBool IsSymbol(PRUnichar aSymbol) {
124 return PRBool((eCSSToken_Symbol == mType) && (mSymbol == aSymbol));
127 void AppendToString(nsString& aBuffer);
130 // CSS Scanner API. Used to tokenize an input stream using the CSS
131 // forward compatible tokenization rules. This implementation is
132 // private to this package and is only used internally by the css
133 // parser.
134 class nsCSSScanner {
135 public:
136 nsCSSScanner();
137 ~nsCSSScanner();
139 // Init the scanner.
140 // |aLineNumber == 1| is the beginning of a file, use |aLineNumber == 0|
141 // when the line number is unknown.
142 // Either aInput or (aBuffer and aCount) must be set.
143 void Init(nsIUnicharInputStream* aInput,
144 const PRUnichar *aBuffer, PRUint32 aCount,
145 nsIURI* aURI, PRUint32 aLineNumber);
146 void Close();
148 static PRBool InitGlobals();
149 static void ReleaseGlobals();
151 #ifdef MOZ_SVG
152 // Set whether or not we are processing SVG
153 void SetSVGMode(PRBool aSVGMode) {
154 NS_ASSERTION(aSVGMode == PR_TRUE || aSVGMode == PR_FALSE,
155 "bad PRBool value");
156 mSVGMode = aSVGMode;
158 PRBool IsSVGMode() const {
159 return mSVGMode;
162 #endif
163 #ifdef CSS_REPORT_PARSE_ERRORS
164 void AddToError(const nsSubstring& aErrorText);
165 void OutputError();
166 void ClearError();
168 // aMessage must take no parameters
169 void ReportUnexpected(const char* aMessage);
170 void ReportUnexpectedParams(const char* aMessage,
171 const PRUnichar **aParams,
172 PRUint32 aParamsLength);
173 // aLookingFor is a plain string, not a format string
174 void ReportUnexpectedEOF(const char* aLookingFor);
175 // aLookingFor is a single character
176 void ReportUnexpectedEOF(PRUnichar aLookingFor);
177 // aMessage must take 1 parameter (for the string representation of the
178 // unexpected token)
179 void ReportUnexpectedToken(nsCSSToken& tok, const char *aMessage);
180 // aParams's first entry must be null, and we'll fill in the token
181 void ReportUnexpectedTokenParams(nsCSSToken& tok,
182 const char* aMessage,
183 const PRUnichar **aParams,
184 PRUint32 aParamsLength);
185 #endif
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
203 // out of memory.
204 nsresult GetLowLevelError();
206 // sometimes the parser wants to make note of a low-level error
207 void SetLowLevelError(nsresult aErrorCode);
209 protected:
210 PRBool EnsureData();
211 PRInt32 Read();
212 PRInt32 Peek();
213 PRBool LookAhead(PRUnichar aChar);
214 void EatWhiteSpace();
216 void ParseAndAppendEscape(nsString& aOutput);
217 PRBool ParseIdent(PRInt32 aChar, nsCSSToken& aResult);
218 PRBool ParseAtKeyword(PRInt32 aChar, nsCSSToken& aResult);
219 PRBool ParseNumber(PRInt32 aChar, nsCSSToken& aResult);
220 PRBool ParseRef(PRInt32 aChar, nsCSSToken& aResult);
221 PRBool ParseString(PRInt32 aChar, nsCSSToken& aResult);
222 PRBool ParseURange(PRInt32 aChar, nsCSSToken& aResult);
223 PRBool SkipCComment();
225 PRBool GatherIdent(PRInt32 aChar, nsString& aIdent);
227 // Only used when input is a stream
228 nsCOMPtr<nsIUnicharInputStream> mInputStream;
229 PRUnichar mBuffer[CSS_BUFFER_SIZE];
231 const PRUnichar *mReadPointer;
232 PRUint32 mOffset;
233 PRUint32 mCount;
234 PRUnichar* mPushback;
235 PRInt32 mPushbackCount;
236 PRInt32 mPushbackSize;
237 PRUnichar mLocalPushback[4];
238 nsresult mLowLevelError;
240 PRUint32 mLineNumber;
241 #ifdef MOZ_SVG
242 // True if we are in SVG mode; false in "normal" CSS
243 PRPackedBool mSVGMode;
244 #endif
245 #ifdef CSS_REPORT_PARSE_ERRORS
246 nsXPIDLCString mFileName;
247 nsCOMPtr<nsIURI> mURI; // Cached so we know to not refetch mFileName
248 PRUint32 mErrorLineNumber, mColNumber, mErrorColNumber;
249 nsFixedString mError;
250 PRUnichar mErrorBuf[200];
251 #endif
254 #endif /* nsCSSScanner_h___ */