2001-09-25 01:32:19 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
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"
|
2010-12-20 16:21:59 +00:00
|
|
|
#include "mozilla/css/Loader.h"
|
|
|
|
#include "nsCSSStyleSheet.h"
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2011-03-11 17:29:45 +00:00
|
|
|
eCSSToken_URL, // mIdent + mSymbol
|
|
|
|
eCSSToken_Bad_URL, // mIdent + mSymbol
|
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
|
|
|
|
|
2011-03-11 17:29:44 +00:00
|
|
|
// An unterminated string, which is always an error.
|
|
|
|
eCSSToken_Bad_String // 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;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mIntegerValid; // for number, dimension, urange
|
|
|
|
bool mHasSign; // for number, percentage, and dimension
|
1998-04-13 20:24:54 +00:00
|
|
|
|
|
|
|
nsCSSToken();
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool IsSymbol(PRUnichar aSymbol) {
|
|
|
|
return bool((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.
|
2011-05-18 17:33:16 +00:00
|
|
|
void Init(const nsAString& aBuffer,
|
2010-12-20 16:21:59 +00:00
|
|
|
nsIURI* aURI, PRUint32 aLineNumber,
|
|
|
|
nsCSSStyleSheet* aSheet, mozilla::css::Loader* aLoader);
|
2004-10-14 03:30:55 +00:00
|
|
|
void Close();
|
1998-04-13 20:24:54 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
static bool InitGlobals();
|
2004-09-30 05:18:05 +00:00
|
|
|
static void ReleaseGlobals();
|
|
|
|
|
2008-08-12 14:02:22 +00:00
|
|
|
// Set whether or not we are processing SVG
|
2011-09-29 06:19:26 +00:00
|
|
|
void SetSVGMode(bool aSVGMode) {
|
2008-08-12 14:02:22 +00:00
|
|
|
mSVGMode = aSVGMode;
|
|
|
|
}
|
2011-09-29 06:19:26 +00:00
|
|
|
bool IsSVGMode() const {
|
2008-08-12 14:02:22 +00:00
|
|
|
return mSVGMode;
|
|
|
|
}
|
|
|
|
|
2000-09-06 02:27:46 +00:00
|
|
|
#ifdef CSS_REPORT_PARSE_ERRORS
|
2010-04-06 21:52:17 +00:00
|
|
|
void AddToError(const nsSubstring& aErrorText);
|
|
|
|
void OutputError();
|
|
|
|
void ClearError();
|
2004-10-12 18:44:14 +00:00
|
|
|
|
|
|
|
// aMessage must take no parameters
|
2010-04-06 21:52:17 +00:00
|
|
|
void ReportUnexpected(const char* aMessage);
|
2012-03-09 02:22:57 +00:00
|
|
|
|
|
|
|
private:
|
2010-04-06 21:52:17 +00:00
|
|
|
void ReportUnexpectedParams(const char* aMessage,
|
2012-03-09 02:22:57 +00:00
|
|
|
const PRUnichar** aParams,
|
2010-04-06 21:52:17 +00:00
|
|
|
PRUint32 aParamsLength);
|
2012-03-09 02:22:57 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
template<PRUint32 N>
|
|
|
|
void ReportUnexpectedParams(const char* aMessage,
|
|
|
|
const PRUnichar* (&aParams)[N])
|
|
|
|
{
|
|
|
|
return ReportUnexpectedParams(aMessage, aParams, N);
|
|
|
|
}
|
2008-07-19 22:52:09 +00:00
|
|
|
// aLookingFor is a plain string, not a format string
|
2010-04-06 21:52:17 +00:00
|
|
|
void ReportUnexpectedEOF(const char* aLookingFor);
|
2008-07-19 22:52:09 +00:00
|
|
|
// aLookingFor is a single character
|
2010-04-06 21:52:17 +00:00
|
|
|
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)
|
2010-04-06 21:52:17 +00:00
|
|
|
void ReportUnexpectedToken(nsCSSToken& tok, const char *aMessage);
|
2004-10-12 18:44:14 +00:00
|
|
|
// aParams's first entry must be null, and we'll fill in the token
|
2010-04-06 21:52:17 +00:00
|
|
|
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
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
// Get the next token. Return false on EOF. aTokenResult
|
1998-04-13 20:24:54 +00:00
|
|
|
// is filled in with the data for the token.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool Next(nsCSSToken& aTokenResult);
|
1998-10-26 23:22:40 +00:00
|
|
|
|
2011-03-11 17:29:44 +00:00
|
|
|
// Get the next token that may be a string or unquoted URL
|
2011-09-29 06:19:26 +00:00
|
|
|
bool 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);
|
|
|
|
|
1998-04-13 20:24:54 +00:00
|
|
|
protected:
|
2008-09-10 04:38:14 +00:00
|
|
|
PRInt32 Read();
|
|
|
|
PRInt32 Peek();
|
2011-09-29 06:19:26 +00:00
|
|
|
bool LookAhead(PRUnichar aChar);
|
|
|
|
bool LookAheadOrEOF(PRUnichar aChar); // expect either aChar or EOF
|
2009-04-09 06:46:26 +00:00
|
|
|
void EatWhiteSpace();
|
2011-05-03 20:19:19 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool ParseAndAppendEscape(nsString& aOutput, bool aInString);
|
|
|
|
bool ParseIdent(PRInt32 aChar, nsCSSToken& aResult);
|
|
|
|
bool ParseAtKeyword(PRInt32 aChar, nsCSSToken& aResult);
|
|
|
|
bool ParseNumber(PRInt32 aChar, nsCSSToken& aResult);
|
|
|
|
bool ParseRef(PRInt32 aChar, nsCSSToken& aResult);
|
|
|
|
bool ParseString(PRInt32 aChar, nsCSSToken& aResult);
|
|
|
|
bool ParseURange(PRInt32 aChar, nsCSSToken& aResult);
|
|
|
|
bool SkipCComment();
|
1998-10-26 23:22:40 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool GatherIdent(PRInt32 aChar, nsString& aIdent);
|
1998-04-13 20:24:54 +00:00
|
|
|
|
2005-11-10 22:26:06 +00:00
|
|
|
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];
|
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
|
|
|
// True if we are in SVG mode; false in "normal" CSS
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mSVGMode;
|
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];
|
2011-08-24 20:44:35 +00:00
|
|
|
PRUint64 mInnerWindowID;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mWindowIDCached;
|
2010-12-20 16:21:59 +00:00
|
|
|
nsCSSStyleSheet* mSheet;
|
|
|
|
mozilla::css::Loader* mLoader;
|
2000-09-06 02:27:46 +00:00
|
|
|
#endif
|
1998-04-13 20:24:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* nsCSSScanner_h___ */
|