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/. */
|
2011-10-11 05:50:08 +00:00
|
|
|
|
|
|
|
|
2006-03-25 05:47:31 +00:00
|
|
|
/* tokenization of CSS style sheets */
|
|
|
|
|
1998-04-13 20:24:54 +00:00
|
|
|
#include "nsCSSScanner.h"
|
2012-11-17 02:53:38 +00:00
|
|
|
#include "nsStyleUtil.h"
|
2013-09-16 01:06:52 +00:00
|
|
|
#include "nsTraceRefcnt.h"
|
2012-11-15 16:36:15 +00:00
|
|
|
#include "mozilla/css/ErrorReporter.h"
|
2012-10-26 13:32:10 +00:00
|
|
|
#include "mozilla/Likely.h"
|
2012-11-15 16:36:15 +00:00
|
|
|
#include "mozilla/Util.h"
|
2013-01-15 12:22:03 +00:00
|
|
|
#include <algorithm>
|
2011-05-25 06:31:59 +00:00
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/* Character class tables and related helper functions. */
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
static const uint8_t IS_HEX_DIGIT = 0x01;
|
2013-02-16 23:27:53 +00:00
|
|
|
static const uint8_t IS_IDSTART = 0x02;
|
|
|
|
static const uint8_t IS_IDCHAR = 0x04;
|
|
|
|
static const uint8_t IS_URL_CHAR = 0x08;
|
|
|
|
static const uint8_t IS_HSPACE = 0x10;
|
|
|
|
static const uint8_t IS_VSPACE = 0x20;
|
|
|
|
static const uint8_t IS_SPACE = IS_HSPACE|IS_VSPACE;
|
2013-02-16 23:27:53 +00:00
|
|
|
static const uint8_t IS_STRING = 0x40;
|
2013-02-16 23:27:53 +00:00
|
|
|
|
|
|
|
#define H IS_HSPACE
|
|
|
|
#define V IS_VSPACE
|
|
|
|
#define I IS_IDCHAR
|
2013-02-16 23:27:53 +00:00
|
|
|
#define J IS_IDSTART
|
|
|
|
#define U IS_URL_CHAR
|
|
|
|
#define S IS_STRING
|
|
|
|
#define X IS_HEX_DIGIT
|
|
|
|
|
|
|
|
#define SH S|H
|
|
|
|
#define SU S|U
|
|
|
|
#define SUI S|U|I
|
|
|
|
#define SUIJ S|U|I|J
|
|
|
|
#define SUIX S|U|I|X
|
|
|
|
#define SUIJX S|U|I|J|X
|
2009-10-15 20:18:21 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
static const uint8_t gLexTable[] = {
|
2013-02-16 23:27:53 +00:00
|
|
|
// 00 01 02 03 04 05 06 07
|
|
|
|
0, S, S, S, S, S, S, S,
|
|
|
|
// 08 TAB LF 0B FF CR 0E 0F
|
|
|
|
S, SH, V, S, V, V, S, S,
|
|
|
|
// 10 11 12 13 14 15 16 17
|
|
|
|
S, S, S, S, S, S, S, S,
|
|
|
|
// 18 19 1A 1B 1C 1D 1E 1F
|
|
|
|
S, S, S, S, S, S, S, S,
|
|
|
|
//SPC ! " # $ % & '
|
|
|
|
SH, SU, 0, SU, SU, SU, SU, 0,
|
|
|
|
// ( ) * + , - . /
|
|
|
|
S, S, SU, SU, SU, SUI, SU, SU,
|
|
|
|
// 0 1 2 3 4 5 6 7
|
|
|
|
SUIX, SUIX, SUIX, SUIX, SUIX, SUIX, SUIX, SUIX,
|
|
|
|
// 8 9 : ; < = > ?
|
|
|
|
SUIX, SUIX, SU, SU, SU, SU, SU, SU,
|
|
|
|
// @ A B C D E F G
|
|
|
|
SU,SUIJX,SUIJX,SUIJX,SUIJX,SUIJX,SUIJX, SUIJ,
|
|
|
|
// H I J K L M N O
|
|
|
|
SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ,
|
|
|
|
// P Q R S T U V W
|
|
|
|
SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ,
|
|
|
|
// X Y Z [ \ ] ^ _
|
|
|
|
SUIJ, SUIJ, SUIJ, SU, J, SU, SU, SUIJ,
|
|
|
|
// ` a b c d e f g
|
|
|
|
SU,SUIJX,SUIJX,SUIJX,SUIJX,SUIJX,SUIJX, SUIJ,
|
|
|
|
// h i j k l m n o
|
|
|
|
SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ,
|
|
|
|
// p q r s t u v w
|
|
|
|
SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ, SUIJ,
|
|
|
|
// x y z { | } ~ 7F
|
|
|
|
SUIJ, SUIJ, SUIJ, SU, SU, SU, SU, S,
|
2009-10-15 20:18:21 +00:00
|
|
|
};
|
|
|
|
|
2013-07-18 17:59:53 +00:00
|
|
|
static_assert(MOZ_ARRAY_LENGTH(gLexTable) == 128,
|
|
|
|
"gLexTable expected to cover all 128 ASCII characters");
|
2011-04-17 21:13:15 +00:00
|
|
|
|
2009-10-15 20:18:21 +00:00
|
|
|
#undef I
|
2013-02-16 23:27:53 +00:00
|
|
|
#undef J
|
2012-05-14 23:01:05 +00:00
|
|
|
#undef U
|
2013-02-16 23:27:53 +00:00
|
|
|
#undef S
|
|
|
|
#undef X
|
|
|
|
#undef SH
|
|
|
|
#undef SU
|
|
|
|
#undef SUI
|
|
|
|
#undef SUIJ
|
|
|
|
#undef SUIX
|
|
|
|
#undef SUIJX
|
1998-04-13 20:24:54 +00:00
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
|
|
|
* True if 'ch' is in character class 'cls', which should be one of
|
|
|
|
* the constants above or some combination of them. All characters
|
|
|
|
* above U+007F are considered to be in 'cls'. EOF is never in 'cls'.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
static inline bool
|
2013-02-16 23:27:53 +00:00
|
|
|
IsOpenCharClass(int32_t ch, uint8_t cls) {
|
|
|
|
return ch >= 0 && (ch >= 128 || (gLexTable[ch] & cls) != 0);
|
2008-09-10 04:38:29 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
|
|
|
* True if 'ch' is in character class 'cls', which should be one of
|
|
|
|
* the constants above or some combination of them. No characters
|
|
|
|
* above U+007F are considered to be in 'cls'. EOF is never in 'cls'.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
static inline bool
|
2013-02-16 23:27:53 +00:00
|
|
|
IsClosedCharClass(int32_t ch, uint8_t cls) {
|
|
|
|
return uint32_t(ch) < 128 && (gLexTable[ch] & cls) != 0;
|
2008-09-10 04:38:29 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
|
|
|
* True if 'ch' is CSS whitespace, i.e. any of the ASCII characters
|
|
|
|
* TAB, LF, FF, CR, or SPC.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
static inline bool
|
2012-08-22 15:56:38 +00:00
|
|
|
IsWhitespace(int32_t ch) {
|
2013-02-16 23:27:53 +00:00
|
|
|
return IsClosedCharClass(ch, IS_SPACE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if 'ch' is horizontal whitespace, i.e. TAB or SPC.
|
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
IsHorzSpace(int32_t ch) {
|
|
|
|
return IsClosedCharClass(ch, IS_HSPACE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if 'ch' is vertical whitespace, i.e. LF, FF, or CR. Vertical
|
|
|
|
* whitespace requires special handling when consumed, see AdvanceLine.
|
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
IsVertSpace(int32_t ch) {
|
|
|
|
return IsClosedCharClass(ch, IS_VSPACE);
|
2008-09-10 04:38:29 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
2013-06-26 22:17:14 +00:00
|
|
|
* True if 'ch' is a character that can appear in the middle of an identifier.
|
|
|
|
* This includes U+0000 since it is handled as U+FFFD, but for purposes of
|
|
|
|
* GatherText it should not be included in IsOpenCharClass.
|
2013-02-16 23:27:53 +00:00
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
static inline bool
|
2013-02-16 23:27:53 +00:00
|
|
|
IsIdentChar(int32_t ch) {
|
2013-06-26 22:17:14 +00:00
|
|
|
return IsOpenCharClass(ch, IS_IDCHAR) || ch == 0;
|
2008-09-10 04:38:29 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
|
|
|
* True if 'ch' is a character that by itself begins an identifier.
|
2013-06-26 22:17:14 +00:00
|
|
|
* This includes U+0000 since it is handled as U+FFFD, but for purposes of
|
|
|
|
* GatherText it should not be included in IsOpenCharClass.
|
2013-02-16 23:27:53 +00:00
|
|
|
* (This is a subset of IsIdentChar.)
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
static inline bool
|
2013-02-16 23:27:53 +00:00
|
|
|
IsIdentStart(int32_t ch) {
|
2013-06-26 22:17:14 +00:00
|
|
|
return IsOpenCharClass(ch, IS_IDSTART) || ch == 0;
|
2008-09-10 04:38:29 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
|
|
|
* True if the two-character sequence aFirstChar+aSecondChar begins an
|
|
|
|
* identifier.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
static inline bool
|
2013-02-16 23:27:53 +00:00
|
|
|
StartsIdent(int32_t aFirstChar, int32_t aSecondChar)
|
|
|
|
{
|
|
|
|
return IsIdentStart(aFirstChar) ||
|
|
|
|
(aFirstChar == '-' && IsIdentStart(aSecondChar));
|
2008-09-10 04:38:29 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
|
|
|
* True if 'ch' is a decimal digit.
|
|
|
|
*/
|
2013-02-16 23:27:53 +00:00
|
|
|
static inline bool
|
|
|
|
IsDigit(int32_t ch) {
|
|
|
|
return (ch >= '0') && (ch <= '9');
|
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
|
|
|
* True if 'ch' is a hexadecimal digit.
|
|
|
|
*/
|
2013-02-16 23:27:53 +00:00
|
|
|
static inline bool
|
|
|
|
IsHexDigit(int32_t ch) {
|
2013-02-16 23:27:53 +00:00
|
|
|
return IsClosedCharClass(ch, IS_HEX_DIGIT);
|
2013-02-16 23:27:53 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
|
|
|
* Assuming that 'ch' is a decimal digit, return its numeric value.
|
|
|
|
*/
|
2012-08-22 15:56:38 +00:00
|
|
|
static inline uint32_t
|
|
|
|
DecimalDigitValue(int32_t ch)
|
2009-08-20 21:52:47 +00:00
|
|
|
{
|
|
|
|
return ch - '0';
|
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
|
|
|
* Assuming that 'ch' is a hexadecimal digit, return its numeric value.
|
|
|
|
*/
|
2012-08-22 15:56:38 +00:00
|
|
|
static inline uint32_t
|
|
|
|
HexDigitValue(int32_t ch)
|
2009-08-20 21:52:47 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
|
|
|
* If 'ch' can be the first character of a two-character match operator
|
|
|
|
* token, return the token type code for that token, otherwise return
|
|
|
|
* eCSSToken_Symbol to indicate that it can't.
|
|
|
|
*/
|
2013-02-16 23:27:53 +00:00
|
|
|
static inline nsCSSTokenType
|
|
|
|
MatchOperatorType(int32_t ch)
|
|
|
|
{
|
|
|
|
switch (ch) {
|
|
|
|
case '~': return eCSSToken_Includes;
|
|
|
|
case '|': return eCSSToken_Dashmatch;
|
|
|
|
case '^': return eCSSToken_Beginsmatch;
|
|
|
|
case '$': return eCSSToken_Endsmatch;
|
|
|
|
case '*': return eCSSToken_Containsmatch;
|
|
|
|
default: return eCSSToken_Symbol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Out-of-line nsCSSToken methods. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append the textual representation of |this| to |aBuffer|.
|
|
|
|
*/
|
2009-08-20 21:52:47 +00:00
|
|
|
void
|
2012-11-15 16:36:15 +00:00
|
|
|
nsCSSToken::AppendToString(nsString& aBuffer) const
|
1999-02-07 21:47:48 +00:00
|
|
|
{
|
|
|
|
switch (mType) {
|
2012-11-16 21:59:38 +00:00
|
|
|
case eCSSToken_Ident:
|
2012-11-17 02:53:38 +00:00
|
|
|
nsStyleUtil::AppendEscapedCSSIdent(mIdent, aBuffer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case eCSSToken_AtKeyword:
|
|
|
|
aBuffer.Append('@');
|
|
|
|
nsStyleUtil::AppendEscapedCSSIdent(mIdent, aBuffer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case eCSSToken_ID:
|
2013-02-16 23:27:53 +00:00
|
|
|
case eCSSToken_Hash:
|
2012-11-17 02:53:38 +00:00
|
|
|
aBuffer.Append('#');
|
|
|
|
nsStyleUtil::AppendEscapedCSSIdent(mIdent, aBuffer);
|
|
|
|
break;
|
|
|
|
|
1999-02-07 21:47:48 +00:00
|
|
|
case eCSSToken_Function:
|
2012-11-17 02:53:38 +00:00
|
|
|
nsStyleUtil::AppendEscapedCSSIdent(mIdent, aBuffer);
|
|
|
|
aBuffer.Append('(');
|
1999-02-07 21:47:48 +00:00
|
|
|
break;
|
2012-11-17 02:53:38 +00:00
|
|
|
|
2011-03-11 17:29:45 +00:00
|
|
|
case eCSSToken_URL:
|
|
|
|
case eCSSToken_Bad_URL:
|
2011-03-11 17:29:45 +00:00
|
|
|
aBuffer.AppendLiteral("url(");
|
2011-03-11 17:29:45 +00:00
|
|
|
if (mSymbol != PRUnichar(0)) {
|
2012-11-17 02:53:38 +00:00
|
|
|
nsStyleUtil::AppendEscapedCSSString(mIdent, aBuffer, mSymbol);
|
|
|
|
} else {
|
|
|
|
aBuffer.Append(mIdent);
|
2011-03-11 17:29:45 +00:00
|
|
|
}
|
|
|
|
if (mType == eCSSToken_URL) {
|
|
|
|
aBuffer.Append(PRUnichar(')'));
|
|
|
|
}
|
|
|
|
break;
|
2012-11-17 02:53:38 +00:00
|
|
|
|
1999-02-07 21:47:48 +00:00
|
|
|
case eCSSToken_Number:
|
|
|
|
if (mIntegerValid) {
|
2000-04-15 20:15:37 +00:00
|
|
|
aBuffer.AppendInt(mInteger, 10);
|
2012-11-17 02:53:38 +00:00
|
|
|
} else {
|
2000-04-15 20:15:37 +00:00
|
|
|
aBuffer.AppendFloat(mNumber);
|
1999-02-07 21:47:48 +00:00
|
|
|
}
|
|
|
|
break;
|
2012-11-17 02:53:38 +00:00
|
|
|
|
1999-02-07 21:47:48 +00:00
|
|
|
case eCSSToken_Percentage:
|
2003-03-18 05:43:12 +00:00
|
|
|
aBuffer.AppendFloat(mNumber * 100.0f);
|
2011-05-19 22:44:14 +00:00
|
|
|
aBuffer.Append(PRUnichar('%'));
|
1999-02-07 21:47:48 +00:00
|
|
|
break;
|
2012-11-17 02:53:38 +00:00
|
|
|
|
1999-02-07 21:47:48 +00:00
|
|
|
case eCSSToken_Dimension:
|
|
|
|
if (mIntegerValid) {
|
2000-04-15 20:15:37 +00:00
|
|
|
aBuffer.AppendInt(mInteger, 10);
|
2012-11-17 02:53:38 +00:00
|
|
|
} else {
|
2000-04-15 20:15:37 +00:00
|
|
|
aBuffer.AppendFloat(mNumber);
|
1999-02-07 21:47:48 +00:00
|
|
|
}
|
2012-11-17 02:53:38 +00:00
|
|
|
nsStyleUtil::AppendEscapedCSSIdent(mIdent, aBuffer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case eCSSToken_Bad_String:
|
|
|
|
nsStyleUtil::AppendEscapedCSSString(mIdent, aBuffer, mSymbol);
|
|
|
|
// remove the trailing quote character
|
|
|
|
aBuffer.Truncate(aBuffer.Length() - 1);
|
1999-02-07 21:47:48 +00:00
|
|
|
break;
|
2012-11-17 02:53:38 +00:00
|
|
|
|
1999-02-07 21:47:48 +00:00
|
|
|
case eCSSToken_String:
|
2012-11-17 02:53:38 +00:00
|
|
|
nsStyleUtil::AppendEscapedCSSString(mIdent, aBuffer, mSymbol);
|
|
|
|
break;
|
|
|
|
|
1999-02-07 21:47:48 +00:00
|
|
|
case eCSSToken_Symbol:
|
|
|
|
aBuffer.Append(mSymbol);
|
|
|
|
break;
|
2012-11-17 02:53:38 +00:00
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
case eCSSToken_Whitespace:
|
2012-11-17 02:53:38 +00:00
|
|
|
aBuffer.Append(' ');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case eCSSToken_HTMLComment:
|
|
|
|
case eCSSToken_URange:
|
1999-02-07 21:47:48 +00:00
|
|
|
aBuffer.Append(mIdent);
|
|
|
|
break;
|
2012-11-17 02:53:38 +00:00
|
|
|
|
2000-04-27 00:12:25 +00:00
|
|
|
case eCSSToken_Includes:
|
2004-06-17 00:13:25 +00:00
|
|
|
aBuffer.AppendLiteral("~=");
|
2000-04-27 00:12:25 +00:00
|
|
|
break;
|
|
|
|
case eCSSToken_Dashmatch:
|
2004-06-17 00:13:25 +00:00
|
|
|
aBuffer.AppendLiteral("|=");
|
2000-04-27 00:12:25 +00:00
|
|
|
break;
|
2008-07-11 22:49:46 +00:00
|
|
|
case eCSSToken_Beginsmatch:
|
|
|
|
aBuffer.AppendLiteral("^=");
|
|
|
|
break;
|
|
|
|
case eCSSToken_Endsmatch:
|
|
|
|
aBuffer.AppendLiteral("$=");
|
|
|
|
break;
|
|
|
|
case eCSSToken_Containsmatch:
|
|
|
|
aBuffer.AppendLiteral("*=");
|
|
|
|
break;
|
2012-11-17 02:53:38 +00:00
|
|
|
|
1999-02-07 21:47:48 +00:00
|
|
|
default:
|
|
|
|
NS_ERROR("invalid token type");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/* nsCSSScanner methods. */
|
|
|
|
|
2012-11-15 16:36:15 +00:00
|
|
|
nsCSSScanner::nsCSSScanner(const nsAString& aBuffer, uint32_t aLineNumber)
|
2013-02-16 23:27:53 +00:00
|
|
|
: mBuffer(aBuffer.BeginReading())
|
2012-11-15 16:36:15 +00:00
|
|
|
, mOffset(0)
|
|
|
|
, mCount(aBuffer.Length())
|
|
|
|
, mLineNumber(aLineNumber)
|
|
|
|
, mLineOffset(0)
|
2013-01-11 17:27:43 +00:00
|
|
|
, mTokenLineNumber(aLineNumber)
|
|
|
|
, mTokenLineOffset(0)
|
|
|
|
, mTokenOffset(0)
|
2012-11-15 16:36:15 +00:00
|
|
|
, mRecordStartOffset(0)
|
2013-12-07 01:25:07 +00:00
|
|
|
, mEOFCharacters(eEOFCharacters_None)
|
2012-11-15 16:36:15 +00:00
|
|
|
, mReporter(nullptr)
|
2011-10-17 14:59:28 +00:00
|
|
|
, mSVGMode(false)
|
2012-11-15 16:36:15 +00:00
|
|
|
, mRecording(false)
|
2013-06-28 00:03:33 +00:00
|
|
|
, mSeenBadToken(false)
|
1998-04-13 20:24:54 +00:00
|
|
|
{
|
1999-10-08 20:41:19 +00:00
|
|
|
MOZ_COUNT_CTOR(nsCSSScanner);
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCSSScanner::~nsCSSScanner()
|
|
|
|
{
|
1999-10-08 20:41:19 +00:00
|
|
|
MOZ_COUNT_DTOR(nsCSSScanner);
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
void
|
|
|
|
nsCSSScanner::StartRecording()
|
|
|
|
{
|
2013-02-16 23:27:53 +00:00
|
|
|
MOZ_ASSERT(!mRecording, "already started recording");
|
2013-02-16 23:27:53 +00:00
|
|
|
mRecording = true;
|
2013-02-16 23:27:53 +00:00
|
|
|
mRecordStartOffset = mOffset;
|
2013-02-16 23:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCSSScanner::StopRecording()
|
|
|
|
{
|
2013-02-16 23:27:53 +00:00
|
|
|
MOZ_ASSERT(mRecording, "haven't started recording");
|
2013-02-16 23:27:53 +00:00
|
|
|
mRecording = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCSSScanner::StopRecording(nsString& aBuffer)
|
|
|
|
{
|
2013-02-16 23:27:53 +00:00
|
|
|
MOZ_ASSERT(mRecording, "haven't started recording");
|
2013-02-16 23:27:53 +00:00
|
|
|
mRecording = false;
|
|
|
|
aBuffer.Append(mBuffer + mRecordStartOffset,
|
2013-02-16 23:27:53 +00:00
|
|
|
mOffset - mRecordStartOffset);
|
2013-02-16 23:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsDependentSubstring
|
|
|
|
nsCSSScanner::GetCurrentLine() const
|
|
|
|
{
|
|
|
|
uint32_t end = mTokenOffset;
|
2013-02-16 23:27:53 +00:00
|
|
|
while (end < mCount && !IsVertSpace(mBuffer[end])) {
|
2013-02-16 23:27:53 +00:00
|
|
|
end++;
|
|
|
|
}
|
|
|
|
return nsDependentSubstring(mBuffer + mTokenLineOffset,
|
|
|
|
mBuffer + end);
|
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
|
|
|
* Return the raw UTF-16 code unit at position |mOffset + n| within
|
|
|
|
* the read buffer. If that is beyond the end of the buffer, returns
|
|
|
|
* -1 to indicate end of input.
|
|
|
|
*/
|
|
|
|
inline int32_t
|
|
|
|
nsCSSScanner::Peek(uint32_t n)
|
1998-04-13 20:24:54 +00:00
|
|
|
{
|
2013-02-16 23:27:53 +00:00
|
|
|
if (mOffset + n >= mCount) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return mBuffer[mOffset + n];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Advance |mOffset| over |n| code units. Advance(0) is a no-op.
|
|
|
|
* If |n| is greater than the distance to end of input, will silently
|
|
|
|
* stop at the end. May not be used to advance over a line boundary;
|
|
|
|
* AdvanceLine() must be used instead.
|
|
|
|
*/
|
|
|
|
inline void
|
|
|
|
nsCSSScanner::Advance(uint32_t n)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
while (mOffset < mCount && n > 0) {
|
|
|
|
MOZ_ASSERT(!IsVertSpace(mBuffer[mOffset]),
|
|
|
|
"may not Advance() over a line boundary");
|
|
|
|
mOffset++;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (mOffset + n >= mCount || mOffset + n < mOffset)
|
|
|
|
mOffset = mCount;
|
|
|
|
else
|
|
|
|
mOffset += n;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Advance |mOffset| over a line boundary.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
nsCSSScanner::AdvanceLine()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsVertSpace(mBuffer[mOffset]),
|
|
|
|
"may not AdvanceLine() over a horizontal character");
|
|
|
|
// Advance over \r\n as a unit.
|
|
|
|
if (mBuffer[mOffset] == '\r' && mOffset + 1 < mCount &&
|
|
|
|
mBuffer[mOffset+1] == '\n')
|
|
|
|
mOffset += 2;
|
|
|
|
else
|
|
|
|
mOffset += 1;
|
|
|
|
// 0 is a magical line number meaning that we don't know (i.e., script)
|
|
|
|
if (mLineNumber != 0)
|
|
|
|
mLineNumber++;
|
|
|
|
mLineOffset = mOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Back up |mOffset| over |n| code units. Backup(0) is a no-op.
|
|
|
|
* If |n| is greater than the distance to beginning of input, will
|
|
|
|
* silently stop at the beginning. May not be used to back up over a
|
|
|
|
* line boundary.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
nsCSSScanner::Backup(uint32_t n)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
while (mOffset > 0 && n > 0) {
|
|
|
|
MOZ_ASSERT(!IsVertSpace(mBuffer[mOffset-1]),
|
|
|
|
"may not Backup() over a line boundary");
|
|
|
|
mOffset--;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (mOffset < n)
|
|
|
|
mOffset = 0;
|
|
|
|
else
|
|
|
|
mOffset -= n;
|
|
|
|
#endif
|
2011-03-11 17:29:45 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
|
|
|
* Skip over a sequence of whitespace characters (vertical or
|
|
|
|
* horizontal) starting at the current read position.
|
|
|
|
*/
|
2009-04-09 06:46:26 +00:00
|
|
|
void
|
2013-02-16 23:27:53 +00:00
|
|
|
nsCSSScanner::SkipWhitespace()
|
1998-04-13 20:24:54 +00:00
|
|
|
{
|
|
|
|
for (;;) {
|
2013-02-16 23:27:53 +00:00
|
|
|
int32_t ch = Peek();
|
|
|
|
if (!IsWhitespace(ch)) { // EOF counts as non-whitespace
|
1998-04-13 20:24:54 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
if (IsVertSpace(ch)) {
|
|
|
|
AdvanceLine();
|
|
|
|
} else {
|
|
|
|
Advance();
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
|
|
|
* Skip over one CSS comment starting at the current read position.
|
|
|
|
*/
|
2013-02-16 23:27:53 +00:00
|
|
|
void
|
|
|
|
nsCSSScanner::SkipComment()
|
1998-10-26 23:22:40 +00:00
|
|
|
{
|
2013-02-16 23:27:53 +00:00
|
|
|
MOZ_ASSERT(Peek() == '/' && Peek(1) == '*', "should not have been called");
|
|
|
|
Advance(2);
|
2009-08-06 00:45:49 +00:00
|
|
|
for (;;) {
|
2013-02-16 23:27:53 +00:00
|
|
|
int32_t ch = Peek();
|
|
|
|
if (ch < 0) {
|
|
|
|
mReporter->ReportUnexpectedEOF("PECommentEOF");
|
2013-12-07 01:25:07 +00:00
|
|
|
SetEOFCharacters(eEOFCharacters_Asterisk | eEOFCharacters_Slash);
|
2013-02-16 23:27:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-12-07 01:25:07 +00:00
|
|
|
if (ch == '*') {
|
|
|
|
Advance();
|
|
|
|
ch = Peek();
|
|
|
|
if (ch < 0) {
|
|
|
|
mReporter->ReportUnexpectedEOF("PECommentEOF");
|
|
|
|
SetEOFCharacters(eEOFCharacters_Slash);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ch == '/') {
|
|
|
|
Advance();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (IsVertSpace(ch)) {
|
2013-02-16 23:27:53 +00:00
|
|
|
AdvanceLine();
|
|
|
|
} else {
|
|
|
|
Advance();
|
2007-08-20 03:39:22 +00:00
|
|
|
}
|
2009-08-06 00:45:49 +00:00
|
|
|
}
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
|
2011-05-03 20:19:19 +00:00
|
|
|
/**
|
2013-02-16 23:27:53 +00:00
|
|
|
* If there is a valid escape sequence starting at the current read
|
|
|
|
* position, consume it, decode it, append the result to |aOutput|,
|
|
|
|
* and return true. Otherwise, consume nothing, leave |aOutput|
|
|
|
|
* unmodified, and return false. If |aInString| is true, accept the
|
|
|
|
* additional form of escape sequence allowed within string-like tokens.
|
2011-05-03 20:19:19 +00:00
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2013-02-16 23:27:53 +00:00
|
|
|
nsCSSScanner::GatherEscape(nsString& aOutput, bool aInString)
|
1998-04-13 20:24:54 +00:00
|
|
|
{
|
2013-02-16 23:27:53 +00:00
|
|
|
MOZ_ASSERT(Peek() == '\\', "should not have been called");
|
|
|
|
int32_t ch = Peek(1);
|
1998-04-13 20:24:54 +00:00
|
|
|
if (ch < 0) {
|
2013-06-10 07:04:27 +00:00
|
|
|
// If we are in a string (or a url() containing a string), we want to drop
|
|
|
|
// the backslash on the floor. Otherwise, we want to treat it as a U+FFFD
|
|
|
|
// character.
|
|
|
|
Advance();
|
2013-12-07 01:25:07 +00:00
|
|
|
if (aInString) {
|
|
|
|
SetEOFCharacters(eEOFCharacters_DropBackslash);
|
|
|
|
} else {
|
2013-06-26 22:17:14 +00:00
|
|
|
aOutput.Append(UCS2_REPLACEMENT_CHAR);
|
2013-12-07 01:25:07 +00:00
|
|
|
SetEOFCharacters(eEOFCharacters_ReplacementChar);
|
2013-06-10 07:04:27 +00:00
|
|
|
}
|
|
|
|
return true;
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
if (IsVertSpace(ch)) {
|
|
|
|
if (aInString) {
|
|
|
|
// In strings (and in url() containing a string), escaped
|
|
|
|
// newlines are completely removed, to allow splitting over
|
|
|
|
// multiple lines.
|
|
|
|
Advance();
|
|
|
|
AdvanceLine();
|
|
|
|
return true;
|
2005-11-17 15:17:00 +00:00
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
// Outside of strings, backslash followed by a newline is not an escape.
|
|
|
|
return false;
|
2012-11-15 16:36:15 +00:00
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
|
|
|
|
if (!IsHexDigit(ch)) {
|
|
|
|
// "Any character (except a hexadecimal digit, linefeed, carriage
|
|
|
|
// return, or form feed) can be escaped with a backslash to remove
|
|
|
|
// its special meaning." -- CSS2.1 section 4.1.3
|
|
|
|
Advance(2);
|
2013-06-26 22:17:14 +00:00
|
|
|
if (ch == 0) {
|
|
|
|
aOutput.Append(UCS2_REPLACEMENT_CHAR);
|
|
|
|
} else {
|
|
|
|
aOutput.Append(ch);
|
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
return true;
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
2011-05-03 20:19:19 +00:00
|
|
|
|
2013-02-16 23:27:53 +00: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
|
|
|
|
|
|
|
|
// At this point we know we have \ followed by at least one
|
|
|
|
// hexadecimal digit, therefore the escape sequence is valid and we
|
|
|
|
// can go ahead and consume the backslash.
|
|
|
|
Advance();
|
|
|
|
uint32_t val = 0;
|
|
|
|
int i = 0;
|
|
|
|
do {
|
|
|
|
val = val * 16 + HexDigitValue(ch);
|
|
|
|
i++;
|
|
|
|
Advance();
|
|
|
|
ch = Peek();
|
|
|
|
} while (i < 6 && IsHexDigit(ch));
|
|
|
|
|
2013-06-26 22:17:14 +00:00
|
|
|
// "Interpret the hex digits as a hexadecimal number. If this number is zero,
|
|
|
|
// or is greater than the maximum allowed codepoint, return U+FFFD
|
|
|
|
// REPLACEMENT CHARACTER" -- CSS Syntax Level 3
|
2013-02-16 23:27:53 +00:00
|
|
|
if (MOZ_UNLIKELY(val == 0)) {
|
2013-06-26 22:17:14 +00:00
|
|
|
aOutput.Append(UCS2_REPLACEMENT_CHAR);
|
2013-02-16 23:27:53 +00:00
|
|
|
} else {
|
|
|
|
AppendUCS4ToUTF16(ENSURE_VALID_CHAR(val), aOutput);
|
2013-06-26 22:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Consume exactly one whitespace character after a
|
|
|
|
// hexadecimal escape sequence.
|
|
|
|
if (IsVertSpace(ch)) {
|
|
|
|
AdvanceLine();
|
|
|
|
} else if (IsHorzSpace(ch)) {
|
|
|
|
Advance();
|
2013-02-16 23:27:53 +00:00
|
|
|
}
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-16 23:27:53 +00:00
|
|
|
* Consume a run of "text" beginning with the current read position,
|
|
|
|
* consisting of characters in the class |aClass| (which must be a
|
|
|
|
* suitable argument to IsOpenCharClass) plus escape sequences.
|
|
|
|
* Append the text to |aText|, after decoding escape sequences.
|
|
|
|
*
|
|
|
|
* Returns true if at least one character was appended to |aText|,
|
|
|
|
* false otherwise.
|
1998-04-13 20:24:54 +00:00
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2013-02-16 23:27:53 +00:00
|
|
|
nsCSSScanner::GatherText(uint8_t aClass, nsString& aText)
|
1998-04-13 20:24:54 +00:00
|
|
|
{
|
2013-02-16 23:27:53 +00:00
|
|
|
// This is all of the character classes currently used with
|
|
|
|
// GatherText. If you have a need to use this function with a
|
|
|
|
// different class, go ahead and add it.
|
|
|
|
MOZ_ASSERT(aClass == IS_STRING ||
|
|
|
|
aClass == IS_IDCHAR ||
|
|
|
|
aClass == IS_URL_CHAR,
|
|
|
|
"possibly-inappropriate character class");
|
|
|
|
|
|
|
|
uint32_t start = mOffset;
|
|
|
|
bool inString = aClass == IS_STRING;
|
2013-02-16 23:27:53 +00:00
|
|
|
|
1998-04-13 20:24:54 +00:00
|
|
|
for (;;) {
|
2013-02-16 23:27:53 +00:00
|
|
|
// Consume runs of unescaped characters in one go.
|
|
|
|
uint32_t n = mOffset;
|
2013-02-16 23:27:53 +00:00
|
|
|
while (n < mCount && IsOpenCharClass(mBuffer[n], aClass)) {
|
2013-02-16 23:27:53 +00:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
if (n > mOffset) {
|
2013-02-16 23:27:53 +00:00
|
|
|
aText.Append(&mBuffer[mOffset], n - mOffset);
|
2013-02-16 23:27:53 +00:00
|
|
|
mOffset = n;
|
2008-02-22 01:37:04 +00:00
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
if (n == mCount) {
|
|
|
|
break;
|
|
|
|
}
|
2008-02-22 01:37:04 +00:00
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
int32_t ch = Peek();
|
|
|
|
MOZ_ASSERT(!IsOpenCharClass(ch, aClass),
|
|
|
|
"should not have exited the inner loop");
|
2013-06-26 22:17:14 +00:00
|
|
|
if (ch == 0) {
|
|
|
|
Advance();
|
|
|
|
aText.Append(UCS2_REPLACEMENT_CHAR);
|
|
|
|
continue;
|
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
|
|
|
|
if (ch != '\\') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!GatherEscape(aText, inString)) {
|
2013-02-16 23:27:53 +00:00
|
|
|
break;
|
2005-07-19 20:49:34 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
return mOffset > start;
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
|
|
|
* Scan an Ident token. This also handles Function and URL tokens,
|
|
|
|
* both of which begin indistinguishably from an identifier. It can
|
|
|
|
* produce a Symbol token when an apparent identifier actually led
|
|
|
|
* into an invalid escape sequence.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2013-02-16 23:27:53 +00:00
|
|
|
nsCSSScanner::ScanIdent(nsCSSToken& aToken)
|
1998-04-13 20:24:54 +00:00
|
|
|
{
|
2013-02-16 23:27:53 +00:00
|
|
|
if (MOZ_UNLIKELY(!GatherText(IS_IDCHAR, aToken.mIdent))) {
|
2013-12-07 01:25:07 +00:00
|
|
|
MOZ_ASSERT(Peek() == '\\',
|
|
|
|
"unexpected IsIdentStart character that did not begin an ident");
|
2013-02-16 23:27:53 +00:00
|
|
|
aToken.mSymbol = Peek();
|
|
|
|
Advance();
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
if (MOZ_LIKELY(Peek() != '(')) {
|
|
|
|
aToken.mType = eCSSToken_Ident;
|
|
|
|
return true;
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
1998-10-26 23:22:40 +00:00
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
Advance();
|
|
|
|
aToken.mType = eCSSToken_Function;
|
|
|
|
if (aToken.mIdent.LowerCaseEqualsLiteral("url")) {
|
|
|
|
NextURL(aToken);
|
|
|
|
}
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
|
|
|
* Scan an AtKeyword token. Also handles production of Symbol when
|
|
|
|
* an '@' is not followed by an identifier.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2013-02-16 23:27:53 +00:00
|
|
|
nsCSSScanner::ScanAtKeyword(nsCSSToken& aToken)
|
1998-04-13 20:24:54 +00:00
|
|
|
{
|
2013-02-16 23:27:53 +00:00
|
|
|
MOZ_ASSERT(Peek() == '@', "should not have been called");
|
|
|
|
|
|
|
|
// Fall back for when '@' isn't followed by an identifier.
|
|
|
|
aToken.mSymbol = '@';
|
|
|
|
Advance();
|
|
|
|
|
|
|
|
int32_t ch = Peek();
|
|
|
|
if (StartsIdent(ch, Peek(1))) {
|
2013-02-16 23:27:53 +00:00
|
|
|
if (GatherText(IS_IDCHAR, aToken.mIdent)) {
|
2013-02-16 23:27:53 +00:00
|
|
|
aToken.mType = eCSSToken_AtKeyword;
|
|
|
|
}
|
2012-09-11 22:20:52 +00:00
|
|
|
}
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
|
|
|
* Scan a Hash token. Handles the distinction between eCSSToken_ID
|
|
|
|
* and eCSSToken_Hash, and handles production of Symbol when a '#'
|
|
|
|
* is not followed by identifier characters.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2013-02-16 23:27:53 +00:00
|
|
|
nsCSSScanner::ScanHash(nsCSSToken& aToken)
|
2013-02-16 23:27:53 +00:00
|
|
|
{
|
2013-02-16 23:27:53 +00:00
|
|
|
MOZ_ASSERT(Peek() == '#', "should not have been called");
|
2013-02-16 23:27:53 +00:00
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
// Fall back for when '#' isn't followed by identifier characters.
|
|
|
|
aToken.mSymbol = '#';
|
|
|
|
Advance();
|
|
|
|
|
|
|
|
int32_t ch = Peek();
|
2013-02-16 23:27:53 +00:00
|
|
|
if (IsIdentChar(ch) || ch == '\\') {
|
2013-02-16 23:27:53 +00:00
|
|
|
nsCSSTokenType type =
|
2013-02-16 23:27:53 +00:00
|
|
|
StartsIdent(ch, Peek(1)) ? eCSSToken_ID : eCSSToken_Hash;
|
2013-02-16 23:27:53 +00:00
|
|
|
aToken.mIdent.SetLength(0);
|
2013-02-16 23:27:53 +00:00
|
|
|
if (GatherText(IS_IDCHAR, aToken.mIdent)) {
|
2013-02-16 23:27:53 +00:00
|
|
|
aToken.mType = type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
|
|
|
* Scan a Number, Percentage, or Dimension token (all of which begin
|
|
|
|
* like a Number). Can produce a Symbol when a '.' is not followed by
|
|
|
|
* digits, or when '+' or '-' are not followed by either a digit or a
|
|
|
|
* '.' and then a digit. Can also produce a HTMLComment when it
|
|
|
|
* encounters '-->'.
|
|
|
|
*/
|
2013-02-16 23:27:53 +00:00
|
|
|
bool
|
2013-02-16 23:27:53 +00:00
|
|
|
nsCSSScanner::ScanNumber(nsCSSToken& aToken)
|
1998-04-13 20:24:54 +00:00
|
|
|
{
|
2013-02-16 23:27:53 +00:00
|
|
|
int32_t c = Peek();
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
int32_t c2 = Peek(1);
|
|
|
|
int32_t c3 = Peek(2);
|
|
|
|
MOZ_ASSERT(IsDigit(c) ||
|
|
|
|
(IsDigit(c2) && (c == '.' || c == '+' || c == '-')) ||
|
|
|
|
(IsDigit(c3) && (c == '+' || c == '-') && c2 == '.'),
|
|
|
|
"should not have been called");
|
|
|
|
}
|
|
|
|
#endif
|
2009-07-10 01:44:20 +00:00
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
// Sign of the mantissa (-1 or 1).
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t sign = c == '-' ? -1 : 1;
|
2009-07-10 01:44:20 +00:00
|
|
|
// 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
|
2012-08-22 15:56:38 +00:00
|
|
|
// floating-point value while still being able to express the full int32_t
|
2009-07-10 01:44:20 +00:00
|
|
|
// 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...
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t exponent = 0;
|
2009-07-10 01:44:20 +00:00
|
|
|
// Sign of the exponent.
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t expSign = 1;
|
2009-07-23 01:35:07 +00:00
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
aToken.mHasSign = (c == '+' || c == '-');
|
2009-07-23 01:35:07 +00:00
|
|
|
if (aToken.mHasSign) {
|
2013-02-16 23:27:53 +00:00
|
|
|
Advance();
|
|
|
|
c = Peek();
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool gotDot = (c == '.');
|
2009-07-23 01:35:07 +00:00
|
|
|
|
|
|
|
if (!gotDot) {
|
2013-02-16 23:27:53 +00:00
|
|
|
// Scan the integer part of the mantissa.
|
|
|
|
MOZ_ASSERT(IsDigit(c), "should have been excluded by logic above");
|
2009-07-23 01:35:07 +00:00
|
|
|
do {
|
2009-08-20 21:52:47 +00:00
|
|
|
intPart = 10*intPart + DecimalDigitValue(c);
|
2013-02-16 23:27:53 +00:00
|
|
|
Advance();
|
|
|
|
c = Peek();
|
2009-07-23 01:35:07 +00:00
|
|
|
} while (IsDigit(c));
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
gotDot = (c == '.') && IsDigit(Peek(1));
|
2009-07-23 01:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (gotDot) {
|
2013-02-16 23:27:53 +00:00
|
|
|
// Scan the fractional part of the mantissa.
|
2013-02-16 23:27:53 +00:00
|
|
|
Advance();
|
|
|
|
c = Peek();
|
|
|
|
MOZ_ASSERT(IsDigit(c), "should have been excluded by logic above");
|
2009-07-23 01:35:07 +00:00
|
|
|
// Power of ten by which we need to divide our next digit
|
2013-02-16 23:27:53 +00:00
|
|
|
double divisor = 10;
|
2009-07-23 01:35:07 +00:00
|
|
|
do {
|
2009-08-20 21:52:47 +00:00
|
|
|
fracPart += DecimalDigitValue(c) / divisor;
|
2009-07-23 01:35:07 +00:00
|
|
|
divisor *= 10;
|
2013-02-16 23:27:53 +00:00
|
|
|
Advance();
|
|
|
|
c = Peek();
|
2009-07-23 01:35:07 +00:00
|
|
|
} while (IsDigit(c));
|
|
|
|
}
|
2009-07-10 03:36:57 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool gotE = false;
|
2009-07-23 01:35:07 +00:00
|
|
|
if (IsSVGMode() && (c == 'e' || c == 'E')) {
|
2013-02-16 23:27:53 +00:00
|
|
|
int32_t expSignChar = Peek(1);
|
|
|
|
int32_t nextChar = Peek(2);
|
|
|
|
if (IsDigit(expSignChar) ||
|
|
|
|
((expSignChar == '-' || expSignChar == '+') && IsDigit(nextChar))) {
|
2011-10-17 14:59:28 +00:00
|
|
|
gotE = true;
|
2009-07-23 01:35:07 +00:00
|
|
|
if (expSignChar == '-') {
|
|
|
|
expSign = -1;
|
2009-07-10 03:36:57 +00:00
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
Advance(); // consumes the E
|
|
|
|
if (expSignChar == '-' || expSignChar == '+') {
|
|
|
|
Advance();
|
|
|
|
c = nextChar;
|
|
|
|
} else {
|
|
|
|
c = expSignChar;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(IsDigit(c), "should have been excluded by logic above");
|
2009-07-23 01:35:07 +00:00
|
|
|
do {
|
2009-08-20 21:52:47 +00:00
|
|
|
exponent = 10*exponent + DecimalDigitValue(c);
|
2013-02-16 23:27:53 +00:00
|
|
|
Advance();
|
|
|
|
c = Peek();
|
2009-07-23 01:35:07 +00:00
|
|
|
} while (IsDigit(c));
|
2009-07-10 01:44:20 +00:00
|
|
|
}
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCSSTokenType type = eCSSToken_Number;
|
|
|
|
|
2008-06-03 03:17:35 +00:00
|
|
|
// Set mIntegerValid for all cases (except %, below) because we need
|
|
|
|
// it for the "2n" in :nth-child(2n).
|
2011-10-17 14:59:28 +00:00
|
|
|
aToken.mIntegerValid = false;
|
2009-07-10 01:44:20 +00:00
|
|
|
|
|
|
|
// Time to reassemble our number.
|
2013-02-16 23:27:53 +00:00
|
|
|
// Do all the math in double precision so it's truncated only once.
|
|
|
|
double value = sign * (intPart + fracPart);
|
2009-07-10 01:44:20 +00:00
|
|
|
if (gotE) {
|
2013-02-16 23:27:53 +00:00
|
|
|
// Explicitly cast expSign*exponent to double to avoid issues with
|
2009-07-10 01:44:20 +00:00
|
|
|
// overloaded pow() on Windows.
|
|
|
|
value *= pow(10.0, double(expSign * exponent));
|
|
|
|
} else if (!gotDot) {
|
2010-10-18 02:36:26 +00:00
|
|
|
// Clamp values outside of integer range.
|
|
|
|
if (sign > 0) {
|
2013-01-15 12:22:03 +00:00
|
|
|
aToken.mInteger = int32_t(std::min(intPart, double(INT32_MAX)));
|
2010-10-18 02:36:26 +00:00
|
|
|
} else {
|
2013-01-15 12:22:03 +00:00
|
|
|
aToken.mInteger = int32_t(std::max(-intPart, double(INT32_MIN)));
|
2009-07-10 01:44:20 +00:00
|
|
|
}
|
2011-10-17 14:59:28 +00:00
|
|
|
aToken.mIntegerValid = true;
|
2008-06-03 03:17:35 +00:00
|
|
|
}
|
2009-07-10 01:44:20 +00:00
|
|
|
|
|
|
|
nsString& ident = aToken.mIdent;
|
2008-06-03 03:17:35 +00:00
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
// Check for Dimension and Percentage tokens.
|
1998-04-13 20:24:54 +00:00
|
|
|
if (c >= 0) {
|
2013-02-16 23:27:53 +00:00
|
|
|
if (StartsIdent(c, Peek(1))) {
|
2013-02-16 23:27:53 +00:00
|
|
|
if (GatherText(IS_IDCHAR, ident)) {
|
2011-05-03 20:19:19 +00:00
|
|
|
type = eCSSToken_Dimension;
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
} else if (c == '%') {
|
|
|
|
Advance();
|
1998-04-13 20:24:54 +00:00
|
|
|
type = eCSSToken_Percentage;
|
|
|
|
value = value / 100.0f;
|
2011-10-17 14:59:28 +00:00
|
|
|
aToken.mIntegerValid = false;
|
1999-02-11 06:42:02 +00:00
|
|
|
}
|
|
|
|
}
|
1998-10-26 23:22:40 +00:00
|
|
|
aToken.mNumber = value;
|
|
|
|
aToken.mType = type;
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
|
|
|
* Scan a string constant ('foo' or "foo"). Will always produce
|
|
|
|
* either a String or a Bad_String token; the latter occurs when the
|
|
|
|
* close quote is missing. Always returns true (for convenience in Next()).
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2013-02-16 23:27:53 +00:00
|
|
|
nsCSSScanner::ScanString(nsCSSToken& aToken)
|
1998-04-13 20:24:54 +00:00
|
|
|
{
|
2013-02-16 23:27:53 +00:00
|
|
|
int32_t aStop = Peek();
|
|
|
|
MOZ_ASSERT(aStop == '"' || aStop == '\'', "should not have been called");
|
2005-03-18 06:56:56 +00:00
|
|
|
aToken.mType = eCSSToken_String;
|
2013-02-16 23:27:53 +00:00
|
|
|
aToken.mSymbol = PRUnichar(aStop); // Remember how it's quoted.
|
|
|
|
Advance();
|
|
|
|
|
1998-04-13 20:24:54 +00:00
|
|
|
for (;;) {
|
2013-02-16 23:27:53 +00:00
|
|
|
GatherText(IS_STRING, aToken.mIdent);
|
|
|
|
|
|
|
|
int32_t ch = Peek();
|
|
|
|
if (ch == -1) {
|
2013-12-07 01:25:07 +00:00
|
|
|
AddEOFCharacters(aStop == '"' ? eEOFCharacters_DoubleQuote :
|
|
|
|
eEOFCharacters_SingleQuote);
|
2013-02-16 23:27:53 +00:00
|
|
|
break; // EOF ends a string token with no error.
|
|
|
|
}
|
|
|
|
if (ch == aStop) {
|
|
|
|
Advance();
|
2008-02-22 01:37:04 +00:00
|
|
|
break;
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
// Both " and ' are excluded from IS_STRING.
|
|
|
|
if (ch == '"' || ch == '\'') {
|
|
|
|
aToken.mIdent.Append(ch);
|
|
|
|
Advance();
|
|
|
|
continue;
|
1999-02-07 21:47:48 +00:00
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
|
2013-06-28 00:03:33 +00:00
|
|
|
mSeenBadToken = true;
|
2013-02-16 23:27:53 +00:00
|
|
|
aToken.mType = eCSSToken_Bad_String;
|
|
|
|
mReporter->ReportUnexpected("SEUnterminatedString", aToken);
|
|
|
|
break;
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
2009-08-20 21:52:47 +00:00
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
|
|
|
* Scan a unicode-range token. These 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.
|
|
|
|
* Note that this does not validate the numeric range, only the syntactic
|
|
|
|
* form.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2013-02-16 23:27:53 +00:00
|
|
|
nsCSSScanner::ScanURange(nsCSSToken& aResult)
|
2009-08-20 21:52:47 +00:00
|
|
|
{
|
2013-02-16 23:27:53 +00:00
|
|
|
int32_t intro1 = Peek();
|
|
|
|
int32_t intro2 = Peek(1);
|
|
|
|
int32_t ch = Peek(2);
|
2009-08-20 21:52:47 +00:00
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
MOZ_ASSERT((intro1 == 'u' || intro1 == 'U') &&
|
|
|
|
intro2 == '+' &&
|
|
|
|
(IsHexDigit(ch) || ch == '?'),
|
|
|
|
"should not have been called");
|
2009-08-20 21:52:47 +00:00
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
aResult.mIdent.Append(intro1);
|
2009-08-20 21:52:47 +00:00
|
|
|
aResult.mIdent.Append(intro2);
|
2013-02-16 23:27:53 +00:00
|
|
|
Advance(2);
|
2009-08-20 21:52:47 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool valid = true;
|
|
|
|
bool haveQues = false;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t low = 0;
|
|
|
|
uint32_t high = 0;
|
2009-08-20 21:52:47 +00:00
|
|
|
int i = 0;
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
do {
|
2009-08-20 21:52:47 +00:00
|
|
|
aResult.mIdent.Append(ch);
|
|
|
|
if (IsHexDigit(ch)) {
|
|
|
|
if (haveQues) {
|
2013-02-16 23:27:53 +00:00
|
|
|
valid = false; // All question marks should be at the end.
|
2009-08-20 21:52:47 +00:00
|
|
|
}
|
|
|
|
low = low*16 + HexDigitValue(ch);
|
|
|
|
high = high*16 + HexDigitValue(ch);
|
|
|
|
} else {
|
2011-10-17 14:59:28 +00:00
|
|
|
haveQues = true;
|
2009-08-20 21:52:47 +00:00
|
|
|
low = low*16 + 0x0;
|
|
|
|
high = high*16 + 0xF;
|
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
i++;
|
|
|
|
Advance();
|
|
|
|
ch = Peek();
|
|
|
|
} while (i < 6 && (IsHexDigit(ch) || ch == '?'));
|
|
|
|
|
|
|
|
if (ch == '-' && IsHexDigit(Peek(1))) {
|
2009-08-20 21:52:47 +00:00
|
|
|
if (haveQues) {
|
2011-10-17 14:59:28 +00:00
|
|
|
valid = false;
|
2009-08-20 21:52:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aResult.mIdent.Append(ch);
|
2013-02-16 23:27:53 +00:00
|
|
|
Advance();
|
|
|
|
ch = Peek();
|
2009-08-20 21:52:47 +00:00
|
|
|
high = 0;
|
|
|
|
i = 0;
|
2013-02-16 23:27:53 +00:00
|
|
|
do {
|
2009-08-20 21:52:47 +00:00
|
|
|
aResult.mIdent.Append(ch);
|
|
|
|
high = high*16 + HexDigitValue(ch);
|
2013-02-16 23:27:53 +00:00
|
|
|
|
|
|
|
i++;
|
|
|
|
Advance();
|
|
|
|
ch = Peek();
|
|
|
|
} while (i < 6 && IsHexDigit(ch));
|
2009-08-20 21:52:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aResult.mInteger = low;
|
|
|
|
aResult.mInteger2 = high;
|
|
|
|
aResult.mIntegerValid = valid;
|
|
|
|
aResult.mType = eCSSToken_URange;
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2009-08-20 21:52:47 +00:00
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
|
2013-12-07 01:25:07 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
/* static */ void
|
|
|
|
nsCSSScanner::AssertEOFCharactersValid(uint32_t c)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(c == eEOFCharacters_None ||
|
|
|
|
c == eEOFCharacters_ReplacementChar ||
|
|
|
|
c == eEOFCharacters_Slash ||
|
|
|
|
c == (eEOFCharacters_Asterisk |
|
|
|
|
eEOFCharacters_Slash) ||
|
|
|
|
c == eEOFCharacters_DoubleQuote ||
|
|
|
|
c == eEOFCharacters_SingleQuote ||
|
|
|
|
c == (eEOFCharacters_DropBackslash |
|
|
|
|
eEOFCharacters_DoubleQuote) ||
|
|
|
|
c == (eEOFCharacters_DropBackslash |
|
|
|
|
eEOFCharacters_SingleQuote) ||
|
|
|
|
c == eEOFCharacters_CloseParen ||
|
|
|
|
c == (eEOFCharacters_ReplacementChar |
|
|
|
|
eEOFCharacters_CloseParen) ||
|
|
|
|
c == (eEOFCharacters_DoubleQuote |
|
|
|
|
eEOFCharacters_CloseParen) ||
|
|
|
|
c == (eEOFCharacters_SingleQuote |
|
|
|
|
eEOFCharacters_CloseParen) ||
|
|
|
|
c == (eEOFCharacters_DropBackslash |
|
|
|
|
eEOFCharacters_DoubleQuote |
|
|
|
|
eEOFCharacters_CloseParen) ||
|
|
|
|
c == (eEOFCharacters_DropBackslash |
|
|
|
|
eEOFCharacters_SingleQuote |
|
|
|
|
eEOFCharacters_CloseParen),
|
|
|
|
"invalid EOFCharacters value");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCSSScanner::SetEOFCharacters(uint32_t aEOFCharacters)
|
|
|
|
{
|
|
|
|
mEOFCharacters = EOFCharacters(aEOFCharacters);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCSSScanner::AddEOFCharacters(uint32_t aEOFCharacters)
|
|
|
|
{
|
|
|
|
mEOFCharacters = EOFCharacters(mEOFCharacters | aEOFCharacters);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const PRUnichar kImpliedEOFCharacters[] = {
|
|
|
|
UCS2_REPLACEMENT_CHAR, '*', '/', '"', '\'', ')', 0
|
|
|
|
};
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
nsCSSScanner::AdjustTokenStreamForEOFCharacters(EOFCharacters aEOFCharacters,
|
|
|
|
nsAString& aResult)
|
|
|
|
{
|
|
|
|
uint32_t c = aEOFCharacters;
|
|
|
|
|
|
|
|
// First, handle eEOFCharacters_DropBackslash.
|
|
|
|
if (c & eEOFCharacters_DropBackslash) {
|
|
|
|
MOZ_ASSERT(aResult[aResult.Length() - 1] == '\\');
|
|
|
|
aResult.SetLength(aResult.Length() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
c >>= 1;
|
|
|
|
|
|
|
|
// All of the remaining EOFCharacters bits represent appended characters,
|
|
|
|
// and the bits are in the order that they need appending.
|
|
|
|
for (const PRUnichar* p = kImpliedEOFCharacters; *p && c; p++, c >>= 1) {
|
|
|
|
if (c & 1) {
|
|
|
|
aResult.Append(*p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(c == 0, "too many bits in mEOFCharacters");
|
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
|
|
|
* Consume the part of an URL token after the initial 'url('. Caller
|
|
|
|
* is assumed to have consumed 'url(' already. Will always produce
|
|
|
|
* either an URL or a Bad_URL token.
|
|
|
|
*
|
|
|
|
* Exposed for use by nsCSSParser::ParseMozDocumentRule, which applies
|
|
|
|
* the special lexical rules for URL tokens in a nonstandard context.
|
|
|
|
*/
|
2013-02-16 23:27:53 +00:00
|
|
|
bool
|
|
|
|
nsCSSScanner::NextURL(nsCSSToken& aToken)
|
|
|
|
{
|
|
|
|
SkipWhitespace();
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
int32_t ch = Peek();
|
2013-02-16 23:27:53 +00:00
|
|
|
if (ch < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
// aToken.mIdent may be "url" at this point; clear that out
|
|
|
|
aToken.mIdent.Truncate();
|
|
|
|
|
|
|
|
// Do we have a string?
|
|
|
|
if (ch == '"' || ch == '\'') {
|
|
|
|
ScanString(aToken);
|
|
|
|
if (MOZ_UNLIKELY(aToken.mType == eCSSToken_Bad_String)) {
|
2013-02-16 23:27:53 +00:00
|
|
|
aToken.mType = eCSSToken_Bad_URL;
|
2013-02-16 23:27:53 +00:00
|
|
|
return true;
|
2013-02-16 23:27:53 +00:00
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
MOZ_ASSERT(aToken.mType == eCSSToken_String, "unexpected token type");
|
2013-02-16 23:27:53 +00:00
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
} else {
|
2013-02-16 23:27:53 +00:00
|
|
|
// Otherwise, this is the start of a non-quoted url (which may be empty).
|
2013-02-18 03:03:55 +00:00
|
|
|
aToken.mSymbol = PRUnichar(0);
|
2013-02-16 23:27:53 +00:00
|
|
|
GatherText(IS_URL_CHAR, aToken.mIdent);
|
2013-02-16 23:27:53 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
// Consume trailing whitespace and then look for a close parenthesis.
|
|
|
|
SkipWhitespace();
|
|
|
|
ch = Peek();
|
|
|
|
if (MOZ_LIKELY(ch < 0 || ch == ')')) {
|
|
|
|
Advance();
|
2013-02-16 23:27:53 +00:00
|
|
|
aToken.mType = eCSSToken_URL;
|
2013-12-07 01:25:07 +00:00
|
|
|
if (ch < 0) {
|
|
|
|
AddEOFCharacters(eEOFCharacters_CloseParen);
|
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
} else {
|
2013-06-28 00:03:33 +00:00
|
|
|
mSeenBadToken = true;
|
2013-02-16 23:27:53 +00:00
|
|
|
aToken.mType = eCSSToken_Bad_URL;
|
2013-02-16 23:27:53 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
/**
|
|
|
|
* Primary scanner entry point. Consume one token and fill in
|
|
|
|
* |aToken| accordingly. Will skip over any number of comments first,
|
|
|
|
* and will also skip over rather than return whitespace tokens if
|
|
|
|
* |aSkipWS| is true.
|
|
|
|
*
|
|
|
|
* Returns true if it successfully consumed a token, false if EOF has
|
|
|
|
* been reached. Will always advance the current read position by at
|
|
|
|
* least one character unless called when already at EOF.
|
|
|
|
*/
|
2013-02-16 23:27:53 +00:00
|
|
|
bool
|
|
|
|
nsCSSScanner::Next(nsCSSToken& aToken, bool aSkipWS)
|
|
|
|
{
|
2013-02-16 23:27:53 +00:00
|
|
|
int32_t ch;
|
|
|
|
|
|
|
|
// do this here so we don't have to do it in dozens of other places
|
|
|
|
aToken.mIdent.Truncate();
|
|
|
|
aToken.mType = eCSSToken_Symbol;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
// Consume any number of comments, and possibly also whitespace tokens,
|
|
|
|
// in between other tokens.
|
2013-02-16 23:27:53 +00:00
|
|
|
mTokenOffset = mOffset;
|
|
|
|
mTokenLineOffset = mLineOffset;
|
|
|
|
mTokenLineNumber = mLineNumber;
|
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
ch = Peek();
|
2013-02-16 23:27:53 +00:00
|
|
|
if (IsWhitespace(ch)) {
|
|
|
|
SkipWhitespace();
|
|
|
|
if (!aSkipWS) {
|
|
|
|
aToken.mType = eCSSToken_Whitespace;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
continue; // start again at the beginning
|
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
if (ch == '/' && !IsSVGMode() && Peek(1) == '*') {
|
|
|
|
// FIXME: Editor wants comments to be preserved (bug 60290).
|
|
|
|
SkipComment();
|
|
|
|
continue; // start again at the beginning
|
2013-02-16 23:27:53 +00:00
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// EOF
|
|
|
|
if (ch < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'u' could be UNICODE-RANGE or an identifier-family token
|
|
|
|
if (ch == 'u' || ch == 'U') {
|
|
|
|
int32_t c2 = Peek(1);
|
|
|
|
int32_t c3 = Peek(2);
|
|
|
|
if (c2 == '+' && (IsHexDigit(c3) || c3 == '?')) {
|
|
|
|
return ScanURange(aToken);
|
2013-02-16 23:27:53 +00:00
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
return ScanIdent(aToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
// identifier family
|
|
|
|
if (IsIdentStart(ch)) {
|
|
|
|
return ScanIdent(aToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
// number family
|
|
|
|
if (IsDigit(ch)) {
|
|
|
|
return ScanNumber(aToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ch == '.' && IsDigit(Peek(1))) {
|
|
|
|
return ScanNumber(aToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ch == '+') {
|
|
|
|
int32_t c2 = Peek(1);
|
|
|
|
if (IsDigit(c2) || (c2 == '.' && IsDigit(Peek(2)))) {
|
|
|
|
return ScanNumber(aToken);
|
2013-02-16 23:27:53 +00:00
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
|
2013-02-16 23:27:53 +00:00
|
|
|
// '-' can start an identifier-family token, a number-family token,
|
|
|
|
// or an HTML-comment
|
|
|
|
if (ch == '-') {
|
|
|
|
int32_t c2 = Peek(1);
|
|
|
|
int32_t c3 = Peek(2);
|
|
|
|
if (IsIdentStart(c2)) {
|
|
|
|
return ScanIdent(aToken);
|
|
|
|
}
|
|
|
|
if (IsDigit(c2) || (c2 == '.' && IsDigit(c3))) {
|
|
|
|
return ScanNumber(aToken);
|
|
|
|
}
|
|
|
|
if (c2 == '-' && c3 == '>') {
|
|
|
|
Advance(3);
|
|
|
|
aToken.mType = eCSSToken_HTMLComment;
|
|
|
|
aToken.mIdent.AssignLiteral("-->");
|
|
|
|
return true;
|
2013-02-16 23:27:53 +00:00
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// the other HTML-comment token
|
|
|
|
if (ch == '<' && Peek(1) == '!' && Peek(2) == '-' && Peek(3) == '-') {
|
|
|
|
Advance(4);
|
|
|
|
aToken.mType = eCSSToken_HTMLComment;
|
|
|
|
aToken.mIdent.AssignLiteral("<!--");
|
2013-02-16 23:27:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-02-16 23:27:53 +00:00
|
|
|
|
|
|
|
// AT_KEYWORD
|
|
|
|
if (ch == '@') {
|
|
|
|
return ScanAtKeyword(aToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
// HASH
|
|
|
|
if (ch == '#') {
|
|
|
|
return ScanHash(aToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
// STRING
|
|
|
|
if (ch == '"' || ch == '\'') {
|
|
|
|
return ScanString(aToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match operators: ~= |= ^= $= *=
|
|
|
|
nsCSSTokenType opType = MatchOperatorType(ch);
|
|
|
|
if (opType != eCSSToken_Symbol && Peek(1) == '=') {
|
|
|
|
aToken.mType = opType;
|
|
|
|
Advance(2);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, a symbol (DELIM).
|
|
|
|
aToken.mSymbol = ch;
|
|
|
|
Advance();
|
|
|
|
return true;
|
2013-02-16 23:27:53 +00:00
|
|
|
}
|