2001-05-19 00:21:26 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2011-05-19 01:22:59 +00:00
|
|
|
*
|
2012-05-21 11:12:37 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-05-19 00:21:26 +00:00
|
|
|
|
2006-03-31 08:00:42 +00:00
|
|
|
/*
|
|
|
|
* A base class which implements nsIStyleSheetLinkingElement and can
|
|
|
|
* be subclassed by various content nodes that want to load
|
|
|
|
* stylesheets (<style>, <link>, processing instructions, etc).
|
|
|
|
*/
|
|
|
|
|
2001-05-19 00:21:26 +00:00
|
|
|
#include "nsStyleLinkElement.h"
|
|
|
|
|
|
|
|
#include "nsIContent.h"
|
2010-06-28 22:49:35 +00:00
|
|
|
#include "mozilla/css/Loader.h"
|
2010-05-11 20:41:47 +00:00
|
|
|
#include "nsCSSStyleSheet.h"
|
2001-05-19 00:21:26 +00:00
|
|
|
#include "nsIDocument.h"
|
2002-04-05 11:29:40 +00:00
|
|
|
#include "nsIDOMComment.h"
|
2001-05-19 00:21:26 +00:00
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIDOMStyleSheet.h"
|
|
|
|
#include "nsNetUtil.h"
|
2002-04-05 11:29:40 +00:00
|
|
|
#include "nsUnicharUtils.h"
|
2002-05-15 18:55:21 +00:00
|
|
|
#include "nsCRT.h"
|
2006-07-21 16:28:51 +00:00
|
|
|
#include "nsXPCOMCIDInternal.h"
|
|
|
|
#include "nsUnicharInputStream.h"
|
2006-09-15 19:54:45 +00:00
|
|
|
#include "nsContentUtils.h"
|
2001-05-19 00:21:26 +00:00
|
|
|
|
2004-09-09 17:32:35 +00:00
|
|
|
nsStyleLinkElement::nsStyleLinkElement()
|
2011-10-17 14:59:28 +00:00
|
|
|
: mDontLoadStyle(false)
|
|
|
|
, mUpdatesEnabled(true)
|
2004-09-09 17:32:35 +00:00
|
|
|
, mLineNumber(1)
|
2001-05-19 00:21:26 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsStyleLinkElement::~nsStyleLinkElement()
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
nsStyleLinkElement::SetStyleSheet(nullptr);
|
2001-05-19 00:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStyleLinkElement::SetStyleSheet(nsIStyleSheet* aStyleSheet)
|
|
|
|
{
|
2010-05-11 20:41:47 +00:00
|
|
|
nsRefPtr<nsCSSStyleSheet> cssSheet = do_QueryObject(mStyleSheet);
|
2003-02-04 04:45:58 +00:00
|
|
|
if (cssSheet) {
|
2012-07-30 14:20:58 +00:00
|
|
|
cssSheet->SetOwningNode(nullptr);
|
2003-02-04 04:45:58 +00:00
|
|
|
}
|
|
|
|
|
2001-05-19 00:21:26 +00:00
|
|
|
mStyleSheet = aStyleSheet;
|
2010-05-11 20:41:47 +00:00
|
|
|
cssSheet = do_QueryObject(mStyleSheet);
|
2002-12-03 05:48:14 +00:00
|
|
|
if (cssSheet) {
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
CallQueryInterface(this,
|
2007-07-08 07:08:04 +00:00
|
|
|
static_cast<nsIDOMNode**>(getter_AddRefs(node)));
|
2002-12-03 05:48:14 +00:00
|
|
|
if (node) {
|
|
|
|
cssSheet->SetOwningNode(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-19 00:21:26 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStyleLinkElement::GetStyleSheet(nsIStyleSheet*& aStyleSheet)
|
|
|
|
{
|
|
|
|
aStyleSheet = mStyleSheet;
|
|
|
|
NS_IF_ADDREF(aStyleSheet);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsStyleLinkElement::InitStyleLinkElement(bool aDontLoadStyle)
|
2001-05-19 00:21:26 +00:00
|
|
|
{
|
|
|
|
mDontLoadStyle = aDontLoadStyle;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStyleLinkElement::GetSheet(nsIDOMStyleSheet** aSheet)
|
|
|
|
{
|
2001-10-15 22:57:17 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aSheet);
|
2012-07-30 14:20:58 +00:00
|
|
|
*aSheet = nullptr;
|
2001-10-15 22:57:17 +00:00
|
|
|
|
|
|
|
if (mStyleSheet) {
|
|
|
|
CallQueryInterface(mStyleSheet, aSheet);
|
|
|
|
}
|
2001-05-19 00:21:26 +00:00
|
|
|
|
|
|
|
// Always return NS_OK to avoid throwing JS exceptions if mStyleSheet
|
|
|
|
// is not a nsIDOMStyleSheet
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsStyleLinkElement::SetEnableUpdates(bool aEnableUpdates)
|
2001-05-19 00:21:26 +00:00
|
|
|
{
|
|
|
|
mUpdatesEnabled = aEnableUpdates;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-10-25 00:34:20 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStyleLinkElement::GetCharset(nsAString& aCharset)
|
|
|
|
{
|
|
|
|
// descendants have to implement this themselves
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2006-12-18 03:59:46 +00:00
|
|
|
/* virtual */ void
|
|
|
|
nsStyleLinkElement::OverrideBaseURI(nsIURI* aNewBaseURI)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("Base URI can't be overriden in this implementation "
|
|
|
|
"of nsIStyleSheetLinkingElement.");
|
|
|
|
}
|
|
|
|
|
2004-09-09 17:32:35 +00:00
|
|
|
/* virtual */ void
|
2012-08-22 15:56:38 +00:00
|
|
|
nsStyleLinkElement::SetLineNumber(uint32_t aLineNumber)
|
2004-09-09 17:32:35 +00:00
|
|
|
{
|
|
|
|
mLineNumber = aLineNumber;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t ToLinkMask(const nsAString& aLink)
|
2011-11-20 11:13:40 +00:00
|
|
|
{
|
|
|
|
if (aLink.EqualsLiteral("prefetch"))
|
|
|
|
return PREFETCH;
|
|
|
|
else if (aLink.EqualsLiteral("dns-prefetch"))
|
|
|
|
return DNS_PREFETCH;
|
|
|
|
else if (aLink.EqualsLiteral("stylesheet"))
|
|
|
|
return STYLESHEET;
|
|
|
|
else if (aLink.EqualsLiteral("next"))
|
|
|
|
return NEXT;
|
|
|
|
else if (aLink.EqualsLiteral("alternate"))
|
|
|
|
return ALTERNATE;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t nsStyleLinkElement::ParseLinkTypes(const nsAString& aTypes)
|
2001-05-19 00:21:26 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t linkMask = 0;
|
2003-07-14 07:37:39 +00:00
|
|
|
nsAString::const_iterator start, done;
|
|
|
|
aTypes.BeginReading(start);
|
2001-05-19 00:21:26 +00:00
|
|
|
aTypes.EndReading(done);
|
2003-07-14 07:37:39 +00:00
|
|
|
if (start == done)
|
2011-11-20 11:13:40 +00:00
|
|
|
return linkMask;
|
2001-05-19 00:21:26 +00:00
|
|
|
|
2003-07-14 07:37:39 +00:00
|
|
|
nsAString::const_iterator current(start);
|
2011-12-03 21:50:15 +00:00
|
|
|
bool inString = !nsContentUtils::IsHTMLWhitespace(*current);
|
2001-05-21 08:24:35 +00:00
|
|
|
nsAutoString subString;
|
2011-11-20 11:13:40 +00:00
|
|
|
|
2001-05-19 00:21:26 +00:00
|
|
|
while (current != done) {
|
2011-12-03 21:50:15 +00:00
|
|
|
if (nsContentUtils::IsHTMLWhitespace(*current)) {
|
2001-05-19 00:21:26 +00:00
|
|
|
if (inString) {
|
2012-03-10 05:50:34 +00:00
|
|
|
nsContentUtils::ASCIIToLower(Substring(start, current), subString);
|
2011-11-20 11:13:40 +00:00
|
|
|
linkMask |= ToLinkMask(subString);
|
2011-10-17 14:59:28 +00:00
|
|
|
inString = false;
|
2001-05-19 00:21:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!inString) {
|
|
|
|
start = current;
|
2011-10-17 14:59:28 +00:00
|
|
|
inString = true;
|
2001-05-19 00:21:26 +00:00
|
|
|
}
|
|
|
|
}
|
2001-06-19 22:38:45 +00:00
|
|
|
++current;
|
2001-05-19 00:21:26 +00:00
|
|
|
}
|
|
|
|
if (inString) {
|
2012-03-10 05:50:34 +00:00
|
|
|
nsContentUtils::ASCIIToLower(Substring(start, current), subString);
|
|
|
|
linkMask |= ToLinkMask(subString);
|
2001-05-19 00:21:26 +00:00
|
|
|
}
|
2011-11-20 11:13:40 +00:00
|
|
|
return linkMask;
|
2001-05-19 00:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-04-20 22:59:18 +00:00
|
|
|
nsStyleLinkElement::UpdateStyleSheet(nsICSSLoaderObserver* aObserver,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool* aWillNotify,
|
|
|
|
bool* aIsAlternate)
|
2007-04-20 22:59:18 +00:00
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
return DoUpdateStyleSheet(nullptr, aObserver, aWillNotify, aIsAlternate,
|
2011-10-17 14:59:28 +00:00
|
|
|
false);
|
2007-04-20 22:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsStyleLinkElement::UpdateStyleSheetInternal(nsIDocument *aOldDocument,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aForceUpdate)
|
2007-04-20 22:59:18 +00:00
|
|
|
{
|
2011-09-29 06:19:26 +00:00
|
|
|
bool notify, alternate;
|
2012-07-30 14:20:58 +00:00
|
|
|
return DoUpdateStyleSheet(aOldDocument, nullptr, ¬ify, &alternate,
|
2007-04-20 22:59:18 +00:00
|
|
|
aForceUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsStyleLinkElement::DoUpdateStyleSheet(nsIDocument *aOldDocument,
|
|
|
|
nsICSSLoaderObserver* aObserver,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool* aWillNotify,
|
|
|
|
bool* aIsAlternate,
|
|
|
|
bool aForceUpdate)
|
2001-05-19 00:21:26 +00:00
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
*aWillNotify = false;
|
2007-04-20 22:59:18 +00:00
|
|
|
|
2003-02-04 04:45:58 +00:00
|
|
|
if (mStyleSheet && aOldDocument) {
|
|
|
|
// We're removing the link element from the document, unload the
|
|
|
|
// stylesheet. We want to do this even if updates are disabled, since
|
|
|
|
// otherwise a sheet with a stale linking element pointer will be hanging
|
|
|
|
// around -- not good!
|
2003-10-02 21:29:40 +00:00
|
|
|
aOldDocument->BeginUpdate(UPDATE_STYLE);
|
2003-02-04 04:45:58 +00:00
|
|
|
aOldDocument->RemoveStyleSheet(mStyleSheet);
|
2003-10-02 21:29:40 +00:00
|
|
|
aOldDocument->EndUpdate(UPDATE_STYLE);
|
2012-07-30 14:20:58 +00:00
|
|
|
nsStyleLinkElement::SetStyleSheet(nullptr);
|
2003-02-04 04:45:58 +00:00
|
|
|
}
|
|
|
|
|
2001-06-14 12:33:38 +00:00
|
|
|
if (mDontLoadStyle || !mUpdatesEnabled) {
|
2001-05-19 00:21:26 +00:00
|
|
|
return NS_OK;
|
2001-06-14 12:33:38 +00:00
|
|
|
}
|
2001-05-19 00:21:26 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> thisContent;
|
|
|
|
QueryInterface(NS_GET_IID(nsIContent), getter_AddRefs(thisContent));
|
|
|
|
|
2001-06-14 12:33:38 +00:00
|
|
|
NS_ENSURE_TRUE(thisContent, NS_ERROR_FAILURE);
|
2001-05-19 00:21:26 +00:00
|
|
|
|
2003-07-28 21:09:56 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc = thisContent->GetDocument();
|
2001-05-19 00:21:26 +00:00
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
if (!doc || !doc->CSSLoader()->GetEnabled()) {
|
2007-11-20 18:50:12 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool isInline;
|
2009-07-28 16:07:31 +00:00
|
|
|
nsCOMPtr<nsIURI> uri = GetStyleSheetURL(&isInline);
|
2001-05-19 00:21:26 +00:00
|
|
|
|
2005-12-09 19:20:42 +00:00
|
|
|
if (!aForceUpdate && mStyleSheet && !isInline && uri) {
|
2010-05-18 04:00:40 +00:00
|
|
|
nsIURI* oldURI = mStyleSheet->GetSheetURI();
|
2003-07-01 02:59:54 +00:00
|
|
|
if (oldURI) {
|
2011-09-29 06:19:26 +00:00
|
|
|
bool equal;
|
2003-07-01 02:59:54 +00:00
|
|
|
nsresult rv = oldURI->Equals(uri, &equal);
|
|
|
|
if (NS_SUCCEEDED(rv) && equal) {
|
|
|
|
return NS_OK; // We already loaded this stylesheet
|
|
|
|
}
|
2001-08-16 11:32:11 +00:00
|
|
|
}
|
2001-05-19 00:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mStyleSheet) {
|
2003-10-02 21:29:40 +00:00
|
|
|
doc->BeginUpdate(UPDATE_STYLE);
|
2001-05-19 00:21:26 +00:00
|
|
|
doc->RemoveStyleSheet(mStyleSheet);
|
2003-10-02 21:29:40 +00:00
|
|
|
doc->EndUpdate(UPDATE_STYLE);
|
2012-07-30 14:20:58 +00:00
|
|
|
nsStyleLinkElement::SetStyleSheet(nullptr);
|
2001-05-19 00:21:26 +00:00
|
|
|
}
|
|
|
|
|
2003-07-01 02:59:54 +00:00
|
|
|
if (!uri && !isInline) {
|
2002-04-05 11:29:40 +00:00
|
|
|
return NS_OK; // If href is empty and this is not inline style then just bail
|
2001-05-19 00:21:26 +00:00
|
|
|
}
|
|
|
|
|
2002-04-05 11:29:40 +00:00
|
|
|
nsAutoString title, type, media;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool isAlternate;
|
2002-04-05 11:29:40 +00:00
|
|
|
|
|
|
|
GetStyleSheetInfo(title, type, media, &isAlternate);
|
|
|
|
|
2004-06-17 00:13:25 +00:00
|
|
|
if (!type.LowerCaseEqualsLiteral("text/css")) {
|
2001-05-19 00:21:26 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool doneLoading = false;
|
2003-07-01 02:59:54 +00:00
|
|
|
nsresult rv = NS_OK;
|
2002-04-05 11:29:40 +00:00
|
|
|
if (isInline) {
|
2011-05-19 01:22:59 +00:00
|
|
|
nsAutoString text;
|
2011-10-17 14:59:28 +00:00
|
|
|
nsContentUtils::GetNodeTextContent(thisContent, false, text);
|
2002-04-05 11:29:40 +00:00
|
|
|
|
2011-05-19 01:22:59 +00:00
|
|
|
// Parse the style sheet.
|
2005-04-19 01:27:11 +00:00
|
|
|
rv = doc->CSSLoader()->
|
2011-05-19 01:22:59 +00:00
|
|
|
LoadInlineStyle(thisContent, text, mLineNumber, title, media,
|
2007-04-20 22:59:18 +00:00
|
|
|
aObserver, &doneLoading, &isAlternate);
|
2002-04-05 11:29:40 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-09-26 22:03:16 +00:00
|
|
|
// XXXbz clone the URI here to work around content policies modifying URIs.
|
|
|
|
nsCOMPtr<nsIURI> clonedURI;
|
|
|
|
uri->Clone(getter_AddRefs(clonedURI));
|
|
|
|
NS_ENSURE_TRUE(clonedURI, NS_ERROR_OUT_OF_MEMORY);
|
2005-04-19 01:27:11 +00:00
|
|
|
rv = doc->CSSLoader()->
|
2011-09-26 22:03:16 +00:00
|
|
|
LoadStyleLink(thisContent, clonedURI, title, media, isAlternate, aObserver,
|
2007-04-20 22:59:18 +00:00
|
|
|
&isAlternate);
|
2008-09-28 19:18:04 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Don't propagate LoadStyleLink() errors further than this, since some
|
|
|
|
// consumers (e.g. nsXMLContentSink) will completely abort on innocuous
|
|
|
|
// things like a stylesheet load being blocked by the security system.
|
2011-10-17 14:59:28 +00:00
|
|
|
doneLoading = true;
|
|
|
|
isAlternate = false;
|
2008-04-03 03:56:38 +00:00
|
|
|
rv = NS_OK;
|
|
|
|
}
|
2002-04-05 11:29:40 +00:00
|
|
|
}
|
2001-05-19 00:21:26 +00:00
|
|
|
|
2007-04-20 22:59:18 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
*aWillNotify = !doneLoading;
|
|
|
|
*aIsAlternate = isAlternate;
|
2001-05-19 00:21:26 +00:00
|
|
|
|
2007-04-20 22:59:18 +00:00
|
|
|
return NS_OK;
|
2001-05-19 00:21:26 +00:00
|
|
|
}
|