improvements to parsing @import, HTML comments and identifiers

This commit is contained in:
peterl%netscape.com 1999-02-03 02:59:51 +00:00
Родитель c4de9e2154
Коммит 6e34e5ef7c
9 изменённых файлов: 126 добавлений и 90 удалений

Просмотреть файл

@ -337,6 +337,9 @@ CSSParserImpl::Parse(nsIUnicharInputStream* aInput,
if (!GetToken(errorCode, PR_TRUE)) {
break;
}
if (eCSSToken_HTMLComment == tk->mType) {
continue; // legal here only
}
if (eCSSToken_AtKeyword == tk->mType) {
ParseAtRule(errorCode);
continue;
@ -572,6 +575,7 @@ PRBool CSSParserImpl::ParseAtRule(PRInt32& aErrorCode)
PRBool CSSParserImpl::GatherMedia(PRInt32& aErrorCode, nsString& aMedia)
{
PRBool first = PR_TRUE;
PRBool expectIdent = PR_TRUE;
for (;;) {
if (!GetToken(aErrorCode, PR_TRUE)) {
break;
@ -585,16 +589,30 @@ PRBool CSSParserImpl::GatherMedia(PRInt32& aErrorCode, nsString& aMedia)
} else if (',' != symbol) {
UngetToken();
return PR_FALSE;
} else if (expectIdent) {
UngetToken();
return PR_FALSE;
}
else {
expectIdent = PR_TRUE;
}
}
else if (eCSSToken_Ident == mToken.mType) {
if (! first) {
aMedia.Append(',');
if (expectIdent) {
if (! first) {
aMedia.Append(',');
}
aMedia.Append(mToken.mIdent);
first = PR_FALSE;
expectIdent = PR_FALSE;
}
else {
UngetToken();
return PR_FALSE;
}
aMedia.Append(mToken.mIdent);
first = PR_FALSE;
}
else {
UngetToken();
break;
}
}
@ -619,9 +637,6 @@ PRBool CSSParserImpl::ParseImportRule(PRInt32& aErrorCode)
return PR_TRUE;
}
}
if ((eCSSToken_Symbol != mToken.mType) || (';' != mToken.mSymbol)) {
SkipUntil(aErrorCode, ';');
}
}
else if (eCSSToken_Function == mToken.mType) {
if (ExpectSymbol(aErrorCode, '(', PR_FALSE)) {
@ -639,12 +654,13 @@ PRBool CSSParserImpl::ParseImportRule(PRInt32& aErrorCode)
}
}
}
if ((eCSSToken_Symbol != mToken.mType) || (';' != mToken.mSymbol)) {
SkipUntil(aErrorCode, ';');
}
}
// invalid @import
if ((eCSSToken_Symbol != mToken.mType) || (';' != mToken.mSymbol)) {
SkipUntil(aErrorCode, ';');
}
mInHead = PR_FALSE;
// mInHead = PR_FALSE; // an invalid @import doesn't block other @imports (I think) awaiting clarification
return PR_TRUE;
}

Просмотреть файл

