From 2c0451a21deecb9f50ca9a1c730a7490b1329aed Mon Sep 17 00:00:00 2001 From: "bzbarsky%mit.edu" Date: Fri, 21 May 2004 21:32:11 +0000 Subject: [PATCH] Add suport for a "text/plain" first arg to document.open() (no support for any other types, and none really planned). Bug 73409, r+sr=jst --- content/html/document/src/nsHTMLDocument.cpp | 27 ++++++++++++-------- content/html/document/src/nsHTMLDocument.h | 3 ++- dom/public/idl/html/nsIDOMNSHTMLDocument.idl | 6 ++++- dom/src/base/nsDOMClassInfo.cpp | 18 ++++++++++++- parser/htmlparser/src/nsExpatDriver.cpp | 5 ++-- 5 files changed, 44 insertions(+), 15 deletions(-) diff --git a/content/html/document/src/nsHTMLDocument.cpp b/content/html/document/src/nsHTMLDocument.cpp index 9d81d8e45c5c..b358ffd2440a 100644 --- a/content/html/document/src/nsHTMLDocument.cpp +++ b/content/html/document/src/nsHTMLDocument.cpp @@ -1957,7 +1957,8 @@ nsHTMLDocument::GetSourceDocumentURI(nsIURI** sourceURI) // XXX TBI: accepting arguments to the open method. nsresult -nsHTMLDocument::OpenCommon(nsIURI* aSourceURI, PRBool aReplace) +nsHTMLDocument::OpenCommon(nsIURI* aSourceURI, const nsACString& aContentType, + PRBool aReplace) { // If we already have a parser we ignore the document.open call. if (mParser) { @@ -2077,6 +2078,9 @@ nsHTMLDocument::OpenCommon(nsIURI* aSourceURI, PRBool aReplace) mParser = do_CreateInstance(kCParserCID, &rv); + // This will be propagated to the parser when someone actually calls write() + mContentType = aContentType; + mIsWriting = 1; if (NS_SUCCEEDED(rv)) { @@ -2137,11 +2141,12 @@ NS_IMETHODIMP nsHTMLDocument::Open() { nsCOMPtr doc; - return Open(PR_FALSE, getter_AddRefs(doc)); + return Open(NS_LITERAL_CSTRING("text/html"), PR_FALSE, getter_AddRefs(doc)); } NS_IMETHODIMP -nsHTMLDocument::Open(PRBool aReplace, nsIDOMDocument** aReturn) +nsHTMLDocument::Open(const nsACString& aContentType, PRBool aReplace, + nsIDOMDocument** aReturn) { // XXX The URI of the newly created document will match // that of the source document. Is this right? @@ -2157,7 +2162,7 @@ nsHTMLDocument::Open(PRBool aReplace, nsIDOMDocument** aReturn) } NS_ENSURE_SUCCESS(rv, rv); - rv = OpenCommon(sourceURI, aReplace); + rv = OpenCommon(sourceURI, aContentType, aReplace); NS_ENSURE_SUCCESS(rv, rv); return CallQueryInterface(this, aReturn); @@ -2179,10 +2184,12 @@ nsHTMLDocument::Close() if (mParser && mIsWriting) { ++mWriteLevel; - rv = mParser->Parse(NS_LITERAL_STRING(""), - NS_GENERATE_PARSER_KEY(), - NS_LITERAL_CSTRING("text/html"), PR_FALSE, - PR_TRUE); + if (mContentType.EqualsLiteral("text/html")) { + rv = mParser->Parse(NS_LITERAL_STRING(""), + NS_GENERATE_PARSER_KEY(), + mContentType, PR_FALSE, + PR_TRUE); + } --mWriteLevel; mIsWriting = 0; mParser = nsnull; @@ -2260,12 +2267,12 @@ nsHTMLDocument::WriteCommon(const nsAString& aText, if (aNewlineTerminate) { rv = mParser->Parse(aText + new_line, NS_GENERATE_PARSER_KEY(), - NS_LITERAL_CSTRING("text/html"), PR_FALSE, + mContentType, PR_FALSE, (!mIsWriting || (mWriteLevel > 1))); } else { rv = mParser->Parse(aText, NS_GENERATE_PARSER_KEY(), - NS_LITERAL_CSTRING("text/html"), PR_FALSE, + mContentType, PR_FALSE, (!mIsWriting || (mWriteLevel > 1))); } diff --git a/content/html/document/src/nsHTMLDocument.h b/content/html/document/src/nsHTMLDocument.h index 7afc447573ee..7246b8280780 100644 --- a/content/html/document/src/nsHTMLDocument.h +++ b/content/html/document/src/nsHTMLDocument.h @@ -247,7 +247,8 @@ protected: nsresult WriteCommon(const nsAString& aText, PRBool aNewlineTerminate); nsresult ScriptWriteCommon(PRBool aNewlineTerminate); - nsresult OpenCommon(nsIURI* aUrl, PRBool aReplace); + nsresult OpenCommon(nsIURI* aUrl, const nsACString& aContentType, + PRBool aReplace); nsresult CreateAndAddWyciwygChannel(void); nsresult RemoveWyciwygChannel(void); diff --git a/dom/public/idl/html/nsIDOMNSHTMLDocument.idl b/dom/public/idl/html/nsIDOMNSHTMLDocument.idl index f39a7470684c..4f8fb5b9004a 100644 --- a/dom/public/idl/html/nsIDOMNSHTMLDocument.idl +++ b/dom/public/idl/html/nsIDOMNSHTMLDocument.idl @@ -57,9 +57,13 @@ interface nsIDOMNSHTMLDocument : nsISupports // This is the internal version of open(); note that the version // scriptable with JS is defined entirely in classinfo. + // If aContentType is not something supported by nsHTMLDocument and + // the HTML content sink, trying to write to the document will + // probably throw. // Pass aReplace = true to trigger a replacement of the previous // document in session history; pass false for normal history handling. - nsIDOMDocument open(in boolean aReplace); + nsIDOMDocument open(in ACString aContentType, + in boolean aReplace); // Scriptable versions of write(), writeln(), clear(). void write(); diff --git a/dom/src/base/nsDOMClassInfo.cpp b/dom/src/base/nsDOMClassInfo.cpp index 7bf6043ab4d7..e4f63c1ef97d 100644 --- a/dom/src/base/nsDOMClassInfo.cpp +++ b/dom/src/base/nsDOMClassInfo.cpp @@ -5396,6 +5396,22 @@ nsHTMLDocumentSH::DocumentOpen(JSContext *cx, JSObject *obj, uintN argc, nsCOMPtr doc(do_QueryInterface(native)); NS_ENSURE_TRUE(doc, JS_FALSE); + nsCAutoString contentType; + if (argc > 0) { + JSString* jsstr = JS_ValueToString(cx, argv[0]); + if (!jsstr) { + nsDOMClassInfo::ThrowJSException(cx, NS_ERROR_OUT_OF_MEMORY); + return JS_FALSE; + } + CopyUTF16toUTF8(NS_REINTERPRET_CAST(const PRUnichar*, + ::JS_GetStringChars(jsstr)), + contentType); + ToLowerCase(contentType); + } + if (!contentType.EqualsLiteral("text/plain")){ + contentType = "text/html"; + } + PRBool replace = PR_FALSE; if (argc > 1) { JSString* jsstr = JS_ValueToString(cx, argv[1]); @@ -5410,7 +5426,7 @@ nsHTMLDocumentSH::DocumentOpen(JSContext *cx, JSObject *obj, uintN argc, } nsCOMPtr retval; - rv = doc->Open(replace, getter_AddRefs(retval)); + rv = doc->Open(contentType, replace, getter_AddRefs(retval)); if (NS_FAILED(rv)) { nsDOMClassInfo::ThrowJSException(cx, rv); diff --git a/parser/htmlparser/src/nsExpatDriver.cpp b/parser/htmlparser/src/nsExpatDriver.cpp index c85afe08cc69..a559dca2de64 100644 --- a/parser/htmlparser/src/nsExpatDriver.cpp +++ b/parser/htmlparser/src/nsExpatDriver.cpp @@ -1099,8 +1099,9 @@ nsExpatDriver::GetMostDerivedIID(void) const NS_IMETHODIMP_(void) nsExpatDriver::Terminate() { - // XXX - not sure what happens to the unparsed data. - XML_BlockParser(mExpatParser); + if (mExpatParser) { + XML_BlockParser(mExpatParser); // XXX - not sure what happens to the unparsed data. + } mInternalState = NS_ERROR_HTMLPARSER_STOPPARSING; }