1999-05-19 03:15:10 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1999-05-19 03:15:10 +04:00
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1999-05-19 03:15:10 +04:00
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-05-19 03:15:10 +04:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:40:37 +03:00
|
|
|
* Copyright (C) 1999 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2000-05-03 03:07:11 +04:00
|
|
|
* This Original Code has been modified by IBM Corporation. Modifications made by IBM
|
|
|
|
* described herein are Copyright (c) International Business Machines Corporation, 2000.
|
|
|
|
* Modifications to Mozilla code or documentation identified per MPL Section 3.3
|
|
|
|
*
|
|
|
|
* Date Modified by Description of modification
|
|
|
|
* 04/20/2000 IBM Corp. OS/2 VisualAge build.
|
1999-05-19 03:15:10 +04:00
|
|
|
*/
|
|
|
|
#include "nsICSSLoader.h"
|
1999-12-04 04:27:14 +03:00
|
|
|
#include "nsICSSLoaderObserver.h"
|
1999-05-19 03:15:10 +04:00
|
|
|
#include "nsICSSParser.h"
|
|
|
|
#include "nsICSSStyleSheet.h"
|
|
|
|
|
|
|
|
#include "nsIParser.h"
|
1999-05-27 03:52:37 +04:00
|
|
|
#include "nsIContent.h"
|
1999-05-19 03:15:10 +04:00
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIStyleSheetLinkingElement.h"
|
|
|
|
#include "nsIDocument.h"
|
1999-06-15 10:19:21 +04:00
|
|
|
#include "nsINameSpaceManager.h"
|
2000-02-03 02:04:47 +03:00
|
|
|
#include "nsIStreamLoader.h"
|
1999-05-19 03:15:10 +04:00
|
|
|
#include "nsIUnicharInputStream.h"
|
2001-12-04 02:03:18 +03:00
|
|
|
#include "nsIConverterInputStream.h"
|
2000-04-23 06:01:40 +04:00
|
|
|
#include "nsICharsetConverterManager.h"
|
|
|
|
#include "nsIUnicodeDecoder.h"
|
|
|
|
#include "nsICharsetAlias.h"
|
2002-02-20 01:49:12 +03:00
|
|
|
#include "nsUnicharUtils.h"
|
1999-05-19 03:15:10 +04:00
|
|
|
#include "nsHashtable.h"
|
|
|
|
#include "nsIURL.h"
|
1999-06-23 07:29:44 +04:00
|
|
|
#include "nsIURL.h"
|
1999-06-18 21:34:08 +04:00
|
|
|
#include "nsIServiceManager.h"
|
1999-11-30 07:50:42 +03:00
|
|
|
#include "nsNetUtil.h"
|
1999-05-19 03:15:10 +04:00
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsVoidArray.h"
|
|
|
|
#include "nsISupportsArray.h"
|
1999-07-20 12:46:33 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2000-05-27 03:28:40 +04:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2001-08-17 06:03:34 +04:00
|
|
|
#include "nsITimelineService.h"
|
2001-10-25 04:34:20 +04:00
|
|
|
#include "nsIHttpChannel.h"
|
|
|
|
#include "nsHTMLAtoms.h"
|
2002-07-02 07:11:57 +04:00
|
|
|
#include "nsIConsoleService.h"
|
|
|
|
#include "nsIScriptError.h"
|
|
|
|
#include "nsIStringBundle.h"
|
1999-05-19 03:15:10 +04:00
|
|
|
|
2000-07-28 13:36:51 +04:00
|
|
|
#ifdef INCLUDE_XUL
|
|
|
|
#include "nsIXULPrototypeCache.h"
|
|
|
|
#endif
|
|
|
|
|
2001-08-05 01:19:53 +04:00
|
|
|
#include "nsIDOMMediaList.h"
|
|
|
|
#include "nsIDOMStyleSheet.h"
|
|
|
|
|
2000-04-23 06:01:40 +04:00
|
|
|
static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
|
2002-07-02 07:11:57 +04:00
|
|
|
static NS_DEFINE_CID(kCStringBundleServiceCID, NS_STRINGBUNDLESERVICE_CID);
|
1999-05-19 03:15:10 +04:00
|
|
|
|
|
|
|
class CSSLoaderImpl;
|
|
|
|
|
2001-01-28 02:06:33 +03:00
|
|
|
MOZ_DECL_CTOR_COUNTER(URLKey)
|
1999-10-09 00:41:19 +04:00
|
|
|
|
1999-05-19 03:15:10 +04:00
|
|
|
class URLKey: public nsHashKey {
|
|
|
|
public:
|
1999-06-23 07:29:44 +04:00
|
|
|
URLKey(nsIURI* aURL)
|
2002-03-06 10:48:55 +03:00
|
|
|
: mURL(aURL)
|
1999-05-19 03:15:10 +04:00
|
|
|
{
|
1999-10-09 00:41:19 +04:00
|
|
|
MOZ_COUNT_CTOR(URLKey);
|
1999-05-19 03:15:10 +04:00
|
|
|
mHashValue = 0;
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
mURL->GetSpec(mSpec);
|
|
|
|
if (!mSpec.IsEmpty()) {
|
|
|
|
mHashValue = nsCRT::HashCode(mSpec.get());
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
URLKey(const URLKey& aKey)
|
2002-03-06 10:48:55 +03:00
|
|
|
: mURL(aKey.mURL),
|
2001-09-09 01:13:20 +04:00
|
|
|
mHashValue(aKey.mHashValue),
|
2002-03-06 10:48:55 +03:00
|
|
|
mSpec(aKey.mSpec)
|
1999-05-19 03:15:10 +04:00
|
|
|
{
|
1999-10-09 00:41:19 +04:00
|
|
|
MOZ_COUNT_CTOR(URLKey);
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~URLKey(void)
|
|
|
|
{
|
1999-10-09 00:41:19 +04:00
|
|
|
MOZ_COUNT_DTOR(URLKey);
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
2000-08-10 10:19:37 +04:00
|
|
|
virtual PRUint32 HashCode(void) const
|
1999-05-19 03:15:10 +04:00
|
|
|
{
|
|
|
|
return mHashValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual PRBool Equals(const nsHashKey* aKey) const
|
|
|
|
{
|
|
|
|
URLKey* key = (URLKey*)aKey;
|
2000-10-09 12:51:50 +04:00
|
|
|
#if 0 // bug 47846
|
1999-07-07 05:29:49 +04:00
|
|
|
PRBool equals = PR_FALSE;
|
|
|
|
nsresult result = mURL->Equals(key->mURL, &equals);
|
|
|
|
return (NS_SUCCEEDED(result) && equals);
|
2000-10-09 12:51:50 +04:00
|
|
|
#else
|
2002-03-06 10:48:55 +03:00
|
|
|
return (nsCRT::strcasecmp(mSpec.get(), key->mSpec.get()) == 0);
|
2000-10-09 12:51:50 +04:00
|
|
|
#endif
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual nsHashKey *Clone(void) const
|
|
|
|
{
|
|
|
|
return new URLKey(*this);
|
|
|
|
}
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCOMPtr<nsIURI> mURL;
|
|
|
|
PRUint32 mHashValue;
|
|
|
|
nsSharableCString mSpec;
|
1999-05-19 03:15:10 +04:00
|
|
|
};
|
|
|
|
|
2000-02-03 02:04:47 +03:00
|
|
|
class SheetLoadData : public nsIStreamLoaderObserver
|
1999-11-02 09:49:44 +03:00
|
|
|
{
|
|
|
|
public:
|
1999-11-10 03:09:32 +03:00
|
|
|
virtual ~SheetLoadData(void);
|
1999-06-23 07:29:44 +04:00
|
|
|
SheetLoadData(CSSLoaderImpl* aLoader, nsIURI* aURL,
|
1999-06-15 10:19:21 +04:00
|
|
|
const nsString& aTitle, const nsString& aMedia,
|
|
|
|
PRInt32 aDefaultNameSpaceID,
|
1999-05-27 03:52:37 +04:00
|
|
|
nsIContent* aOwner, PRInt32 aDocIndex,
|
1999-12-04 04:27:14 +03:00
|
|
|
nsIParser* aParserToUnblock, PRBool aIsInline,
|
|
|
|
nsICSSLoaderObserver* aObserver);
|
1999-06-23 07:29:44 +04:00
|
|
|
SheetLoadData(CSSLoaderImpl* aLoader, nsIURI* aURL, const nsString& aMedia,
|
1999-06-15 10:19:21 +04:00
|
|
|
PRInt32 aDefaultNameSpaceID,
|
2001-08-05 01:19:53 +04:00
|
|
|
nsICSSStyleSheet* aParentSheet, PRInt32 aSheetIndex,
|
|
|
|
nsICSSImportRule* aParentRule);
|
1999-12-04 04:27:14 +03:00
|
|
|
SheetLoadData(CSSLoaderImpl* aLoader, nsIURI* aURL,
|
|
|
|
nsICSSLoaderObserver* aObserver);
|
1999-11-02 09:49:44 +03:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
2000-02-03 02:04:47 +03:00
|
|
|
NS_DECL_NSISTREAMLOADEROBSERVER
|
1999-05-19 03:15:10 +04:00
|
|
|
|
|
|
|
CSSLoaderImpl* mLoader;
|
1999-06-23 07:29:44 +04:00
|
|
|
nsIURI* mURL;
|
1999-05-19 03:15:10 +04:00
|
|
|
nsString mTitle;
|
|
|
|
nsString mMedia;
|
1999-06-15 10:19:21 +04:00
|
|
|
PRInt32 mDefaultNameSpaceID;
|
1999-05-19 03:15:10 +04:00
|
|
|
PRInt32 mSheetIndex;
|
|
|
|
|
1999-05-27 03:52:37 +04:00
|
|
|
nsIContent* mOwningElement;
|
1999-05-19 03:15:10 +04:00
|
|
|
nsIParser* mParserToUnblock;
|
|
|
|
|
|
|
|
nsICSSStyleSheet* mParentSheet;
|
2001-08-05 01:19:53 +04:00
|
|
|
nsICSSImportRule* mParentRule;
|
1999-05-19 03:15:10 +04:00
|
|
|
|
|
|
|
SheetLoadData* mNext;
|
|
|
|
SheetLoadData* mParentData;
|
|
|
|
|
|
|
|
PRUint32 mPendingChildren;
|
|
|
|
|
2001-12-15 02:10:42 +03:00
|
|
|
PRPackedBool mDidBlockParser;
|
|
|
|
|
|
|
|
PRPackedBool mIsInline;
|
|
|
|
PRPackedBool mIsAgent;
|
|
|
|
PRPackedBool mSyncLoad;
|
1999-05-27 03:52:37 +04:00
|
|
|
|
1999-12-04 04:27:14 +03:00
|
|
|
nsICSSLoaderObserver* mObserver;
|
1999-05-19 03:15:10 +04:00
|
|
|
};
|
|
|
|
|
2000-02-03 02:04:47 +03:00
|
|
|
NS_IMPL_ISUPPORTS1(SheetLoadData, nsIStreamLoaderObserver);
|
1999-11-02 09:49:44 +03:00
|
|
|
|
2001-01-28 02:06:33 +03:00
|
|
|
MOZ_DECL_CTOR_COUNTER(PendingSheetData)
|
1999-10-09 00:41:19 +04:00
|
|
|
|
1999-05-19 03:15:10 +04:00
|
|
|
struct PendingSheetData {
|
|
|
|
PendingSheetData(nsICSSStyleSheet* aSheet, PRInt32 aDocIndex,
|
1999-12-04 04:27:14 +03:00
|
|
|
nsIContent* aElement, nsICSSLoaderObserver* aObserver)
|
1999-05-19 03:15:10 +04:00
|
|
|
: mSheet(aSheet),
|
|
|
|
mDocIndex(aDocIndex),
|
|
|
|
mOwningElement(aElement),
|
1999-12-04 04:27:14 +03:00
|
|
|
mNotify(PR_FALSE),
|
|
|
|
mObserver(aObserver)
|
1999-05-19 03:15:10 +04:00
|
|
|
{
|
1999-10-09 00:41:19 +04:00
|
|
|
MOZ_COUNT_CTOR(PendingSheetData);
|
1999-05-19 03:15:10 +04:00
|
|
|
NS_ADDREF(mSheet);
|
|
|
|
NS_IF_ADDREF(mOwningElement);
|
1999-12-04 04:27:14 +03:00
|
|
|
NS_IF_ADDREF(mObserver);
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
~PendingSheetData(void)
|
|
|
|
{
|
1999-10-09 00:41:19 +04:00
|
|
|
MOZ_COUNT_DTOR(PendingSheetData);
|
1999-05-19 03:15:10 +04:00
|
|
|
NS_RELEASE(mSheet);
|
|
|
|
NS_IF_RELEASE(mOwningElement);
|
1999-12-04 04:27:14 +03:00
|
|
|
NS_IF_RELEASE(mObserver);
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsICSSStyleSheet* mSheet;
|
|
|
|
PRInt32 mDocIndex;
|
1999-05-27 03:52:37 +04:00
|
|
|
nsIContent* mOwningElement;
|
1999-05-19 03:15:10 +04:00
|
|
|
PRBool mNotify;
|
1999-12-04 04:27:14 +03:00
|
|
|
nsICSSLoaderObserver* mObserver;
|
1999-05-19 03:15:10 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class CSSLoaderImpl: public nsICSSLoader {
|
|
|
|
public:
|
|
|
|
CSSLoaderImpl(void);
|
|
|
|
virtual ~CSSLoaderImpl(void);
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD Init(nsIDocument* aDocument);
|
1999-05-27 03:52:37 +04:00
|
|
|
NS_IMETHOD DropDocumentReference(void);
|
1999-05-19 03:15:10 +04:00
|
|
|
|
|
|
|
NS_IMETHOD SetCaseSensitive(PRBool aCaseSensitive);
|
2002-06-26 01:16:17 +04:00
|
|
|
NS_IMETHOD SetCompatibilityMode(nsCompatibility aCompatMode);
|
2002-03-24 01:56:05 +03:00
|
|
|
NS_IMETHOD SetPreferredSheet(const nsAString& aTitle);
|
1999-05-19 03:15:10 +04:00
|
|
|
|
|
|
|
NS_IMETHOD GetParserFor(nsICSSStyleSheet* aSheet,
|
|
|
|
nsICSSParser** aParser);
|
|
|
|
NS_IMETHOD RecycleParser(nsICSSParser* aParser);
|
|
|
|
|
1999-05-27 03:52:37 +04:00
|
|
|
NS_IMETHOD LoadInlineStyle(nsIContent* aElement,
|
1999-05-19 03:15:10 +04:00
|
|
|
nsIUnicharInputStream* aIn,
|
|
|
|
const nsString& aTitle,
|
|
|
|
const nsString& aMedia,
|
1999-06-15 10:19:21 +04:00
|
|
|
PRInt32 aDefaultNameSpaceID,
|
1999-05-19 03:15:10 +04:00
|
|
|
PRInt32 aIndex,
|
|
|
|
nsIParser* aParserToUnblock,
|
1999-12-04 04:27:14 +03:00
|
|
|
PRBool& aCompleted,
|
|
|
|
nsICSSLoaderObserver* aObserver);
|
1999-05-19 03:15:10 +04:00
|
|
|
|
1999-05-27 03:52:37 +04:00
|
|
|
NS_IMETHOD LoadStyleLink(nsIContent* aElement,
|
1999-06-23 07:29:44 +04:00
|
|
|
nsIURI* aURL,
|
1999-05-19 03:15:10 +04:00
|
|
|
const nsString& aTitle,
|
|
|
|
const nsString& aMedia,
|
1999-06-15 10:19:21 +04:00
|
|
|
PRInt32 aDefaultNameSpaceID,
|
1999-05-19 03:15:10 +04:00
|
|
|
PRInt32 aIndex,
|
|
|
|
nsIParser* aParserToUnblock,
|
1999-12-04 04:27:14 +03:00
|
|
|
PRBool& aCompleted,
|
|
|
|
nsICSSLoaderObserver* aObserver);
|
1999-05-19 03:15:10 +04:00
|
|
|
|
|
|
|
NS_IMETHOD LoadChildSheet(nsICSSStyleSheet* aParentSheet,
|
1999-06-23 07:29:44 +04:00
|
|
|
nsIURI* aURL,
|
1999-05-19 03:15:10 +04:00
|
|
|
const nsString& aMedia,
|
1999-06-15 10:19:21 +04:00
|
|
|
PRInt32 aDefaultNameSpaceID,
|
2001-08-05 01:19:53 +04:00
|
|
|
PRInt32 aIndex,
|
|
|
|
nsICSSImportRule* aRule);
|
1999-05-19 03:15:10 +04:00
|
|
|
|
1999-06-23 07:29:44 +04:00
|
|
|
NS_IMETHOD LoadAgentSheet(nsIURI* aURL,
|
1999-12-04 04:27:14 +03:00
|
|
|
nsICSSStyleSheet*& aSheet,
|
|
|
|
PRBool& aCompleted,
|
|
|
|
nsICSSLoaderObserver* aObserver);
|
1999-05-27 03:52:37 +04:00
|
|
|
|
1999-05-19 03:15:10 +04:00
|
|
|
// local helper methods (public for access from statics)
|
|
|
|
void Cleanup(URLKey& aKey, SheetLoadData* aLoadData);
|
|
|
|
nsresult SheetComplete(nsICSSStyleSheet* aSheet, SheetLoadData* aLoadData);
|
|
|
|
|
|
|
|
nsresult ParseSheet(nsIUnicharInputStream* aIn, SheetLoadData* aLoadData,
|
1999-05-27 03:52:37 +04:00
|
|
|
PRBool& aCompleted, nsICSSStyleSheet*& aSheet);
|
1999-05-19 03:15:10 +04:00
|
|
|
|
2000-02-03 02:04:47 +03:00
|
|
|
void DidLoadStyle(nsIStreamLoader* aLoader,
|
2000-04-27 03:36:45 +04:00
|
|
|
nsString* aStyleData, // takes ownership, will delete when done
|
1999-05-19 03:15:10 +04:00
|
|
|
SheetLoadData* aLoadData,
|
|
|
|
nsresult aStatus);
|
|
|
|
|
|
|
|
nsresult SetMedia(nsICSSStyleSheet* aSheet, const nsString& aMedia);
|
|
|
|
|
|
|
|
nsresult PrepareSheet(nsICSSStyleSheet* aSheet, const nsString& aTitle,
|
|
|
|
const nsString& aMedia);
|
|
|
|
|
|
|
|
nsresult AddPendingSheet(nsICSSStyleSheet* aSheet, PRInt32 aDocIndex,
|
1999-12-04 04:27:14 +03:00
|
|
|
nsIContent* aElement,
|
|
|
|
nsICSSLoaderObserver* aObserver);
|
1999-05-19 03:15:10 +04:00
|
|
|
|
|
|
|
PRBool IsAlternate(const nsString& aTitle);
|
|
|
|
|
|
|
|
nsresult InsertSheetInDoc(nsICSSStyleSheet* aSheet, PRInt32 aDocIndex,
|
1999-12-04 04:27:14 +03:00
|
|
|
nsIContent* aElement, PRBool aNotify,
|
|
|
|
nsICSSLoaderObserver* aObserver);
|
1999-05-19 03:15:10 +04:00
|
|
|
|
|
|
|
nsresult InsertChildSheet(nsICSSStyleSheet* aSheet, nsICSSStyleSheet* aParentSheet,
|
|
|
|
PRInt32 aIndex);
|
|
|
|
|
|
|
|
nsresult LoadSheet(URLKey& aKey, SheetLoadData* aData);
|
|
|
|
|
|
|
|
nsIDocument* mDocument; // the document we live for
|
|
|
|
|
|
|
|
PRBool mCaseSensitive; // is document CSS case sensitive
|
2002-06-26 01:16:17 +04:00
|
|
|
nsCompatibility mCompatMode;
|
1999-05-19 03:15:10 +04:00
|
|
|
nsString mPreferredSheet; // title of preferred sheet
|
|
|
|
|
|
|
|
nsISupportsArray* mParsers; // array of CSS parsers
|
|
|
|
|
|
|
|
nsHashtable mLoadedSheets; // url to first sheet fully loaded for URL
|
|
|
|
nsHashtable mLoadingSheets; // all current loads
|
|
|
|
|
2001-09-06 23:16:03 +04:00
|
|
|
// mParsingData is (almost?) always needed, so create with storage
|
|
|
|
nsAutoVoidArray mParsingData; // array of data for sheets currently parsing
|
1999-05-19 03:15:10 +04:00
|
|
|
|
|
|
|
nsVoidArray mPendingDocSheets; // loaded sheet waiting for doc insertion
|
|
|
|
nsVoidArray mPendingAlternateSheets; // alternates waiting for load to start
|
|
|
|
|
2001-10-31 22:03:46 +03:00
|
|
|
nsHashtable mSheetMapTable; // map to insertion index arrays
|
|
|
|
|
2000-04-23 06:01:40 +04:00
|
|
|
// @charset support
|
|
|
|
nsString mCharset; // the charset we are using
|
|
|
|
|
2001-10-25 04:34:20 +04:00
|
|
|
NS_IMETHOD GetCharset(/*out*/nsAString &aCharsetDest) const; // PUBLIC
|
|
|
|
NS_IMETHOD SetCharset(/*in*/ const nsAString &aCharsetSrc); // PUBLIC
|
|
|
|
// public method for clients to set the charset if they know it
|
|
|
|
// NOTE: the SetCharset method will always get the preferred
|
|
|
|
// charset from the charset passed in unless it is the
|
|
|
|
// emptystring, which causes the default charset (that of the
|
|
|
|
// document, falling back to ISO-8869-1) to be set
|
2000-04-23 06:01:40 +04:00
|
|
|
|
2001-10-25 04:34:20 +04:00
|
|
|
nsresult SetCharset(/*in*/ const char* aStyleSheetData,
|
|
|
|
/*in*/ PRUint32 aDataLength);
|
2000-04-23 06:01:40 +04:00
|
|
|
// sets the charset based upon the data passed in
|
2001-10-25 04:34:20 +04:00
|
|
|
// - if the StyleSheetData is not empty and it has '@charset'
|
|
|
|
// as the first substring, then use that
|
|
|
|
// - otherwise return an error
|
2000-04-23 06:01:40 +04:00
|
|
|
|
2001-05-19 06:59:15 +04:00
|
|
|
// stop loading all sheets
|
|
|
|
NS_IMETHOD Stop(void);
|
|
|
|
|
|
|
|
// stop loading one sheet
|
|
|
|
NS_IMETHOD StopLoadingSheet(nsIURI* aURL);
|
|
|
|
|
1999-07-24 23:04:22 +04:00
|
|
|
#ifdef NS_DEBUG
|
|
|
|
PRBool mSyncCallback;
|
|
|
|
#endif
|
1999-05-19 03:15:10 +04:00
|
|
|
};
|
|
|
|
|
1999-06-23 07:29:44 +04:00
|
|
|
SheetLoadData::SheetLoadData(CSSLoaderImpl* aLoader, nsIURI* aURL,
|
1999-05-19 03:15:10 +04:00
|
|
|
const nsString& aTitle, const nsString& aMedia,
|
1999-06-15 10:19:21 +04:00
|
|
|
PRInt32 aDefaultNameSpaceID,
|
1999-05-27 03:52:37 +04:00
|
|
|
nsIContent* aOwner, PRInt32 aDocIndex,
|
1999-12-04 04:27:14 +03:00
|
|
|
nsIParser* aParserToUnblock, PRBool aIsInline,
|
|
|
|
nsICSSLoaderObserver* aObserver)
|
1999-05-19 03:15:10 +04:00
|
|
|
: mLoader(aLoader),
|
|
|
|
mURL(aURL),
|
|
|
|
mTitle(aTitle),
|
|
|
|
mMedia(aMedia),
|
1999-06-15 10:19:21 +04:00
|
|
|
mDefaultNameSpaceID(aDefaultNameSpaceID),
|
1999-05-19 03:15:10 +04:00
|
|
|
mSheetIndex(aDocIndex),
|
1999-07-18 04:26:14 +04:00
|
|
|
mOwningElement(aOwner),
|
1999-05-19 03:15:10 +04:00
|
|
|
mParserToUnblock(aParserToUnblock),
|
|
|
|
mParentSheet(nsnull),
|
2001-08-05 01:19:53 +04:00
|
|
|
mParentRule(nsnull),
|
1999-05-19 03:15:10 +04:00
|
|
|
mNext(nsnull),
|
|
|
|
mParentData(nsnull),
|
|
|
|
mPendingChildren(0),
|
2001-12-15 02:10:42 +03:00
|
|
|
mDidBlockParser(PR_FALSE),
|
1999-05-27 03:52:37 +04:00
|
|
|
mIsInline(aIsInline),
|
|
|
|
mIsAgent(PR_FALSE),
|
1999-09-30 05:39:57 +04:00
|
|
|
mSyncLoad(PR_FALSE),
|
1999-12-04 04:27:14 +03:00
|
|
|
mObserver(aObserver)
|
1999-05-19 03:15:10 +04:00
|
|
|
{
|
1999-11-02 09:49:44 +03:00
|
|
|
NS_INIT_REFCNT();
|
1999-05-19 03:15:10 +04:00
|
|
|
NS_ADDREF(mLoader);
|
|
|
|
NS_ADDREF(mURL);
|
|
|
|
NS_IF_ADDREF(mOwningElement);
|
|
|
|
NS_IF_ADDREF(mParserToUnblock);
|
1999-12-04 04:27:14 +03:00
|
|
|
NS_IF_ADDREF(mObserver);
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
1999-06-23 07:29:44 +04:00
|
|
|
SheetLoadData::SheetLoadData(CSSLoaderImpl* aLoader, nsIURI* aURL,
|
1999-06-15 10:19:21 +04:00
|
|
|
const nsString& aMedia,
|
|
|
|
PRInt32 aDefaultNameSpaceID,
|
2001-08-05 01:19:53 +04:00
|
|
|
nsICSSStyleSheet* aParentSheet,
|
|
|
|
PRInt32 aSheetIndex,
|
|
|
|
nsICSSImportRule* aParentRule)
|
1999-05-19 03:15:10 +04:00
|
|
|
: mLoader(aLoader),
|
|
|
|
mURL(aURL),
|
|
|
|
mTitle(),
|
|
|
|
mMedia(aMedia),
|
1999-06-15 10:19:21 +04:00
|
|
|
mDefaultNameSpaceID(aDefaultNameSpaceID),
|
1999-05-19 03:15:10 +04:00
|
|
|
mSheetIndex(aSheetIndex),
|
|
|
|
mOwningElement(nsnull),
|
|
|
|
mParserToUnblock(nsnull),
|
|
|
|
mParentSheet(aParentSheet),
|
2001-08-05 01:19:53 +04:00
|
|
|
mParentRule(aParentRule),
|
1999-05-19 03:15:10 +04:00
|
|
|
mNext(nsnull),
|
|
|
|
mParentData(nsnull),
|
|
|
|
mPendingChildren(0),
|
2002-06-18 06:46:11 +04:00
|
|
|
mDidBlockParser(PR_FALSE),
|
1999-05-27 03:52:37 +04:00
|
|
|
mIsInline(PR_FALSE),
|
|
|
|
mIsAgent(PR_FALSE),
|
1999-09-30 05:39:57 +04:00
|
|
|
mSyncLoad(PR_FALSE),
|
1999-12-04 04:27:14 +03:00
|
|
|
mObserver(nsnull)
|
1999-05-19 03:15:10 +04:00
|
|
|
{
|
1999-11-02 09:49:44 +03:00
|
|
|
NS_INIT_REFCNT();
|
1999-05-19 03:15:10 +04:00
|
|
|
NS_ADDREF(mLoader);
|
|
|
|
NS_ADDREF(mURL);
|
|
|
|
NS_ADDREF(mParentSheet);
|
2001-08-05 01:19:53 +04:00
|
|
|
NS_ADDREF(mParentRule);
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
1999-06-23 07:29:44 +04:00
|
|
|
SheetLoadData::SheetLoadData(CSSLoaderImpl* aLoader, nsIURI* aURL,
|
1999-12-04 04:27:14 +03:00
|
|
|
nsICSSLoaderObserver* aObserver)
|
1999-05-27 03:52:37 +04:00
|
|
|
: mLoader(aLoader),
|
|
|
|
mURL(aURL),
|
|
|
|
mTitle(),
|
|
|
|
mMedia(),
|
1999-06-22 23:21:54 +04:00
|
|
|
mDefaultNameSpaceID(kNameSpaceID_Unknown),
|
1999-05-27 03:52:37 +04:00
|
|
|
mSheetIndex(-1),
|
|
|
|
mOwningElement(nsnull),
|
|
|
|
mParserToUnblock(nsnull),
|
|
|
|
mParentSheet(nsnull),
|
2001-08-05 01:19:53 +04:00
|
|
|
mParentRule(nsnull),
|
1999-05-27 03:52:37 +04:00
|
|
|
mNext(nsnull),
|
|
|
|
mParentData(nsnull),
|
|
|
|
mPendingChildren(0),
|
2002-06-18 06:46:11 +04:00
|
|
|
mDidBlockParser(PR_FALSE),
|
1999-05-27 03:52:37 +04:00
|
|
|
mIsInline(PR_FALSE),
|
|
|
|
mIsAgent(PR_TRUE),
|
1999-12-04 04:27:14 +03:00
|
|
|
mSyncLoad(PRBool(nsnull == aObserver)),
|
|
|
|
mObserver(aObserver)
|
1999-05-27 03:52:37 +04:00
|
|
|
{
|
1999-11-02 09:49:44 +03:00
|
|
|
NS_INIT_REFCNT();
|
1999-05-27 03:52:37 +04:00
|
|
|
NS_ADDREF(mLoader);
|
|
|
|
NS_ADDREF(mURL);
|
1999-12-04 04:27:14 +03:00
|
|
|
NS_IF_ADDREF(mObserver);
|
1999-05-27 03:52:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-05-19 03:15:10 +04:00
|
|
|
SheetLoadData::~SheetLoadData(void)
|
|
|
|
{
|
|
|
|
NS_RELEASE(mLoader);
|
|
|
|
NS_RELEASE(mURL);
|
|
|
|
NS_IF_RELEASE(mOwningElement);
|
|
|
|
NS_IF_RELEASE(mParserToUnblock);
|
|
|
|
NS_IF_RELEASE(mParentSheet);
|
2001-08-05 01:19:53 +04:00
|
|
|
NS_IF_RELEASE(mParentRule);
|
1999-12-04 04:27:14 +03:00
|
|
|
NS_IF_RELEASE(mObserver);
|
2000-05-17 22:29:30 +04:00
|
|
|
NS_IF_RELEASE(mNext);
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CSSLoaderImpl::CSSLoaderImpl(void)
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
mDocument = nsnull;
|
|
|
|
mCaseSensitive = PR_FALSE;
|
2002-06-26 01:16:17 +04:00
|
|
|
mCompatMode = eCompatibility_FullStandards;
|
1999-05-19 03:15:10 +04:00
|
|
|
mParsers = nsnull;
|
2001-10-25 04:34:20 +04:00
|
|
|
SetCharset(NS_LITERAL_STRING(""));
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
2000-05-03 03:07:11 +04:00
|
|
|
static PRBool PR_CALLBACK ReleaseSheet(nsHashKey* aKey, void* aData, void* aClosure)
|
1999-05-19 03:15:10 +04:00
|
|
|
{
|
|
|
|
nsICSSStyleSheet* sheet = (nsICSSStyleSheet*)aData;
|
|
|
|
NS_RELEASE(sheet);
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2000-05-03 03:07:11 +04:00
|
|
|
static PRBool PR_CALLBACK DeleteHashLoadData(nsHashKey* aKey, void* aData, void* aClosure)
|
1999-05-19 03:15:10 +04:00
|
|
|
{
|
|
|
|
SheetLoadData* data = (SheetLoadData*)aData;
|
1999-11-02 09:49:44 +03:00
|
|
|
NS_RELEASE(data);
|
1999-05-19 03:15:10 +04:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2000-05-03 03:07:11 +04:00
|
|
|
static PRBool PR_CALLBACK DeletePendingData(void* aData, void* aClosure)
|
1999-05-19 03:15:10 +04:00
|
|
|
{
|
|
|
|
PendingSheetData* data = (PendingSheetData*)aData;
|
|
|
|
delete data;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2000-05-03 03:07:11 +04:00
|
|
|
static PRBool PR_CALLBACK DeleteLoadData(void* aData, void* aClosure)
|
1999-05-19 03:15:10 +04:00
|
|
|
{
|
|
|
|
SheetLoadData* data = (SheetLoadData*)aData;
|
1999-11-02 09:49:44 +03:00
|
|
|
NS_RELEASE(data);
|
1999-05-19 03:15:10 +04:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2001-10-31 22:03:46 +03:00
|
|
|
static PRBool PR_CALLBACK DeleteSheetMap(nsHashKey* aKey, void* aData, void* aClosure)
|
|
|
|
{
|
|
|
|
nsAutoVoidArray* map = (nsAutoVoidArray*)aData;
|
|
|
|
delete map;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
1999-05-19 03:15:10 +04:00
|
|
|
CSSLoaderImpl::~CSSLoaderImpl(void)
|
|
|
|
{
|
2001-05-19 06:59:15 +04:00
|
|
|
if (mLoadingSheets.Count() > 0) {
|
|
|
|
Stop();
|
|
|
|
}
|
1999-05-19 03:15:10 +04:00
|
|
|
NS_IF_RELEASE(mParsers);
|
|
|
|
mLoadedSheets.Enumerate(ReleaseSheet);
|
|
|
|
mLoadingSheets.Enumerate(DeleteHashLoadData);
|
|
|
|
mPendingDocSheets.EnumerateForwards(DeletePendingData, nsnull);
|
|
|
|
mPendingAlternateSheets.EnumerateForwards(DeleteLoadData, nsnull);
|
2001-10-31 22:03:46 +03:00
|
|
|
mSheetMapTable.Enumerate(DeleteSheetMap);
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
2001-08-21 05:48:11 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(CSSLoaderImpl, nsICSSLoader)
|
1999-05-19 03:15:10 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CSSLoaderImpl::Init(nsIDocument* aDocument)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(! mDocument, "already initialized");
|
|
|
|
if (! mDocument) {
|
|
|
|
mDocument = aDocument;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_ALREADY_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
1999-05-27 03:52:37 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
CSSLoaderImpl::DropDocumentReference(void)
|
|
|
|
{
|
|
|
|
mDocument = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-05-19 03:15:10 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
CSSLoaderImpl::SetCaseSensitive(PRBool aCaseSensitive)
|
|
|
|
{
|
|
|
|
mCaseSensitive = aCaseSensitive;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-07-07 05:29:49 +04:00
|
|
|
NS_IMETHODIMP
|
2002-06-26 01:16:17 +04:00
|
|
|
CSSLoaderImpl::SetCompatibilityMode(nsCompatibility aCompatMode)
|
1999-07-07 05:29:49 +04:00
|
|
|
{
|
2002-06-26 01:16:17 +04:00
|
|
|
mCompatMode = aCompatMode;
|
1999-07-07 05:29:49 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-05-19 03:15:10 +04:00
|
|
|
NS_IMETHODIMP
|
2002-03-24 01:56:05 +03:00
|
|
|
CSSLoaderImpl::SetPreferredSheet(const nsAString& aTitle)
|
1999-05-19 03:15:10 +04:00
|
|
|
{
|
|
|
|
mPreferredSheet = aTitle;
|
|
|
|
|
|
|
|
// start any pending alternates that aren't alternates anymore
|
|
|
|
PRInt32 index = 0;
|
|
|
|
while (index < mPendingAlternateSheets.Count()) { // count will change during loop
|
|
|
|
SheetLoadData* data = (SheetLoadData*)mPendingAlternateSheets.ElementAt(index);
|
|
|
|
if (! IsAlternate(data->mTitle)) {
|
|
|
|
mPendingAlternateSheets.RemoveElementAt(index);
|
|
|
|
URLKey key(data->mURL);
|
|
|
|
LoadSheet(key, data); // this may steal pending alternates too
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CSSLoaderImpl::GetParserFor(nsICSSStyleSheet* aSheet,
|
|
|
|
nsICSSParser** aParser)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aParser, "null pointer");
|
|
|
|
if (! aParser) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
*aParser = nsnull;
|
|
|
|
if (mParsers) {
|
|
|
|
PRUint32 count = 0;
|
|
|
|
mParsers->Count(&count);
|
|
|
|
if (0 < count--) {
|
|
|
|
*aParser = (nsICSSParser*)mParsers->ElementAt(count);
|
|
|
|
mParsers->RemoveElementAt(count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! *aParser) {
|
|
|
|
result = NS_NewCSSParser(aParser);
|
|
|
|
}
|
|
|
|
if (*aParser) {
|
|
|
|
(*aParser)->SetCaseSensitive(mCaseSensitive);
|
2002-06-26 01:16:17 +04:00
|
|
|
(*aParser)->SetQuirkMode(mCompatMode == eCompatibility_NavQuirks);
|
2000-04-23 06:01:40 +04:00
|
|
|
(*aParser)->SetCharset(mCharset);
|
1999-05-19 03:15:10 +04:00
|
|
|
if (aSheet) {
|
|
|
|
(*aParser)->SetStyleSheet(aSheet);
|
|
|
|
}
|
|
|
|
(*aParser)->SetChildLoader(this);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CSSLoaderImpl::RecycleParser(nsICSSParser* aParser)
|
|
|
|
{
|
|
|
|
nsresult result = NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
if (aParser) {
|
|
|
|
if (! mParsers) {
|
|
|
|
result = NS_NewISupportsArray(&mParsers);
|
|
|
|
}
|
|
|
|
if (mParsers) {
|
2001-12-21 04:10:07 +03:00
|
|
|
result = mParsers->AppendElement(aParser);
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2002-07-02 07:11:57 +04:00
|
|
|
/**
|
|
|
|
* Report an error to the error console.
|
|
|
|
* @param aErrorName The name of a string in css.properties.
|
|
|
|
* @param aParams The parameters for that string in css.properties.
|
|
|
|
* @param aParamsLength The length of aParams.
|
|
|
|
* @param aErrorFlags Error/warning flag to pass to nsIScriptError::Init.
|
|
|
|
*
|
|
|
|
* XXX This should be a static method on a class called something like
|
|
|
|
* nsCSSUtils, since it's a general way of accessing css.properties and
|
|
|
|
* will be useful for localizability work on CSS parser error reporting.
|
|
|
|
* However, it would need some way of reporting source file name, text,
|
|
|
|
* line, and column information.
|
|
|
|
*/
|
|
|
|
static nsresult
|
|
|
|
ReportToConsole(const PRUnichar* aMessageName, const PRUnichar **aParams,
|
|
|
|
PRUint32 aParamsLength, PRUint32 aErrorFlags)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIConsoleService> consoleService =
|
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsIScriptError> errorObject =
|
|
|
|
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsIStringBundleService> stringBundleService =
|
|
|
|
do_GetService(kCStringBundleServiceCID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsIStringBundle> bundle;
|
|
|
|
rv = stringBundleService->CreateBundle(
|
|
|
|
"chrome://global/locale/css.properties", getter_AddRefs(bundle));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsXPIDLString errorText;
|
|
|
|
rv = bundle->FormatStringFromName(aMessageName, aParams, aParamsLength,
|
|
|
|
getter_Copies(errorText));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = errorObject->Init(errorText.get(),
|
|
|
|
NS_LITERAL_STRING("").get(), /* file name */
|
|
|
|
NS_LITERAL_STRING("").get(), /* source line */
|
|
|
|
0, /* line number */
|
|
|
|
0, /* column number */
|
|
|
|
aErrorFlags,
|
|
|
|
"CSS Loader");
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
consoleService->LogMessage(errorObject);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-11-02 09:49:44 +03:00
|
|
|
NS_IMETHODIMP
|
2000-02-03 02:04:47 +03:00
|
|
|
SheetLoadData::OnStreamComplete(nsIStreamLoader* aLoader,
|
2001-10-25 04:34:20 +04:00
|
|
|
nsISupports* aContext,
|
2000-02-03 02:04:47 +03:00
|
|
|
nsresult aStatus,
|
2001-10-25 04:34:20 +04:00
|
|
|
PRUint32 aStringLen,
|
|
|
|
const char* aString)
|
1999-05-19 03:15:10 +04:00
|
|
|
{
|
2001-08-17 06:03:34 +04:00
|
|
|
NS_TIMELINE_OUTDENT();
|
|
|
|
NS_TIMELINE_MARK_LOADER("SheetLoadData::OnStreamComplete(%s)", aLoader);
|
2000-04-23 06:01:40 +04:00
|
|
|
nsresult result = NS_OK;
|
2000-04-27 04:51:06 +04:00
|
|
|
nsString *strUnicodeBuffer = nsnull;
|
2000-04-23 06:01:40 +04:00
|
|
|
|
2002-06-18 06:46:11 +04:00
|
|
|
nsCOMPtr<nsIRequest> request;
|
|
|
|
result = aLoader->GetRequest(getter_AddRefs(request));
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
request = nsnull;
|
|
|
|
// If it's an HTTP channel, we want to make sure this is not an
|
|
|
|
// error document we got.
|
|
|
|
PRBool realDocument = PR_TRUE;
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(request));
|
|
|
|
if (httpChannel) {
|
2002-07-02 03:12:13 +04:00
|
|
|
PRBool requestSucceeded;
|
|
|
|
result = httpChannel->GetRequestSucceeded(&requestSucceeded);
|
|
|
|
if (NS_SUCCEEDED(result) && !requestSucceeded) {
|
2002-06-18 06:46:11 +04:00
|
|
|
realDocument = PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (realDocument && aString && aStringLen>0) {
|
2002-03-21 01:50:33 +03:00
|
|
|
nsCAutoString contentType;
|
2002-07-02 07:11:57 +04:00
|
|
|
nsCOMPtr<nsIChannel> channel(do_QueryInterface(request));
|
|
|
|
if (channel) {
|
|
|
|
channel->GetContentType(contentType);
|
2001-10-25 04:34:20 +04:00
|
|
|
}
|
2002-06-26 01:16:17 +04:00
|
|
|
if (mLoader->mCompatMode == eCompatibility_NavQuirks ||
|
2002-03-21 01:50:33 +03:00
|
|
|
contentType.Equals(NS_LITERAL_CSTRING("text/css")) ||
|
2001-11-19 23:30:29 +03:00
|
|
|
contentType.IsEmpty()) {
|
2002-07-02 07:11:57 +04:00
|
|
|
|
|
|
|
if (!contentType.IsEmpty() &&
|
|
|
|
contentType != NS_LITERAL_CSTRING("text/css")) {
|
|
|
|
nsCAutoString spec;
|
|
|
|
if (channel) {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
channel->GetURI(getter_AddRefs(uri));
|
|
|
|
if (uri)
|
|
|
|
uri->GetSpec(spec);
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsAFlatString& specUCS2 = NS_ConvertUTF8toUCS2(spec);
|
|
|
|
const nsAFlatString& ctypeUCS2 = NS_ConvertASCIItoUCS2(contentType);
|
|
|
|
const PRUnichar *strings[] = { specUCS2.get(), ctypeUCS2.get() };
|
|
|
|
|
|
|
|
ReportToConsole(NS_LITERAL_STRING("MimeNotCssWarn").get(), strings, 2,
|
|
|
|
nsIScriptError::warningFlag);
|
|
|
|
}
|
|
|
|
|
2001-11-19 23:30:29 +03:00
|
|
|
/*
|
|
|
|
* First determine the charset (if one is indicated)
|
2002-03-21 01:50:33 +03:00
|
|
|
* 1) Check nsIChannel::contentCharset
|
2001-11-19 23:30:29 +03:00
|
|
|
* 2) Check @charset rules
|
|
|
|
* 3) Check "charset" attribute of the <LINK> or <?xml-stylesheet?>
|
|
|
|
*
|
|
|
|
* If all these fail to give us a charset, fall back on our
|
|
|
|
* default (document charset or ISO-8859-1 if we have no document
|
|
|
|
* charset)
|
|
|
|
*/
|
2002-03-21 01:50:33 +03:00
|
|
|
nsAutoString strChannelCharset;
|
|
|
|
if (channel) {
|
|
|
|
nsCAutoString charsetVal;
|
|
|
|
channel->GetContentCharset(charsetVal);
|
|
|
|
CopyASCIItoUCS2(charsetVal, strChannelCharset);
|
2001-11-19 23:30:29 +03:00
|
|
|
}
|
|
|
|
result = NS_ERROR_NOT_AVAILABLE;
|
2002-03-21 01:50:33 +03:00
|
|
|
if (! strChannelCharset.IsEmpty()) {
|
|
|
|
result = mLoader->SetCharset(strChannelCharset);
|
2001-11-19 23:30:29 +03:00
|
|
|
}
|
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
// We have no charset or the HTTP charset is not recognized.
|
|
|
|
// Try @charset rules
|
|
|
|
result = mLoader->SetCharset(aString, aStringLen);
|
|
|
|
}
|
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
// Now try the charset on the <link> or processing instruction
|
|
|
|
// that loaded us
|
|
|
|
nsCOMPtr<nsIStyleSheetLinkingElement>
|
|
|
|
element(do_QueryInterface(mOwningElement));
|
|
|
|
if (element) {
|
|
|
|
nsAutoString linkCharset;
|
|
|
|
element->GetCharset(linkCharset);
|
|
|
|
if (! linkCharset.IsEmpty()) {
|
|
|
|
result = mLoader->SetCharset(linkCharset);
|
|
|
|
}
|
2001-10-25 04:34:20 +04:00
|
|
|
}
|
|
|
|
}
|
2001-11-19 23:30:29 +03:00
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
// no useful data on charset. Just set to empty string, and let
|
|
|
|
// SetCharset pick a sane default
|
|
|
|
mLoader->SetCharset(NS_LITERAL_STRING(""));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// now get the decoder
|
|
|
|
nsCOMPtr<nsICharsetConverterManager> ccm =
|
|
|
|
do_GetService(kCharsetConverterManagerCID, &result);
|
|
|
|
if (NS_SUCCEEDED(result) && ccm) {
|
|
|
|
nsString charset;
|
|
|
|
mLoader->GetCharset(charset);
|
|
|
|
nsIUnicodeDecoder *decoder = nsnull;
|
|
|
|
ccm->GetUnicodeDecoder(&charset,&decoder);
|
|
|
|
if (decoder) {
|
|
|
|
PRInt32 unicodeLength=0;
|
|
|
|
if (NS_SUCCEEDED(decoder->GetMaxLength(aString,aStringLen,&unicodeLength))) {
|
|
|
|
PRUnichar *unicodeString = nsnull;
|
|
|
|
strUnicodeBuffer = new nsString;
|
|
|
|
if (nsnull == strUnicodeBuffer) {
|
|
|
|
result = NS_ERROR_OUT_OF_MEMORY;
|
2000-04-27 03:36:45 +04:00
|
|
|
} else {
|
2001-11-19 23:30:29 +03:00
|
|
|
// make space for the decoding
|
|
|
|
strUnicodeBuffer->SetCapacity(unicodeLength);
|
|
|
|
unicodeString = (PRUnichar *) strUnicodeBuffer->get();
|
2002-04-09 04:29:03 +04:00
|
|
|
PRInt32 totalChars = 0;
|
|
|
|
PRInt32 unicharLength = unicodeLength;
|
|
|
|
do {
|
|
|
|
PRInt32 srcLength = aStringLen;
|
|
|
|
result = decoder->Convert(aString, &srcLength, unicodeString, &unicharLength);
|
|
|
|
|
|
|
|
totalChars += unicharLength;
|
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
// if we failed, we consume one byte, replace it with U+FFFD
|
|
|
|
// and try the conversion again.
|
|
|
|
unicodeString[unicharLength++] = (PRUnichar)0xFFFD;
|
|
|
|
unicodeString = unicodeString + unicharLength;
|
|
|
|
unicharLength = unicodeLength - (++totalChars);
|
|
|
|
|
|
|
|
decoder->Reset();
|
|
|
|
|
|
|
|
if (((PRUint32) (srcLength + 1)) > aStringLen) {
|
|
|
|
srcLength = aStringLen;
|
|
|
|
} else {
|
|
|
|
srcLength++;
|
|
|
|
}
|
|
|
|
|
|
|
|
aString += srcLength;
|
|
|
|
aStringLen -= srcLength;
|
|
|
|
}
|
|
|
|
} while (NS_FAILED(result) && (aStringLen > 0));
|
|
|
|
|
|
|
|
// Don't propagate return code of unicode decoder
|
|
|
|
// since it doesn't reflect on our success or failure
|
|
|
|
// - Ref. bug 87110
|
|
|
|
result = NS_OK;
|
|
|
|
strUnicodeBuffer->SetLength(totalChars);
|
2000-04-27 03:36:45 +04:00
|
|
|
}
|
2000-04-23 06:01:40 +04:00
|
|
|
}
|
2001-11-19 23:30:29 +03:00
|
|
|
NS_RELEASE(decoder);
|
2000-04-23 06:01:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-07-02 07:11:57 +04:00
|
|
|
} else {
|
|
|
|
nsCAutoString spec;
|
|
|
|
if (channel) {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
channel->GetURI(getter_AddRefs(uri));
|
|
|
|
if (uri)
|
|
|
|
uri->GetSpec(spec);
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsAFlatString& specUCS2 = NS_ConvertUTF8toUCS2(spec);
|
|
|
|
const nsAFlatString& ctypeUCS2 = NS_ConvertASCIItoUCS2(contentType);
|
|
|
|
const PRUnichar *strings[] = { specUCS2.get(), ctypeUCS2.get() };
|
|
|
|
|
|
|
|
ReportToConsole(NS_LITERAL_STRING("MimeNotCss").get(), strings, 2,
|
|
|
|
nsIScriptError::errorFlag);
|
2000-04-23 06:01:40 +04:00
|
|
|
}
|
|
|
|
}
|
1999-11-02 09:49:44 +03:00
|
|
|
|
2000-04-27 04:51:06 +04:00
|
|
|
mLoader->DidLoadStyle(aLoader, strUnicodeBuffer, this, aStatus);
|
|
|
|
// NOTE: passed ownership of strUnicodeBuffer to mLoader in the call,
|
|
|
|
// so nulling it out for clarity / safety
|
|
|
|
strUnicodeBuffer = nsnull;
|
|
|
|
|
1999-05-19 03:15:10 +04:00
|
|
|
// We added a reference when the loader was created. This
|
|
|
|
// release should destroy it.
|
|
|
|
NS_RELEASE(aLoader);
|
2000-04-23 06:01:40 +04:00
|
|
|
return result;
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
2000-05-03 03:07:11 +04:00
|
|
|
static PRBool PR_CALLBACK
|
1999-05-19 03:15:10 +04:00
|
|
|
InsertPendingSheet(void* aPendingData, void* aLoader)
|
|
|
|
{
|
|
|
|
PendingSheetData* data = (PendingSheetData*)aPendingData;
|
|
|
|
CSSLoaderImpl* loader = (CSSLoaderImpl*)aLoader;
|
|
|
|
loader->InsertSheetInDoc(data->mSheet, data->mDocIndex,
|
1999-12-04 04:27:14 +03:00
|
|
|
data->mOwningElement, data->mNotify,
|
|
|
|
data->mObserver);
|
1999-05-19 03:15:10 +04:00
|
|
|
delete data;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2000-05-03 03:07:11 +04:00
|
|
|
static PRBool PR_CALLBACK
|
1999-05-19 03:15:10 +04:00
|
|
|
AreAllPendingAlternateSheets(void* aPendingData, void* aLoader)
|
|
|
|
{
|
|
|
|
PendingSheetData* data = (PendingSheetData*)aPendingData;
|
|
|
|
CSSLoaderImpl* loader = (CSSLoaderImpl*)aLoader;
|
|
|
|
nsAutoString title;
|
|
|
|
data->mSheet->GetTitle(title);
|
|
|
|
if (loader->IsAlternate(title)) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CSSLoaderImpl::Cleanup(URLKey& aKey, SheetLoadData* aLoadData)
|
|
|
|
{
|
1999-05-27 03:52:37 +04:00
|
|
|
// notify any parents that child is done
|
1999-05-19 03:15:10 +04:00
|
|
|
SheetLoadData* data = aLoadData;
|
|
|
|
do {
|
|
|
|
if (data->mParentData) {
|
|
|
|
if (0 == --(data->mParentData->mPendingChildren)) { // all children are done, handle parent
|
|
|
|
NS_ASSERTION(data->mParentSheet, "bug");
|
1999-09-30 05:39:57 +04:00
|
|
|
if (! data->mSyncLoad) {
|
|
|
|
SheetComplete(data->mParentSheet, data->mParentData);
|
|
|
|
}
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
data = data->mNext;
|
|
|
|
} while (data);
|
|
|
|
|
|
|
|
if (! aLoadData->mIsInline) { // inline sheets don't go in loading table
|
1999-06-03 05:56:14 +04:00
|
|
|
SheetLoadData* oldData = (SheetLoadData*)mLoadingSheets.Remove(&aKey);
|
|
|
|
NS_ASSERTION(oldData == aLoadData, "broken loading table");
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
2000-08-17 03:04:42 +04:00
|
|
|
// release all parsers, but we only need to unblock one
|
1999-05-27 03:52:37 +04:00
|
|
|
data = aLoadData;
|
2000-08-17 03:04:42 +04:00
|
|
|
PRBool done = PR_FALSE;
|
1999-05-27 03:52:37 +04:00
|
|
|
do {
|
2000-08-17 03:04:42 +04:00
|
|
|
if (data->mParserToUnblock) {
|
|
|
|
if (!done && data->mDidBlockParser) {
|
|
|
|
done = PR_TRUE;
|
2001-02-10 01:36:23 +03:00
|
|
|
data->mParserToUnblock->ContinueParsing(); // this may result in re-entrant calls to loader
|
2000-08-17 03:04:42 +04:00
|
|
|
}
|
|
|
|
// To reduce the risk of leaking a parser if we leak a SheetLoadData,
|
|
|
|
// release the parser here.
|
|
|
|
NS_RELEASE(data->mParserToUnblock); // assigns nsnull
|
1999-05-27 03:52:37 +04:00
|
|
|
}
|
|
|
|
data = data->mNext;
|
|
|
|
} while (data);
|
|
|
|
|
1999-05-19 03:15:10 +04:00
|
|
|
// if all loads complete, put pending sheets into doc
|
|
|
|
if (0 == mLoadingSheets.Count()) {
|
|
|
|
PRInt32 count = mPendingDocSheets.Count();
|
|
|
|
if (count) {
|
|
|
|
if (! mPendingDocSheets.EnumerateForwards(AreAllPendingAlternateSheets, this)) {
|
|
|
|
PendingSheetData* last = (PendingSheetData*)mPendingDocSheets.ElementAt(count - 1);
|
|
|
|
last->mNotify = PR_TRUE;
|
|
|
|
}
|
|
|
|
mPendingDocSheets.EnumerateForwards(InsertPendingSheet, this);
|
|
|
|
mPendingDocSheets.Clear();
|
|
|
|
}
|
|
|
|
// start pending alternate loads
|
|
|
|
while (mPendingAlternateSheets.Count()) {
|
1999-07-18 04:26:14 +04:00
|
|
|
data = (SheetLoadData*)mPendingAlternateSheets.ElementAt(0);
|
1999-05-19 03:15:10 +04:00
|
|
|
mPendingAlternateSheets.RemoveElementAt(0);
|
|
|
|
URLKey key(data->mURL);
|
|
|
|
LoadSheet(key, data); // this may pull other pending alternates (with same URL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-02 09:49:44 +03:00
|
|
|
NS_RELEASE(aLoadData); // delete data last, it may have last ref on loader...
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
2000-07-28 13:36:51 +04:00
|
|
|
#ifdef INCLUDE_XUL
|
|
|
|
static PRBool IsChromeURI(nsIURI* aURI)
|
|
|
|
{
|
2001-05-02 08:53:38 +04:00
|
|
|
NS_ASSERTION(aURI, "bad caller");
|
|
|
|
PRBool isChrome = PR_FALSE;
|
|
|
|
aURI->SchemeIs("chrome", &isChrome);
|
|
|
|
return isChrome;
|
2000-07-28 13:36:51 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-05-19 03:15:10 +04:00
|
|
|
nsresult
|
|
|
|
CSSLoaderImpl::SheetComplete(nsICSSStyleSheet* aSheet, SheetLoadData* aLoadData)
|
|
|
|
{
|
2000-07-28 13:36:51 +04:00
|
|
|
#ifdef INCLUDE_XUL
|
|
|
|
if (IsChromeURI(aLoadData->mURL)) {
|
2001-02-22 06:01:34 +03:00
|
|
|
nsCOMPtr<nsIXULPrototypeCache> cache(do_GetService("@mozilla.org/xul/xul-prototype-cache;1"));
|
|
|
|
if (cache) {
|
|
|
|
PRBool enabled;
|
|
|
|
cache->GetEnabled(&enabled);
|
|
|
|
if (enabled) {
|
2000-07-28 13:36:51 +04:00
|
|
|
nsCOMPtr<nsICSSStyleSheet> sheet;
|
|
|
|
cache->GetStyleSheet(aLoadData->mURL, getter_AddRefs(sheet));
|
|
|
|
if (!sheet) {
|
|
|
|
cache->PutStyleSheet(aSheet);
|
|
|
|
aSheet->SetModified(PR_FALSE); // We're clean.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-05-19 03:15:10 +04:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
URLKey key(aLoadData->mURL);
|
|
|
|
|
2000-07-28 13:36:51 +04:00
|
|
|
if (! aLoadData->mIsInline) { // don't remember inline sheets
|
1999-05-19 03:15:10 +04:00
|
|
|
NS_ADDREF(aSheet); // add ref for table
|
1999-06-03 05:56:14 +04:00
|
|
|
nsICSSStyleSheet* oldSheet = (nsICSSStyleSheet*)mLoadedSheets.Put(&key, aSheet);
|
|
|
|
NS_IF_RELEASE(oldSheet); // relase predecessor (was dirty)
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
SheetLoadData* data = aLoadData;
|
|
|
|
do { // add to parent sheet, parent doc or pending doc sheet list
|
|
|
|
PrepareSheet(aSheet, data->mTitle, data->mMedia);
|
1999-05-27 03:52:37 +04:00
|
|
|
if (data->mParentSheet) { // is child sheet
|
1999-05-19 03:15:10 +04:00
|
|
|
InsertChildSheet(aSheet, data->mParentSheet, data->mSheetIndex);
|
2001-08-05 01:19:53 +04:00
|
|
|
if (data->mParentRule) { // we have the @import rule that loaded this sheet
|
|
|
|
data->mParentRule->SetSheet(aSheet);
|
|
|
|
}
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
1999-05-27 03:52:37 +04:00
|
|
|
else if (data->mIsAgent) { // is agent sheet
|
1999-12-04 04:27:14 +03:00
|
|
|
if (data->mObserver) {
|
|
|
|
data->mObserver->StyleSheetLoaded(aSheet, PR_FALSE);
|
1999-05-27 03:52:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else { // doc sheet
|
|
|
|
if (data->mParserToUnblock) { // if blocking, insert it immediately
|
1999-12-04 04:27:14 +03:00
|
|
|
InsertSheetInDoc(aSheet, data->mSheetIndex, data->mOwningElement, PR_TRUE, data->mObserver);
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
1999-05-27 03:52:37 +04:00
|
|
|
else { // otherwise wait until all are loaded (even inlines)
|
1999-12-04 04:27:14 +03:00
|
|
|
AddPendingSheet(aSheet, data->mSheetIndex, data->mOwningElement, data->mObserver);
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data = data->mNext;
|
|
|
|
if (data) { // clone sheet for next insertion
|
|
|
|
nsICSSStyleSheet* clone = nsnull;
|
|
|
|
result = aSheet->Clone(clone);
|
|
|
|
NS_RELEASE(aSheet);
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
aSheet = clone;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_RELEASE(aSheet);
|
|
|
|
}
|
|
|
|
} while (data && aSheet);
|
|
|
|
|
|
|
|
Cleanup(key, aLoadData);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CSSLoaderImpl::ParseSheet(nsIUnicharInputStream* aIn,
|
|
|
|
SheetLoadData* aLoadData,
|
1999-05-27 03:52:37 +04:00
|
|
|
PRBool& aCompleted,
|
|
|
|
nsICSSStyleSheet*& aSheet)
|
1999-05-19 03:15:10 +04:00
|
|
|
{
|
|
|
|
nsresult result;
|
1999-05-27 03:52:37 +04:00
|
|
|
PRBool failed = PR_TRUE;
|
1999-05-19 03:15:10 +04:00
|
|
|
|
|
|
|
aCompleted = PR_TRUE;
|
1999-05-27 03:52:37 +04:00
|
|
|
aSheet = nsnull;
|
|
|
|
result = NS_NewCSSStyleSheet(&aSheet, aLoadData->mURL);
|
1999-05-19 03:15:10 +04:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
2001-09-28 05:38:36 +04:00
|
|
|
nsCOMPtr<nsICSSParser> parser;
|
|
|
|
result = GetParserFor(aSheet, getter_AddRefs(parser));
|
1999-05-19 03:15:10 +04:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
mParsingData.AppendElement(aLoadData);
|
1999-06-15 10:19:21 +04:00
|
|
|
if (kNameSpaceID_Unknown != aLoadData->mDefaultNameSpaceID) {
|
|
|
|
aSheet->SetDefaultNameSpaceID(aLoadData->mDefaultNameSpaceID);
|
|
|
|
}
|
1999-05-27 03:52:37 +04:00
|
|
|
result = parser->Parse(aIn, aLoadData->mURL, aSheet); // this may result in re-entrant load child sheet calls
|
1999-05-19 03:15:10 +04:00
|
|
|
mParsingData.RemoveElementAt(mParsingData.Count() - 1);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
1999-06-03 05:56:14 +04:00
|
|
|
aSheet->SetModified(PR_FALSE); // make it clean from the load
|
1999-05-27 03:52:37 +04:00
|
|
|
failed = PR_FALSE;
|
1999-05-19 03:15:10 +04:00
|
|
|
if (0 == aLoadData->mPendingChildren) { // sheet isn't still loading children
|
|
|
|
if (aLoadData->mIsInline) {
|
1999-05-27 03:52:37 +04:00
|
|
|
aLoadData->mDidBlockParser = PR_FALSE; // don't need to unblock, we're done and won't block
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
1999-05-27 03:52:37 +04:00
|
|
|
SheetComplete(aSheet, aLoadData);
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
else { // else sheet is still waiting for children to load, last child will complete it
|
|
|
|
aCompleted = PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RecycleParser(parser);
|
|
|
|
}
|
1999-05-27 03:52:37 +04:00
|
|
|
}
|
|
|
|
if (failed) {
|
|
|
|
URLKey key(aLoadData->mURL);
|
|
|
|
Cleanup(key, aLoadData);
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-02-03 02:04:47 +03:00
|
|
|
CSSLoaderImpl::DidLoadStyle(nsIStreamLoader* aLoader,
|
2000-04-27 03:36:45 +04:00
|
|
|
nsString* aStyleData,
|
1999-05-19 03:15:10 +04:00
|
|
|
SheetLoadData* aLoadData,
|
|
|
|
nsresult aStatus)
|
|
|
|
{
|
1999-07-24 23:04:22 +04:00
|
|
|
#ifdef NS_DEBUG
|
|
|
|
NS_ASSERTION(! mSyncCallback, "getting synchronous callback from netlib");
|
|
|
|
#endif
|
|
|
|
|
2002-01-24 05:03:19 +03:00
|
|
|
if (NS_SUCCEEDED(aStatus) && (aStyleData) && (!aStyleData->IsEmpty()) && (mDocument)) {
|
1999-05-19 03:15:10 +04:00
|
|
|
nsresult result;
|
|
|
|
nsIUnicharInputStream* uin = nsnull;
|
2000-04-23 06:01:40 +04:00
|
|
|
|
1999-05-19 03:15:10 +04:00
|
|
|
// wrap the string with the CSS data up in a unicode input stream.
|
2000-04-27 03:36:45 +04:00
|
|
|
result = NS_NewStringUnicharInputStream(&uin, aStyleData);
|
2000-04-23 06:01:40 +04:00
|
|
|
|
1999-05-19 03:15:10 +04:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
// XXX We have no way of indicating failure. Silently fail?
|
|
|
|
PRBool completed;
|
1999-05-27 03:52:37 +04:00
|
|
|
nsICSSStyleSheet* sheet;
|
|
|
|
result = ParseSheet(uin, aLoadData, completed, sheet);
|
|
|
|
NS_IF_RELEASE(sheet);
|
2000-04-23 06:01:40 +04:00
|
|
|
NS_IF_RELEASE(uin);
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
1999-05-27 03:52:37 +04:00
|
|
|
else {
|
|
|
|
URLKey key(aLoadData->mURL);
|
|
|
|
Cleanup(key, aLoadData);
|
|
|
|
}
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
1999-06-16 10:16:53 +04:00
|
|
|
else { // load failed or document now gone, cleanup
|
2001-02-03 12:09:24 +03:00
|
|
|
#ifdef DEBUG
|
1999-09-04 03:38:40 +04:00
|
|
|
if (mDocument && NS_FAILED(aStatus)) { // still have doc, must have failed
|
1999-06-16 10:16:53 +04:00
|
|
|
// Dump error message to console.
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCAutoString url;
|
|
|
|
aLoadData->mURL->GetSpec(url);
|
2001-02-03 12:09:24 +03:00
|
|
|
nsCAutoString errorMessage(NS_LITERAL_CSTRING("CSSLoaderImpl::DidLoadStyle: Load of URL '") +
|
2002-03-06 10:48:55 +03:00
|
|
|
url +
|
2001-02-03 12:09:24 +03:00
|
|
|
NS_LITERAL_CSTRING("' failed. Error code: "));
|
|
|
|
errorMessage.AppendInt(NS_ERROR_GET_CODE(aStatus));
|
|
|
|
NS_WARNING(errorMessage.get());
|
1999-06-16 10:16:53 +04:00
|
|
|
}
|
2001-02-03 12:09:24 +03:00
|
|
|
#endif
|
1999-06-16 05:29:27 +04:00
|
|
|
|
|
|
|
URLKey key(aLoadData->mURL);
|
|
|
|
Cleanup(key, aLoadData);
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2000-12-02 18:53:42 +03:00
|
|
|
typedef nsresult (*nsStringEnumFunc)(const nsString& aSubString, void *aData);
|
1999-05-19 03:15:10 +04:00
|
|
|
|
2000-12-02 18:53:42 +03:00
|
|
|
static nsresult EnumerateMediaString(const nsString& aStringList, nsStringEnumFunc aFunc, void* aData)
|
1999-05-19 03:15:10 +04:00
|
|
|
{
|
2000-12-02 18:53:42 +03:00
|
|
|
nsresult status = NS_OK;
|
1999-05-19 03:15:10 +04:00
|
|
|
|
|
|
|
nsAutoString stringList(aStringList); // copy to work buffer
|
|
|
|
nsAutoString subStr;
|
|
|
|
|
|
|
|
stringList.Append(kNullCh); // put an extra null at the end
|
|
|
|
|
2001-06-30 15:02:25 +04:00
|
|
|
PRUnichar* start = (PRUnichar*)(const PRUnichar*)stringList.get();
|
1999-05-19 03:15:10 +04:00
|
|
|
PRUnichar* end = start;
|
|
|
|
|
2000-12-02 18:53:42 +03:00
|
|
|
while (NS_SUCCEEDED(status) && (kNullCh != *start)) {
|
1999-05-19 03:15:10 +04:00
|
|
|
PRBool quoted = PR_FALSE;
|
|
|
|
|
2000-03-12 14:10:07 +03:00
|
|
|
while ((kNullCh != *start) && nsCRT::IsAsciiSpace(*start)) { // skip leading space
|
1999-05-19 03:15:10 +04:00
|
|
|
start++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((kApostrophe == *start) || (kQuote == *start)) { // quoted string
|
|
|
|
PRUnichar quote = *start++;
|
|
|
|
quoted = PR_TRUE;
|
|
|
|
end = start;
|
|
|
|
while (kNullCh != *end) {
|
|
|
|
if (quote == *end) { // found closing quote
|
|
|
|
*end++ = kNullCh; // end string here
|
|
|
|
while ((kNullCh != *end) && (kComma != *end)) { // keep going until comma
|
|
|
|
end++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
end++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { // non-quoted string or ended
|
|
|
|
end = start;
|
|
|
|
|
|
|
|
while ((kNullCh != *end) && (kComma != *end)) { // look for comma
|
|
|
|
end++;
|
|
|
|
}
|
|
|
|
*end = kNullCh; // end string here
|
|
|
|
}
|
|
|
|
|
|
|
|
// truncate at first non letter, digit or hyphen
|
|
|
|
PRUnichar* test = start;
|
|
|
|
while (test <= end) {
|
2000-03-12 14:10:07 +03:00
|
|
|
if ((PR_FALSE == nsCRT::IsAsciiAlpha(*test)) &&
|
|
|
|
(PR_FALSE == nsCRT::IsAsciiDigit(*test)) && (kMinus != *test)) {
|
1999-05-19 03:15:10 +04:00
|
|
|
*test = kNullCh;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
test++;
|
|
|
|
}
|
|
|
|
subStr = start;
|
|
|
|
|
|
|
|
if (PR_FALSE == quoted) {
|
|
|
|
subStr.CompressWhitespace(PR_FALSE, PR_TRUE);
|
|
|
|
}
|
|
|
|
|
2002-01-24 05:03:19 +03:00
|
|
|
if (!subStr.IsEmpty()) {
|
2000-12-02 18:53:42 +03:00
|
|
|
status = (*aFunc)(subStr, aData);
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
start = ++end;
|
|
|
|
}
|
|
|
|
|
2000-12-02 18:53:42 +03:00
|
|
|
return status;
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
2000-12-02 18:53:42 +03:00
|
|
|
static nsresult MediumEnumFunc(const nsString& aSubString, void* aData)
|
1999-05-19 03:15:10 +04:00
|
|
|
{
|
|
|
|
nsIAtom* medium = NS_NewAtom(aSubString);
|
2000-12-02 18:53:42 +03:00
|
|
|
if (!medium) return NS_ERROR_OUT_OF_MEMORY;
|
1999-05-19 03:15:10 +04:00
|
|
|
((nsICSSStyleSheet*)aData)->AppendMedium(medium);
|
2000-12-02 18:53:42 +03:00
|
|
|
NS_RELEASE(medium);
|
|
|
|
return NS_OK;
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CSSLoaderImpl::SetMedia(nsICSSStyleSheet* aSheet, const nsString& aMedia)
|
|
|
|
{
|
2000-12-02 18:53:42 +03:00
|
|
|
nsresult rv = NS_OK;
|
1999-05-27 03:52:37 +04:00
|
|
|
aSheet->ClearMedia();
|
2002-01-24 05:03:19 +03:00
|
|
|
if (!aMedia.IsEmpty()) {
|
2000-12-02 18:53:42 +03:00
|
|
|
rv = EnumerateMediaString(aMedia, MediumEnumFunc, aSheet);
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
2000-12-02 18:53:42 +03:00
|
|
|
return rv;
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CSSLoaderImpl::PrepareSheet(nsICSSStyleSheet* aSheet, const nsString& aTitle,
|
|
|
|
const nsString& aMedia)
|
|
|
|
{
|
|
|
|
nsresult result = SetMedia(aSheet, aMedia);
|
|
|
|
|
|
|
|
aSheet->SetTitle(aTitle);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CSSLoaderImpl::AddPendingSheet(nsICSSStyleSheet* aSheet, PRInt32 aDocIndex,
|
1999-12-04 04:27:14 +03:00
|
|
|
nsIContent* aElement,
|
|
|
|
nsICSSLoaderObserver* aObserver)
|
1999-05-19 03:15:10 +04:00
|
|
|
{
|
1999-12-04 04:27:14 +03:00
|
|
|
PendingSheetData* data = new PendingSheetData(aSheet,
|
|
|
|
aDocIndex,
|
|
|
|
aElement,
|
|
|
|
aObserver);
|
1999-05-19 03:15:10 +04:00
|
|
|
if (data) {
|
|
|
|
mPendingDocSheets.AppendElement(data);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
CSSLoaderImpl::IsAlternate(const nsString& aTitle)
|
|
|
|
{
|
2002-01-24 05:03:19 +03:00
|
|
|
if (!aTitle.IsEmpty()) {
|
2002-02-20 01:49:12 +03:00
|
|
|
return PRBool(! aTitle.Equals(mPreferredSheet, nsCaseInsensitiveStringComparator()));
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CSSLoaderImpl::InsertSheetInDoc(nsICSSStyleSheet* aSheet, PRInt32 aDocIndex,
|
1999-12-04 04:27:14 +03:00
|
|
|
nsIContent* aElement, PRBool aNotify,
|
|
|
|
nsICSSLoaderObserver* aObserver)
|
1999-05-19 03:15:10 +04:00
|
|
|
{
|
|
|
|
if ((! mDocument) || (! aSheet)) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
2001-10-31 22:03:46 +03:00
|
|
|
if (nsnull != aElement) {
|
1999-05-19 03:15:10 +04:00
|
|
|
nsIDOMNode* domNode = nsnull;
|
2001-01-04 23:44:42 +03:00
|
|
|
if (NS_SUCCEEDED(aElement->QueryInterface(NS_GET_IID(nsIDOMNode), (void**)&domNode))) {
|
1999-05-19 03:15:10 +04:00
|
|
|
aSheet->SetOwningNode(domNode);
|
|
|
|
NS_RELEASE(domNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIStyleSheetLinkingElement* element;
|
2001-01-04 23:44:42 +03:00
|
|
|
if (NS_SUCCEEDED(aElement->QueryInterface(NS_GET_IID(nsIStyleSheetLinkingElement),
|
1999-05-19 03:15:10 +04:00
|
|
|
(void**)&element))) {
|
|
|
|
element->SetStyleSheet(aSheet);
|
|
|
|
NS_RELEASE(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString title;
|
|
|
|
aSheet->GetTitle(title);
|
|
|
|
aSheet->SetEnabled(! IsAlternate(title));
|
|
|
|
|
2001-10-31 22:03:46 +03:00
|
|
|
nsVoidKey key(mDocument);
|
|
|
|
nsAutoVoidArray* sheetMap = (nsAutoVoidArray*)mSheetMapTable.Get(&key);
|
|
|
|
if (! sheetMap) {
|
|
|
|
sheetMap = new nsAutoVoidArray();
|
|
|
|
if (sheetMap) {
|
|
|
|
mSheetMapTable.Put(&key, sheetMap);
|
|
|
|
}
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
2001-10-31 22:03:46 +03:00
|
|
|
|
|
|
|
if (sheetMap) {
|
|
|
|
PRInt32 insertIndex = sheetMap->Count();
|
|
|
|
PRBool insertedSheet = PR_FALSE;
|
|
|
|
while (0 <= --insertIndex) {
|
|
|
|
PRInt32 targetIndex = NS_PTR_TO_INT32(sheetMap->ElementAt(insertIndex));
|
|
|
|
if (targetIndex < aDocIndex) {
|
|
|
|
mDocument->InsertStyleSheetAt(aSheet, insertIndex + 1, aNotify);
|
|
|
|
sheetMap->InsertElementAt((void*)aDocIndex, insertIndex + 1);
|
|
|
|
insertedSheet = PR_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!insertedSheet) { // didn't insert yet
|
|
|
|
mDocument->InsertStyleSheetAt(aSheet, 0, aNotify);
|
|
|
|
sheetMap->InsertElementAt((void*)aDocIndex, 0);
|
|
|
|
}
|
|
|
|
if (nsnull != aObserver) {
|
|
|
|
aObserver->StyleSheetLoaded(aSheet, aNotify);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CSSLoaderImpl::InsertChildSheet(nsICSSStyleSheet* aSheet, nsICSSStyleSheet* aParentSheet,
|
|
|
|
PRInt32 aIndex)
|
|
|
|
{
|
|
|
|
if ((! aParentSheet) || (! aSheet)) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
2002-02-26 01:26:26 +03:00
|
|
|
// child sheets should always start out enabled, even if they got
|
|
|
|
// cloned off of top-level sheets which were disabled
|
|
|
|
aSheet->SetEnabled(PR_TRUE);
|
|
|
|
|
2001-10-31 22:03:46 +03:00
|
|
|
nsVoidKey key(aParentSheet);
|
|
|
|
nsAutoVoidArray* sheetMap = (nsAutoVoidArray*)mSheetMapTable.Get(&key);
|
|
|
|
if (! sheetMap) {
|
|
|
|
sheetMap = new nsAutoVoidArray();
|
|
|
|
if (sheetMap) {
|
|
|
|
mSheetMapTable.Put(&key, sheetMap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sheetMap) {
|
|
|
|
PRInt32 insertIndex = sheetMap->Count();
|
|
|
|
while (0 <= --insertIndex) {
|
|
|
|
PRInt32 targetIndex = NS_PTR_TO_INT32(sheetMap->ElementAt(insertIndex));
|
|
|
|
if (targetIndex < aIndex) {
|
|
|
|
aParentSheet->InsertStyleSheetAt(aSheet, insertIndex + 1);
|
|
|
|
sheetMap->InsertElementAt((void*)aIndex, insertIndex + 1);
|
|
|
|
aSheet = nsnull;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nsnull != aSheet) { // didn't insert yet
|
|
|
|
aParentSheet->InsertStyleSheetAt(aSheet, 0);
|
|
|
|
sheetMap->InsertElementAt((void*)aIndex, 0);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CSSLoaderImpl::LoadSheet(URLKey& aKey, SheetLoadData* aData)
|
|
|
|
{
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
SheetLoadData* loadingData = (SheetLoadData*)mLoadingSheets.Get(&aKey);
|
|
|
|
if (loadingData) { // already loading this sheet, glom on to the load
|
|
|
|
while (loadingData->mNext) {
|
|
|
|
loadingData = loadingData->mNext;
|
|
|
|
}
|
|
|
|
loadingData->mNext = aData;
|
|
|
|
}
|
|
|
|
else { // not loading, go load it
|
1999-09-30 05:39:57 +04:00
|
|
|
if (aData->mSyncLoad) { // handle agent sheets synchronously
|
|
|
|
nsIURI* urlClone;
|
|
|
|
result = aKey.mURL->Clone(&urlClone); // dont give key URL to netlib, it gets munged
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
nsIInputStream* in;
|
|
|
|
result = NS_OpenURI(&in, urlClone);
|
|
|
|
NS_RELEASE(urlClone);
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
// Translate the input using the argument character set id into unicode
|
|
|
|
nsIUnicharInputStream* uin;
|
2001-12-04 04:10:43 +03:00
|
|
|
result = NS_NewUTF8ConverterStream(&uin, in, 0);
|
1999-09-30 05:39:57 +04:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
mLoadingSheets.Put(&aKey, aData);
|
|
|
|
PRBool completed;
|
|
|
|
nsICSSStyleSheet* sheet;
|
|
|
|
result = ParseSheet(uin, aData, completed, sheet);
|
|
|
|
NS_ASSERTION(completed, "sync sheet load failed to complete");
|
|
|
|
NS_IF_RELEASE(sheet);
|
|
|
|
NS_RELEASE(uin);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fputs("CSSLoader::LoadSheet - failed to get converter stream\n", stderr);
|
|
|
|
}
|
|
|
|
NS_RELEASE(in);
|
|
|
|
}
|
2001-02-03 12:09:24 +03:00
|
|
|
#ifdef DEBUG
|
1999-09-30 05:39:57 +04:00
|
|
|
else {
|
|
|
|
// Dump an error message to the console
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCAutoString url;
|
|
|
|
aKey.mURL->GetSpec(url);
|
2001-02-03 12:09:24 +03:00
|
|
|
nsCAutoString errorMessage(NS_LITERAL_CSTRING("CSSLoaderImpl::LoadSheet: Load of URL '") +
|
2002-03-06 10:48:55 +03:00
|
|
|
url +
|
2001-02-03 12:09:24 +03:00
|
|
|
NS_LITERAL_CSTRING("' failed. Error code: "));
|
|
|
|
errorMessage.AppendInt(NS_ERROR_GET_CODE(result));
|
|
|
|
NS_WARNING(errorMessage.get());
|
1999-09-30 05:39:57 +04:00
|
|
|
}
|
2001-02-03 12:09:24 +03:00
|
|
|
#endif
|
1999-09-30 05:39:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (mDocument || aData->mIsAgent) { // we're still live, start an async load
|
2000-02-03 02:04:47 +03:00
|
|
|
nsIStreamLoader* loader;
|
1999-09-09 04:42:53 +04:00
|
|
|
nsIURI* urlClone;
|
|
|
|
result = aKey.mURL->Clone(&urlClone); // dont give key URL to netlib, it gets munged
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
1999-07-24 23:04:22 +04:00
|
|
|
#ifdef NS_DEBUG
|
1999-09-09 04:42:53 +04:00
|
|
|
mSyncCallback = PR_TRUE;
|
1999-06-26 10:36:26 +04:00
|
|
|
#endif
|
1999-09-18 10:20:49 +04:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
|
|
|
mDocument->GetDocumentLoadGroup(getter_AddRefs(loadGroup));
|
|
|
|
|
2001-10-24 04:01:09 +04:00
|
|
|
NS_ASSERTION(loadGroup, "A stylesheet was unable to locate a load group. This means the onload is going to fire too early!");
|
|
|
|
|
2002-01-09 08:56:43 +03:00
|
|
|
nsCOMPtr<nsIURI> document_uri;
|
|
|
|
mDocument->GetDocumentURL(getter_AddRefs(document_uri));
|
|
|
|
|
2001-08-17 06:03:34 +04:00
|
|
|
#ifdef MOZ_TIMELINE
|
|
|
|
NS_TIMELINE_MARK_URI("Loading style sheet: %s", urlClone);
|
|
|
|
NS_TIMELINE_INDENT();
|
|
|
|
#endif
|
2002-01-09 08:56:43 +03:00
|
|
|
result = NS_NewStreamLoader(&loader, urlClone, aData, nsnull,
|
|
|
|
loadGroup, nsnull, nsIChannel::LOAD_NORMAL,
|
|
|
|
document_uri,
|
|
|
|
nsIHttpChannel::REFERRER_INLINES);
|
1999-07-24 23:04:22 +04:00
|
|
|
#ifdef NS_DEBUG
|
1999-09-09 04:42:53 +04:00
|
|
|
mSyncCallback = PR_FALSE;
|
1999-07-24 23:04:22 +04:00
|
|
|
#endif
|
1999-09-09 04:42:53 +04:00
|
|
|
NS_RELEASE(urlClone);
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
mLoadingSheets.Put(&aKey, aData);
|
|
|
|
// grab any pending alternates that have this URL
|
|
|
|
loadingData = aData;
|
|
|
|
PRInt32 index = 0;
|
|
|
|
while (index < mPendingAlternateSheets.Count()) {
|
|
|
|
SheetLoadData* data = (SheetLoadData*)mPendingAlternateSheets.ElementAt(index);
|
|
|
|
PRBool equals = PR_FALSE;
|
|
|
|
result = aKey.mURL->Equals(data->mURL, &equals);
|
|
|
|
if (NS_SUCCEEDED(result) && equals)
|
|
|
|
{
|
|
|
|
mPendingAlternateSheets.RemoveElementAt(index);
|
2000-08-07 07:06:22 +04:00
|
|
|
NS_IF_RELEASE(loadingData->mNext);
|
1999-09-09 04:42:53 +04:00
|
|
|
loadingData->mNext = data;
|
|
|
|
loadingData = data;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
index++;
|
|
|
|
}
|
1999-05-28 10:50:52 +04:00
|
|
|
}
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-09-09 04:42:53 +04:00
|
|
|
else { // document gone, no point in starting the load
|
1999-11-02 09:49:44 +03:00
|
|
|
NS_RELEASE(aData);
|
1999-09-09 04:42:53 +04:00
|
|
|
}
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-05-27 03:52:37 +04:00
|
|
|
CSSLoaderImpl::LoadInlineStyle(nsIContent* aElement,
|
1999-05-19 03:15:10 +04:00
|
|
|
nsIUnicharInputStream* aIn,
|
|
|
|
const nsString& aTitle,
|
|
|
|
const nsString& aMedia,
|
1999-06-15 10:19:21 +04:00
|
|
|
PRInt32 aDefaultNameSpaceID,
|
1999-05-19 03:15:10 +04:00
|
|
|
PRInt32 aDocIndex,
|
|
|
|
nsIParser* aParserToUnblock,
|
1999-12-04 04:27:14 +03:00
|
|
|
PRBool& aCompleted,
|
|
|
|
nsICSSLoaderObserver* aObserver)
|
1999-05-19 03:15:10 +04:00
|
|
|
{
|
1999-05-27 03:52:37 +04:00
|
|
|
NS_ASSERTION(mDocument, "not initialized");
|
|
|
|
if (! mDocument) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
1999-05-19 03:15:10 +04:00
|
|
|
// XXX need to add code to cancel any pending sheets for element
|
|
|
|
nsresult result = NS_ERROR_NULL_POINTER;
|
|
|
|
if (aIn) {
|
1999-06-23 07:29:44 +04:00
|
|
|
nsIURI* docURL;
|
1999-05-19 03:15:10 +04:00
|
|
|
mDocument->GetBaseURL(docURL);
|
1999-06-15 10:19:21 +04:00
|
|
|
SheetLoadData* data = new SheetLoadData(this, docURL, aTitle, aMedia, aDefaultNameSpaceID,
|
1999-05-19 03:15:10 +04:00
|
|
|
aElement,
|
|
|
|
aDocIndex, aParserToUnblock,
|
1999-12-04 04:27:14 +03:00
|
|
|
PR_TRUE, aObserver);
|
1999-11-02 09:49:44 +03:00
|
|
|
if (data == nsnull) {
|
|
|
|
result = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_ADDREF(data);
|
|
|
|
nsICSSStyleSheet* sheet;
|
|
|
|
result = ParseSheet(aIn, data, aCompleted, sheet);
|
|
|
|
NS_IF_RELEASE(sheet);
|
|
|
|
if ((! aCompleted) && (aParserToUnblock)) {
|
|
|
|
data->mDidBlockParser = PR_TRUE;
|
|
|
|
}
|
1999-05-27 03:52:37 +04:00
|
|
|
}
|
1999-05-19 03:15:10 +04:00
|
|
|
NS_RELEASE(docURL);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-05-27 03:52:37 +04:00
|
|
|
CSSLoaderImpl::LoadStyleLink(nsIContent* aElement,
|
1999-06-23 07:29:44 +04:00
|
|
|
nsIURI* aURL,
|
1999-05-19 03:15:10 +04:00
|
|
|
const nsString& aTitle,
|
|
|
|
const nsString& aMedia,
|
1999-06-15 10:19:21 +04:00
|
|
|
PRInt32 aDefaultNameSpaceID,
|
1999-05-19 03:15:10 +04:00
|
|
|
PRInt32 aDocIndex,
|
|
|
|
nsIParser* aParserToUnblock,
|
1999-12-04 04:27:14 +03:00
|
|
|
PRBool& aCompleted,
|
|
|
|
nsICSSLoaderObserver* aObserver)
|
1999-05-19 03:15:10 +04:00
|
|
|
{
|
1999-05-27 03:52:37 +04:00
|
|
|
NS_ASSERTION(mDocument, "not initialized");
|
|
|
|
if (! mDocument) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2000-05-27 09:22:39 +04:00
|
|
|
|
2000-05-27 03:28:40 +04:00
|
|
|
//-- Make sure this page is allowed to load this URL
|
2001-11-17 05:36:05 +03:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIScriptSecurityManager> secMan =
|
|
|
|
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
nsIURI* docURI;
|
|
|
|
rv = mDocument->GetBaseURL(docURI);
|
|
|
|
if (NS_FAILED(rv) || !docURI) return NS_ERROR_FAILURE;
|
|
|
|
rv = secMan->CheckLoadURI(docURI, aURL, nsIScriptSecurityManager::ALLOW_CHROME);
|
|
|
|
NS_IF_RELEASE(docURI);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-05-27 03:52:37 +04:00
|
|
|
|
1999-05-19 03:15:10 +04:00
|
|
|
// XXX need to add code to cancel any pending sheets for element
|
|
|
|
nsresult result = NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
aCompleted = PR_TRUE;
|
|
|
|
if (aURL) {
|
|
|
|
URLKey key(aURL);
|
2000-04-16 04:23:57 +04:00
|
|
|
PRBool sheetModified = PR_FALSE;
|
1999-05-19 03:15:10 +04:00
|
|
|
|
|
|
|
nsICSSStyleSheet* sheet = (nsICSSStyleSheet*)mLoadedSheets.Get(&key);
|
2000-07-28 13:36:51 +04:00
|
|
|
#ifdef INCLUDE_XUL
|
|
|
|
if (!sheet && IsChromeURI(aURL)) {
|
2001-02-22 06:01:34 +03:00
|
|
|
nsCOMPtr<nsIXULPrototypeCache> cache(do_GetService("@mozilla.org/xul/xul-prototype-cache;1"));
|
|
|
|
if (cache) {
|
|
|
|
PRBool enabled;
|
|
|
|
cache->GetEnabled(&enabled);
|
|
|
|
if (enabled) {
|
2000-07-28 13:36:51 +04:00
|
|
|
nsCOMPtr<nsICSSStyleSheet> cachedSheet;
|
|
|
|
cache->GetStyleSheet(aURL, getter_AddRefs(cachedSheet));
|
|
|
|
if (cachedSheet)
|
|
|
|
sheet = cachedSheet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-04-16 04:23:57 +04:00
|
|
|
if (sheet && (NS_OK == sheet->IsModified(&sheetModified)) && sheetModified) { // if dirty, forget it
|
1999-06-03 05:56:14 +04:00
|
|
|
sheet = nsnull;
|
|
|
|
}
|
1999-05-19 03:15:10 +04:00
|
|
|
|
1999-06-03 05:56:14 +04:00
|
|
|
if (sheet) { // already have one fully loaded and unmodified
|
1999-05-19 03:15:10 +04:00
|
|
|
nsICSSStyleSheet* clone = nsnull;
|
|
|
|
result = sheet->Clone(clone);
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
PrepareSheet(clone, aTitle, aMedia);
|
2001-04-11 07:55:39 +04:00
|
|
|
if (aParserToUnblock || (0 == mLoadingSheets.Count())) {
|
|
|
|
// we either have a parser that needs the sheet now, or we aren't currently
|
|
|
|
// busy. go ahead and put the sheet right into the doc.
|
1999-12-04 04:27:14 +03:00
|
|
|
result = InsertSheetInDoc(clone, aDocIndex, aElement, PR_TRUE, aObserver);
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
1999-05-27 03:52:37 +04:00
|
|
|
else { // add to pending list
|
1999-12-04 04:27:14 +03:00
|
|
|
result = AddPendingSheet(clone, aDocIndex, aElement, aObserver);
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
NS_RELEASE(clone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { // need to load it
|
1999-06-15 10:19:21 +04:00
|
|
|
SheetLoadData* data = new SheetLoadData(this, aURL, aTitle, aMedia, aDefaultNameSpaceID,
|
1999-05-19 03:15:10 +04:00
|
|
|
aElement, aDocIndex,
|
1999-12-04 04:27:14 +03:00
|
|
|
aParserToUnblock, PR_FALSE,
|
|
|
|
aObserver);
|
1999-11-02 09:49:44 +03:00
|
|
|
if (data == nsnull) {
|
|
|
|
result = NS_ERROR_OUT_OF_MEMORY;
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
else {
|
1999-11-02 09:49:44 +03:00
|
|
|
NS_ADDREF(data);
|
|
|
|
if (IsAlternate(aTitle) && mLoadingSheets.Count() &&
|
|
|
|
(! mLoadingSheets.Get(&key)) && (! aParserToUnblock)) {
|
|
|
|
// this is an alternate, and we're already loading others, but not loading this, defer it
|
|
|
|
mPendingAlternateSheets.AppendElement(data);
|
|
|
|
result = NS_OK;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (aParserToUnblock) {
|
|
|
|
data->mDidBlockParser = PR_TRUE;
|
|
|
|
}
|
|
|
|
result = LoadSheet(key, data);
|
1999-09-09 04:42:53 +04:00
|
|
|
}
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
aCompleted = PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CSSLoaderImpl::LoadChildSheet(nsICSSStyleSheet* aParentSheet,
|
1999-06-23 07:29:44 +04:00
|
|
|
nsIURI* aURL,
|
1999-05-19 03:15:10 +04:00
|
|
|
const nsString& aMedia,
|
1999-06-15 10:19:21 +04:00
|
|
|
PRInt32 aDefaultNameSpaceID,
|
2001-08-05 01:19:53 +04:00
|
|
|
PRInt32 aIndex,
|
|
|
|
nsICSSImportRule* aParentRule)
|
1999-05-19 03:15:10 +04:00
|
|
|
{
|
|
|
|
nsresult result = NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
if (aURL) {
|
|
|
|
URLKey key(aURL);
|
2000-04-16 04:23:57 +04:00
|
|
|
PRBool sheetModified = PR_FALSE;
|
1999-05-19 03:15:10 +04:00
|
|
|
|
|
|
|
nsICSSStyleSheet* sheet = (nsICSSStyleSheet*)mLoadedSheets.Get(&key);
|
2000-07-28 13:36:51 +04:00
|
|
|
#ifdef INCLUDE_XUL
|
|
|
|
if (!sheet && IsChromeURI(aURL)) {
|
2001-02-22 06:01:34 +03:00
|
|
|
nsCOMPtr<nsIXULPrototypeCache> cache(do_GetService("@mozilla.org/xul/xul-prototype-cache;1"));
|
|
|
|
if (cache) {
|
|
|
|
PRBool enabled;
|
|
|
|
cache->GetEnabled(&enabled);
|
|
|
|
if (enabled) {
|
2000-07-28 13:36:51 +04:00
|
|
|
nsCOMPtr<nsICSSStyleSheet> cachedSheet;
|
|
|
|
cache->GetStyleSheet(aURL, getter_AddRefs(cachedSheet));
|
|
|
|
if (cachedSheet)
|
|
|
|
sheet = cachedSheet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-04-16 04:23:57 +04:00
|
|
|
if (sheet && (NS_OK == sheet->IsModified(&sheetModified)) && sheetModified) { // if dirty, forget it
|
1999-06-03 05:56:14 +04:00
|
|
|
sheet = nsnull;
|
|
|
|
}
|
1999-05-19 03:15:10 +04:00
|
|
|
|
1999-06-03 05:56:14 +04:00
|
|
|
if (sheet) { // already have one loaded and unmodified
|
1999-05-19 03:15:10 +04:00
|
|
|
nsICSSStyleSheet* clone = nsnull;
|
|
|
|
result = sheet->Clone(clone);
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
2000-12-02 18:53:42 +03:00
|
|
|
result = SetMedia(clone, aMedia);
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
result = InsertChildSheet(clone, aParentSheet, aIndex);
|
2001-08-05 01:19:53 +04:00
|
|
|
if (NS_SUCCEEDED(result) && aParentRule) {
|
|
|
|
aParentRule->SetSheet(clone);
|
|
|
|
}
|
2000-12-02 18:53:42 +03:00
|
|
|
}
|
1999-05-19 03:15:10 +04:00
|
|
|
NS_RELEASE(clone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
1999-06-15 10:19:21 +04:00
|
|
|
SheetLoadData* data = new SheetLoadData(this, aURL, aMedia, aDefaultNameSpaceID,
|
2001-08-05 01:19:53 +04:00
|
|
|
aParentSheet, aIndex, aParentRule);
|
1999-11-02 09:49:44 +03:00
|
|
|
if (data == nsnull) {
|
|
|
|
result = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_ADDREF(data);
|
|
|
|
PRInt32 count = mParsingData.Count();
|
|
|
|
if (count) { // still parsing the parent (expected for @import)
|
|
|
|
// XXX assert that last parsing == parent sheet
|
|
|
|
SheetLoadData* parentData = (SheetLoadData*)mParsingData.ElementAt(count - 1);
|
|
|
|
data->mParentData = parentData;
|
|
|
|
data->mIsAgent = parentData->mIsAgent;
|
|
|
|
data->mSyncLoad = parentData->mSyncLoad;
|
|
|
|
|
|
|
|
// verify that sheet doesn't have new child as a parent
|
|
|
|
do {
|
|
|
|
PRBool equals;
|
|
|
|
result = parentData->mURL->Equals(aURL, &equals);
|
|
|
|
if (NS_SUCCEEDED(result) && equals) { // houston, we have a loop, blow off this child
|
|
|
|
data->mParentData = nsnull;
|
|
|
|
NS_RELEASE(data);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
parentData = parentData->mParentData;
|
|
|
|
} while (parentData);
|
2002-07-02 07:56:08 +04:00
|
|
|
|
|
|
|
++(data->mParentData->mPendingChildren);
|
1999-11-02 09:49:44 +03:00
|
|
|
}
|
|
|
|
result = LoadSheet(key, data);
|
2002-07-02 07:56:08 +04:00
|
|
|
if (count && NS_FAILED(result)) {
|
|
|
|
// If the load failed, undo the addition of the pending child.
|
|
|
|
--(data->mParentData->mPendingChildren);
|
2002-07-02 06:34:57 +04:00
|
|
|
}
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-05-27 03:52:37 +04:00
|
|
|
NS_IMETHODIMP
|
1999-06-23 07:29:44 +04:00
|
|
|
CSSLoaderImpl::LoadAgentSheet(nsIURI* aURL,
|
1999-05-27 03:52:37 +04:00
|
|
|
nsICSSStyleSheet*& aSheet,
|
|
|
|
PRBool& aCompleted,
|
1999-12-04 04:27:14 +03:00
|
|
|
nsICSSLoaderObserver* aObserver)
|
1999-05-27 03:52:37 +04:00
|
|
|
{
|
|
|
|
nsresult result = NS_ERROR_NULL_POINTER;
|
|
|
|
if (aURL) {
|
|
|
|
// Get an input stream from the url
|
|
|
|
nsIInputStream* in;
|
1999-06-26 10:36:26 +04:00
|
|
|
nsIURI* urlClone;
|
|
|
|
result = aURL->Clone(&urlClone); // dont give key URL to netlib, it gets munged
|
1999-07-07 05:29:49 +04:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
1999-07-20 12:46:33 +04:00
|
|
|
result = NS_OpenURI(&in, urlClone);
|
1999-05-28 10:50:52 +04:00
|
|
|
NS_RELEASE(urlClone);
|
1999-05-27 03:52:37 +04:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
2000-04-23 06:01:40 +04:00
|
|
|
// Translate the input, using our characterset, into unicode
|
2001-12-04 02:03:18 +03:00
|
|
|
nsCOMPtr<nsIConverterInputStream> uin =
|
|
|
|
do_CreateInstance("@mozilla.org/intl/converter-input-stream;1",
|
|
|
|
&result);
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
result = uin->Init(in, mCharset.get(), 0);
|
1999-05-28 10:50:52 +04:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
1999-12-04 04:27:14 +03:00
|
|
|
SheetLoadData* data = new SheetLoadData(this, aURL, aObserver);
|
1999-11-02 09:49:44 +03:00
|
|
|
if (data == nsnull) {
|
|
|
|
result = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_ADDREF(data);
|
|
|
|
URLKey key(aURL);
|
2000-03-16 14:44:00 +03:00
|
|
|
if (aObserver == nsnull) {
|
|
|
|
mLoadingSheets.Put(&key, data);
|
|
|
|
result = ParseSheet(uin, data, aCompleted, aSheet);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = LoadSheet(key, data); // this may steal pending alternates too
|
|
|
|
aCompleted = PR_FALSE;
|
|
|
|
}
|
1999-11-02 09:49:44 +03:00
|
|
|
}
|
1999-05-28 10:50:52 +04:00
|
|
|
}
|
1999-06-22 23:21:54 +04:00
|
|
|
else {
|
|
|
|
fputs("CSSLoader::LoadAgentSheet - failed to get converter stream\n", stderr);
|
|
|
|
}
|
1999-05-28 10:50:52 +04:00
|
|
|
NS_RELEASE(in);
|
1999-05-27 03:52:37 +04:00
|
|
|
}
|
2001-02-03 12:09:24 +03:00
|
|
|
#ifdef DEBUG
|
1999-06-12 03:10:59 +04:00
|
|
|
else {
|
|
|
|
// Dump an error message to the console
|
2001-12-09 19:14:02 +03:00
|
|
|
PRBool ignoreError = PR_FALSE;
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCAutoString url;
|
|
|
|
aURL->GetSpec(url);
|
2001-12-09 19:14:02 +03:00
|
|
|
// Ignore userChrome.css and userContent.css failures
|
|
|
|
#define USERCHROMECSS "userChrome.css"
|
|
|
|
#define USERCONTENTCSS "userContent.css"
|
|
|
|
if (url.Length() > sizeof(USERCHROMECSS) &&
|
|
|
|
Substring(url, url.Length()-sizeof(USERCHROMECSS)+1, sizeof(USERCHROMECSS)).Equals(NS_LITERAL_CSTRING(USERCHROMECSS)))
|
|
|
|
ignoreError = PR_TRUE;
|
|
|
|
|
|
|
|
if (!ignoreError &&
|
|
|
|
url.Length() > sizeof(USERCONTENTCSS) &&
|
|
|
|
Substring(url, url.Length()-sizeof(USERCONTENTCSS)+1, sizeof(USERCONTENTCSS)).Equals(NS_LITERAL_CSTRING(USERCONTENTCSS)))
|
|
|
|
ignoreError = PR_TRUE;
|
|
|
|
|
|
|
|
if (!ignoreError) {
|
|
|
|
nsCAutoString errorMessage(NS_LITERAL_CSTRING("CSSLoaderImpl::LoadAgentSheet: Load of URL '") +
|
2002-03-06 10:48:55 +03:00
|
|
|
url +
|
2001-12-09 19:14:02 +03:00
|
|
|
NS_LITERAL_CSTRING("' failed. Error code: "));
|
|
|
|
errorMessage.AppendInt(NS_ERROR_GET_CODE(result));
|
|
|
|
NS_WARNING(errorMessage.get());
|
|
|
|
}
|
1999-06-12 03:10:59 +04:00
|
|
|
}
|
2001-02-03 12:09:24 +03:00
|
|
|
#endif
|
1999-05-27 03:52:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
1999-05-19 03:15:10 +04:00
|
|
|
|
|
|
|
|
|
|
|
nsresult NS_NewCSSLoader(nsIDocument* aDocument, nsICSSLoader** aLoader)
|
|
|
|
{
|
|
|
|
CSSLoaderImpl* it = new CSSLoaderImpl();
|
|
|
|
|
|
|
|
if (it == nsnull) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
it->Init(aDocument);
|
2001-01-04 23:44:42 +03:00
|
|
|
return it->QueryInterface(NS_GET_IID(nsICSSLoader), (void **)aLoader);
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult NS_NewCSSLoader(nsICSSLoader** aLoader)
|
|
|
|
{
|
|
|
|
CSSLoaderImpl* it = new CSSLoaderImpl();
|
|
|
|
|
|
|
|
if (it == nsnull) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2001-01-04 23:44:42 +03:00
|
|
|
return it->QueryInterface(NS_GET_IID(nsICSSLoader), (void **)aLoader);
|
1999-05-19 03:15:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-10-25 04:34:20 +04:00
|
|
|
NS_IMETHODIMP CSSLoaderImpl::GetCharset(/*out*/nsAString &aCharsetDest) const
|
2000-04-23 06:01:40 +04:00
|
|
|
{
|
2002-01-24 05:03:19 +03:00
|
|
|
NS_ASSERTION(!mCharset.IsEmpty(), "CSSLoader charset should be set in ctor" );
|
2000-04-23 06:01:40 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
aCharsetDest = mCharset;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-10-25 04:34:20 +04:00
|
|
|
NS_IMETHODIMP CSSLoaderImpl::SetCharset(/*in*/ const nsAString &aCharsetSrc)
|
|
|
|
// public method for clients to set the charset if they know it
|
|
|
|
// NOTE: the SetCharset method will always get the preferred
|
|
|
|
// charset from the charset passed in unless it is the
|
|
|
|
// emptystring, which causes the default charset (that of the
|
|
|
|
// document, falling back to ISO-8869-1) to be set
|
2000-04-23 06:01:40 +04:00
|
|
|
{
|
2001-10-25 04:34:20 +04:00
|
|
|
nsresult rv = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
if (! aCharsetSrc.IsEmpty()) {
|
2001-07-25 11:54:28 +04:00
|
|
|
nsCOMPtr<nsICharsetAlias> calias(do_GetService(kCharsetAliasCID, &rv));
|
2000-04-23 06:01:40 +04:00
|
|
|
NS_ASSERTION(calias, "cannot find charset alias");
|
2001-10-25 04:34:20 +04:00
|
|
|
if (calias)
|
2000-04-23 06:01:40 +04:00
|
|
|
{
|
|
|
|
PRBool same = PR_FALSE;
|
|
|
|
rv = calias->Equals(aCharsetSrc, mCharset, &same);
|
|
|
|
if(NS_SUCCEEDED(rv) && same)
|
|
|
|
{
|
|
|
|
return NS_OK; // no difference, don't change it
|
|
|
|
}
|
2001-10-25 04:34:20 +04:00
|
|
|
rv = calias->GetPreferred(aCharsetSrc, mCharset);
|
2000-04-23 06:01:40 +04:00
|
|
|
}
|
2001-10-25 04:34:20 +04:00
|
|
|
} else if (mDocument) {
|
|
|
|
// GetDocumentCharacterSet returns a charset which already has
|
|
|
|
// alias resolution done
|
|
|
|
rv = mDocument->GetDocumentCharacterSet(mCharset);
|
|
|
|
}
|
|
|
|
if (mCharset.IsEmpty()) {
|
|
|
|
mCharset = NS_LITERAL_STRING("ISO-8859-1");
|
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
2000-04-23 06:01:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-10-25 04:34:20 +04:00
|
|
|
nsresult CSSLoaderImpl::SetCharset(/*in*/ const char* aStyleSheetData,
|
|
|
|
/*in*/ PRUint32 aDataLength)
|
|
|
|
// sets the charset based upon the data passed in
|
|
|
|
// - if the StyleSheetData is not empty and it has '@charset'
|
|
|
|
// as the first substring, then use that
|
|
|
|
// - otherwise return an error
|
2000-04-23 06:01:40 +04:00
|
|
|
{
|
2001-10-25 04:34:20 +04:00
|
|
|
nsresult rv = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
nsString strStyleDataUndecoded;
|
|
|
|
strStyleDataUndecoded.AssignWithConversion(aStyleSheetData, aDataLength);
|
2001-09-09 01:13:20 +04:00
|
|
|
PRInt32 charsetOffset;
|
2002-01-24 05:03:19 +03:00
|
|
|
if (!strStyleDataUndecoded.IsEmpty()) {
|
2001-10-25 04:34:20 +04:00
|
|
|
nsString str;
|
2001-05-15 07:03:19 +04:00
|
|
|
static const char atCharsetStr[] = "@charset";
|
2001-10-25 04:34:20 +04:00
|
|
|
if ((charsetOffset = strStyleDataUndecoded.Find(atCharsetStr)) > -1) {
|
2000-04-23 06:01:40 +04:00
|
|
|
nsString strValue;
|
|
|
|
// skip past the ident
|
2001-10-25 04:34:20 +04:00
|
|
|
strStyleDataUndecoded.Right(str, strStyleDataUndecoded.Length() -
|
|
|
|
(sizeof(atCharsetStr)-1));
|
2000-04-23 06:01:40 +04:00
|
|
|
// strip any whitespace
|
|
|
|
str.StripWhitespace();
|
|
|
|
// truncate everything past the delimiter (semicolon)
|
|
|
|
PRInt32 pos = str.Find(";");
|
|
|
|
if (pos > -1) {
|
|
|
|
str.Left(strValue,pos);
|
|
|
|
}
|
|
|
|
// strip any quotes
|
|
|
|
strValue.Trim("\"\'");
|
|
|
|
|
|
|
|
// that's the charset!
|
2001-10-25 04:34:20 +04:00
|
|
|
if (!strValue.IsEmpty()) {
|
|
|
|
rv = SetCharset(strValue);
|
|
|
|
}
|
2000-04-23 06:01:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
2001-05-19 06:59:15 +04:00
|
|
|
|
|
|
|
static PRBool PR_CALLBACK StopLoadingSheetCallback(nsHashKey* aKey, void* aData, void* aClosure)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aData, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
SheetLoadData* data = (SheetLoadData*)aData;
|
|
|
|
NS_ENSURE_TRUE(data->mLoader, NS_ERROR_NULL_POINTER);
|
|
|
|
NS_ENSURE_TRUE(data->mURL, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
data->mLoader->StopLoadingSheet(data->mURL);
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CSSLoaderImpl::Stop()
|
|
|
|
{
|
|
|
|
if (mLoadingSheets.Count() > 0) {
|
|
|
|
mLoadingSheets.Enumerate(StopLoadingSheetCallback);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CSSLoaderImpl::StopLoadingSheet(nsIURI* aURL)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aURL, NS_ERROR_NULL_POINTER);
|
|
|
|
if (mLoadingSheets.Count() > 0) {
|
|
|
|
URLKey key(aURL);
|
|
|
|
SheetLoadData* loadData = (SheetLoadData*)mLoadingSheets.Get(&key);
|
|
|
|
if (loadData) {
|
|
|
|
Cleanup(key, loadData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|