2001-09-25 01:32:19 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-17 21:52:36 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1998-04-13 20:24:54 +00:00
|
|
|
*
|
2004-04-17 21:52:36 +00:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
1998-04-13 20:24:54 +00:00
|
|
|
*
|
2001-09-25 01:32:19 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1998-04-13 20:24:54 +00:00
|
|
|
*
|
1999-11-06 03:40:37 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-17 21:52:36 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-25 01:32:19 +00:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 03:40:37 +00:00
|
|
|
*
|
2001-09-25 01:32:19 +00:00
|
|
|
* Contributor(s):
|
2003-01-01 23:53:20 +00:00
|
|
|
* L. David Baron <dbaron@dbaron.org>
|
2001-03-03 00:54:04 +00:00
|
|
|
* Daniel Glazman <glazman@netscape.com>
|
2001-09-25 01:32:19 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-17 21:52:36 +00:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2001-09-25 01:32:19 +00:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-17 21:52:36 +00:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-25 01:32:19 +00:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-17 21:52:36 +00:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-25 01:32:19 +00:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2006-03-25 05:47:31 +00:00
|
|
|
|
|
|
|
/* tokenization of CSS style sheets */
|
|
|
|
|
1998-04-13 20:24:54 +00:00
|
|
|
#ifndef nsCSSScanner_h___
|
|
|
|
#define nsCSSScanner_h___
|
|
|
|
|
|
|
|
#include "nsString.h"
|
2004-10-14 03:30:55 +00:00
|
|
|
#include "nsCOMPtr.h"
|
1998-04-13 20:24:54 +00:00
|
|
|
class nsIUnicharInputStream;
|
|
|
|
|
2004-09-21 04:41:42 +00:00
|
|
|
// XXX turn this off for minimo builds
|
2001-03-06 01:57:30 +00:00
|
|
|
#define CSS_REPORT_PARSE_ERRORS
|
2000-09-06 02:27:46 +00:00
|
|
|
|
2005-11-10 22:26:06 +00:00
|
|
|
#define CSS_BUFFER_SIZE 256
|
|
|
|
|
2001-03-06 01:57:30 +00:00
|
|
|
// for #ifdef CSS_REPORT_PARSE_ERRORS
|
2000-09-06 02:27:46 +00:00
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
class nsIURI;
|
|
|
|
|
1998-04-13 20:24:54 +00:00
|
|
|
// Token types
|
|
|
|
enum nsCSSTokenType {
|
|
|
|
// A css identifier (e.g. foo)
|
2005-03-18 06:56:56 +00:00
|
|
|
eCSSToken_Ident, // mIdent
|
1998-04-13 20:24:54 +00:00
|
|
|
|
|
|
|
// A css at keyword (e.g. @foo)
|
2005-03-18 06:56:56 +00:00
|
|
|
eCSSToken_AtKeyword, // mIdent
|
1998-04-13 20:24:54 +00:00
|
|
|
|
|
|
|
// A css number without a percentage or dimension; with percentage;
|
|
|
|
// without percentage but with a dimension
|
2005-03-18 06:56:56 +00:00
|
|
|
eCSSToken_Number, // mNumber
|
|
|
|
eCSSToken_Percentage, // mNumber
|
|
|
|
eCSSToken_Dimension, // mNumber + mIdent
|
1998-04-13 20:24:54 +00:00
|
|
|
|
|
|
|
// A css string (e.g. "foo" or 'foo')
|
2005-03-18 06:56:56 +00:00
|
|
|
eCSSToken_String, // mSymbol + mIdent + mSymbol
|
1998-04-13 20:24:54 +00:00
|
|
|
|
1999-02-03 02:59:51 +00:00
|
|
|
// Whitespace (e.g. " " or "/* abc */")
|
2005-03-18 06:56:56 +00:00
|
|
|
eCSSToken_WhiteSpace, // mIdent
|
1998-04-13 20:24:54 +00:00
|
|
|
|
|
|
|
// A css symbol (e.g. ':', ';', '+', etc.)
|
2005-03-18 06:56:56 +00:00
|
|
|
eCSSToken_Symbol, // mSymbol
|
1998-04-13 20:24:54 +00:00
|
|
|
|
|
|
|
// A css1 id (e.g. #foo3)
|
2005-03-18 06:56:56 +00:00
|
|
|
eCSSToken_ID, // mIdent
|
2005-07-19 20:49:34 +00:00
|
|
|
// Just like eCSSToken_ID, except the part following the '#' is not
|
|
|
|
// a valid CSS identifier (eg. starts with a digit, is the empty
|
|
|
|
// string, etc).
|
|
|
|
eCSSToken_Ref, // mIdent
|
1998-10-26 23:22:40 +00:00
|
|
|
|
2005-03-18 06:56:56 +00:00
|
|
|
eCSSToken_Function, // mIdent
|
1998-10-26 23:22:40 +00:00
|
|
|
|
2005-03-18 06:56:56 +00:00
|
|
|
eCSSToken_URL, // mIdent
|
|
|
|
eCSSToken_InvalidURL, // doesn't matter
|
1999-02-03 02:59:51 +00:00
|
|
|
|
2005-03-18 06:56:56 +00:00
|
|
|
eCSSToken_HTMLComment, // "<!--" or "-->"
|
2000-04-27 00:12:25 +00:00
|
|
|
|
2005-03-18 06:56:56 +00:00
|
|
|
eCSSToken_Includes, // "~="
|
|
|
|
eCSSToken_Dashmatch, // "|="
|
|
|
|
eCSSToken_Beginsmatch, // "^="
|
|
|
|
eCSSToken_Endsmatch, // "$="
|
|
|
|
eCSSToken_Containsmatch, // "*="
|
1998-10-26 23:22:40 +00:00
|
|
|
|
2009-08-20 21:52:47 +00:00
|
|
|
eCSSToken_URange, // Low in mInteger, high in mInteger2;
|
|
|
|
// mIntegerValid is true if the token is a
|
|
|
|
// valid range; mIdent preserves the textual
|
|
|
|
// form of the token for error reporting
|
|
|
|
|
2005-03-18 06:56:56 +00:00
|
|
|
// A special token indicating that there was an error in tokenization.
|
|
|
|
// It's always an unterminated string.
|
|
|
|
eCSSToken_Error // mSymbol + mIdent
|
1998-04-13 20:24:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct nsCSSToken {
|
2008-08-27 14:58:50 +00:00
|
|
|
nsAutoString mIdent NS_OKONHEAP;
|
1998-10-26 23:22:40 +00:00
|
|
|
float mNumber;
|
|
|
|
PRInt32 mInteger;
|
2009-08-20 21:52:47 +00:00
|
|
|
PRInt32 mInteger2;
|
2009-05-13 08:26:48 +00:00
|
|
|
nsCSSTokenType mType;
|
1998-10-26 23:22:40 +00:00
|
|
|
PRUnichar mSymbol;
|
2009-08-20 21:52:47 +00:00
|
|
|
PRPackedBool mIntegerValid; // for number, dimension, urange
|
2009-05-13 08:26:48 +00:00
|
|
|
PRPackedBool mHasSign; // for number, percentage, and dimension
|
1998-04-13 20:24:54 +00:00
|
|
|
|
|
|
|
nsCSSToken();
|
|
|
|
|
1999-06-10 05:27:01 +00:00
|
|
|
PRBool IsSymbol(PRUnichar aSymbol) {
|
|
|
|
return PRBool((eCSSToken_Symbol == mType) && (mSymbol == aSymbol));
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
1999-02-07 21:47:48 +00:00
|
|
|
|
|
|
|
void AppendToString(nsString& aBuffer);
|
1998-04-13 20:24:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// CSS Scanner API. Used to tokenize an input stream using the CSS
|
|
|
|
// forward compatible tokenization rules. This implementation is
|
|
|
|
// private to this package and is only used internally by the css
|
|
|
|
// parser.
|
|
|
|
class nsCSSScanner {
|
|
|
|
public:
|
|
|
|
nsCSSScanner();
|
|
|
|
~nsCSSScanner();
|
|
|
|
|
|
|
|
// Init the scanner.
|
2004-09-10 23:31:48 +00:00
|
|
|
// |aLineNumber == 1| is the beginning of a file, use |aLineNumber == 0|
|
|
|
|
// when the line number is unknown.
|
2005-11-10 22:26:06 +00:00
|
|
|
// Either aInput or (aBuffer and aCount) must be set.
|
|
|
|
void Init(nsIUnicharInputStream* aInput,
|
2007-08-22 17:32:59 +00:00
|
|
|
const PRUnichar *aBuffer, PRUint32 aCount,
|
2005-11-10 22:26:06 +00:00
|
|
|
nsIURI* aURI, PRUint32 aLineNumber);
|
2004-10-14 03:30:55 +00:00
|
|
|
void Close();
|
1998-04-13 20:24:54 +00:00
|
|
|
|
2004-09-30 05:18:05 +00:00
|
|
|
static PRBool InitGlobals();
|
|
|
|
static void ReleaseGlobals();
|
|
|
|
|
2008-08-12 14:02:22 +00:00
|
|
|
#ifdef MOZ_SVG
|
|
|
|
// Set whether or not we are processing SVG
|
|
|
|
void SetSVGMode(PRBool aSVGMode) {
|
|
|
|
NS_ASSERTION(aSVGMode == PR_TRUE || aSVGMode == PR_FALSE,
|
|
|
|
"bad PRBool value");
|
|
|
|
mSVGMode = aSVGMode;
|
|
|
|
}
|
|
|
|
PRBool IsSVGMode() const {
|
|
|
|
return mSVGMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2000-09-06 02:27:46 +00:00
|
|
|
#ifdef CSS_REPORT_PARSE_ERRORS
|
2004-10-12 18:44:14 +00:00
|
|
|
NS_HIDDEN_(void) AddToError(const nsSubstring& aErrorText);
|
|
|
|
NS_HIDDEN_(void) OutputError();
|
|
|
|
NS_HIDDEN_(void) ClearError();
|
|
|
|
|
|
|
|
// aMessage must take no parameters
|
|
|
|
NS_HIDDEN_(void) ReportUnexpected(const char* aMessage);
|
|
|
|
NS_HIDDEN_(void) ReportUnexpectedParams(const char* aMessage,
|
|
|
|
const PRUnichar **aParams,
|
|
|
|
PRUint32 aParamsLength);
|
2008-07-19 22:52:09 +00:00
|
|
|
// aLookingFor is a plain string, not a format string
|
2004-10-12 18:44:14 +00:00
|
|
|
NS_HIDDEN_(void) ReportUnexpectedEOF(const char* aLookingFor);
|
2008-07-19 22:52:09 +00:00
|
|
|
// aLookingFor is a single character
|
|
|
|
NS_HIDDEN_(void) ReportUnexpectedEOF(PRUnichar aLookingFor);
|
2004-10-12 18:44:14 +00:00
|
|
|
// aMessage must take 1 parameter (for the string representation of the
|
|
|
|
// unexpected token)
|
|
|
|
NS_HIDDEN_(void) ReportUnexpectedToken(nsCSSToken& tok,
|
|
|
|
const char *aMessage);
|
|
|
|
// aParams's first entry must be null, and we'll fill in the token
|
|
|
|
NS_HIDDEN_(void) ReportUnexpectedTokenParams(nsCSSToken& tok,
|
|
|
|
const char* aMessage,
|
|
|
|
const PRUnichar **aParams,
|
|
|
|
PRUint32 aParamsLength);
|
2000-09-06 02:27:46 +00:00
|
|
|
#endif
|
|
|
|
|
2004-09-09 17:32:35 +00:00
|
|
|
PRUint32 GetLineNumber() { return mLineNumber; }
|
2001-01-22 04:03:48 +00:00
|
|
|
|
2005-03-18 06:56:56 +00:00
|
|
|
// Get the next token. Return PR_FALSE on EOF. aTokenResult
|
1998-04-13 20:24:54 +00:00
|
|
|
// is filled in with the data for the token.
|
2008-09-10 04:38:14 +00:00
|
|
|
PRBool Next(nsCSSToken& aTokenResult);
|
1998-10-26 23:22:40 +00:00
|
|
|
|
|
|
|
// Get the next token that may be a string or unquoted URL or whitespace
|
2008-09-10 04:38:14 +00:00
|
|
|
PRBool NextURL(nsCSSToken& aTokenResult);
|
1998-04-13 20:24:54 +00:00
|
|
|
|
2008-06-03 03:17:35 +00:00
|
|
|
// It's really ugly that we have to expose this, but it's the easiest
|
|
|
|
// way to do :nth-child() parsing sanely. (In particular, in
|
|
|
|
// :nth-child(2n-1), "2n-1" is a dimension, and we need to push the
|
|
|
|
// "-1" back so we can read it again as a number.)
|
|
|
|
void Pushback(PRUnichar aChar);
|
|
|
|
|
2008-09-10 04:38:14 +00:00
|
|
|
// Reports operating-system level errors, e.g. read failures and
|
|
|
|
// out of memory.
|
|
|
|
nsresult GetLowLevelError();
|
|
|
|
|
|
|
|
// sometimes the parser wants to make note of a low-level error
|
|
|
|
void SetLowLevelError(nsresult aErrorCode);
|
2004-04-03 17:18:45 +00:00
|
|
|
|
1998-04-13 20:24:54 +00:00
|
|
|
protected:
|
2008-09-10 04:38:14 +00:00
|
|
|
PRBool EnsureData();
|
|
|
|
PRInt32 Read();
|
|
|
|
PRInt32 Peek();
|
|
|
|
PRBool LookAhead(PRUnichar aChar);
|
2009-04-09 06:46:26 +00:00
|
|
|
void EatWhiteSpace();
|
|
|
|
|
2008-09-10 04:38:14 +00:00
|
|
|
void ParseAndAppendEscape(nsString& aOutput);
|
|
|
|
PRBool ParseIdent(PRInt32 aChar, nsCSSToken& aResult);
|
|
|
|
PRBool ParseAtKeyword(PRInt32 aChar, nsCSSToken& aResult);
|
|
|
|
PRBool ParseNumber(PRInt32 aChar, nsCSSToken& aResult);
|
|
|
|
PRBool ParseRef(PRInt32 aChar, nsCSSToken& aResult);
|
|
|
|
PRBool ParseString(PRInt32 aChar, nsCSSToken& aResult);
|
2009-08-20 21:52:47 +00:00
|
|
|
PRBool ParseURange(PRInt32 aChar, nsCSSToken& aResult);
|
2008-09-10 04:38:14 +00:00
|
|
|
PRBool SkipCComment();
|
1998-10-26 23:22:40 +00:00
|
|
|
|
2008-09-10 04:38:14 +00:00
|
|
|
PRBool GatherIdent(PRInt32 aChar, nsString& aIdent);
|
1998-04-13 20:24:54 +00:00
|
|
|
|
2005-11-10 22:26:06 +00:00
|
|
|
// Only used when input is a stream
|
|
|
|
nsCOMPtr<nsIUnicharInputStream> mInputStream;
|
|
|
|
PRUnichar mBuffer[CSS_BUFFER_SIZE];
|
|
|
|
|
|
|
|
const PRUnichar *mReadPointer;
|
2007-08-22 17:32:59 +00:00
|
|
|
PRUint32 mOffset;
|
|
|
|
PRUint32 mCount;
|
1998-09-25 01:50:51 +00:00
|
|
|
PRUnichar* mPushback;
|
|
|
|
PRInt32 mPushbackCount;
|
|
|
|
PRInt32 mPushbackSize;
|
|
|
|
PRUnichar mLocalPushback[4];
|
2008-09-10 04:38:14 +00:00
|
|
|
nsresult mLowLevelError;
|
2000-09-06 02:27:46 +00:00
|
|
|
|
2001-01-22 04:03:48 +00:00
|
|
|
PRUint32 mLineNumber;
|
2008-08-12 14:02:22 +00:00
|
|
|
#ifdef MOZ_SVG
|
|
|
|
// True if we are in SVG mode; false in "normal" CSS
|
|
|
|
PRPackedBool mSVGMode;
|
|
|
|
#endif
|
2000-09-06 02:27:46 +00:00
|
|
|
#ifdef CSS_REPORT_PARSE_ERRORS
|
|
|
|
nsXPIDLCString mFileName;
|
2004-10-14 03:30:55 +00:00
|
|
|
nsCOMPtr<nsIURI> mURI; // Cached so we know to not refetch mFileName
|
2001-03-06 01:57:30 +00:00
|
|
|
PRUint32 mErrorLineNumber, mColNumber, mErrorColNumber;
|
2004-09-30 05:18:05 +00:00
|
|
|
nsFixedString mError;
|
|
|
|
PRUnichar mErrorBuf[200];
|
2000-09-06 02:27:46 +00:00
|
|
|
#endif
|
1998-04-13 20:24:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* nsCSSScanner_h___ */
|