Remove bogus data-sniffing code in CanParse() impls, clean up signature, don't

peek a buffer if we're not planning to autodetect the parsemode.  Bug 113201,
r=rbs, sr=jst


git-svn-id: svn://10.0.0.236/trunk@156393 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
bzbarsky%mit.edu
2004-05-13 23:39:06 +00:00
parent 2d3ba6ffa2
commit 6a3b796123
9 changed files with 76 additions and 245 deletions

View File

@@ -107,14 +107,13 @@ public:
* a document in a given source-type.
* NOTE: Parsing always assumes that the end result will involve
* storing the result in the main content model.
* @update gess6/24/98
* @param aContentType -- string representing type of doc to be
* converted (ie text/html)
* @return TRUE if this DTD can satisfy the request; FALSE otherwise.
* @param aParserContext -- the context for this document (knows
* the content type, document type, parser command, etc).
* @return eUnknownDetect if you don't know how to parse it,
* eValidDetect if you do, but someone may have a better idea,
* ePrimaryDetect if you think you know best
*/
NS_IMETHOD_(eAutoDetectResult) CanParse(CParserContext& aParserContext,
const nsString& aBuffer,
PRInt32 aVersion) = 0;
NS_IMETHOD_(eAutoDetectResult) CanParse(CParserContext& aParserContext) = 0;
NS_IMETHOD WillBuildModel(const CParserContext& aParserContext,
nsITokenizer* aTokenizer,
@@ -232,7 +231,7 @@ public:
#define NS_DECL_NSIDTD \
NS_IMETHOD_(const nsIID&) GetMostDerivedIID(void) const;\
NS_IMETHOD CreateNewInstance(nsIDTD** aInstancePtrResult);\
NS_IMETHOD_(eAutoDetectResult) CanParse(CParserContext& aParserContext, const nsString& aBuffer, PRInt32 aVersion);\
NS_IMETHOD_(eAutoDetectResult) CanParse(CParserContext& aParserContext);\
NS_IMETHOD WillBuildModel( const CParserContext& aParserContext, nsITokenizer* aTokenizer, nsIContentSink* aSink);\
NS_IMETHOD DidBuildModel(nsresult anErrorCode,PRBool aNotifySink,nsIParser* aParser,nsIContentSink* aSink);\
NS_IMETHOD BuildModel(nsIParser* aParser,nsITokenizer* aTokenizer,nsITokenObserver* anObserver,nsIContentSink* aSink);\

View File

@@ -356,6 +356,7 @@ const PRUnichar kLeftSquareBracket = '[';
const PRUnichar kRightSquareBracket = ']';
const PRUnichar kNullCh = '\0';
// XXXbz these type defines should really just go away....
#define kHTMLTextContentType "text/html"
#define kXMLTextContentType "text/xml"
#define kXMLApplicationContentType "application/xml"

View File

@@ -295,72 +295,33 @@ nsresult CNavDTD::CreateNewInstance(nsIDTD** aInstancePtrResult)
/**
* This method is called to determine if the given DTD can parse
* a document in a given source-type.
* a document in a given source-type.
* NOTE: Parsing always assumes that the end result will involve
* storing the result in the main content model.
* @update gess 02/24/00
* @param
* @return TRUE if this DTD can satisfy the request; FALSE otherwise.
*/
* @param aParserContext -- the context for this document (knows
* the content type, document type, parser command, etc).
* @return eUnknownDetect if you don't know how to parse it,
* eValidDetect if you do, but someone may have a better idea,
* ePrimaryDetect if you think you know best
*/
NS_IMETHODIMP_(eAutoDetectResult)
CNavDTD::CanParse(CParserContext& aParserContext,
const nsString& aBuffer, PRInt32 aVersion)
CNavDTD::CanParse(CParserContext& aParserContext)
{
eAutoDetectResult result=eUnknownDetect;
if(aParserContext.mParserCommand != eViewSource) {
if(PR_TRUE==aParserContext.mMimeType.Equals(NS_LITERAL_CSTRING(kHTMLTextContentType))) {
result=ePrimaryDetect;
}
else if(PR_TRUE==aParserContext.mMimeType.Equals(NS_LITERAL_CSTRING(kPlainTextContentType))) {
result=ePrimaryDetect;
}
else if(PR_TRUE==aParserContext.mMimeType.Equals(NS_LITERAL_CSTRING(kTextCSSContentType))) {
result=ePrimaryDetect;
}
else if(PR_TRUE==aParserContext.mMimeType.Equals(NS_LITERAL_CSTRING(kApplicationJSContentType))) {
result=ePrimaryDetect;
}
else if(PR_TRUE==aParserContext.mMimeType.Equals(NS_LITERAL_CSTRING(kTextJSContentType))) {
result=ePrimaryDetect;
}
// do this for XML-based content-types so that we don't fall back
// to BufferContainsHTML() for known content types
// see bug 132681
// this will be cleaned up after moz 1.0 -alecf
else if (aParserContext.mMimeType.Equals(NS_LITERAL_CSTRING(kRDFTextContentType)) ||
aParserContext.mMimeType.Equals(NS_LITERAL_CSTRING(kXULTextContentType)) ||
aParserContext.mMimeType.Equals(NS_LITERAL_CSTRING(kXMLTextContentType)) ||
#ifdef MOZ_SVG
aParserContext.mMimeType.Equals(NS_LITERAL_CSTRING(kSVGTextContentType)) ||
#endif
aParserContext.mMimeType.Equals(NS_LITERAL_CSTRING(kXMLApplicationContentType))) {
result=eUnknownDetect;
}
else {
//otherwise, look into the buffer to see if you recognize anything...
PRBool theBufHasXML=PR_FALSE;
if(BufferContainsHTML(aBuffer,theBufHasXML)){
result = eValidDetect ;
if(0==aParserContext.mMimeType.Length()) {
aParserContext.SetMimeType(NS_LITERAL_CSTRING(kHTMLTextContentType));
if(!theBufHasXML) {
switch(aParserContext.mDTDMode) {
case eDTDMode_full_standards:
case eDTDMode_almost_standards:
result=eValidDetect;
break;
default:
result=ePrimaryDetect;
break;
}
}
else result=eValidDetect;
}
}
}
NS_ASSERTION(!aParserContext.mMimeType.IsEmpty(),
"How'd we get here with an unknown type?");
if (aParserContext.mParserCommand != eViewSource &&
aParserContext.mDocType != eXML) {
// This means that we're
// 1) Looking at a type the parser claimed to know how to handle (so XML
// or HTML or a plaintext type)
// 2) Not looking at XML
//
// Therefore, we want to handle this data with this DTD
return ePrimaryDetect;
}
return result;
return eUnknownDetect;
}
/**

View File

@@ -242,18 +242,19 @@ COtherDTD::CreateNewInstance(nsIDTD** aInstancePtrResult)
return result;
}
/**
/**
* This method is called to determine if the given DTD can parse
* a document in a given source-type.
* a document in a given source-type.
* NOTE: Parsing always assumes that the end result will involve
* storing the result in the main content model.
* @update gess6/24/98
* @param
* @return TRUE if this DTD can satisfy the request; FALSE otherwise.
* @param aParserContext -- the context for this document (knows
* the content type, document type, parser command, etc).
* @return eUnknownDetect if you don't know how to parse it,
* eValidDetect if you do, but someone may have a better idea,
* ePrimaryDetect if you think you know best
*/
NS_IMETHODIMP_(eAutoDetectResult)
COtherDTD::CanParse(CParserContext& aParserContext, const nsString& aBuffer,
PRInt32 aVersion)
COtherDTD::CanParse(CParserContext& aParserContext)
{
eAutoDetectResult result=eUnknownDetect;
@@ -273,29 +274,7 @@ COtherDTD::CanParse(CParserContext& aParserContext, const nsString& aBuffer,
break;
}
}
else {
//otherwise, look into the buffer to see if you recognize anything...
PRBool theBufHasXML=PR_FALSE;
if(BufferContainsHTML(aBuffer,theBufHasXML)){
result = eValidDetect ;
if(0==aParserContext.mMimeType.Length()) {
aParserContext.SetMimeType(NS_LITERAL_CSTRING(kHTMLTextContentType));
if(!theBufHasXML) {
switch(aParserContext.mDTDMode) {
case eDTDMode_full_standards:
case eDTDMode_almost_standards:
result=ePrimaryDetect;
break;
default:
result=eValidDetect;
break;
}
}
else result=eValidDetect;
}
}
}
}
}
}
return result;
}

