From 6a0d31cce8d356b88948efa0d3ac32e2e299d8cc Mon Sep 17 00:00:00 2001 From: "heikki%netscape.com" Date: Fri, 17 May 2002 05:38:46 +0000 Subject: [PATCH] Bug 137628, depending on network speed the contents of comments could appear on a page in strict mode. Treat unclosed comment in strict mode as text. Fix line numbers in case we need to wait for more content before parsing. r=harishd, sr=jst. --- htmlparser/src/nsHTMLTokens.cpp | 110 +++++++++++++++---------- parser/htmlparser/src/nsHTMLTokens.cpp | 110 +++++++++++++++---------- 2 files changed, 134 insertions(+), 86 deletions(-) diff --git a/htmlparser/src/nsHTMLTokens.cpp b/htmlparser/src/nsHTMLTokens.cpp index ee36b8e756c4..4e816e0ca115 100644 --- a/htmlparser/src/nsHTMLTokens.cpp +++ b/htmlparser/src/nsHTMLTokens.cpp @@ -1032,62 +1032,83 @@ nsresult ConsumeStrictComment(nsScanner& aScanner, nsString& aString) { aScanner.EndReading(end); aScanner.CurrentPosition(current); - nsReadingIterator currentEnd = end, beginData = end; - PRBool balancedComment = PR_TRUE; + nsReadingIterator beginData = end; - static NS_NAMED_LITERAL_STRING(dashes,"--"); + // Regular comment must start with + if (balancedComment && IsCommentEnd(current, end, gt)) { + // done + current.advance(-2); + if (beginData != current) { // protects from #if 0 - // XXX We should do this, but it HANGS until bug 112943 is fixed: - aString = Substring(beginData, current); + // XXX We should do this, but it HANGS until bug 112943 is fixed: + aString = Substring(beginData, current); #else - // XXX Instead we can do this EVIL HACK (from jag): - PRUint32 len = Distance(beginData, current); - aString.SetLength(len); - PRUnichar* dest = NS_CONST_CAST(PRUnichar*, aString.get()); - copy_string(beginData, current, dest); + // XXX Instead we can do this EVIL HACK (from jag): + PRUint32 len = Distance(beginData, current); + aString.SetLength(len); + PRUnichar* dest = NS_CONST_CAST(PRUnichar*, aString.get()); + copy_string(beginData, current, dest); #endif + } + aScanner.SetPosition(++gt); + return NS_OK; + } else { + // Continue after the last '--' + currentEnd = end; } - aScanner.SetPosition(++gt); - return NS_OK; - } else { - // Continue after the last '--' - currentEnd = end; } } - // This might have been empty comment: - // Or it could have been something completely bogus like: - // Handle both cases below - aScanner.CurrentPosition(current); - beginData = current; - if (FindCharInReadable('>', current, end)) { + // If beginData == end, we did not find opening '--' + if (beginData == end) { + // This might have been empty comment: + // Or it could have been something completely bogus like: + // Handle both cases below + aScanner.CurrentPosition(current); + beginData = current; + if (FindCharInReadable('>', current, end)) { #if 0 - // XXX We should do this, but it HANGS until bug 112943 is fixed: - aString = Substring(beginData, current); + // XXX We should do this, but it HANGS until bug 112943 is fixed: + aString = Substring(beginData, current); #else - // XXX Instead we can do this EVIL HACK (from jag): - PRUint32 len = Distance(beginData, current); - aString.SetLength(len); - PRUnichar* dest = NS_CONST_CAST(PRUnichar*, aString.get()); - copy_string(beginData, current, dest); + // XXX Instead we can do this EVIL HACK (from jag): + PRUint32 len = Distance(beginData, current); + aString.SetLength(len); + PRUnichar* dest = NS_CONST_CAST(PRUnichar*, aString.get()); + copy_string(beginData, current, dest); #endif - aScanner.SetPosition(++current); - return NS_OK; + aScanner.SetPosition(++current); + return NS_OK; + } } - - return kEOF; // not really an nsresult, but... + + if (aScanner.IsIncremental()) { + // We got here because we saw the beginning of a comment, + // but not yet the end, and we are still loading the page. In that + // case the return value here will cause us to unwind, + // wait for more content, and try again. + // XXX For performance reasons we should cache where we were, and + // continue from there for next call + return kEOF; // not really an nsresult, but... + } + + // XXX We should return kNotAComment, parse comment open as text, and parse + // the rest of the document normally. Now we ALMOST do that: currentEnd = end, beginData = end; - PRBool balancedComment = PR_TRUE; + nsReadingIterator beginData = end; - static NS_NAMED_LITERAL_STRING(dashes,"--"); + // Regular comment must start with + if (balancedComment && IsCommentEnd(current, end, gt)) { + // done + current.advance(-2); + if (beginData != current) { // protects from #if 0 - // XXX We should do this, but it HANGS until bug 112943 is fixed: - aString = Substring(beginData, current); + // XXX We should do this, but it HANGS until bug 112943 is fixed: + aString = Substring(beginData, current); #else - // XXX Instead we can do this EVIL HACK (from jag): - PRUint32 len = Distance(beginData, current); - aString.SetLength(len); - PRUnichar* dest = NS_CONST_CAST(PRUnichar*, aString.get()); - copy_string(beginData, current, dest); + // XXX Instead we can do this EVIL HACK (from jag): + PRUint32 len = Distance(beginData, current); + aString.SetLength(len); + PRUnichar* dest = NS_CONST_CAST(PRUnichar*, aString.get()); + copy_string(beginData, current, dest); #endif + } + aScanner.SetPosition(++gt); + return NS_OK; + } else { + // Continue after the last '--' + currentEnd = end; } - aScanner.SetPosition(++gt); - return NS_OK; - } else { - // Continue after the last '--' - currentEnd = end; } } - // This might have been empty comment: - // Or it could have been something completely bogus like: - // Handle both cases below - aScanner.CurrentPosition(current); - beginData = current; - if (FindCharInReadable('>', current, end)) { + // If beginData == end, we did not find opening '--' + if (beginData == end) { + // This might have been empty comment: + // Or it could have been something completely bogus like: + // Handle both cases below + aScanner.CurrentPosition(current); + beginData = current; + if (FindCharInReadable('>', current, end)) { #if 0 - // XXX We should do this, but it HANGS until bug 112943 is fixed: - aString = Substring(beginData, current); + // XXX We should do this, but it HANGS until bug 112943 is fixed: + aString = Substring(beginData, current); #else - // XXX Instead we can do this EVIL HACK (from jag): - PRUint32 len = Distance(beginData, current); - aString.SetLength(len); - PRUnichar* dest = NS_CONST_CAST(PRUnichar*, aString.get()); - copy_string(beginData, current, dest); + // XXX Instead we can do this EVIL HACK (from jag): + PRUint32 len = Distance(beginData, current); + aString.SetLength(len); + PRUnichar* dest = NS_CONST_CAST(PRUnichar*, aString.get()); + copy_string(beginData, current, dest); #endif - aScanner.SetPosition(++current); - return NS_OK; + aScanner.SetPosition(++current); + return NS_OK; + } } - - return kEOF; // not really an nsresult, but... + + if (aScanner.IsIncremental()) { + // We got here because we saw the beginning of a comment, + // but not yet the end, and we are still loading the page. In that + // case the return value here will cause us to unwind, + // wait for more content, and try again. + // XXX For performance reasons we should cache where we were, and + // continue from there for next call + return kEOF; // not really an nsresult, but... + } + + // XXX We should return kNotAComment, parse comment open as text, and parse + // the rest of the document normally. Now we ALMOST do that: