2007-03-22 10:30:00 -07:00
|
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
|
*
|
|
|
|
|
* 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/
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
|
*
|
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Contributor(s):
|
|
|
|
|
* L. David Baron <dbaron@dbaron.org>
|
|
|
|
|
* Daniel Glazman <glazman@netscape.com>
|
|
|
|
|
*
|
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
|
* 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"),
|
|
|
|
|
* 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
|
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
|
* 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
|
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
|
*
|
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2009-07-09 18:44:20 -07:00
|
|
|
|
#include <math.h>
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
/* tokenization of CSS style sheets */
|
|
|
|
|
|
|
|
|
|
#include "nsCSSScanner.h"
|
|
|
|
|
#include "nsIFactory.h"
|
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
|
#include "nsIUnicharInputStream.h"
|
|
|
|
|
#include "nsString.h"
|
|
|
|
|
#include "nsCRT.h"
|
|
|
|
|
|
|
|
|
|
// for #ifdef CSS_REPORT_PARSE_ERRORS
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
|
#include "nsIURI.h"
|
|
|
|
|
#include "nsIConsoleService.h"
|
|
|
|
|
#include "nsIScriptError.h"
|
|
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
|
|
|
|
|
|
#ifdef CSS_REPORT_PARSE_ERRORS
|
|
|
|
|
static PRBool gReportErrors = PR_TRUE;
|
|
|
|
|
static nsIConsoleService *gConsoleService;
|
|
|
|
|
static nsIFactory *gScriptErrorFactory;
|
|
|
|
|
static nsIStringBundle *gStringBundle;
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-10-15 13:18:21 -07:00
|
|
|
|
// Don't bother collecting whitespace characters in token's mIdent buffer
|
|
|
|
|
#undef COLLECT_WHITESPACE
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
2009-10-15 13:18:21 -07:00
|
|
|
|
// Table of character classes
|
|
|
|
|
static const PRUnichar CSS_ESCAPE = PRUnichar('\\');
|
|
|
|
|
|
|
|
|
|
static const PRUint8 IS_HEX_DIGIT = 0x01;
|
|
|
|
|
static const PRUint8 START_IDENT = 0x02;
|
|
|
|
|
static const PRUint8 IS_IDENT = 0x04;
|
|
|
|
|
static const PRUint8 IS_WHITESPACE = 0x08;
|
|
|
|
|
|
|
|
|
|
#define W IS_WHITESPACE
|
|
|
|
|
#define I IS_IDENT
|
|
|
|
|
#define S START_IDENT
|
|
|
|
|
#define SI IS_IDENT|START_IDENT
|
|
|
|
|
#define XI IS_IDENT |IS_HEX_DIGIT
|
|
|
|
|
#define XSI IS_IDENT|START_IDENT|IS_HEX_DIGIT
|
|
|
|
|
|
|
|
|
|
static const PRUint8 gLexTable[256] = {
|
|
|
|
|
// TAB LF FF CR
|
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, W, W, 0, W, W, 0, 0,
|
|
|
|
|
//
|
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
|
// SPC ! " # $ % & ' ( ) * + , - . /
|
|
|
|
|
W, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, I, 0, 0,
|
|
|
|
|
// 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
|
|
|
|
|
XI, XI, XI, XI, XI, XI, XI, XI, XI, XI, 0, 0, 0, 0, 0, 0,
|
|
|
|
|
// @ A B C D E F G H I J K L M N O
|
|
|
|
|
0, XSI,XSI,XSI,XSI,XSI,XSI,SI, SI, SI, SI, SI, SI, SI, SI, SI,
|
|
|
|
|
// P Q R S T U V W X Y Z [ \ ] ^ _
|
|
|
|
|
SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, 0, S, 0, 0, SI,
|
|
|
|
|
// ` a b c d e f g h i j k l m n o
|
|
|
|
|
0, XSI,XSI,XSI,XSI,XSI,XSI,SI, SI, SI, SI, SI, SI, SI, SI, SI,
|
|
|
|
|
// p q r s t u v w x y z { | } ~
|
|
|
|
|
SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, 0, 0, 0, 0, 0,
|
|
|
|
|
//
|
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
|
//
|
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
|
// ¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ¬ ® ¯
|
|
|
|
|
0, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI,
|
|
|
|
|
// ° ± ² ³ ´ µ ¶ · ¸ ¹ º » ¼ ½ ¾ ¿
|
|
|
|
|
SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI,
|
|
|
|
|
// À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï
|
|
|
|
|
SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI,
|
|
|
|
|
// Ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ü Ý Þ ß
|
|
|
|
|
SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI,
|
|
|
|
|
// à á â ã ä å æ ç è é ê ë ì í î ï
|
|
|
|
|
SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI,
|
|
|
|
|
// ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ
|
|
|
|
|
SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI, SI,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#undef W
|
|
|
|
|
#undef S
|
|
|
|
|
#undef I
|
|
|
|
|
#undef XI
|
|
|
|
|
#undef SI
|
|
|
|
|
#undef XSI
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
2008-09-09 21:38:29 -07:00
|
|
|
|
static inline PRBool
|
|
|
|
|
IsIdentStart(PRInt32 aChar)
|
|
|
|
|
{
|
|
|
|
|
return aChar >= 0 &&
|
|
|
|
|
(aChar >= 256 || (gLexTable[aChar] & START_IDENT) != 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline PRBool
|
|
|
|
|
StartsIdent(PRInt32 aFirstChar, PRInt32 aSecondChar)
|
|
|
|
|
{
|
|
|
|
|
return IsIdentStart(aFirstChar) ||
|
|
|
|
|
(aFirstChar == '-' && IsIdentStart(aSecondChar));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline PRBool
|
|
|
|
|
IsWhitespace(PRInt32 ch) {
|
|
|
|
|
return PRUint32(ch) < 256 && (gLexTable[ch] & IS_WHITESPACE) != 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline PRBool
|
|
|
|
|
IsDigit(PRInt32 ch) {
|
2009-09-04 05:25:27 -07:00
|
|
|
|
return (ch >= '0') && (ch <= '9');
|
2008-09-09 21:38:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline PRBool
|
|
|
|
|
IsHexDigit(PRInt32 ch) {
|
|
|
|
|
return PRUint32(ch) < 256 && (gLexTable[ch] & IS_HEX_DIGIT) != 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline PRBool
|
|
|
|
|
IsIdent(PRInt32 ch) {
|
|
|
|
|
return ch >= 0 && (ch >= 256 || (gLexTable[ch] & IS_IDENT) != 0);
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-20 14:52:47 -07:00
|
|
|
|
static inline PRUint32
|
|
|
|
|
DecimalDigitValue(PRInt32 ch)
|
|
|
|
|
{
|
|
|
|
|
return ch - '0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline PRUint32
|
|
|
|
|
HexDigitValue(PRInt32 ch)
|
|
|
|
|
{
|
|
|
|
|
if (IsDigit(ch)) {
|
|
|
|
|
return DecimalDigitValue(ch);
|
|
|
|
|
} else {
|
|
|
|
|
// Note: c&7 just keeps the low three bits which causes
|
|
|
|
|
// upper and lower case alphabetics to both yield their
|
|
|
|
|
// "relative to 10" value for computing the hex value.
|
|
|
|
|
return (ch & 0x7) + 9;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
|
nsCSSToken::nsCSSToken()
|
|
|
|
|
{
|
|
|
|
|
mType = eCSSToken_Symbol;
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-20 14:52:47 -07:00
|
|
|
|
void
|
2007-03-22 10:30:00 -07:00
|
|
|
|
nsCSSToken::AppendToString(nsString& aBuffer)
|
|
|
|
|
{
|
|
|
|
|
switch (mType) {
|
|
|
|
|
case eCSSToken_AtKeyword:
|
|
|
|
|
aBuffer.Append(PRUnichar('@')); // fall through intentional
|
|
|
|
|
case eCSSToken_Ident:
|
|
|
|
|
case eCSSToken_WhiteSpace:
|
|
|
|
|
case eCSSToken_Function:
|
|
|
|
|
case eCSSToken_URL:
|
|
|
|
|
case eCSSToken_InvalidURL:
|
|
|
|
|
case eCSSToken_HTMLComment:
|
2009-08-20 14:52:47 -07:00
|
|
|
|
case eCSSToken_URange:
|
2007-03-22 10:30:00 -07:00
|
|
|
|
aBuffer.Append(mIdent);
|
2010-01-27 16:20:04 -08:00
|
|
|
|
if (mType == eCSSToken_Function)
|
|
|
|
|
aBuffer.Append(PRUnichar('('));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
break;
|
|
|
|
|
case eCSSToken_Number:
|
|
|
|
|
if (mIntegerValid) {
|
|
|
|
|
aBuffer.AppendInt(mInteger, 10);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
aBuffer.AppendFloat(mNumber);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case eCSSToken_Percentage:
|
|
|
|
|
NS_ASSERTION(!mIntegerValid, "How did a percentage token get this set?");
|
|
|
|
|
aBuffer.AppendFloat(mNumber * 100.0f);
|
|
|
|
|
aBuffer.Append(PRUnichar('%')); // STRING USE WARNING: technically, this should be |AppendWithConversion|
|
|
|
|
|
break;
|
|
|
|
|
case eCSSToken_Dimension:
|
|
|
|
|
if (mIntegerValid) {
|
|
|
|
|
aBuffer.AppendInt(mInteger, 10);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
aBuffer.AppendFloat(mNumber);
|
|
|
|
|
}
|
|
|
|
|
aBuffer.Append(mIdent);
|
|
|
|
|
break;
|
|
|
|
|
case eCSSToken_String:
|
|
|
|
|
aBuffer.Append(mSymbol);
|
|
|
|
|
aBuffer.Append(mIdent); // fall through intentional
|
|
|
|
|
case eCSSToken_Symbol:
|
|
|
|
|
aBuffer.Append(mSymbol);
|
|
|
|
|
break;
|
|
|
|
|
case eCSSToken_ID:
|
|
|
|
|
case eCSSToken_Ref:
|
|
|
|
|
aBuffer.Append(PRUnichar('#'));
|
|
|
|
|
aBuffer.Append(mIdent);
|
|
|
|
|
break;
|
|
|
|
|
case eCSSToken_Includes:
|
|
|
|
|
aBuffer.AppendLiteral("~=");
|
|
|
|
|
break;
|
|
|
|
|
case eCSSToken_Dashmatch:
|
|
|
|
|
aBuffer.AppendLiteral("|=");
|
|
|
|
|
break;
|
2008-07-11 15:49:46 -07:00
|
|
|
|
case eCSSToken_Beginsmatch:
|
|
|
|
|
aBuffer.AppendLiteral("^=");
|
|
|
|
|
break;
|
|
|
|
|
case eCSSToken_Endsmatch:
|
|
|
|
|
aBuffer.AppendLiteral("$=");
|
|
|
|
|
break;
|
|
|
|
|
case eCSSToken_Containsmatch:
|
|
|
|
|
aBuffer.AppendLiteral("*=");
|
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
case eCSSToken_Error:
|
|
|
|
|
aBuffer.Append(mSymbol);
|
|
|
|
|
aBuffer.Append(mIdent);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
NS_ERROR("invalid token type");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsCSSScanner::nsCSSScanner()
|
|
|
|
|
: mInputStream(nsnull)
|
|
|
|
|
, mReadPointer(nsnull)
|
2008-09-09 21:38:14 -07:00
|
|
|
|
, mLowLevelError(NS_OK)
|
2008-08-12 07:02:22 -07:00
|
|
|
|
#ifdef MOZ_SVG
|
|
|
|
|
, mSVGMode(PR_FALSE)
|
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
#ifdef CSS_REPORT_PARSE_ERRORS
|
|
|
|
|
, mError(mErrorBuf, NS_ARRAY_LENGTH(mErrorBuf), 0)
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
MOZ_COUNT_CTOR(nsCSSScanner);
|
|
|
|
|
mPushback = mLocalPushback;
|
2008-02-14 20:21:57 -08:00
|
|
|
|
mPushbackSize = NS_ARRAY_LENGTH(mLocalPushback);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
// No need to init the other members, since they represent state
|
|
|
|
|
// which can get cleared. We'll init them every time Init() is
|
|
|
|
|
// called.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsCSSScanner::~nsCSSScanner()
|
|
|
|
|
{
|
|
|
|
|
MOZ_COUNT_DTOR(nsCSSScanner);
|
|
|
|
|
Close();
|
|
|
|
|
if (mLocalPushback != mPushback) {
|
|
|
|
|
delete [] mPushback;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
nsresult
|
|
|
|
|
nsCSSScanner::GetLowLevelError()
|
|
|
|
|
{
|
|
|
|
|
return mLowLevelError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
nsCSSScanner::SetLowLevelError(nsresult aErrorCode)
|
|
|
|
|
{
|
|
|
|
|
NS_ASSERTION(aErrorCode != NS_OK, "SetLowLevelError() used to clear error");
|
|
|
|
|
NS_ASSERTION(mLowLevelError == NS_OK, "there is already a low-level error");
|
|
|
|
|
mLowLevelError = aErrorCode;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
|
#ifdef CSS_REPORT_PARSE_ERRORS
|
|
|
|
|
#define CSS_ERRORS_PREF "layout.css.report_errors"
|
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
|
static int
|
2008-09-09 21:38:14 -07:00
|
|
|
|
CSSErrorsPrefChanged(const char *aPref, void *aClosure)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
gReportErrors = nsContentUtils::GetBoolPref(CSS_ERRORS_PREF, PR_TRUE);
|
|
|
|
|
return NS_OK;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
/* static */ PRBool
|
|
|
|
|
nsCSSScanner::InitGlobals()
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
#ifdef CSS_REPORT_PARSE_ERRORS
|
|
|
|
|
if (gConsoleService && gScriptErrorFactory)
|
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
|
|
nsresult rv = CallGetService(NS_CONSOLESERVICE_CONTRACTID, &gConsoleService);
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, PR_FALSE);
|
|
|
|
|
|
|
|
|
|
rv = CallGetClassObject(NS_SCRIPTERROR_CONTRACTID, &gScriptErrorFactory);
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, PR_FALSE);
|
|
|
|
|
NS_ASSERTION(gConsoleService && gScriptErrorFactory,
|
|
|
|
|
"unexpected null pointer without failure");
|
|
|
|
|
|
|
|
|
|
nsContentUtils::RegisterPrefCallback(CSS_ERRORS_PREF, CSSErrorsPrefChanged, nsnull);
|
|
|
|
|
CSSErrorsPrefChanged(CSS_ERRORS_PREF, nsnull);
|
|
|
|
|
#endif
|
|
|
|
|
return PR_TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
/* static */ void
|
|
|
|
|
nsCSSScanner::ReleaseGlobals()
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
#ifdef CSS_REPORT_PARSE_ERRORS
|
|
|
|
|
nsContentUtils::UnregisterPrefCallback(CSS_ERRORS_PREF, CSSErrorsPrefChanged, nsnull);
|
|
|
|
|
NS_IF_RELEASE(gConsoleService);
|
|
|
|
|
NS_IF_RELEASE(gScriptErrorFactory);
|
|
|
|
|
NS_IF_RELEASE(gStringBundle);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
void
|
|
|
|
|
nsCSSScanner::Init(nsIUnicharInputStream* aInput,
|
|
|
|
|
const PRUnichar * aBuffer, PRUint32 aCount,
|
|
|
|
|
nsIURI* aURI, PRUint32 aLineNumber)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
NS_PRECONDITION(!mInputStream, "Should not have an existing input stream!");
|
|
|
|
|
NS_PRECONDITION(!mReadPointer, "Should not have an existing input buffer!");
|
|
|
|
|
|
|
|
|
|
// Read from stream via my own buffer
|
|
|
|
|
if (aInput) {
|
|
|
|
|
NS_PRECONDITION(!aBuffer, "Shouldn't have both input and buffer!");
|
|
|
|
|
NS_PRECONDITION(aCount == 0, "Shouldn't have count with a stream");
|
|
|
|
|
mInputStream = aInput;
|
|
|
|
|
mReadPointer = mBuffer;
|
|
|
|
|
mCount = 0;
|
|
|
|
|
} else {
|
|
|
|
|
NS_PRECONDITION(aBuffer, "Either aInput or aBuffer must be set");
|
|
|
|
|
// Read directly from the provided buffer
|
|
|
|
|
mInputStream = nsnull;
|
|
|
|
|
mReadPointer = aBuffer;
|
|
|
|
|
mCount = aCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef CSS_REPORT_PARSE_ERRORS
|
|
|
|
|
// If aURI is the same as mURI, no need to reget mFileName -- it
|
|
|
|
|
// shouldn't have changed.
|
|
|
|
|
if (aURI != mURI) {
|
|
|
|
|
mURI = aURI;
|
|
|
|
|
if (aURI) {
|
|
|
|
|
aURI->GetSpec(mFileName);
|
|
|
|
|
} else {
|
|
|
|
|
mFileName.Adopt(NS_strdup("from DOM"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif // CSS_REPORT_PARSE_ERRORS
|
|
|
|
|
mLineNumber = aLineNumber;
|
|
|
|
|
|
|
|
|
|
// Reset variables that we use to keep track of our progress through the input
|
|
|
|
|
mOffset = 0;
|
|
|
|
|
mPushbackCount = 0;
|
2008-09-09 21:38:14 -07:00
|
|
|
|
mLowLevelError = NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
#ifdef CSS_REPORT_PARSE_ERRORS
|
|
|
|
|
mColNumber = 0;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef CSS_REPORT_PARSE_ERRORS
|
|
|
|
|
|
|
|
|
|
// @see REPORT_UNEXPECTED_EOF in nsCSSParser.cpp
|
|
|
|
|
#define REPORT_UNEXPECTED_EOF(lf_) \
|
|
|
|
|
ReportUnexpectedEOF(#lf_)
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
void
|
|
|
|
|
nsCSSScanner::AddToError(const nsSubstring& aErrorText)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
if (mError.IsEmpty()) {
|
|
|
|
|
mErrorLineNumber = mLineNumber;
|
|
|
|
|
mErrorColNumber = mColNumber;
|
|
|
|
|
mError = aErrorText;
|
|
|
|
|
} else {
|
|
|
|
|
mError.Append(NS_LITERAL_STRING(" ") + aErrorText);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
void
|
|
|
|
|
nsCSSScanner::ClearError()
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
mError.Truncate();
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
void
|
|
|
|
|
nsCSSScanner::OutputError()
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
if (mError.IsEmpty()) return;
|
|
|
|
|
|
|
|
|
|
// Log it to the Error console
|
|
|
|
|
|
|
|
|
|
if (InitGlobals() && gReportErrors) {
|
|
|
|
|
nsresult rv;
|
|
|
|
|
nsCOMPtr<nsIScriptError> errorObject =
|
|
|
|
|
do_CreateInstance(gScriptErrorFactory, &rv);
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
|
rv = errorObject->Init(mError.get(),
|
|
|
|
|
NS_ConvertUTF8toUTF16(mFileName).get(),
|
|
|
|
|
EmptyString().get(),
|
|
|
|
|
mErrorLineNumber,
|
|
|
|
|
mErrorColNumber,
|
|
|
|
|
nsIScriptError::warningFlag,
|
|
|
|
|
"CSS Parser");
|
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
|
gConsoleService->LogMessage(errorObject);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ClearError();
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
static PRBool
|
|
|
|
|
InitStringBundle()
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
if (gStringBundle)
|
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStringBundleService> sbs =
|
|
|
|
|
do_GetService(NS_STRINGBUNDLE_CONTRACTID);
|
|
|
|
|
if (!sbs)
|
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
|
sbs->CreateBundle("chrome://global/locale/css.properties", &gStringBundle);
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
|
gStringBundle = nsnull;
|
|
|
|
|
return PR_FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define ENSURE_STRINGBUNDLE \
|
|
|
|
|
PR_BEGIN_MACRO if (!InitStringBundle()) return; PR_END_MACRO
|
|
|
|
|
|
|
|
|
|
// aMessage must take no parameters
|
|
|
|
|
void nsCSSScanner::ReportUnexpected(const char* aMessage)
|
|
|
|
|
{
|
|
|
|
|
ENSURE_STRINGBUNDLE;
|
|
|
|
|
|
|
|
|
|
nsXPIDLString str;
|
|
|
|
|
gStringBundle->GetStringFromName(NS_ConvertASCIItoUTF16(aMessage).get(),
|
|
|
|
|
getter_Copies(str));
|
|
|
|
|
AddToError(str);
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
void
|
|
|
|
|
nsCSSScanner::ReportUnexpectedParams(const char* aMessage,
|
|
|
|
|
const PRUnichar **aParams,
|
|
|
|
|
PRUint32 aParamsLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
NS_PRECONDITION(aParamsLength > 0, "use the non-params version");
|
|
|
|
|
ENSURE_STRINGBUNDLE;
|
|
|
|
|
|
|
|
|
|
nsXPIDLString str;
|
|
|
|
|
gStringBundle->FormatStringFromName(NS_ConvertASCIItoUTF16(aMessage).get(),
|
|
|
|
|
aParams, aParamsLength,
|
|
|
|
|
getter_Copies(str));
|
|
|
|
|
AddToError(str);
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-19 15:52:09 -07:00
|
|
|
|
// aLookingFor is a plain string, not a format string
|
2008-09-09 21:38:14 -07:00
|
|
|
|
void
|
|
|
|
|
nsCSSScanner::ReportUnexpectedEOF(const char* aLookingFor)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
ENSURE_STRINGBUNDLE;
|
|
|
|
|
|
|
|
|
|
nsXPIDLString innerStr;
|
|
|
|
|
gStringBundle->GetStringFromName(NS_ConvertASCIItoUTF16(aLookingFor).get(),
|
|
|
|
|
getter_Copies(innerStr));
|
|
|
|
|
|
|
|
|
|
const PRUnichar *params[] = {
|
|
|
|
|
innerStr.get()
|
|
|
|
|
};
|
|
|
|
|
nsXPIDLString str;
|
|
|
|
|
gStringBundle->FormatStringFromName(NS_LITERAL_STRING("PEUnexpEOF2").get(),
|
|
|
|
|
params, NS_ARRAY_LENGTH(params),
|
|
|
|
|
getter_Copies(str));
|
|
|
|
|
AddToError(str);
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-19 15:52:09 -07:00
|
|
|
|
// aLookingFor is a single character
|
2008-09-09 21:38:14 -07:00
|
|
|
|
void
|
|
|
|
|
nsCSSScanner::ReportUnexpectedEOF(PRUnichar aLookingFor)
|
2008-07-19 15:52:09 -07:00
|
|
|
|
{
|
|
|
|
|
ENSURE_STRINGBUNDLE;
|
|
|
|
|
|
|
|
|
|
const PRUnichar lookingForStr[] = {
|
|
|
|
|
PRUnichar('\''), aLookingFor, PRUnichar('\''), PRUnichar(0)
|
|
|
|
|
};
|
|
|
|
|
const PRUnichar *params[] = { lookingForStr };
|
|
|
|
|
nsXPIDLString str;
|
|
|
|
|
gStringBundle->FormatStringFromName(NS_LITERAL_STRING("PEUnexpEOF2").get(),
|
|
|
|
|
params, NS_ARRAY_LENGTH(params),
|
|
|
|
|
getter_Copies(str));
|
|
|
|
|
AddToError(str);
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
|
// aMessage must take 1 parameter (for the string representation of the
|
|
|
|
|
// unexpected token)
|
2008-09-09 21:38:14 -07:00
|
|
|
|
void
|
|
|
|
|
nsCSSScanner::ReportUnexpectedToken(nsCSSToken& tok,
|
|
|
|
|
const char *aMessage)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
ENSURE_STRINGBUNDLE;
|
|
|
|
|
|
|
|
|
|
nsAutoString tokenString;
|
|
|
|
|
tok.AppendToString(tokenString);
|
|
|
|
|
|
|
|
|
|
const PRUnichar *params[] = {
|
|
|
|
|
tokenString.get()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ReportUnexpectedParams(aMessage, params, NS_ARRAY_LENGTH(params));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// aParams's first entry must be null, and we'll fill in the token
|
2008-09-09 21:38:14 -07:00
|
|
|
|
void
|
|
|
|
|
nsCSSScanner::ReportUnexpectedTokenParams(nsCSSToken& tok,
|
|
|
|
|
const char* aMessage,
|
|
|
|
|
const PRUnichar **aParams,
|
|
|
|
|
PRUint32 aParamsLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
NS_PRECONDITION(aParamsLength > 1, "use the non-params version");
|
|
|
|
|
NS_PRECONDITION(aParams[0] == nsnull, "first param should be empty");
|
|
|
|
|
|
|
|
|
|
ENSURE_STRINGBUNDLE;
|
|
|
|
|
|
|
|
|
|
nsAutoString tokenString;
|
|
|
|
|
tok.AppendToString(tokenString);
|
|
|
|
|
aParams[0] = tokenString.get();
|
|
|
|
|
|
|
|
|
|
ReportUnexpectedParams(aMessage, aParams, aParamsLength);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
#define REPORT_UNEXPECTED_EOF(lf_)
|
|
|
|
|
|
|
|
|
|
#endif // CSS_REPORT_PARSE_ERRORS
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
void
|
|
|
|
|
nsCSSScanner::Close()
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
mInputStream = nsnull;
|
|
|
|
|
mReadPointer = nsnull;
|
2008-02-14 20:21:57 -08:00
|
|
|
|
|
|
|
|
|
// Clean things up so we don't hold on to memory if our parser gets recycled.
|
2008-02-23 23:39:47 -08:00
|
|
|
|
#ifdef CSS_REPORT_PARSE_ERRORS
|
2008-02-14 20:21:57 -08:00
|
|
|
|
mFileName.Truncate();
|
|
|
|
|
mURI = nsnull;
|
|
|
|
|
mError.Truncate();
|
2008-02-23 23:39:47 -08:00
|
|
|
|
#endif
|
2008-02-14 20:21:57 -08:00
|
|
|
|
if (mPushback != mLocalPushback) {
|
|
|
|
|
delete [] mPushback;
|
|
|
|
|
mPushback = mLocalPushback;
|
|
|
|
|
mPushbackSize = NS_ARRAY_LENGTH(mLocalPushback);
|
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef CSS_REPORT_PARSE_ERRORS
|
|
|
|
|
#define TAB_STOP_WIDTH 8
|
|
|
|
|
#endif
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
PRBool
|
|
|
|
|
nsCSSScanner::EnsureData()
|
2007-08-23 16:01:52 -07:00
|
|
|
|
{
|
|
|
|
|
if (mOffset < mCount)
|
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
if (!mInputStream)
|
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
|
|
mOffset = 0;
|
|
|
|
|
nsresult rv = mInputStream->Read(mBuffer, CSS_BUFFER_SIZE, &mCount);
|
|
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
|
mCount = 0;
|
|
|
|
|
SetLowLevelError(rv);
|
|
|
|
|
return PR_FALSE;
|
2007-08-23 16:01:52 -07:00
|
|
|
|
}
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
return mCount > 0;
|
2007-08-23 16:01:52 -07:00
|
|
|
|
}
|
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
|
// Returns -1 on error or eof
|
2008-09-09 21:38:14 -07:00
|
|
|
|
PRInt32
|
|
|
|
|
nsCSSScanner::Read()
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
PRInt32 rv;
|
|
|
|
|
if (0 < mPushbackCount) {
|
|
|
|
|
rv = PRInt32(mPushback[--mPushbackCount]);
|
|
|
|
|
} else {
|
2008-09-09 21:38:14 -07:00
|
|
|
|
if (mOffset == mCount && !EnsureData()) {
|
2007-08-23 16:01:52 -07:00
|
|
|
|
return -1;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
rv = PRInt32(mReadPointer[mOffset++]);
|
2007-08-23 16:01:52 -07:00
|
|
|
|
// There are four types of newlines in CSS: "\r", "\n", "\r\n", and "\f".
|
|
|
|
|
// To simplify dealing with newlines, they are all normalized to "\n" here
|
|
|
|
|
if (rv == '\r') {
|
2008-09-09 21:38:14 -07:00
|
|
|
|
if (EnsureData() && mReadPointer[mOffset] == '\n') {
|
2007-08-23 16:01:52 -07:00
|
|
|
|
mOffset++;
|
|
|
|
|
}
|
|
|
|
|
rv = '\n';
|
|
|
|
|
} else if (rv == '\f') {
|
|
|
|
|
rv = '\n';
|
|
|
|
|
}
|
|
|
|
|
if (rv == '\n') {
|
2007-03-22 10:30:00 -07:00
|
|
|
|
// 0 is a magical line number meaning that we don't know (i.e., script)
|
|
|
|
|
if (mLineNumber != 0)
|
|
|
|
|
++mLineNumber;
|
|
|
|
|
#ifdef CSS_REPORT_PARSE_ERRORS
|
|
|
|
|
mColNumber = 0;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
#ifdef CSS_REPORT_PARSE_ERRORS
|
|
|
|
|
else if (rv == '\t') {
|
|
|
|
|
mColNumber = ((mColNumber - 1 + TAB_STOP_WIDTH) / TAB_STOP_WIDTH)
|
|
|
|
|
* TAB_STOP_WIDTH;
|
|
|
|
|
} else if (rv != '\n') {
|
|
|
|
|
mColNumber++;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
//printf("Read => %x\n", rv);
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
PRInt32
|
|
|
|
|
nsCSSScanner::Peek()
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
if (0 == mPushbackCount) {
|
2008-09-09 21:38:14 -07:00
|
|
|
|
PRInt32 ch = Read();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
if (ch < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
mPushback[0] = PRUnichar(ch);
|
|
|
|
|
mPushbackCount++;
|
|
|
|
|
}
|
|
|
|
|
//printf("Peek => %x\n", mLookAhead);
|
|
|
|
|
return PRInt32(mPushback[mPushbackCount - 1]);
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
void
|
|
|
|
|
nsCSSScanner::Pushback(PRUnichar aChar)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
if (mPushbackCount == mPushbackSize) { // grow buffer
|
|
|
|
|
PRUnichar* newPushback = new PRUnichar[mPushbackSize + 4];
|
|
|
|
|
if (nsnull == newPushback) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mPushbackSize += 4;
|
|
|
|
|
memcpy(newPushback, mPushback, sizeof(PRUnichar) * mPushbackCount);
|
|
|
|
|
if (mPushback != mLocalPushback) {
|
|
|
|
|
delete [] mPushback;
|
|
|
|
|
}
|
|
|
|
|
mPushback = newPushback;
|
|
|
|
|
}
|
|
|
|
|
mPushback[mPushbackCount++] = aChar;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
PRBool
|
|
|
|
|
nsCSSScanner::LookAhead(PRUnichar aChar)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
2008-09-09 21:38:14 -07:00
|
|
|
|
PRInt32 ch = Read();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
if (ch < 0) {
|
|
|
|
|
return PR_FALSE;
|
|
|
|
|
}
|
|
|
|
|
if (ch == aChar) {
|
|
|
|
|
return PR_TRUE;
|
|
|
|
|
}
|
2007-08-21 11:29:50 -07:00
|
|
|
|
Pushback(ch);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
return PR_FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-08 23:46:26 -07:00
|
|
|
|
void
|
2008-09-09 21:38:14 -07:00
|
|
|
|
nsCSSScanner::EatWhiteSpace()
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
for (;;) {
|
2008-09-09 21:38:14 -07:00
|
|
|
|
PRInt32 ch = Read();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
if (ch < 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2009-04-08 23:46:26 -07:00
|
|
|
|
if ((ch != ' ') && (ch != '\n') && (ch != '\t')) {
|
|
|
|
|
Pushback(ch);
|
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
PRBool
|
|
|
|
|
nsCSSScanner::Next(nsCSSToken& aToken)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
2009-01-16 19:44:21 -08:00
|
|
|
|
for (;;) { // Infinite loop so we can restart after comments.
|
|
|
|
|
PRInt32 ch = Read();
|
|
|
|
|
if (ch < 0) {
|
|
|
|
|
return PR_FALSE;
|
2008-07-11 14:02:16 -07:00
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
2009-08-20 14:52:47 -07:00
|
|
|
|
// UNICODE-RANGE
|
|
|
|
|
if ((ch == 'u' || ch == 'U') && Peek() == '+')
|
|
|
|
|
return ParseURange(ch, aToken);
|
|
|
|
|
|
2009-01-16 19:44:21 -08:00
|
|
|
|
// IDENT
|
|
|
|
|
if (StartsIdent(ch, Peek()))
|
|
|
|
|
return ParseIdent(ch, aToken);
|
|
|
|
|
|
|
|
|
|
// AT_KEYWORD
|
|
|
|
|
if (ch == '@') {
|
|
|
|
|
PRInt32 nextChar = Read();
|
|
|
|
|
if (nextChar >= 0) {
|
|
|
|
|
PRInt32 followingChar = Peek();
|
|
|
|
|
Pushback(nextChar);
|
|
|
|
|
if (StartsIdent(nextChar, followingChar))
|
|
|
|
|
return ParseAtKeyword(ch, aToken);
|
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
2009-01-16 19:44:21 -08:00
|
|
|
|
|
|
|
|
|
// NUMBER or DIM
|
|
|
|
|
if ((ch == '.') || (ch == '+') || (ch == '-')) {
|
|
|
|
|
PRInt32 nextChar = Peek();
|
|
|
|
|
if (IsDigit(nextChar)) {
|
2008-09-09 21:38:14 -07:00
|
|
|
|
return ParseNumber(ch, aToken);
|
2009-01-16 19:44:21 -08:00
|
|
|
|
}
|
|
|
|
|
else if (('.' == nextChar) && ('.' != ch)) {
|
|
|
|
|
nextChar = Read();
|
|
|
|
|
PRInt32 followingChar = Peek();
|
|
|
|
|
Pushback(nextChar);
|
|
|
|
|
if (IsDigit(followingChar))
|
|
|
|
|
return ParseNumber(ch, aToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (IsDigit(ch)) {
|
|
|
|
|
return ParseNumber(ch, aToken);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
2009-01-16 19:44:21 -08:00
|
|
|
|
// ID
|
|
|
|
|
if (ch == '#') {
|
|
|
|
|
return ParseRef(ch, aToken);
|
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
2009-01-16 19:44:21 -08:00
|
|
|
|
// STRING
|
|
|
|
|
if ((ch == '"') || (ch == '\'')) {
|
|
|
|
|
return ParseString(ch, aToken);
|
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
2009-01-16 19:44:21 -08:00
|
|
|
|
// WS
|
|
|
|
|
if (IsWhitespace(ch)) {
|
|
|
|
|
aToken.mType = eCSSToken_WhiteSpace;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
aToken.mIdent.Assign(PRUnichar(ch));
|
2009-04-08 23:46:26 -07:00
|
|
|
|
EatWhiteSpace();
|
2009-01-16 19:44:21 -08:00
|
|
|
|
return PR_TRUE;
|
|
|
|
|
}
|
|
|
|
|
if (ch == '/') {
|
|
|
|
|
PRInt32 nextChar = Peek();
|
|
|
|
|
if (nextChar == '*') {
|
|
|
|
|
(void) Read();
|
|
|
|
|
#if 0
|
|
|
|
|
// If we change our storage data structures such that comments are
|
|
|
|
|
// stored (for Editor), we should reenable this code, condition it
|
|
|
|
|
// on being in editor mode, and apply glazou's patch from bug
|
|
|
|
|
// 60290.
|
|
|
|
|
aToken.mIdent.SetCapacity(2);
|
|
|
|
|
aToken.mIdent.Assign(PRUnichar(ch));
|
|
|
|
|
aToken.mIdent.Append(PRUnichar(nextChar));
|
|
|
|
|
return ParseCComment(aToken);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
#endif
|
2009-01-16 19:44:21 -08:00
|
|
|
|
if (!SkipCComment()) {
|
|
|
|
|
return PR_FALSE;
|
|
|
|
|
}
|
|
|
|
|
continue; // start again at the beginning
|
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
2009-01-16 19:44:21 -08:00
|
|
|
|
if (ch == '<') { // consume HTML comment tags
|
|
|
|
|
if (LookAhead('!')) {
|
2008-09-09 21:38:14 -07:00
|
|
|
|
if (LookAhead('-')) {
|
2009-01-16 19:44:21 -08:00
|
|
|
|
if (LookAhead('-')) {
|
|
|
|
|
aToken.mType = eCSSToken_HTMLComment;
|
|
|
|
|
aToken.mIdent.AssignLiteral("<!--");
|
|
|
|
|
return PR_TRUE;
|
|
|
|
|
}
|
|
|
|
|
Pushback('-');
|
|
|
|
|
}
|
|
|
|
|
Pushback('!');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (ch == '-') { // check for HTML comment end
|
|
|
|
|
if (LookAhead('-')) {
|
|
|
|
|
if (LookAhead('>')) {
|
2007-03-22 10:30:00 -07:00
|
|
|
|
aToken.mType = eCSSToken_HTMLComment;
|
2009-01-16 19:44:21 -08:00
|
|
|
|
aToken.mIdent.AssignLiteral("-->");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
return PR_TRUE;
|
|
|
|
|
}
|
|
|
|
|
Pushback('-');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-16 19:44:21 -08:00
|
|
|
|
// INCLUDES ("~=") and DASHMATCH ("|=")
|
|
|
|
|
if (( ch == '|' ) || ( ch == '~' ) || ( ch == '^' ) ||
|
|
|
|
|
( ch == '$' ) || ( ch == '*' )) {
|
|
|
|
|
PRInt32 nextChar = Read();
|
|
|
|
|
if ( nextChar == '=' ) {
|
|
|
|
|
if (ch == '~') {
|
|
|
|
|
aToken.mType = eCSSToken_Includes;
|
|
|
|
|
}
|
|
|
|
|
else if (ch == '|') {
|
|
|
|
|
aToken.mType = eCSSToken_Dashmatch;
|
|
|
|
|
}
|
|
|
|
|
else if (ch == '^') {
|
|
|
|
|
aToken.mType = eCSSToken_Beginsmatch;
|
|
|
|
|
}
|
|
|
|
|
else if (ch == '$') {
|
|
|
|
|
aToken.mType = eCSSToken_Endsmatch;
|
|
|
|
|
}
|
|
|
|
|
else if (ch == '*') {
|
|
|
|
|
aToken.mType = eCSSToken_Containsmatch;
|
|
|
|
|
}
|
|
|
|
|
return PR_TRUE;
|
|
|
|
|
} else if (nextChar >= 0) {
|
|
|
|
|
Pushback(nextChar);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2009-01-16 19:44:21 -08:00
|
|
|
|
aToken.mType = eCSSToken_Symbol;
|
|
|
|
|
aToken.mSymbol = ch;
|
|
|
|
|
return PR_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
PRBool
|
|
|
|
|
nsCSSScanner::NextURL(nsCSSToken& aToken)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
2008-09-09 21:38:14 -07:00
|
|
|
|
PRInt32 ch = Read();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
if (ch < 0) {
|
|
|
|
|
return PR_FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-19 20:39:22 -07:00
|
|
|
|
// STRING
|
|
|
|
|
if ((ch == '"') || (ch == '\'')) {
|
2008-09-09 21:38:14 -07:00
|
|
|
|
return ParseString(ch, aToken);
|
2007-08-19 20:39:22 -07:00
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
2007-08-19 20:39:22 -07:00
|
|
|
|
// WS
|
2007-08-25 19:20:27 -07:00
|
|
|
|
if (IsWhitespace(ch)) {
|
2007-08-19 20:39:22 -07:00
|
|
|
|
aToken.mType = eCSSToken_WhiteSpace;
|
|
|
|
|
aToken.mIdent.Assign(PRUnichar(ch));
|
2009-04-08 23:46:26 -07:00
|
|
|
|
EatWhiteSpace();
|
2007-08-19 20:39:22 -07:00
|
|
|
|
return PR_TRUE;
|
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
2007-08-19 20:39:22 -07:00
|
|
|
|
// Process a url lexical token. A CSS1 url token can contain
|
|
|
|
|
// characters beyond identifier characters (e.g. '/', ':', etc.)
|
|
|
|
|
// Because of this the normal rules for tokenizing the input don't
|
|
|
|
|
// apply very well. To simplify the parser and relax some of the
|
|
|
|
|
// requirements on the scanner we parse url's here. If we find a
|
|
|
|
|
// malformed URL then we emit a token of type "InvalidURL" so that
|
2009-08-05 17:45:49 -07:00
|
|
|
|
// the CSS1 parser can ignore the invalid input. The parser must
|
|
|
|
|
// treat an InvalidURL token like a Function token, and process
|
|
|
|
|
// tokens until a matching parenthesis.
|
2007-08-19 20:39:22 -07:00
|
|
|
|
|
|
|
|
|
aToken.mType = eCSSToken_InvalidURL;
|
|
|
|
|
nsString& ident = aToken.mIdent;
|
|
|
|
|
ident.SetLength(0);
|
|
|
|
|
|
2009-08-05 17:45:49 -07:00
|
|
|
|
Pushback(ch);
|
|
|
|
|
|
|
|
|
|
// start of a non-quoted url (which may be empty)
|
|
|
|
|
PRBool ok = PR_TRUE;
|
|
|
|
|
for (;;) {
|
|
|
|
|
ch = Read();
|
|
|
|
|
if (ch < 0) break;
|
|
|
|
|
if (ch == CSS_ESCAPE) {
|
|
|
|
|
ParseAndAppendEscape(ident);
|
|
|
|
|
} else if ((ch == '"') || (ch == '\'') || (ch == '(')) {
|
|
|
|
|
// This is an invalid URL spec
|
|
|
|
|
ok = PR_FALSE;
|
|
|
|
|
Pushback(ch); // push it back so the parser can match tokens and
|
|
|
|
|
// then closing parenthesis
|
|
|
|
|
break;
|
|
|
|
|
} else if (IsWhitespace(ch)) {
|
|
|
|
|
// Whitespace is allowed at the end of the URL
|
2009-04-08 23:46:26 -07:00
|
|
|
|
EatWhiteSpace();
|
2008-09-09 21:38:14 -07:00
|
|
|
|
if (LookAhead(')')) {
|
2009-08-05 17:45:49 -07:00
|
|
|
|
Pushback(')'); // leave the closing symbol
|
|
|
|
|
// done!
|
2007-08-19 20:39:22 -07:00
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
2009-08-05 17:45:49 -07:00
|
|
|
|
// Whitespace is followed by something other than a
|
|
|
|
|
// ")". This is an invalid url spec.
|
|
|
|
|
ok = PR_FALSE;
|
|
|
|
|
break;
|
|
|
|
|
} else if (ch == ')') {
|
|
|
|
|
Pushback(ch);
|
|
|
|
|
// All done
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
// A regular url character.
|
|
|
|
|
ident.Append(PRUnichar(ch));
|
2007-08-19 20:39:22 -07:00
|
|
|
|
}
|
2009-08-05 17:45:49 -07:00
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
2009-08-05 17:45:49 -07:00
|
|
|
|
// If the result of the above scanning is ok then change the token
|
|
|
|
|
// type to a useful one.
|
|
|
|
|
if (ok) {
|
|
|
|
|
aToken.mType = eCSSToken_URL;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
return PR_TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2008-09-09 21:38:14 -07:00
|
|
|
|
nsCSSScanner::ParseAndAppendEscape(nsString& aOutput)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
2008-09-09 21:38:14 -07:00
|
|
|
|
PRInt32 ch = Peek();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
if (ch < 0) {
|
|
|
|
|
aOutput.Append(CSS_ESCAPE);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2007-08-25 19:20:27 -07:00
|
|
|
|
if (IsHexDigit(ch)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
|
PRInt32 rv = 0;
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < 6; i++) { // up to six digits
|
2008-09-09 21:38:14 -07:00
|
|
|
|
ch = Read();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
if (ch < 0) {
|
|
|
|
|
// Whoops: error or premature eof
|
|
|
|
|
break;
|
|
|
|
|
}
|
2007-08-25 19:20:27 -07:00
|
|
|
|
if (!IsHexDigit(ch) && !IsWhitespace(ch)) {
|
2007-08-21 11:29:50 -07:00
|
|
|
|
Pushback(ch);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
break;
|
2007-08-25 19:20:27 -07:00
|
|
|
|
} else if (IsHexDigit(ch)) {
|
2009-08-20 14:52:47 -07:00
|
|
|
|
rv = rv * 16 + HexDigitValue(ch);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
} else {
|
2007-08-25 19:20:27 -07:00
|
|
|
|
NS_ASSERTION(IsWhitespace(ch), "bad control flow");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
// single space ends escape
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (6 == i) { // look for trailing whitespace and eat it
|
2008-09-09 21:38:14 -07:00
|
|
|
|
ch = Peek();
|
2007-08-25 19:20:27 -07:00
|
|
|
|
if (IsWhitespace(ch)) {
|
2008-10-23 09:29:24 -07:00
|
|
|
|
(void) Read();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
NS_ASSERTION(rv >= 0, "How did rv become negative?");
|
2008-10-23 09:29:24 -07:00
|
|
|
|
// "[at most six hexadecimal digits following a backslash] stand
|
|
|
|
|
// for the ISO 10646 character with that number, which must not be
|
|
|
|
|
// zero. (It is undefined in CSS 2.1 what happens if a style sheet
|
|
|
|
|
// does contain a character with Unicode codepoint zero.)"
|
|
|
|
|
// -- CSS2.1 section 4.1.3
|
|
|
|
|
//
|
|
|
|
|
// Silently deleting \0 opens a content-filtration loophole (see
|
|
|
|
|
// bug 228856), so what we do instead is pretend the "cancels the
|
|
|
|
|
// meaning of special characters" rule applied.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
if (rv > 0) {
|
|
|
|
|
AppendUCS4ToUTF16(ENSURE_VALID_CHAR(rv), aOutput);
|
2008-10-23 09:29:24 -07:00
|
|
|
|
} else {
|
|
|
|
|
while (i--)
|
|
|
|
|
aOutput.Append('0');
|
|
|
|
|
if (IsWhitespace(ch))
|
|
|
|
|
Pushback(ch);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
return;
|
2009-04-08 23:46:26 -07:00
|
|
|
|
}
|
|
|
|
|
// "Any character except a hexidecimal digit can be escaped to
|
|
|
|
|
// remove its special meaning by putting a backslash in front"
|
|
|
|
|
// -- CSS1 spec section 7.1
|
|
|
|
|
ch = Read(); // Consume the escaped character
|
|
|
|
|
if ((ch > 0) && (ch != '\n')) {
|
|
|
|
|
aOutput.Append(ch);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gather up the characters in an identifier. The identfier was
|
|
|
|
|
* started by "aChar" which will be appended to aIdent. The result
|
|
|
|
|
* will be aIdent with all of the identifier characters appended
|
|
|
|
|
* until the first non-identifier character is seen. The termination
|
|
|
|
|
* character is unread for the future re-reading.
|
|
|
|
|
*/
|
2008-09-09 21:38:14 -07:00
|
|
|
|
PRBool
|
|
|
|
|
nsCSSScanner::GatherIdent(PRInt32 aChar, nsString& aIdent)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
if (aChar == CSS_ESCAPE) {
|
2008-09-09 21:38:14 -07:00
|
|
|
|
ParseAndAppendEscape(aIdent);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
else if (0 < aChar) {
|
|
|
|
|
aIdent.Append(aChar);
|
|
|
|
|
}
|
|
|
|
|
for (;;) {
|
2008-02-21 17:37:04 -08:00
|
|
|
|
// If nothing in pushback, first try to get as much as possible in one go
|
2008-09-09 21:38:14 -07:00
|
|
|
|
if (!mPushbackCount && EnsureData()) {
|
2008-02-21 17:37:04 -08:00
|
|
|
|
// See how much we can consume and append in one go
|
|
|
|
|
PRUint32 n = mOffset;
|
|
|
|
|
// Count number of Ident characters that can be processed
|
|
|
|
|
while (n < mCount && IsIdent(mReadPointer[n])) {
|
|
|
|
|
++n;
|
|
|
|
|
}
|
|
|
|
|
// Add to the token what we have so far
|
|
|
|
|
if (n > mOffset) {
|
|
|
|
|
#ifdef CSS_REPORT_PARSE_ERRORS
|
|
|
|
|
mColNumber += n - mOffset;
|
|
|
|
|
#endif
|
|
|
|
|
aIdent.Append(&mReadPointer[mOffset], n - mOffset);
|
|
|
|
|
mOffset = n;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
aChar = Read();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
if (aChar < 0) break;
|
|
|
|
|
if (aChar == CSS_ESCAPE) {
|
2008-09-09 21:38:14 -07:00
|
|
|
|
ParseAndAppendEscape(aIdent);
|
2007-08-25 19:20:27 -07:00
|
|
|
|
} else if (IsIdent(aChar)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
|
aIdent.Append(PRUnichar(aChar));
|
|
|
|
|
} else {
|
2007-08-21 11:29:50 -07:00
|
|
|
|
Pushback(aChar);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return PR_TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
PRBool
|
|
|
|
|
nsCSSScanner::ParseRef(PRInt32 aChar, nsCSSToken& aToken)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
aToken.mIdent.SetLength(0);
|
|
|
|
|
aToken.mType = eCSSToken_Ref;
|
2008-09-09 21:38:14 -07:00
|
|
|
|
PRInt32 ch = Read();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
if (ch < 0) {
|
|
|
|
|
return PR_FALSE;
|
|
|
|
|
}
|
2007-08-25 19:20:27 -07:00
|
|
|
|
if (IsIdent(ch) || ch == CSS_ESCAPE) {
|
2007-03-22 10:30:00 -07:00
|
|
|
|
// First char after the '#' is a valid ident char (or an escape),
|
|
|
|
|
// so it makes sense to keep going
|
2008-09-09 21:38:14 -07:00
|
|
|
|
if (StartsIdent(ch, Peek())) {
|
2007-03-22 10:30:00 -07:00
|
|
|
|
aToken.mType = eCSSToken_ID;
|
|
|
|
|
}
|
2008-09-09 21:38:14 -07:00
|
|
|
|
return GatherIdent(ch, aToken.mIdent);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No ident chars after the '#'. Just unread |ch| and get out of here.
|
2007-08-21 11:29:50 -07:00
|
|
|
|
Pushback(ch);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
return PR_TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
PRBool
|
|
|
|
|
nsCSSScanner::ParseIdent(PRInt32 aChar, nsCSSToken& aToken)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
nsString& ident = aToken.mIdent;
|
|
|
|
|
ident.SetLength(0);
|
2008-09-09 21:38:14 -07:00
|
|
|
|
if (!GatherIdent(aChar, ident)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
|
return PR_FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsCSSTokenType tokenType = eCSSToken_Ident;
|
|
|
|
|
// look for functions (ie: "ident(")
|
2010-01-27 16:20:04 -08:00
|
|
|
|
if (Peek() == PRUnichar('(')) {
|
|
|
|
|
Read();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
tokenType = eCSSToken_Function;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
aToken.mType = tokenType;
|
|
|
|
|
return PR_TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
PRBool
|
|
|
|
|
nsCSSScanner::ParseAtKeyword(PRInt32 aChar, nsCSSToken& aToken)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
aToken.mIdent.SetLength(0);
|
|
|
|
|
aToken.mType = eCSSToken_AtKeyword;
|
2008-09-09 21:38:14 -07:00
|
|
|
|
return GatherIdent(0, aToken.mIdent);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
PRBool
|
|
|
|
|
nsCSSScanner::ParseNumber(PRInt32 c, nsCSSToken& aToken)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
2009-07-09 18:44:20 -07:00
|
|
|
|
NS_PRECONDITION(c == '.' || c == '+' || c == '-' || IsDigit(c),
|
|
|
|
|
"Why did we get called?");
|
2008-06-02 20:17:35 -07:00
|
|
|
|
aToken.mHasSign = (c == '+' || c == '-');
|
2009-07-09 18:44:20 -07:00
|
|
|
|
|
|
|
|
|
// Our sign.
|
|
|
|
|
PRInt32 sign = c == '-' ? -1 : 1;
|
|
|
|
|
// Absolute value of the integer part of the mantissa. This is a double so
|
|
|
|
|
// we don't run into overflow issues for consumers that only care about our
|
|
|
|
|
// floating-point value while still being able to express the full PRInt32
|
|
|
|
|
// range for consumers who want integers.
|
|
|
|
|
double intPart = 0;
|
|
|
|
|
// Fractional part of the mantissa. This is a double so that when we convert
|
|
|
|
|
// to float at the end we'll end up rounding to nearest float instead of
|
|
|
|
|
// truncating down (as we would if fracPart were a float and we just
|
|
|
|
|
// effectively lost the last several digits).
|
|
|
|
|
double fracPart = 0;
|
|
|
|
|
// Absolute value of the power of 10 that we should multiply by (only
|
|
|
|
|
// relevant for numbers in scientific notation). Has to be a signed integer,
|
|
|
|
|
// because multiplication of signed by unsigned converts the unsigned to
|
|
|
|
|
// signed, so if we plan to actually multiply by expSign...
|
|
|
|
|
PRInt32 exponent = 0;
|
|
|
|
|
// Sign of the exponent.
|
|
|
|
|
PRInt32 expSign = 1;
|
2009-07-22 18:35:07 -07:00
|
|
|
|
|
|
|
|
|
if (aToken.mHasSign) {
|
|
|
|
|
NS_ASSERTION(c != '.', "How did that happen?");
|
|
|
|
|
c = Read();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
2009-07-22 18:35:07 -07:00
|
|
|
|
PRBool gotDot = (c == '.');
|
|
|
|
|
|
|
|
|
|
if (!gotDot) {
|
|
|
|
|
// Parse the integer part of the mantisssa
|
|
|
|
|
NS_ASSERTION(IsDigit(c), "Why did we get called?");
|
|
|
|
|
do {
|
2009-08-20 14:52:47 -07:00
|
|
|
|
intPart = 10*intPart + DecimalDigitValue(c);
|
2009-07-22 18:35:07 -07:00
|
|
|
|
c = Read();
|
|
|
|
|
// The IsDigit check will do the right thing even if Read() returns < 0
|
|
|
|
|
} while (IsDigit(c));
|
|
|
|
|
|
|
|
|
|
gotDot = (c == '.') && IsDigit(Peek());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (gotDot) {
|
|
|
|
|
// Parse the fractional part of the mantissa.
|
2008-09-09 21:38:14 -07:00
|
|
|
|
c = Read();
|
2009-07-22 18:35:07 -07:00
|
|
|
|
NS_ASSERTION(IsDigit(c), "How did we get here?");
|
|
|
|
|
// Power of ten by which we need to divide our next digit
|
|
|
|
|
float divisor = 10;
|
|
|
|
|
do {
|
2009-08-20 14:52:47 -07:00
|
|
|
|
fracPart += DecimalDigitValue(c) / divisor;
|
2009-07-22 18:35:07 -07:00
|
|
|
|
divisor *= 10;
|
|
|
|
|
c = Read();
|
|
|
|
|
// The IsDigit check will do the right thing even if Read() returns < 0
|
|
|
|
|
} while (IsDigit(c));
|
|
|
|
|
}
|
2009-07-09 20:36:57 -07:00
|
|
|
|
|
2009-07-22 18:35:07 -07:00
|
|
|
|
PRBool gotE = PR_FALSE;
|
2009-07-09 20:36:57 -07:00
|
|
|
|
#ifdef MOZ_SVG
|
2009-07-22 18:35:07 -07:00
|
|
|
|
if (IsSVGMode() && (c == 'e' || c == 'E')) {
|
|
|
|
|
PRInt32 nextChar = Peek();
|
|
|
|
|
PRInt32 expSignChar = 0;
|
|
|
|
|
if (nextChar == '-' || nextChar == '+') {
|
|
|
|
|
expSignChar = Read();
|
|
|
|
|
nextChar = Peek();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
2009-07-22 18:35:07 -07:00
|
|
|
|
if (IsDigit(nextChar)) {
|
|
|
|
|
gotE = PR_TRUE;
|
|
|
|
|
if (expSignChar == '-') {
|
|
|
|
|
expSign = -1;
|
2009-07-09 20:36:57 -07:00
|
|
|
|
}
|
2009-07-22 18:35:07 -07:00
|
|
|
|
|
|
|
|
|
c = Read();
|
|
|
|
|
NS_ASSERTION(IsDigit(c), "Peek() must have lied");
|
|
|
|
|
do {
|
2009-08-20 14:52:47 -07:00
|
|
|
|
exponent = 10*exponent + DecimalDigitValue(c);
|
2009-07-22 18:35:07 -07:00
|
|
|
|
c = Read();
|
|
|
|
|
// The IsDigit check will do the right thing even if Read() returns < 0
|
|
|
|
|
} while (IsDigit(c));
|
2009-07-09 18:44:20 -07:00
|
|
|
|
} else {
|
2009-07-22 18:35:07 -07:00
|
|
|
|
if (expSignChar) {
|
|
|
|
|
Pushback(expSignChar);
|
|
|
|
|
}
|
2009-07-09 18:44:20 -07:00
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
2009-07-22 18:35:07 -07:00
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
nsCSSTokenType type = eCSSToken_Number;
|
|
|
|
|
|
2008-06-02 20:17:35 -07:00
|
|
|
|
// Set mIntegerValid for all cases (except %, below) because we need
|
|
|
|
|
// it for the "2n" in :nth-child(2n).
|
2007-03-22 10:30:00 -07:00
|
|
|
|
aToken.mIntegerValid = PR_FALSE;
|
2009-07-09 18:44:20 -07:00
|
|
|
|
|
|
|
|
|
// Time to reassemble our number.
|
|
|
|
|
float value = float(sign * (intPart + fracPart));
|
|
|
|
|
if (gotE) {
|
|
|
|
|
// pow(), not powf(), because at least wince doesn't have the latter.
|
|
|
|
|
// And explicitly cast everything to doubles to avoid issues with
|
|
|
|
|
// overloaded pow() on Windows.
|
|
|
|
|
value *= pow(10.0, double(expSign * exponent));
|
|
|
|
|
} else if (!gotDot) {
|
|
|
|
|
if (intPart > PR_INT32_MAX) {
|
|
|
|
|
// Just clamp it.
|
|
|
|
|
intPart = PR_INT32_MAX;
|
|
|
|
|
}
|
|
|
|
|
aToken.mInteger = PRInt32(sign * intPart);
|
2008-06-02 20:17:35 -07:00
|
|
|
|
aToken.mIntegerValid = PR_TRUE;
|
|
|
|
|
}
|
2009-07-09 18:44:20 -07:00
|
|
|
|
|
|
|
|
|
nsString& ident = aToken.mIdent;
|
|
|
|
|
ident.Truncate();
|
2008-06-02 20:17:35 -07:00
|
|
|
|
|
|
|
|
|
// Look at character that terminated the number
|
2007-03-22 10:30:00 -07:00
|
|
|
|
if (c >= 0) {
|
2008-09-09 21:38:14 -07:00
|
|
|
|
if (StartsIdent(c, Peek())) {
|
|
|
|
|
if (!GatherIdent(c, ident)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
|
return PR_FALSE;
|
|
|
|
|
}
|
|
|
|
|
type = eCSSToken_Dimension;
|
|
|
|
|
} else if ('%' == c) {
|
|
|
|
|
type = eCSSToken_Percentage;
|
|
|
|
|
value = value / 100.0f;
|
2008-06-02 20:17:35 -07:00
|
|
|
|
aToken.mIntegerValid = PR_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
} else {
|
|
|
|
|
// Put back character that stopped numeric scan
|
2007-08-21 11:29:50 -07:00
|
|
|
|
Pushback(c);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
aToken.mNumber = value;
|
|
|
|
|
aToken.mType = type;
|
|
|
|
|
return PR_TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
PRBool
|
|
|
|
|
nsCSSScanner::SkipCComment()
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
for (;;) {
|
2008-09-09 21:38:14 -07:00
|
|
|
|
PRInt32 ch = Read();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
if (ch < 0) break;
|
|
|
|
|
if (ch == '*') {
|
2008-09-09 21:38:14 -07:00
|
|
|
|
if (LookAhead('/')) {
|
2007-03-22 10:30:00 -07:00
|
|
|
|
return PR_TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
REPORT_UNEXPECTED_EOF(PECommentEOF);
|
|
|
|
|
return PR_FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-09 21:38:14 -07:00
|
|
|
|
PRBool
|
|
|
|
|
nsCSSScanner::ParseString(PRInt32 aStop, nsCSSToken& aToken)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
{
|
|
|
|
|
aToken.mIdent.SetLength(0);
|
|
|
|
|
aToken.mType = eCSSToken_String;
|
|
|
|
|
aToken.mSymbol = PRUnichar(aStop); // remember how it's quoted
|
|
|
|
|
for (;;) {
|
2008-02-21 17:37:04 -08:00
|
|
|
|
// If nothing in pushback, first try to get as much as possible in one go
|
2008-09-09 21:38:14 -07:00
|
|
|
|
if (!mPushbackCount && EnsureData()) {
|
2008-02-21 17:37:04 -08:00
|
|
|
|
// See how much we can consume and append in one go
|
|
|
|
|
PRUint32 n = mOffset;
|
|
|
|
|
// Count number of characters that can be processed
|
|
|
|
|
for (;n < mCount; ++n) {
|
|
|
|
|
PRUnichar nextChar = mReadPointer[n];
|
|
|
|
|
if ((nextChar == aStop) || (nextChar == CSS_ESCAPE) ||
|
|
|
|
|
(nextChar == '\n') || (nextChar == '\r') || (nextChar == '\f')) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
#ifdef CSS_REPORT_PARSE_ERRORS
|
2008-02-21 17:37:04 -08:00
|
|
|
|
if (nextChar == '\t') {
|
|
|
|
|
mColNumber = ((mColNumber - 1 + TAB_STOP_WIDTH) / TAB_STOP_WIDTH)
|
|
|
|
|
* TAB_STOP_WIDTH;
|
|
|
|
|
} else {
|
|
|
|
|
++mColNumber;
|
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
#endif
|
2008-02-21 17:37:04 -08:00
|
|
|
|
}
|
|
|
|
|
// Add to the token what we have so far
|
|
|
|
|
if (n > mOffset) {
|
|
|
|
|
aToken.mIdent.Append(&mReadPointer[mOffset], n - mOffset);
|
|
|
|
|
mOffset = n;
|
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
2008-09-09 21:38:14 -07:00
|
|
|
|
PRInt32 ch = Read();
|
2008-02-21 17:37:04 -08:00
|
|
|
|
if (ch < 0 || ch == aStop) {
|
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
}
|
2008-02-21 17:37:04 -08:00
|
|
|
|
if (ch == '\n') {
|
|
|
|
|
aToken.mType = eCSSToken_Error;
|
|
|
|
|
#ifdef CSS_REPORT_PARSE_ERRORS
|
|
|
|
|
ReportUnexpectedToken(aToken, "SEUnterminatedString");
|
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (ch == CSS_ESCAPE) {
|
2008-09-09 21:38:14 -07:00
|
|
|
|
ParseAndAppendEscape(aToken.mIdent);
|
2008-02-21 17:37:04 -08:00
|
|
|
|
} else {
|
2007-03-22 10:30:00 -07:00
|
|
|
|
aToken.mIdent.Append(ch);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return PR_TRUE;
|
|
|
|
|
}
|
2009-08-20 14:52:47 -07:00
|
|
|
|
|
|
|
|
|
// UNICODE-RANGE tokens match the regular expression
|
|
|
|
|
//
|
|
|
|
|
// u\+[0-9a-f?]{1,6}(-[0-9a-f]{1,6})?
|
|
|
|
|
//
|
|
|
|
|
// However, some such tokens are "invalid". There are three valid forms:
|
|
|
|
|
//
|
|
|
|
|
// u+[0-9a-f]{x} 1 <= x <= 6
|
|
|
|
|
// u+[0-9a-f]{x}\?{y} 1 <= x+y <= 6
|
|
|
|
|
// u+[0-9a-f]{x}-[0-9a-f]{y} 1 <= x <= 6, 1 <= y <= 6
|
|
|
|
|
//
|
|
|
|
|
// All unicode-range tokens have their text recorded in mIdent; valid ones
|
|
|
|
|
// are also decoded into mInteger and mInteger2, and mIntegerValid is set.
|
|
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
|
nsCSSScanner::ParseURange(PRInt32 aChar, nsCSSToken& aResult)
|
|
|
|
|
{
|
|
|
|
|
PRInt32 intro2 = Read();
|
|
|
|
|
PRInt32 ch = Peek();
|
|
|
|
|
|
|
|
|
|
// We should only ever be called if these things are true.
|
|
|
|
|
NS_ASSERTION(aChar == 'u' || aChar == 'U',
|
|
|
|
|
"unicode-range called with improper introducer (U)");
|
|
|
|
|
NS_ASSERTION(intro2 == '+',
|
|
|
|
|
"unicode-range called with improper introducer (+)");
|
|
|
|
|
|
|
|
|
|
// If the character immediately after the '+' is not a hex digit or
|
|
|
|
|
// '?', this is not really a unicode-range token; push everything
|
|
|
|
|
// back and scan the U as an ident.
|
|
|
|
|
if (!IsHexDigit(ch) && ch != '?') {
|
|
|
|
|
Pushback(intro2);
|
|
|
|
|
Pushback(aChar);
|
|
|
|
|
return ParseIdent(aChar, aResult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
aResult.mIdent.Truncate();
|
|
|
|
|
aResult.mIdent.Append(aChar);
|
|
|
|
|
aResult.mIdent.Append(intro2);
|
|
|
|
|
|
|
|
|
|
PRBool valid = PR_TRUE;
|
|
|
|
|
PRBool haveQues = PR_FALSE;
|
|
|
|
|
PRUint32 low = 0;
|
|
|
|
|
PRUint32 high = 0;
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
ch = Read();
|
|
|
|
|
i++;
|
|
|
|
|
if (i == 7 || !(IsHexDigit(ch) || ch == '?')) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
aResult.mIdent.Append(ch);
|
|
|
|
|
if (IsHexDigit(ch)) {
|
|
|
|
|
if (haveQues) {
|
|
|
|
|
valid = PR_FALSE; // all question marks should be at the end
|
|
|
|
|
}
|
|
|
|
|
low = low*16 + HexDigitValue(ch);
|
|
|
|
|
high = high*16 + HexDigitValue(ch);
|
|
|
|
|
} else {
|
|
|
|
|
haveQues = PR_TRUE;
|
|
|
|
|
low = low*16 + 0x0;
|
|
|
|
|
high = high*16 + 0xF;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ch == '-' && IsHexDigit(Peek())) {
|
|
|
|
|
if (haveQues) {
|
|
|
|
|
valid = PR_FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
aResult.mIdent.Append(ch);
|
|
|
|
|
high = 0;
|
|
|
|
|
i = 0;
|
|
|
|
|
for (;;) {
|
|
|
|
|
ch = Read();
|
|
|
|
|
i++;
|
|
|
|
|
if (i == 7 || !IsHexDigit(ch)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
aResult.mIdent.Append(ch);
|
|
|
|
|
high = high*16 + HexDigitValue(ch);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Pushback(ch);
|
|
|
|
|
|
|
|
|
|
aResult.mInteger = low;
|
|
|
|
|
aResult.mInteger2 = high;
|
|
|
|
|
aResult.mIntegerValid = valid;
|
|
|
|
|
aResult.mType = eCSSToken_URange;
|
|
|
|
|
return PR_TRUE;
|
|
|
|
|
}
|