View File

@@ -154,16 +154,16 @@ void CParserContext::SetMimeType(const nsACString& aMimeType){
mDocType=ePlainText;
if(mMimeType.Equals(NS_LITERAL_CSTRING(kHTMLTextContentType)))
if(mMimeType.EqualsLiteral(kHTMLTextContentType))
mDocType=eHTML_Strict;
else if (mMimeType.Equals(NS_LITERAL_CSTRING(kXMLTextContentType)) ||
mMimeType.Equals(NS_LITERAL_CSTRING(kXMLApplicationContentType)) ||
mMimeType.Equals(NS_LITERAL_CSTRING(kXHTMLApplicationContentType)) ||
mMimeType.Equals(NS_LITERAL_CSTRING(kXULTextContentType)) ||
else if (mMimeType.EqualsLiteral(kXMLTextContentType) ||
mMimeType.EqualsLiteral(kXMLApplicationContentType) ||
mMimeType.EqualsLiteral(kXHTMLApplicationContentType) ||
mMimeType.EqualsLiteral(kXULTextContentType) ||
#ifdef MOZ_SVG
mMimeType.Equals(NS_LITERAL_CSTRING(kSVGTextContentType)) ||
mMimeType.EqualsLiteral(kSVGTextContentType) ||
#endif
mMimeType.Equals(NS_LITERAL_CSTRING(kRDFTextContentType)))
mMimeType.EqualsLiteral(kRDFTextContentType))
mDocType=eXML;
}

