From 5b28996bde1b7d9b59c7264ff97ba41f242affd2 Mon Sep 17 00:00:00 2001 From: "bzbarsky%mit.edu" Date: Mon, 24 Sep 2001 22:43:23 +0000 Subject: [PATCH] Add a document.contentType attribute to allow getting the MIME type of the current document from JS. The rest is moving around stuff to deal with nsIDocument already declaring GetContentType. bug 73847, r=peterv, sr=jst --- accessible/src/base/nsRootAccessible.cpp | 7 +++++-- accessible/src/nsRootAccessible.cpp | 7 +++++-- content/base/public/nsIDocument.h | 5 ----- content/base/src/nsDocument.cpp | 16 +++++++++++++--- content/base/src/nsDocument.h | 6 +----- content/base/src/nsRange.cpp | 6 ++++-- content/html/document/src/nsHTMLDocument.cpp | 7 ------- content/html/document/src/nsHTMLDocument.h | 2 -- content/xml/document/src/nsXMLDocument.cpp | 8 -------- content/xml/document/src/nsXMLDocument.h | 2 -- content/xul/document/src/nsXULDocument.cpp | 5 ++++- content/xul/document/src/nsXULDocument.h | 2 -- dom/public/idl/core/nsIDOMNSDocument.idl | 2 ++ 13 files changed, 34 insertions(+), 41 deletions(-) diff --git a/accessible/src/base/nsRootAccessible.cpp b/accessible/src/base/nsRootAccessible.cpp index a8549fa0daf7..fe44fda34d8e 100644 --- a/accessible/src/base/nsRootAccessible.cpp +++ b/accessible/src/base/nsRootAccessible.cpp @@ -24,6 +24,7 @@ #include "nsRootAccessible.h" #include "nsCOMPtr.h" #include "nsIDocument.h" +#include "nsIDOMNSDocument.h" #include "nsIPresShell.h" #include "nsIPresContext.h" #include "nsIContent.h" @@ -401,8 +402,10 @@ NS_IMETHODIMP nsDocAccessibleMixin::GetTitle(nsAWritableString& aTitle) NS_IMETHODIMP nsDocAccessibleMixin::GetMimeType(nsAWritableString& aMimeType) { - if (mDocument) - return mDocument->GetContentType(aMimeType); + nsCOMPtr domnsDocument(do_QueryInterface(mDocument)); + if (domnsDocument) { + return domnsDocument->GetContentType(aMimeType); + } return NS_ERROR_FAILURE; } diff --git a/accessible/src/nsRootAccessible.cpp b/accessible/src/nsRootAccessible.cpp index a8549fa0daf7..fe44fda34d8e 100644 --- a/accessible/src/nsRootAccessible.cpp +++ b/accessible/src/nsRootAccessible.cpp @@ -24,6 +24,7 @@ #include "nsRootAccessible.h" #include "nsCOMPtr.h" #include "nsIDocument.h" +#include "nsIDOMNSDocument.h" #include "nsIPresShell.h" #include "nsIPresContext.h" #include "nsIContent.h" @@ -401,8 +402,10 @@ NS_IMETHODIMP nsDocAccessibleMixin::GetTitle(nsAWritableString& aTitle) NS_IMETHODIMP nsDocAccessibleMixin::GetMimeType(nsAWritableString& aMimeType) { - if (mDocument) - return mDocument->GetContentType(aMimeType); + nsCOMPtr domnsDocument(do_QueryInterface(mDocument)); + if (domnsDocument) { + return domnsDocument->GetContentType(aMimeType); + } return NS_ERROR_FAILURE; } diff --git a/content/base/public/nsIDocument.h b/content/base/public/nsIDocument.h index 02f4c46ae040..8817c5420e1a 100644 --- a/content/base/public/nsIDocument.h +++ b/content/base/public/nsIDocument.h @@ -137,11 +137,6 @@ public: NS_IMETHOD GetBaseTarget(nsAWritableString &aBaseTarget)=0; NS_IMETHOD SetBaseTarget(const nsAReadableString &aBaseTarget)=0; - /** - * Return the content (mime) type of this document. - */ - NS_IMETHOD GetContentType(nsAWritableString& aContentType) const = 0; - /** * Return a standard name for the document's character set. This will * trigger a startDocumentLoad if necessary to answer the question. diff --git a/content/base/src/nsDocument.cpp b/content/base/src/nsDocument.cpp index 1011db3f7935..11d6606df955 100644 --- a/content/base/src/nsDocument.cpp +++ b/content/base/src/nsDocument.cpp @@ -708,6 +708,16 @@ nsDocument::StartDocumentLoad(const char* aCommand, if (aReset) rv = Reset(aChannel, aLoadGroup); + nsXPIDLCString contentType; + if (NS_SUCCEEDED(aChannel->GetContentType(getter_Copies(contentType)))) { + nsXPIDLCString::const_iterator start, end, semicolon; + contentType.BeginReading(start); + contentType.EndReading(end); + semicolon = start; + FindCharInReadable(';', semicolon, end); + CopyASCIItoUCS2(Substring(start, semicolon), mContentType); + } + PRBool have_contentLanguage = PR_FALSE; nsCOMPtr httpChannel = do_QueryInterface(aChannel); if (httpChannel) { @@ -791,10 +801,10 @@ nsDocument::AddPrincipal(nsIPrincipal *aNewPrincipal) } NS_IMETHODIMP -nsDocument::GetContentType(nsAWritableString& aContentType) const +nsDocument::GetContentType(nsAWritableString& aContentType) { - // Must be implemented by derived class. - return NS_ERROR_NOT_IMPLEMENTED; + aContentType = mContentType; + return NS_OK; } NS_IMETHODIMP diff --git a/content/base/src/nsDocument.h b/content/base/src/nsDocument.h index 02057c295933..dbeef530f820 100644 --- a/content/base/src/nsDocument.h +++ b/content/base/src/nsDocument.h @@ -249,11 +249,6 @@ public: */ NS_IMETHOD AddPrincipal(nsIPrincipal *aPrincipal); - /** - * Return the content (mime) type of this document. - */ - NS_IMETHOD GetContentType(nsAWritableString& aContentType) const; - /** * Return the content language of this document. */ @@ -596,6 +591,7 @@ protected: nsWeakPtr mFocusController; nsString mContentLanguage; + nsString mContentType; private: // These are not implemented and not supported. diff --git a/content/base/src/nsRange.cpp b/content/base/src/nsRange.cpp index 5d8ea8f8ec6d..3fcf40483ced 100644 --- a/content/base/src/nsRange.cpp +++ b/content/base/src/nsRange.cpp @@ -29,6 +29,7 @@ #include "nsIDOMNode.h" #include "nsIDOMDocument.h" +#include "nsIDOMNSDocument.h" #include "nsIDOMDocumentFragment.h" #include "nsIContent.h" #include "nsIDocument.h" @@ -2305,8 +2306,9 @@ nsRange::CreateContextualFragment(const nsAReadableString& aFragment, result = NS_NewHTMLFragmentContentSink(&sink); if (NS_SUCCEEDED(result)) { parser->SetContentSink(sink); - if (document) { - document->GetContentType(contentType); + nsCOMPtr domnsDocument(do_QueryInterface(document)); + if (domnsDocument) { + domnsDocument->GetContentType(contentType); } else { // Who're we kidding. This only works for html. diff --git a/content/html/document/src/nsHTMLDocument.cpp b/content/html/document/src/nsHTMLDocument.cpp index 12a62141412a..860942dd5e0f 100644 --- a/content/html/document/src/nsHTMLDocument.cpp +++ b/content/html/document/src/nsHTMLDocument.cpp @@ -361,13 +361,6 @@ nsHTMLDocument::Reset(nsIChannel* aChannel, nsILoadGroup* aLoadGroup) return result; } -NS_IMETHODIMP -nsHTMLDocument::GetContentType(nsAWritableString& aContentType) const -{ - aContentType.Assign(NS_LITERAL_STRING("text/html")); - return NS_OK; -} - NS_IMETHODIMP nsHTMLDocument::CreateShell(nsIPresContext* aContext, nsIViewManager* aViewManager, diff --git a/content/html/document/src/nsHTMLDocument.h b/content/html/document/src/nsHTMLDocument.h index 3d29f5738c5a..253b4dd0ab60 100644 --- a/content/html/document/src/nsHTMLDocument.h +++ b/content/html/document/src/nsHTMLDocument.h @@ -66,8 +66,6 @@ public: NS_IMETHOD Reset(nsIChannel* aChannel, nsILoadGroup* aLoadGroup); - NS_IMETHOD GetContentType(nsAWritableString& aContentType) const; - NS_IMETHOD CreateShell(nsIPresContext* aContext, nsIViewManager* aViewManager, nsIStyleSet* aStyleSet, diff --git a/content/xml/document/src/nsXMLDocument.cpp b/content/xml/document/src/nsXMLDocument.cpp index c7a0829cd235..34487304b25a 100644 --- a/content/xml/document/src/nsXMLDocument.cpp +++ b/content/xml/document/src/nsXMLDocument.cpp @@ -246,14 +246,6 @@ nsXMLDocument::Reset(nsIChannel* aChannel, nsILoadGroup* aLoadGroup) return result; } -NS_IMETHODIMP -nsXMLDocument::GetContentType(nsAWritableString& aContentType) const -{ - // XXX Should get document type from incoming stream - aContentType.Assign(NS_LITERAL_STRING("text/xml")); - return NS_OK; -} - NS_IMETHODIMP nsXMLDocument::GetInterface(const nsIID& aIID, void** aSink) { diff --git a/content/xml/document/src/nsXMLDocument.h b/content/xml/document/src/nsXMLDocument.h index 72e42223dc65..2d5ae8a56e85 100644 --- a/content/xml/document/src/nsXMLDocument.h +++ b/content/xml/document/src/nsXMLDocument.h @@ -54,8 +54,6 @@ public: NS_IMETHOD Reset(nsIChannel* aChannel, nsILoadGroup* aLoadGroup); - NS_IMETHOD GetContentType(nsAWritableString& aContentType) const; - NS_IMETHOD StartDocumentLoad(const char* aCommand, nsIChannel* channel, nsILoadGroup* aLoadGroup, diff --git a/content/xul/document/src/nsXULDocument.cpp b/content/xul/document/src/nsXULDocument.cpp index 7b745ecd7da5..79dc20975ba7 100644 --- a/content/xul/document/src/nsXULDocument.cpp +++ b/content/xul/document/src/nsXULDocument.cpp @@ -612,8 +612,11 @@ nsXULDocument::GetArena(nsIArena** aArena) return NS_OK; } +// Override the nsDocument.cpp method to keep from returning the +// "cached XUL" type which is completely internal and may confuse +// people NS_IMETHODIMP -nsXULDocument::GetContentType(nsAWritableString& aContentType) const +nsXULDocument::GetContentType(nsAWritableString& aContentType) { aContentType.Assign(NS_LITERAL_STRING("application/vnd.mozilla.xul+xml")); return NS_OK; diff --git a/content/xul/document/src/nsXULDocument.h b/content/xul/document/src/nsXULDocument.h index caeed3c2309f..6c1420113cd1 100644 --- a/content/xul/document/src/nsXULDocument.h +++ b/content/xul/document/src/nsXULDocument.h @@ -129,8 +129,6 @@ public: NS_IMETHOD Reset(nsIChannel* aChannel, nsILoadGroup* aLoadGroup); - NS_IMETHOD GetContentType(nsAWritableString& aContentType) const; - NS_IMETHOD StartDocumentLoad(const char* aCommand, nsIChannel *channel, nsILoadGroup* aLoadGroup, diff --git a/dom/public/idl/core/nsIDOMNSDocument.idl b/dom/public/idl/core/nsIDOMNSDocument.idl index 1fb60dfbc921..ac4f98bc2e88 100644 --- a/dom/public/idl/core/nsIDOMNSDocument.idl +++ b/dom/public/idl/core/nsIDOMNSDocument.idl @@ -39,6 +39,8 @@ interface nsIDOMNSDocument : nsISupports attribute DOMString title; + readonly attribute DOMString contentType; + nsIBoxObject getBoxObjectFor(in nsIDOMElement elt); void setBoxObjectFor(in nsIDOMElement elt, in nsIBoxObject boxObject);