Bug 485012. Allow passing a charset hint (e.g. the element's 'charset' attribute for preloads) to nsICSSLoader::LoadSheet. r+sr=peterv

This commit is contained in:
Boris Zbarsky 2009-04-10 10:29:08 -04:00
Родитель d0abc43b05
Коммит c2aeda131e
10 изменённых файлов: 68 добавлений и 9 удалений

Просмотреть файл

@ -165,7 +165,7 @@ nsXBLResourceLoader::LoadResources(PRBool* aResult)
} }
else else
{ {
rv = cssLoader->LoadSheet(url, docPrincipal, this); rv = cssLoader->LoadSheet(url, docPrincipal, EmptyCString(), this);
if (NS_SUCCEEDED(rv)) if (NS_SUCCEEDED(rv))
++mPendingSheets; ++mPendingSheets;
} }

Просмотреть файл

@ -3899,7 +3899,8 @@ nsXULDocument::AddPrototypeSheets()
nsCOMPtr<nsICSSStyleSheet> incompleteSheet; nsCOMPtr<nsICSSStyleSheet> incompleteSheet;
rv = CSSLoader()->LoadSheet(uri, rv = CSSLoader()->LoadSheet(uri,
mCurrentPrototype->DocumentPrincipal(), mCurrentPrototype->DocumentPrincipal(),
this, getter_AddRefs(incompleteSheet)); EmptyCString(), this,
getter_AddRefs(incompleteSheet));
// XXXldb We need to prevent bogus sheets from being held in the // XXXldb We need to prevent bogus sheets from being held in the
// prototype's list, but until then, don't propagate the failure // prototype's list, but until then, don't propagate the failure

Просмотреть файл

@ -3401,7 +3401,7 @@ nsHTMLEditor::ReplaceStyleSheet(const nsAString& aURL)
rv = NS_NewURI(getter_AddRefs(uaURI), aURL); rv = NS_NewURI(getter_AddRefs(uaURI), aURL);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
rv = cssLoader->LoadSheet(uaURI, nsnull, this); rv = cssLoader->LoadSheet(uaURI, nsnull, EmptyCString(), this);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
return NS_OK; return NS_OK;

Просмотреть файл

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<style>
body { color: green }
</style>
</head>
<body>
This should be green
</body>
</html>

Просмотреть файл

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<style>
body { color: red }
</style>
<script src="data:application/javascript,"></script>
<link rel="stylesheet" type="text/css"
charset="UTF-16BE"
href="data:text/css,%00b%00o%00d%00y%00{%00c%00o%00l%00o%00r%00:%00g%00r%00e%00e%00n}">
</head>
<body>
This should be green
</body>
</html>

Просмотреть файл

@ -1192,6 +1192,7 @@ fails == 472020-2.xul 472020-2-ref.xul
== 482659-1c.html 482659-1-ref.html == 482659-1c.html 482659-1-ref.html
== 482659-1d.html 482659-1-ref.html == 482659-1d.html 482659-1-ref.html
== 483565.xul 483565-ref.xul == 483565.xul 483565-ref.xul
== 485012-1.html 485012-1-ref.html
== 485275-1.html 485275-1-ref.html == 485275-1.html 485275-1-ref.html
== 485275-1.svg 485275-1-ref.html == 485275-1.svg 485275-1-ref.html
== 486052-1.html 486052-1-ref.html == 486052-1.html 486052-1-ref.html

Просмотреть файл