View File

@@ -471,89 +471,6 @@ inline PRBool FindTagInSet(PRInt32 aTag,const eHTMLTags *aTagSet,PRInt32 aCount)
return PRBool(-1<IndexOfTagInSet(aTag,aTagSet,aCount));
}
/**
* Called from various DTD's to determine the type of data in the buffer...
* @update gess 06Jun2000
* @param aBuffer: contains a string with first block of html from source document
* @param aHasXMLFragment: tells us whether we detect XML in the buffer (based on PI)
* @return TRUE if we find HTML
*/
// This really doesn't need to be inline!
inline PRBool BufferContainsHTML(const nsString& aBuffer,
PRBool& aHasXMLFragment)
{
PRBool result=PR_FALSE;
aHasXMLFragment=PRBool(-1!=aBuffer.Find("<?XML",PR_TRUE,100));
PRInt32 theDocTypePos=aBuffer.Find("DOCTYPE",PR_TRUE,0,200);
if(-1!=theDocTypePos) {
PRInt32 theHTMLPos=aBuffer.Find("HTML",PR_TRUE,theDocTypePos+8,200);
if(-1==theHTMLPos) {
theHTMLPos=aBuffer.Find("ISO/IEC 15445",PR_TRUE,theDocTypePos+8,200);
if(-1==theHTMLPos) {
theHTMLPos=aBuffer.Find("HYPERTEXT MARKUP",PR_TRUE,theDocTypePos+8,200);
}
}
result=PRBool(-1!=theHTMLPos);
}
else {
//worst case scenario: let's look for a few HTML tags...
PRInt32 theCount = 0;
PRInt32 theTagCount = 0;
nsAString::const_iterator iter, end;
aBuffer.BeginReading(iter);
aBuffer.EndReading(end);
if (Distance(iter, end) > 200) {
end = iter;
end.advance(200);
}
for(theCount = 0; theCount < 5; ++theCount) {
if (!FindCharInReadable('<', iter, end)) {
break;
}
// we found what may be a start tag...
++iter; // step over the '<' character
nsAString::const_iterator tag_end(iter);
aBuffer.EndReading(end);
while (tag_end != end) {
const PRUnichar c = *tag_end;
if (c == ' ' || c == '>' || c == '"') {
break;
}
++tag_end;
}
nsHTMLTag theTag = nsHTMLTags::LookupTag(Substring(iter, tag_end));
if (theTag != eHTMLTag_userdefined) {
++theTagCount;
}
iter = tag_end;
}
// Claim HTML if we find at least 2 real html tags...
result = (2 <= theTagCount);
}
return result;
}
/******************************************************************************
This little structure is used to compute CRC32 values for our debug validator
******************************************************************************/

View File

