From 3b8aee3337a1fc9b270717f0c03373ca5cdaeb8f Mon Sep 17 00:00:00 2001 From: "peterl%netscape.com" Date: Fri, 25 Sep 1998 01:50:51 +0000 Subject: [PATCH] handle HTML comment tags as whitespace --- content/html/style/src/nsCSSScanner.cpp | 100 +++++++++++++++++++++--- content/html/style/src/nsCSSScanner.h | 6 +- layout/html/style/src/nsCSSScanner.cpp | 100 +++++++++++++++++++++--- layout/html/style/src/nsCSSScanner.h | 6 +- layout/style/nsCSSScanner.cpp | 100 +++++++++++++++++++++--- layout/style/nsCSSScanner.h | 6 +- 6 files changed, 282 insertions(+), 36 deletions(-) diff --git a/content/html/style/src/nsCSSScanner.cpp b/content/html/style/src/nsCSSScanner.cpp index ccc8032ffcf4..748460b1ab1e 100644 --- a/content/html/style/src/nsCSSScanner.cpp +++ b/content/html/style/src/nsCSSScanner.cpp @@ -87,16 +87,21 @@ nsCSSScanner::nsCSSScanner() mBuffer = new PRUnichar[BUFFER_SIZE]; mOffset = 0; mCount = 0; - mLookAhead = -1; + mPushback = mLocalPushback; + mPushbackCount = 0; + mPushbackSize = 4; } nsCSSScanner::~nsCSSScanner() { Close(); if (nsnull != mBuffer) { - delete mBuffer; + delete [] mBuffer; mBuffer = nsnull; } + if (mLocalPushback != mPushback) { + delete [] mPushback; + } } void nsCSSScanner::Init(nsIUnicharInputStream* aInput) @@ -118,9 +123,8 @@ void nsCSSScanner::Close() PRInt32 nsCSSScanner::Read(PRInt32* aErrorCode) { PRInt32 rv; - if (mLookAhead >= 0) { - rv = mLookAhead; - mLookAhead = -1; + if (0 < mPushbackCount) { + rv = PRInt32(mPushback[--mPushbackCount]); } else { if (mCount < 0) { return -1; @@ -142,23 +146,41 @@ PRInt32 nsCSSScanner::Read(PRInt32* aErrorCode) PRInt32 nsCSSScanner::Peek(PRInt32* aErrorCode) { - if (mLookAhead < 0) { - mLookAhead = Read(aErrorCode); - if (mLookAhead < 0) { + if (0 == mPushbackCount) { + mPushback[0] = Read(aErrorCode); + if (mPushback[0] < 0) { return -1; } + mPushbackCount++; } //printf("Peek => %x\n", mLookAhead); - return mLookAhead; + return PRInt32(mPushback[mPushbackCount - 1]); } void nsCSSScanner::Unread() { - NS_PRECONDITION((mLastRead >= 0) && (mLookAhead < 0), "double pushback"); - mLookAhead = mLastRead; + NS_PRECONDITION((mLastRead >= 0), "double pushback"); + Pushback(PRUnichar(mLastRead)); mLastRead = -1; } +void nsCSSScanner::Pushback(PRUnichar aChar) +{ + if (mPushbackCount == mPushbackSize) { // grow buffer + PRUnichar* newPushback = new PRUnichar[mPushbackSize + 4]; + if (nsnull == newPushback) { + return; + } + mPushbackSize += 4; + nsCRT::memcpy(newPushback, mPushback, sizeof(PRUnichar) * mPushbackCount); + if (mPushback != mLocalPushback) { + delete [] mPushback; + } + mPushback = newPushback; + } + mPushback[mPushbackCount++] = aChar; +} + PRBool nsCSSScanner::LookAhead(PRInt32* aErrorCode, PRUnichar aChar) { PRInt32 ch = Read(aErrorCode); @@ -282,6 +304,62 @@ PRBool nsCSSScanner::Next(PRInt32* aErrorCode, nsCSSToken* aToken) return ParseCComment(aErrorCode, aToken); } } + if (ch == '<') { // consume HTML comment tags as comments + PRInt32 nextChar = Peek(aErrorCode); + if (nextChar == '!') { + (void) Read(aErrorCode); + aToken->mType = eCSSToken_WhiteSpace; + aToken->mIdent.SetLength(0); + aToken->mIdent.Append(PRUnichar(ch)); + aToken->mIdent.Append(PRUnichar(nextChar)); + nextChar = Peek(aErrorCode); + while ((0 < nextChar) && (nextChar == '-')) { + Read(aErrorCode); + aToken->mIdent.Append(PRUnichar(nextChar)); + nextChar = Peek(aErrorCode); + } + return PR_TRUE; + } + } + if (ch == '-') { // check for HTML comment end + PRInt32 nextChar = Peek(aErrorCode); + if (nextChar == '-') { + PRInt32 dashCount = 1; + PRBool white = PR_FALSE; + while (nextChar == '-') { + (void) Read(aErrorCode); + dashCount++; + nextChar = Peek(aErrorCode); + } + if ((nextChar == ' ') || (nextChar == '\n') || + (nextChar == '\r') || (nextChar == '\t')) { + EatWhiteSpace(aErrorCode); + white = PR_TRUE; + nextChar = Peek(aErrorCode); + } + if (nextChar == '>') { // HTML end + (void) Read(aErrorCode); + aToken->mType = eCSSToken_WhiteSpace; + aToken->mIdent.SetLength(0); + while (0 < dashCount--) { + aToken->mIdent.Append('-'); + } + if (white) { + aToken->mIdent.Append(' '); + } + aToken->mIdent.Append('>'); + return PR_TRUE; + } + else { // wasn't an end comment, push it all back + if (white) { + Pushback(' '); + } + while (0 < --dashCount) { + Pushback('-'); + } + } + } + } } aToken->mType = eCSSToken_Symbol; aToken->mSymbol = ch; diff --git a/content/html/style/src/nsCSSScanner.h b/content/html/style/src/nsCSSScanner.h index afbe1da67fdb..9b9b24ac5641 100644 --- a/content/html/style/src/nsCSSScanner.h +++ b/content/html/style/src/nsCSSScanner.h @@ -89,6 +89,7 @@ protected: PRInt32 Read(PRInt32* aErrorCode); PRInt32 Peek(PRInt32* aErrorCode); void Unread(); + void Pushback(PRUnichar aChar); PRBool LookAhead(PRInt32* aErrorCode, PRUnichar aChar); PRBool EatWhiteSpace(PRInt32* aErrorCode); PRBool EatNewline(PRInt32* aErrorCode); @@ -111,8 +112,11 @@ protected: PRUnichar* mBuffer; PRInt32 mOffset; PRInt32 mCount; - PRInt32 mLookAhead; + PRUnichar* mPushback; + PRInt32 mPushbackCount; + PRInt32 mPushbackSize; PRInt32 mLastRead; + PRUnichar mLocalPushback[4]; }; #endif /* nsCSSScanner_h___ */ diff --git a/layout/html/style/src/nsCSSScanner.cpp b/layout/html/style/src/nsCSSScanner.cpp index ccc8032ffcf4..748460b1ab1e 100644 --- a/layout/html/style/src/nsCSSScanner.cpp +++ b/layout/html/style/src/nsCSSScanner.cpp @@ -87,16 +87,21 @@ nsCSSScanner::nsCSSScanner() mBuffer = new PRUnichar[BUFFER_SIZE]; mOffset = 0; mCount = 0; - mLookAhead = -1; + mPushback = mLocalPushback; + mPushbackCount = 0; + mPushbackSize = 4; } nsCSSScanner::~nsCSSScanner() { Close(); if (nsnull != mBuffer) { - delete mBuffer; + delete [] mBuffer; mBuffer = nsnull; } + if (mLocalPushback != mPushback) { + delete [] mPushback; + } } void nsCSSScanner::Init(nsIUnicharInputStream* aInput) @@ -118,9 +123,8 @@ void nsCSSScanner::Close() PRInt32 nsCSSScanner::Read(PRInt32* aErrorCode) { PRInt32 rv; - if (mLookAhead >= 0) { - rv = mLookAhead; - mLookAhead = -1; + if (0 < mPushbackCount) { + rv = PRInt32(mPushback[--mPushbackCount]); } else { if (mCount < 0) { return -1; @@ -142,23 +146,41 @@ PRInt32 nsCSSScanner::Read(PRInt32* aErrorCode) PRInt32 nsCSSScanner::Peek(PRInt32* aErrorCode) { - if (mLookAhead < 0) { - mLookAhead = Read(aErrorCode); - if (mLookAhead < 0) { + if (0 == mPushbackCount) { + mPushback[0] = Read(aErrorCode); + if (mPushback[0] < 0) { return -1; } + mPushbackCount++; } //printf("Peek => %x\n", mLookAhead); - return mLookAhead; + return PRInt32(mPushback[mPushbackCount - 1]); } void nsCSSScanner::Unread() { - NS_PRECONDITION((mLastRead >= 0) && (mLookAhead < 0), "double pushback"); - mLookAhead = mLastRead; + NS_PRECONDITION((mLastRead >= 0), "double pushback"); + Pushback(PRUnichar(mLastRead)); mLastRead = -1; } +void nsCSSScanner::Pushback(PRUnichar aChar) +{ + if (mPushbackCount == mPushbackSize) { // grow buffer + PRUnichar* newPushback = new PRUnichar[mPushbackSize + 4]; + if (nsnull == newPushback) { + return; + } + mPushbackSize += 4; + nsCRT::memcpy(newPushback, mPushback, sizeof(PRUnichar) * mPushbackCount); + if (mPushback != mLocalPushback) { + delete [] mPushback; + } + mPushback = newPushback; + } + mPushback[mPushbackCount++] = aChar; +} + PRBool nsCSSScanner::LookAhead(PRInt32* aErrorCode, PRUnichar aChar) { PRInt32 ch = Read(aErrorCode); @@ -282,6 +304,62 @@ PRBool nsCSSScanner::Next(PRInt32* aErrorCode, nsCSSToken* aToken) return ParseCComment(aErrorCode, aToken); } } + if (ch == '<') { // consume HTML comment tags as comments + PRInt32 nextChar = Peek(aErrorCode); + if (nextChar == '!') { + (void) Read(aErrorCode); + aToken->mType = eCSSToken_WhiteSpace; + aToken->mIdent.SetLength(0); + aToken->mIdent.Append(PRUnichar(ch)); + aToken->mIdent.Append(PRUnichar(nextChar)); + nextChar = Peek(aErrorCode); + while ((0 < nextChar) && (nextChar == '-')) { + Read(aErrorCode); + aToken->mIdent.Append(PRUnichar(nextChar)); + nextChar = Peek(aErrorCode); + } + return PR_TRUE; + } + } + if (ch == '-') { // check for HTML comment end + PRInt32 nextChar = Peek(aErrorCode); + if (nextChar == '-') { + PRInt32 dashCount = 1; + PRBool white = PR_FALSE; + while (nextChar == '-') { + (void) Read(aErrorCode); + dashCount++; + nextChar = Peek(aErrorCode); + } + if ((nextChar == ' ') || (nextChar == '\n') || + (nextChar == '\r') || (nextChar == '\t')) { + EatWhiteSpace(aErrorCode); + white = PR_TRUE; + nextChar = Peek(aErrorCode); + } + if (nextChar == '>') { // HTML end + (void) Read(aErrorCode); + aToken->mType = eCSSToken_WhiteSpace; + aToken->mIdent.SetLength(0); + while (0 < dashCount--) { + aToken->mIdent.Append('-'); + } + if (white) { + aToken->mIdent.Append(' '); + } + aToken->mIdent.Append('>'); + return PR_TRUE; + } + else { // wasn't an end comment, push it all back + if (white) { + Pushback(' '); + } + while (0 < --dashCount) { + Pushback('-'); + } + } + } + } } aToken->mType = eCSSToken_Symbol; aToken->mSymbol = ch; diff --git a/layout/html/style/src/nsCSSScanner.h b/layout/html/style/src/nsCSSScanner.h index afbe1da67fdb..9b9b24ac5641 100644 --- a/layout/html/style/src/nsCSSScanner.h +++ b/layout/html/style/src/nsCSSScanner.h @@ -89,6 +89,7 @@ protected: PRInt32 Read(PRInt32* aErrorCode); PRInt32 Peek(PRInt32* aErrorCode); void Unread(); + void Pushback(PRUnichar aChar); PRBool LookAhead(PRInt32* aErrorCode, PRUnichar aChar); PRBool EatWhiteSpace(PRInt32* aErrorCode); PRBool EatNewline(PRInt32* aErrorCode); @@ -111,8 +112,11 @@ protected: PRUnichar* mBuffer; PRInt32 mOffset; PRInt32 mCount; - PRInt32 mLookAhead; + PRUnichar* mPushback; + PRInt32 mPushbackCount; + PRInt32 mPushbackSize; PRInt32 mLastRead; + PRUnichar mLocalPushback[4]; }; #endif /* nsCSSScanner_h___ */ diff --git a/layout/style/nsCSSScanner.cpp b/layout/style/nsCSSScanner.cpp index ccc8032ffcf4..748460b1ab1e 100644 --- a/layout/style/nsCSSScanner.cpp +++ b/layout/style/nsCSSScanner.cpp @@ -87,16 +87,21 @@ nsCSSScanner::nsCSSScanner() mBuffer = new PRUnichar[BUFFER_SIZE]; mOffset = 0; mCount = 0; - mLookAhead = -1; + mPushback = mLocalPushback; + mPushbackCount = 0; + mPushbackSize = 4; } nsCSSScanner::~nsCSSScanner() { Close(); if (nsnull != mBuffer) { - delete mBuffer; + delete [] mBuffer; mBuffer = nsnull; } + if (mLocalPushback != mPushback) { + delete [] mPushback; + } } void nsCSSScanner::Init(nsIUnicharInputStream* aInput) @@ -118,9 +123,8 @@ void nsCSSScanner::Close() PRInt32 nsCSSScanner::Read(PRInt32* aErrorCode) { PRInt32 rv; - if (mLookAhead >= 0) { - rv = mLookAhead; - mLookAhead = -1; + if (0 < mPushbackCount) { + rv = PRInt32(mPushback[--mPushbackCount]); } else { if (mCount < 0) { return -1; @@ -142,23 +146,41 @@ PRInt32 nsCSSScanner::Read(PRInt32* aErrorCode) PRInt32 nsCSSScanner::Peek(PRInt32* aErrorCode) { - if (mLookAhead < 0) { - mLookAhead = Read(aErrorCode); - if (mLookAhead < 0) { + if (0 == mPushbackCount) { + mPushback[0] = Read(aErrorCode); + if (mPushback[0] < 0) { return -1; } + mPushbackCount++; } //printf("Peek => %x\n", mLookAhead); - return mLookAhead; + return PRInt32(mPushback[mPushbackCount - 1]); } void nsCSSScanner::Unread() { - NS_PRECONDITION((mLastRead >= 0) && (mLookAhead < 0), "double pushback"); - mLookAhead = mLastRead; + NS_PRECONDITION((mLastRead >= 0), "double pushback"); + Pushback(PRUnichar(mLastRead)); mLastRead = -1; } +void nsCSSScanner::Pushback(PRUnichar aChar) +{ + if (mPushbackCount == mPushbackSize) { // grow buffer + PRUnichar* newPushback = new PRUnichar[mPushbackSize + 4]; + if (nsnull == newPushback) { + return; + } + mPushbackSize += 4; + nsCRT::memcpy(newPushback, mPushback, sizeof(PRUnichar) * mPushbackCount); + if (mPushback != mLocalPushback) { + delete [] mPushback; + } + mPushback = newPushback; + } + mPushback[mPushbackCount++] = aChar; +} + PRBool nsCSSScanner::LookAhead(PRInt32* aErrorCode, PRUnichar aChar) { PRInt32 ch = Read(aErrorCode); @@ -282,6 +304,62 @@ PRBool nsCSSScanner::Next(PRInt32* aErrorCode, nsCSSToken* aToken) return ParseCComment(aErrorCode, aToken); } } + if (ch == '<') { // consume HTML comment tags as comments + PRInt32 nextChar = Peek(aErrorCode); + if (nextChar == '!') { + (void) Read(aErrorCode); + aToken->mType = eCSSToken_WhiteSpace; + aToken->mIdent.SetLength(0); + aToken->mIdent.Append(PRUnichar(ch)); + aToken->mIdent.Append(PRUnichar(nextChar)); + nextChar = Peek(aErrorCode); + while ((0 < nextChar) && (nextChar == '-')) { + Read(aErrorCode); + aToken->mIdent.Append(PRUnichar(nextChar)); + nextChar = Peek(aErrorCode); + } + return PR_TRUE; + } + } + if (ch == '-') { // check for HTML comment end + PRInt32 nextChar = Peek(aErrorCode); + if (nextChar == '-') { + PRInt32 dashCount = 1; + PRBool white = PR_FALSE; + while (nextChar == '-') { + (void) Read(aErrorCode); + dashCount++; + nextChar = Peek(aErrorCode); + } + if ((nextChar == ' ') || (nextChar == '\n') || + (nextChar == '\r') || (nextChar == '\t')) { + EatWhiteSpace(aErrorCode); + white = PR_TRUE; + nextChar = Peek(aErrorCode); + } + if (nextChar == '>') { // HTML end + (void) Read(aErrorCode); + aToken->mType = eCSSToken_WhiteSpace; + aToken->mIdent.SetLength(0); + while (0 < dashCount--) { + aToken->mIdent.Append('-'); + } + if (white) { + aToken->mIdent.Append(' '); + } + aToken->mIdent.Append('>'); + return PR_TRUE; + } + else { // wasn't an end comment, push it all back + if (white) { + Pushback(' '); + } + while (0 < --dashCount) { + Pushback('-'); + } + } + } + } } aToken->mType = eCSSToken_Symbol; aToken->mSymbol = ch; diff --git a/layout/style/nsCSSScanner.h b/layout/style/nsCSSScanner.h index afbe1da67fdb..9b9b24ac5641 100644 --- a/layout/style/nsCSSScanner.h +++ b/layout/style/nsCSSScanner.h @@ -89,6 +89,7 @@ protected: PRInt32 Read(PRInt32* aErrorCode); PRInt32 Peek(PRInt32* aErrorCode); void Unread(); + void Pushback(PRUnichar aChar); PRBool LookAhead(PRInt32* aErrorCode, PRUnichar aChar); PRBool EatWhiteSpace(PRInt32* aErrorCode); PRBool EatNewline(PRInt32* aErrorCode); @@ -111,8 +112,11 @@ protected: PRUnichar* mBuffer; PRInt32 mOffset; PRInt32 mCount; - PRInt32 mLookAhead; + PRUnichar* mPushback; + PRInt32 mPushbackCount; + PRInt32 mPushbackSize; PRInt32 mLastRead; + PRUnichar mLocalPushback[4]; }; #endif /* nsCSSScanner_h___ */