@ -221,6 +221,7 @@ SheetLoadData::SheetLoadData(CSSLoaderImpl* aLoader,
PRBool aSyncLoad, PRBool aSyncLoad,
PRBool aAllowUnsafeRules, PRBool aAllowUnsafeRules,
PRBool aUseSystemPrincipal, PRBool aUseSystemPrincipal,
const nsCString& aCharset,
nsICSSLoaderObserver* aObserver, nsICSSLoaderObserver* aObserver,
nsIPrincipal* aLoaderPrincipal) nsIPrincipal* aLoaderPrincipal)
: mLoader(aLoader), : mLoader(aLoader),
@ -240,7 +241,8 @@ SheetLoadData::SheetLoadData(CSSLoaderImpl* aLoader,
mUseSystemPrincipal(aUseSystemPrincipal), mUseSystemPrincipal(aUseSystemPrincipal),
mOwningElement(nsnull), mOwningElement(nsnull),
mObserver(aObserver), mObserver(aObserver),
mLoaderPrincipal(aLoaderPrincipal) mLoaderPrincipal(aLoaderPrincipal),
mCharsetHint(aCharset)
{ {
NS_PRECONDITION(mLoader, "Must have a loader!"); NS_PRECONDITION(mLoader, "Must have a loader!");
NS_ADDREF(mLoader); NS_ADDREF(mLoader);
@ -607,6 +609,9 @@ SheetLoadData::OnDetermineCharset(nsIUnicharStreamLoader* aLoader,
PRUint32 aDataLength, PRUint32 aDataLength,
nsACString& aCharset) nsACString& aCharset)
{ {
NS_PRECONDITION(!mOwningElement || mCharsetHint.IsEmpty(),
"Can't have element _and_ charset hint");
LOG_URI("SheetLoadData::OnDetermineCharset for '%s'", mURI); LOG_URI("SheetLoadData::OnDetermineCharset for '%s'", mURI);
nsCOMPtr<nsIChannel> channel; nsCOMPtr<nsIChannel> channel;
nsresult result = aLoader->GetChannel(getter_AddRefs(channel)); nsresult result = aLoader->GetChannel(getter_AddRefs(channel));
@ -662,6 +667,9 @@ SheetLoadData::OnDetermineCharset(nsIUnicharStreamLoader* aLoader,
PromiseFlatCString(aCharset).get())); PromiseFlatCString(aCharset).get()));
} }
#endif #endif
} else {
// If mCharsetHint is empty, that's ok; aCharset is known empty here
aCharset = mCharsetHint;
} }
} }
@ -1995,30 +2003,32 @@ CSSLoaderImpl::LoadSheetSync(nsIURI* aURL, PRBool aAllowUnsafeRules,
LOG(("CSSLoaderImpl::LoadSheetSync")); LOG(("CSSLoaderImpl::LoadSheetSync"));
return InternalLoadNonDocumentSheet(aURL, aAllowUnsafeRules, return InternalLoadNonDocumentSheet(aURL, aAllowUnsafeRules,
aUseSystemPrincipal, nsnull, aUseSystemPrincipal, nsnull,
aSheet, nsnull); EmptyCString(), aSheet, nsnull);
} }
NS_IMETHODIMP NS_IMETHODIMP
CSSLoaderImpl::LoadSheet(nsIURI* aURL, CSSLoaderImpl::LoadSheet(nsIURI* aURL,
nsIPrincipal* aOriginPrincipal, nsIPrincipal* aOriginPrincipal,
const nsCString& aCharset,
nsICSSLoaderObserver* aObserver, nsICSSLoaderObserver* aObserver,
nsICSSStyleSheet** aSheet) nsICSSStyleSheet** aSheet)
{ {
LOG(("CSSLoaderImpl::LoadSheet(aURL, aObserver, aSheet) api call")); LOG(("CSSLoaderImpl::LoadSheet(aURL, aObserver, aSheet) api call"));
NS_PRECONDITION(aSheet, "aSheet is null"); NS_PRECONDITION(aSheet, "aSheet is null");
return InternalLoadNonDocumentSheet(aURL, PR_FALSE, PR_FALSE, return InternalLoadNonDocumentSheet(aURL, PR_FALSE, PR_FALSE,
aOriginPrincipal, aOriginPrincipal, aCharset,
aSheet, aObserver); aSheet, aObserver);
} }
NS_IMETHODIMP NS_IMETHODIMP
CSSLoaderImpl::LoadSheet(nsIURI* aURL, CSSLoaderImpl::LoadSheet(nsIURI* aURL,
nsIPrincipal* aOriginPrincipal, nsIPrincipal* aOriginPrincipal,
const nsCString& aCharset,
nsICSSLoaderObserver* aObserver) nsICSSLoaderObserver* aObserver)
{ {
LOG(("CSSLoaderImpl::LoadSheet(aURL, aObserver) api call")); LOG(("CSSLoaderImpl::LoadSheet(aURL, aObserver) api call"));
return InternalLoadNonDocumentSheet(aURL, PR_FALSE, PR_FALSE, return InternalLoadNonDocumentSheet(aURL, PR_FALSE, PR_FALSE,
aOriginPrincipal, aOriginPrincipal, aCharset,
nsnull, aObserver); nsnull, aObserver);
} }
@ -2027,6 +2037,7 @@ CSSLoaderImpl::InternalLoadNonDocumentSheet(nsIURI* aURL,
PRBool aAllowUnsafeRules, PRBool aAllowUnsafeRules,
PRBool aUseSystemPrincipal, PRBool aUseSystemPrincipal,
nsIPrincipal* aOriginPrincipal, nsIPrincipal* aOriginPrincipal,
const nsCString& aCharset,
nsICSSStyleSheet** aSheet, nsICSSStyleSheet** aSheet,
nsICSSLoaderObserver* aObserver) nsICSSLoaderObserver* aObserver)
{ {
@ -2077,7 +2088,8 @@ CSSLoaderImpl::InternalLoadNonDocumentSheet(nsIURI* aURL,
SheetLoadData* data = SheetLoadData* data =
new SheetLoadData(this, aURL, sheet, syncLoad, aAllowUnsafeRules, new SheetLoadData(this, aURL, sheet, syncLoad, aAllowUnsafeRules,
aUseSystemPrincipal, aObserver, aOriginPrincipal); aUseSystemPrincipal, aCharset, aObserver,
aOriginPrincipal);
if (!data) { if (!data) {
sheet->SetComplete(); sheet->SetComplete();

Просмотреть файл

@ -136,6 +136,7 @@ public:
PRBool aSyncLoad, PRBool aSyncLoad,
PRBool aAllowUnsafeRules, PRBool aAllowUnsafeRules,
PRBool aUseSystemPrincipal, PRBool aUseSystemPrincipal,
const nsCString& aCharset,
nsICSSLoaderObserver* aObserver, nsICSSLoaderObserver* aObserver,
nsIPrincipal* aLoaderPrincipal); nsIPrincipal* aLoaderPrincipal);
@ -223,6 +224,10 @@ public:
// The principal that identifies who started loading us. // The principal that identifies who started loading us.
nsCOMPtr<nsIPrincipal> mLoaderPrincipal; nsCOMPtr<nsIPrincipal> mLoaderPrincipal;
// The charset to use if the transport and sheet don't indicate one.
// May be empty. Must be empty if mOwningElement is non-null.
nsCString mCharsetHint;
}; };
class nsURIAndPrincipalHashKey : public nsURIHashKey class nsURIAndPrincipalHashKey : public nsURIHashKey
@ -350,11 +355,13 @@ public:
NS_IMETHOD LoadSheet(nsIURI* aURL, NS_IMETHOD LoadSheet(nsIURI* aURL,
nsIPrincipal* aOriginPrincipal, nsIPrincipal* aOriginPrincipal,
const nsCString& aCharset,
nsICSSLoaderObserver* aObserver, nsICSSLoaderObserver* aObserver,
nsICSSStyleSheet** aSheet); nsICSSStyleSheet** aSheet);
NS_IMETHOD LoadSheet(nsIURI* aURL, NS_IMETHOD LoadSheet(nsIURI* aURL,
nsIPrincipal* aOriginPrincipal, nsIPrincipal* aOriginPrincipal,
const nsCString& aCharset,
nsICSSLoaderObserver* aObserver); nsICSSLoaderObserver* aObserver);
// stop loading all sheets // stop loading all sheets
@ -424,6 +431,7 @@ private:
PRBool aAllowUnsafeRules, PRBool aAllowUnsafeRules,
PRBool aUseSystemPrincipal, PRBool aUseSystemPrincipal,
nsIPrincipal* aOriginPrincipal, nsIPrincipal* aOriginPrincipal,
const nsCString& aCharset,
nsICSSStyleSheet** aSheet, nsICSSStyleSheet** aSheet,
nsICSSLoaderObserver* aObserver); nsICSSLoaderObserver* aObserver);