@@ -979,33 +979,18 @@ nsExpatDriver::ConsumeToken(nsScanner& aScanner,
}
NS_IMETHODIMP_(eAutoDetectResult)
nsExpatDriver::CanParse(CParserContext& aParserContext,
const nsString& aBuffer,
PRInt32 aVersion)
nsExpatDriver::CanParse(CParserContext& aParserContext)
{
if (aParserContext.mParserCommand == eViewSource) {
return eUnknownDetect;
}
if (aParserContext.mMimeType.Equals(kXMLTextContentType) ||
aParserContext.mMimeType.Equals(kXMLApplicationContentType) ||
aParserContext.mMimeType.Equals(kXHTMLApplicationContentType)||
aParserContext.mMimeType.Equals(kRDFTextContentType) ||
#ifdef MOZ_SVG
aParserContext.mMimeType.Equals(kSVGTextContentType) ||
#endif
aParserContext.mMimeType.Equals(kXULTextContentType)) {
NS_ASSERTION(!aParserContext.mMimeType.IsEmpty(),
"How'd we get here with an unknown type?");
if (eViewSource != aParserContext.mParserCommand &&
aParserContext.mDocType == eXML) {
// The parser context already looked at the MIME type for us
return ePrimaryDetect;
}
if (aParserContext.mMimeType.IsEmpty() &&
aBuffer.Find("<?xml ") != kNotFound) {
aParserContext.SetMimeType(NS_LITERAL_CSTRING(kXMLTextContentType));
return eValidDetect;
}
return eUnknownDetect;
}

View File

@@ -1104,14 +1104,15 @@ void DetermineParseMode(const nsString& aBuffer,
static
nsresult
FindSuitableDTD(CParserContext& aParserContext,
const nsString& aBuffer,
PRBool* aReturn)
{
*aReturn = PR_FALSE;
//Let's start by trying the defaultDTD, if one exists...
if(aParserContext.mDTD)
if(aParserContext.mDTD->CanParse(aParserContext,aBuffer,0))
if(aParserContext.mDTD) {
eAutoDetectResult canParse = aParserContext.mDTD->CanParse(aParserContext);
if(canParse != eUnknownDetect && canParse != eInvalidDetect)
return PR_TRUE;
}
CSharedParserObjects* sharedObjects;
nsresult rv = GetSharedObjects(&sharedObjects);
@@ -1131,7 +1132,7 @@ FindSuitableDTD(CParserContext& aParserContext,
// 36233, 36754, 36491, 36323. Basically, we should avoid calling DTD's
// WillBuildModel() multiple times, i.e., we shouldn't leave auto-detect-status
// unknown.
eAutoDetectResult theResult = theDTD->CanParse(aParserContext,aBuffer,0);
eAutoDetectResult theResult = theDTD->CanParse(aParserContext);
if (eValidDetect == theResult){
aParserContext.mAutoDetectStatus = eValidDetect;
theBestDTD = theDTD;
@@ -1206,16 +1207,18 @@ nsParser::WillBuildModel(nsString& aFilename)
if (eUnknownDetect != mParserContext->mAutoDetectStatus)
return NS_OK;
nsAutoString theBuffer;
// XXXVidur Make a copy and only check in the first 1k
mParserContext->mScanner->Peek(theBuffer, 1024);
if (eDTDMode_unknown == mParserContext->mDTDMode ||
eDTDMode_autodetect == mParserContext->mDTDMode)
eDTDMode_autodetect == mParserContext->mDTDMode) {
nsAutoString theBuffer;
// XXXVidur Make a copy and only check in the first 1k
mParserContext->mScanner->Peek(theBuffer, 1024);
DetermineParseMode(theBuffer, mParserContext->mDTDMode,
mParserContext->mDocType, mParserContext->mMimeType);
}
PRBool found;
nsresult rv = FindSuitableDTD(*mParserContext,theBuffer, &found);
nsresult rv = FindSuitableDTD(*mParserContext, &found);
NS_ENSURE_SUCCESS(rv, rv);
if (!found)

View File

@@ -411,32 +411,18 @@ nsresult CViewSourceHTML::CreateNewInstance(nsIDTD** aInstancePtrResult){
* @return TRUE if this DTD can satisfy the request; FALSE otherwise.
*/
NS_IMETHODIMP_(eAutoDetectResult)
CViewSourceHTML::CanParse(CParserContext& aParserContext,
const nsString& aBuffer, PRInt32 aVersion)
CViewSourceHTML::CanParse(CParserContext& aParserContext)
{
eAutoDetectResult result=eUnknownDetect;
if (eViewSource == aParserContext.mParserCommand) {
if (aParserContext.mDocType == ePlainText) {
return eValidDetect;
}
if(eViewSource==aParserContext.mParserCommand) {
if(aParserContext.mMimeType.Equals(NS_LITERAL_CSTRING(kPlainTextContentType)) ||
aParserContext.mMimeType.Equals(NS_LITERAL_CSTRING(kTextCSSContentType)) ||
aParserContext.mMimeType.Equals(NS_LITERAL_CSTRING(kTextJSContentType)) ||
aParserContext.mMimeType.Equals(NS_LITERAL_CSTRING(kApplicationJSContentType))) {
result=eValidDetect;
}
if(aParserContext.mMimeType.Equals(NS_LITERAL_CSTRING(kXMLTextContentType)) ||
aParserContext.mMimeType.Equals(NS_LITERAL_CSTRING(kXMLApplicationContentType)) ||
aParserContext.mMimeType.Equals(NS_LITERAL_CSTRING(kXHTMLApplicationContentType)) ||
aParserContext.mMimeType.Equals(NS_LITERAL_CSTRING(kRDFTextContentType)) ||
aParserContext.mMimeType.Equals(NS_LITERAL_CSTRING(kHTMLTextContentType)) ||
aParserContext.mMimeType.Equals(NS_LITERAL_CSTRING(kXULTextContentType)) ||
#ifdef MOZ_SVG
aParserContext.mMimeType.Equals(NS_LITERAL_CSTRING(kSVGTextContentType)) ||
#endif
aParserContext.mMimeType.Equals(kSGMLTextContentType)) {
result=ePrimaryDetect;
}
// We claim to parse XML... now _that_ is funny.
return ePrimaryDetect;
}
return result;
return eUnknownDetect;
}