@ -245,6 +245,12 @@ PRBool nsCSSScanner::Next(PRInt32& aErrorCode, nsCSSToken& aToken)
if ((lexTable[ch] & START_IDENT) != 0) {
return ParseIdent(aErrorCode, ch, aToken);
}
if (ch == '-') { // possible ident
PRInt32 nextChar = Peek(aErrorCode);
if ((0 <= nextChar) && (0 != (lexTable[nextChar] & START_IDENT))) {
return ParseIdent(aErrorCode, ch, aToken);
}
}
// AT_KEYWORD
if (ch == '@') {
@ -290,13 +296,7 @@ PRBool nsCSSScanner::Next(PRInt32& aErrorCode, nsCSSToken& aToken)
}
if (ch == '/') {
PRInt32 nextChar = Peek(aErrorCode);
if (nextChar == '/') {
(void) Read(aErrorCode);
aToken.mIdent.SetLength(0);
aToken.mIdent.Append(PRUnichar(ch));
aToken.mIdent.Append(PRUnichar(ch));
return ParseEOLComment(aErrorCode, aToken);
} else if (nextChar == '*') {
if (nextChar == '*') {
(void) Read(aErrorCode);
aToken.mIdent.SetLength(0);
aToken.mIdent.Append(PRUnichar(ch));
@ -308,7 +308,7 @@ PRBool nsCSSScanner::Next(PRInt32& aErrorCode, nsCSSToken& aToken)
PRInt32 nextChar = Peek(aErrorCode);
if (nextChar == '!') {
(void) Read(aErrorCode);
aToken.mType = eCSSToken_WhiteSpace;
aToken.mType = eCSSToken_HTMLComment;
aToken.mIdent.SetLength(0);
aToken.mIdent.Append(PRUnichar(ch));
aToken.mIdent.Append(PRUnichar(nextChar));
@ -339,7 +339,7 @@ PRBool nsCSSScanner::Next(PRInt32& aErrorCode, nsCSSToken& aToken)
}
if (nextChar == '>') { // HTML end
(void) Read(aErrorCode);
aToken.mType = eCSSToken_WhiteSpace;
aToken.mType = eCSSToken_HTMLComment;
aToken.mIdent.SetLength(0);
while (0 < dashCount--) {
aToken.mIdent.Append('-');
@ -390,13 +390,7 @@ PRBool nsCSSScanner::NextURL(PRInt32& aErrorCode, nsCSSToken& aToken)
}
if (ch == '/') {
PRInt32 nextChar = Peek(aErrorCode);
if (nextChar == '/') {
(void) Read(aErrorCode);
aToken.mIdent.SetLength(0);
aToken.mIdent.Append(PRUnichar(ch));
aToken.mIdent.Append(PRUnichar(ch));
return ParseEOLComment(aErrorCode, aToken);
} else if (nextChar == '*') {
if (nextChar == '*') {
(void) Read(aErrorCode);
aToken.mIdent.SetLength(0);
aToken.mIdent.Append(PRUnichar(ch));

Просмотреть файл

@ -38,7 +38,7 @@ enum nsCSSTokenType {
// A css string (e.g. "foo" or 'foo')
eCSSToken_String = 5, // mSymbol + mIdent + mSymbol
// Whitespace (e.g. " " or "/* abc */" or "// foo <eol>")
// Whitespace (e.g. " " or "/* abc */")
eCSSToken_WhiteSpace = 6, // mIdent
// A css symbol (e.g. ':', ';', '+', etc.)
@ -49,8 +49,10 @@ enum nsCSSTokenType {
eCSSToken_Function = 9, // mIdent
eCSSToken_URL = 10, // mIdent
eCSSToken_InvalidURL = 11 // doesn't matter
eCSSToken_URL = 10, // mIdent
eCSSToken_InvalidURL = 11, // doesn't matter
eCSSToken_HTMLComment = 12 // "<!--" or "--{w}>"
};

Просмотреть файл

@ -337,6 +337,9 @@ CSSParserImpl::Parse(nsIUnicharInputStream* aInput,
if (!GetToken(errorCode, PR_TRUE)) {
break;
}
if (eCSSToken_HTMLComment == tk->mType) {
continue; // legal here only
}
if (eCSSToken_AtKeyword == tk->mType) {
ParseAtRule(errorCode);
continue;
@ -572,6 +575,7 @@ PRBool CSSParserImpl::ParseAtRule(PRInt32& aErrorCode)
PRBool CSSParserImpl::GatherMedia(PRInt32& aErrorCode, nsString& aMedia)
{
PRBool first = PR_TRUE;
PRBool expectIdent = PR_TRUE;
for (;;) {
if (!GetToken(aErrorCode, PR_TRUE)) {
break;
@ -585,16 +589,30 @@ PRBool CSSParserImpl::GatherMedia(PRInt32& aErrorCode, nsString& aMedia)
} else if (',' != symbol) {
UngetToken();
return PR_FALSE;
} else if (expectIdent) {
UngetToken();
return PR_FALSE;
}
else {
expectIdent = PR_TRUE;
}
}
else if (eCSSToken_Ident == mToken.mType) {
if (! first) {
aMedia.Append(',');
if (expectIdent) {
if (! first) {
aMedia.Append(',');
}
aMedia.Append(mToken.mIdent);
first = PR_FALSE;
expectIdent = PR_FALSE;
}
else {
UngetToken();
return PR_FALSE;
}
aMedia.Append(mToken.mIdent);
first = PR_FALSE;
}
else {
UngetToken();
break;
}
}
@ -619,9 +637,6 @@ PRBool CSSParserImpl::ParseImportRule(PRInt32& aErrorCode)
return PR_TRUE;
}
}
if ((eCSSToken_Symbol != mToken.mType) || (';' != mToken.mSymbol)) {
SkipUntil(aErrorCode, ';');
}
}
else if (eCSSToken_Function == mToken.mType) {
if (ExpectSymbol(aErrorCode, '(', PR_FALSE)) {
@ -639,12 +654,13 @@ PRBool CSSParserImpl::ParseImportRule(PRInt32& aErrorCode)
}
}
}
if ((eCSSToken_Symbol != mToken.mType) || (';' != mToken.mSymbol)) {
SkipUntil(aErrorCode, ';');
}
}
// invalid @import
if ((eCSSToken_Symbol != mToken.mType) || (';' != mToken.mSymbol)) {
SkipUntil(aErrorCode, ';');
}
mInHead = PR_FALSE;
// mInHead = PR_FALSE; // an invalid @import doesn't block other @imports (I think) awaiting clarification
return PR_TRUE;
}

Просмотреть файл

@ -245,6 +245,12 @@ PRBool nsCSSScanner::Next(PRInt32& aErrorCode, nsCSSToken& aToken)
if ((lexTable[ch] & START_IDENT) != 0) {
return ParseIdent(aErrorCode, ch, aToken);
}
if (ch == '-') { // possible ident
PRInt32 nextChar = Peek(aErrorCode);
if ((0 <= nextChar) && (0 != (lexTable[nextChar] & START_IDENT))) {
return ParseIdent(aErrorCode, ch, aToken);
}
}
// AT_KEYWORD
if (ch == '@') {
@ -290,13 +296,7 @@ PRBool nsCSSScanner::Next(PRInt32& aErrorCode, nsCSSToken& aToken)
}
if (ch == '/') {
PRInt32 nextChar = Peek(aErrorCode);
if (nextChar == '/') {
(void) Read(aErrorCode);
aToken.mIdent.SetLength(0);
aToken.mIdent.Append(PRUnichar(ch));
aToken.mIdent.Append(PRUnichar(ch));
return ParseEOLComment(aErrorCode, aToken);
} else if (nextChar == '*') {
if (nextChar == '*') {
(void) Read(aErrorCode);
aToken.mIdent.SetLength(0);
aToken.mIdent.Append(PRUnichar(ch));
@ -308,7 +308,7 @@ PRBool nsCSSScanner::Next(PRInt32& aErrorCode, nsCSSToken& aToken)
PRInt32 nextChar = Peek(aErrorCode);
if (nextChar == '!') {
(void) Read(aErrorCode);
aToken.mType = eCSSToken_WhiteSpace;
aToken.mType = eCSSToken_HTMLComment;
aToken.mIdent.SetLength(0);
aToken.mIdent.Append(PRUnichar(ch));
aToken.mIdent.Append(PRUnichar(nextChar));
@ -339,7 +339,7 @@ PRBool nsCSSScanner::Next(PRInt32& aErrorCode, nsCSSToken& aToken)
}
if (nextChar == '>') { // HTML end
(void) Read(aErrorCode);
aToken.mType = eCSSToken_WhiteSpace;
aToken.mType = eCSSToken_HTMLComment;
aToken.mIdent.SetLength(0);
while (0 < dashCount--) {
aToken.mIdent.Append('-');
@ -390,13 +390,7 @@ PRBool nsCSSScanner::NextURL(PRInt32& aErrorCode, nsCSSToken& aToken)
}
if (ch == '/') {
PRInt32 nextChar = Peek(aErrorCode);
if (nextChar == '/') {
(void) Read(aErrorCode);
aToken.mIdent.SetLength(0);
aToken.mIdent.Append(PRUnichar(ch));
aToken.mIdent.Append(PRUnichar(ch));
return ParseEOLComment(aErrorCode, aToken);
} else if (nextChar == '*') {
if (nextChar == '*') {
(void) Read(aErrorCode);
aToken.mIdent.SetLength(0);
aToken.mIdent.Append(PRUnichar(ch));

Просмотреть файл

@ -38,7 +38,7 @@ enum nsCSSTokenType {
// A css string (e.g. "foo" or 'foo')
eCSSToken_String = 5, // mSymbol + mIdent + mSymbol
// Whitespace (e.g. " " or "/* abc */" or "// foo <eol>")
// Whitespace (e.g. " " or "/* abc */")
eCSSToken_WhiteSpace = 6, // mIdent
// A css symbol (e.g. ':', ';', '+', etc.)
@ -49,8 +49,10 @@ enum nsCSSTokenType {
eCSSToken_Function = 9, // mIdent
eCSSToken_URL = 10, // mIdent
eCSSToken_InvalidURL = 11 // doesn't matter
eCSSToken_URL = 10, // mIdent
eCSSToken_InvalidURL = 11, // doesn't matter
eCSSToken_HTMLComment = 12 // "<!--" or "--{w}>"
};

Просмотреть файл

@ -337,6 +337,9 @@ CSSParserImpl::Parse(nsIUnicharInputStream* aInput,
if (!GetToken(errorCode, PR_TRUE)) {
break;
}
if (eCSSToken_HTMLComment == tk->mType) {
continue; // legal here only
}
if (eCSSToken_AtKeyword == tk->mType) {
ParseAtRule(errorCode);
continue;
@ -572,6 +575,7 @@ PRBool CSSParserImpl::ParseAtRule(PRInt32& aErrorCode)
PRBool CSSParserImpl::GatherMedia(PRInt32& aErrorCode, nsString& aMedia)
{
PRBool first = PR_TRUE;
PRBool expectIdent = PR_TRUE;
for (;;) {
if (!GetToken(aErrorCode, PR_TRUE)) {
break;
@ -585,16 +589,30 @@ PRBool CSSParserImpl::GatherMedia(PRInt32& aErrorCode, nsString& aMedia)
} else if (',' != symbol) {
UngetToken();
return PR_FALSE;
} else if (expectIdent) {
UngetToken();
return PR_FALSE;
}
else {
expectIdent = PR_TRUE;
}
}
else if (eCSSToken_Ident == mToken.mType) {
if (! first) {
aMedia.Append(',');
if (expectIdent) {
if (! first) {
aMedia.Append(',');
}
aMedia.Append(mToken.mIdent);
first = PR_FALSE;
expectIdent = PR_FALSE;
}
else {
UngetToken();
return PR_FALSE;
}
aMedia.Append(mToken.mIdent);
first = PR_FALSE;
}
else {
UngetToken();
break;
}
}
@ -619,9 +637,6 @@ PRBool CSSParserImpl::ParseImportRule(PRInt32& aErrorCode)
return PR_TRUE;
}
}
if ((eCSSToken_Symbol != mToken.mType) || (';' != mToken.mSymbol)) {
SkipUntil(aErrorCode, ';');
}
}
else if (eCSSToken_Function == mToken.mType) {
if (ExpectSymbol(aErrorCode, '(', PR_FALSE)) {
@ -639,12 +654,13 @@ PRBool CSSParserImpl::ParseImportRule(PRInt32& aErrorCode)
}
}
}
if ((eCSSToken_Symbol != mToken.mType) || (';' != mToken.mSymbol)) {
SkipUntil(aErrorCode, ';');
}
}
// invalid @import
if ((eCSSToken_Symbol != mToken.mType) || (';' != mToken.mSymbol)) {
SkipUntil(aErrorCode, ';');
}
mInHead = PR_FALSE;
// mInHead = PR_FALSE; // an invalid @import doesn't block other @imports (I think) awaiting clarification
return PR_TRUE;
}

Просмотреть файл

@ -245,6 +245,12 @@ PRBool nsCSSScanner::Next(PRInt32& aErrorCode, nsCSSToken& aToken)
if ((lexTable[ch] & START_IDENT) != 0) {
return ParseIdent(aErrorCode, ch, aToken);
}
if (ch == '-') { // possible ident
PRInt32 nextChar = Peek(aErrorCode);
if ((0 <= nextChar) && (0 != (lexTable[nextChar] & START_IDENT))) {
return ParseIdent(aErrorCode, ch, aToken);
}
}
// AT_KEYWORD
if (ch == '@') {
@ -290,13 +296,7 @@ PRBool nsCSSScanner::Next(PRInt32& aErrorCode, nsCSSToken& aToken)
}
if (ch == '/') {
PRInt32 nextChar = Peek(aErrorCode);
if (nextChar == '/') {
(void) Read(aErrorCode);
aToken.mIdent.SetLength(0);
aToken.mIdent.Append(PRUnichar(ch));
aToken.mIdent.Append(PRUnichar(ch));
return ParseEOLComment(aErrorCode, aToken);
} else if (nextChar == '*') {
if (nextChar == '*') {
(void) Read(aErrorCode);
aToken.mIdent.SetLength(0);
aToken.mIdent.Append(PRUnichar(ch));
@ -308,7 +308,7 @@ PRBool nsCSSScanner::Next(PRInt32& aErrorCode, nsCSSToken& aToken)
PRInt32 nextChar = Peek(aErrorCode);
if (nextChar == '!') {
(void) Read(aErrorCode);
aToken.mType = eCSSToken_WhiteSpace;
aToken.mType = eCSSToken_HTMLComment;
aToken.mIdent.SetLength(0);
aToken.mIdent.Append(PRUnichar(ch));
aToken.mIdent.Append(PRUnichar(nextChar));
@ -339,7 +339,7 @@ PRBool nsCSSScanner::Next(PRInt32& aErrorCode, nsCSSToken& aToken)
}
if (nextChar == '>') { // HTML end
(void) Read(aErrorCode);
aToken.mType = eCSSToken_WhiteSpace;
aToken.mType = eCSSToken_HTMLComment;
aToken.mIdent.SetLength(0);
while (0 < dashCount--) {
aToken.mIdent.Append('-');
@ -390,13 +390,7 @@ PRBool nsCSSScanner::NextURL(PRInt32& aErrorCode, nsCSSToken& aToken)
}
if (ch == '/') {
PRInt32 nextChar = Peek(aErrorCode);
if (nextChar == '/') {
(void) Read(aErrorCode);
aToken.mIdent.SetLength(0);
aToken.mIdent.Append(PRUnichar(ch));
aToken.mIdent.Append(PRUnichar(ch));
return ParseEOLComment(aErrorCode, aToken);
} else if (nextChar == '*') {
if (nextChar == '*') {
(void) Read(aErrorCode);
aToken.mIdent.SetLength(0);
aToken.mIdent.Append(PRUnichar(ch));

Просмотреть файл

@ -38,7 +38,7 @@ enum nsCSSTokenType {
// A css string (e.g. "foo" or 'foo')
eCSSToken_String = 5, // mSymbol + mIdent + mSymbol
// Whitespace (e.g. " " or "/* abc */" or "// foo <eol>")
// Whitespace (e.g. " " or "/* abc */")
eCSSToken_WhiteSpace = 6, // mIdent
// A css symbol (e.g. ':', ';', '+', etc.)
@ -49,8 +49,10 @@ enum nsCSSTokenType {
eCSSToken_Function = 9, // mIdent
eCSSToken_URL = 10, // mIdent
eCSSToken_InvalidURL = 11 // doesn't matter
eCSSToken_URL = 10, // mIdent
eCSSToken_InvalidURL = 11, // doesn't matter
eCSSToken_HTMLComment = 12 // "<!--" or "--{w}>"
};