Просмотреть файл

@ -209,6 +209,11 @@ public:
* @param aOriginPrincipal the principal to use for security checks. This * @param aOriginPrincipal the principal to use for security checks. This
* can be null to indicate that these checks should * can be null to indicate that these checks should
* be skipped. * be skipped.
* @param aCharset the encoding to use for converting the sheet data
* from bytes to Unicode. May be empty to indicate that the
* charset of the CSSLoader's document should be used. This
* is only used if neither the network transport nor the
* sheet itself indicate an encoding.
* @param aObserver the observer to notify when the load completes. * @param aObserver the observer to notify when the load completes.
* Must not be null. * Must not be null.
* @param [out] aSheet the sheet to load. Note that the sheet may well * @param [out] aSheet the sheet to load. Note that the sheet may well
@ -216,6 +221,7 @@ public:
*/ */
NS_IMETHOD LoadSheet(nsIURI* aURL, NS_IMETHOD LoadSheet(nsIURI* aURL,
nsIPrincipal* aOriginPrincipal, nsIPrincipal* aOriginPrincipal,
const nsCString& aCharset,
nsICSSLoaderObserver* aObserver, nsICSSLoaderObserver* aObserver,
nsICSSStyleSheet** aSheet) = 0; nsICSSStyleSheet** aSheet) = 0;
@ -225,6 +231,7 @@ public:
*/ */
NS_IMETHOD LoadSheet(nsIURI* aURL, NS_IMETHOD LoadSheet(nsIURI* aURL,
nsIPrincipal* aOriginPrincipal, nsIPrincipal* aOriginPrincipal,
const nsCString& aCharset,
nsICSSLoaderObserver* aObserver) = 0; nsICSSLoaderObserver* aObserver) = 0;
/** /**

Просмотреть файл

@ -384,7 +384,9 @@ nsPreloadURIs::PreloadURIs(const nsAutoTArray<nsSpeculativeScriptThread::Prefetc
break; break;
case nsSpeculativeScriptThread::STYLESHEET: case nsSpeculativeScriptThread::STYLESHEET:
nsCOMPtr<nsICSSLoaderObserver> obs = new nsDummyCSSLoaderObserver(); nsCOMPtr<nsICSSLoaderObserver> obs = new nsDummyCSSLoaderObserver();
doc->CSSLoader()->LoadSheet(uri, doc->NodePrincipal(), obs); doc->CSSLoader()->LoadSheet(uri, doc->NodePrincipal(),
NS_LossyConvertUTF16toASCII(pe.charset),
obs);
break; break;
} }
} }