2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2011-05-18 18:22:59 -07:00
|
|
|
*
|
2007-03-22 10:30:00 -07:00
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla 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/MPL/
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998-1999
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A base class which implements nsIStyleSheetLinkingElement and can
|
|
|
|
* be subclassed by various content nodes that want to load
|
|
|
|
* stylesheets (<style>, <link>, processing instructions, etc).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsStyleLinkElement.h"
|
|
|
|
|
|
|
|
#include "nsIContent.h"
|
2010-06-28 15:49:35 -07:00
|
|
|
#include "mozilla/css/Loader.h"
|
2010-05-11 13:41:47 -07:00
|
|
|
#include "nsCSSStyleSheet.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDOMComment.h"
|
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIDOMStyleSheet.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsXPCOMCIDInternal.h"
|
|
|
|
#include "nsUnicharInputStream.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
|
|
|
|
nsStyleLinkElement::nsStyleLinkElement()
|
2011-10-17 07:59:28 -07:00
|
|
|
: mDontLoadStyle(false)
|
|
|
|
, mUpdatesEnabled(true)
|
2007-03-22 10:30:00 -07:00
|
|
|
, mLineNumber(1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsStyleLinkElement::~nsStyleLinkElement()
|
|
|
|
{
|
2009-01-29 12:39:21 -08:00
|
|
|
nsStyleLinkElement::SetStyleSheet(nsnull);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStyleLinkElement::SetStyleSheet(nsIStyleSheet* aStyleSheet)
|
|
|
|
{
|
2010-05-11 13:41:47 -07:00
|
|
|
nsRefPtr<nsCSSStyleSheet> cssSheet = do_QueryObject(mStyleSheet);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (cssSheet) {
|
|
|
|
cssSheet->SetOwningNode(nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
mStyleSheet = aStyleSheet;
|
2010-05-11 13:41:47 -07:00
|
|
|
cssSheet = do_QueryObject(mStyleSheet);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (cssSheet) {
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
CallQueryInterface(this,
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsIDOMNode**>(getter_AddRefs(node)));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (node) {
|
|
|
|
cssSheet->SetOwningNode(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStyleLinkElement::GetStyleSheet(nsIStyleSheet*& aStyleSheet)
|
|
|
|
{
|
|
|
|
aStyleSheet = mStyleSheet;
|
|
|
|
NS_IF_ADDREF(aStyleSheet);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsStyleLinkElement::InitStyleLinkElement(bool aDontLoadStyle)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mDontLoadStyle = aDontLoadStyle;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStyleLinkElement::GetSheet(nsIDOMStyleSheet** aSheet)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aSheet);
|
|
|
|
*aSheet = nsnull;
|
|
|
|
|
|
|
|
if (mStyleSheet) {
|
|
|
|
CallQueryInterface(mStyleSheet, aSheet);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always return NS_OK to avoid throwing JS exceptions if mStyleSheet
|
|
|
|
// is not a nsIDOMStyleSheet
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsStyleLinkElement::SetEnableUpdates(bool aEnableUpdates)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mUpdatesEnabled = aEnableUpdates;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStyleLinkElement::GetCharset(nsAString& aCharset)
|
|
|
|
{
|
|
|
|
// descendants have to implement this themselves
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ void
|
|
|
|
nsStyleLinkElement::OverrideBaseURI(nsIURI* aNewBaseURI)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("Base URI can't be overriden in this implementation "
|
|
|
|
"of nsIStyleSheetLinkingElement.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ void
|
|
|
|
nsStyleLinkElement::SetLineNumber(PRUint32 aLineNumber)
|
|
|
|
{
|
|
|
|
mLineNumber = aLineNumber;
|
|
|
|
}
|
|
|
|
|
2011-11-20 03:13:40 -08:00
|
|
|
PRUint32 ToLinkMask(const nsAString& aLink)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 nsStyleLinkElement::ParseLinkTypes(const nsAString& aTypes)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-11-20 03:13:40 -08:00
|
|
|
PRUint32 linkMask = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAString::const_iterator start, done;
|
|
|
|
aTypes.BeginReading(start);
|
|
|
|
aTypes.EndReading(done);
|
|
|
|
if (start == done)
|
2011-11-20 03:13:40 -08:00
|
|
|
return linkMask;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsAString::const_iterator current(start);
|
2011-12-03 13:50:15 -08:00
|
|
|
bool inString = !nsContentUtils::IsHTMLWhitespace(*current);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAutoString subString;
|
2011-11-20 03:13:40 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
while (current != done) {
|
2011-12-03 13:50:15 -08:00
|
|
|
if (nsContentUtils::IsHTMLWhitespace(*current)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (inString) {
|
2012-03-09 21:50:34 -08:00
|
|
|
nsContentUtils::ASCIIToLower(Substring(start, current), subString);
|
2011-11-20 03:13:40 -08:00
|
|
|
linkMask |= ToLinkMask(subString);
|
2011-10-17 07:59:28 -07:00
|
|
|
inString = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!inString) {
|
|
|
|
start = current;
|
2011-10-17 07:59:28 -07:00
|
|
|
inString = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
++current;
|
|
|
|
}
|
|
|
|
if (inString) {
|
2012-03-09 21:50:34 -08:00
|
|
|
nsContentUtils::ASCIIToLower(Substring(start, current), subString);
|
|
|
|
linkMask |= ToLinkMask(subString);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-11-20 03:13:40 -08:00
|
|
|
return linkMask;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-04-20 15:59:18 -07:00
|
|
|
nsStyleLinkElement::UpdateStyleSheet(nsICSSLoaderObserver* aObserver,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool* aWillNotify,
|
|
|
|
bool* aIsAlternate)
|
2007-04-20 15:59:18 -07:00
|
|
|
{
|
|
|
|
return DoUpdateStyleSheet(nsnull, aObserver, aWillNotify, aIsAlternate,
|
2011-10-17 07:59:28 -07:00
|
|
|
false);
|
2007-04-20 15:59:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsStyleLinkElement::UpdateStyleSheetInternal(nsIDocument *aOldDocument,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aForceUpdate)
|
2007-04-20 15:59:18 -07:00
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool notify, alternate;
|
2007-04-20 15:59:18 -07:00
|
|
|
return DoUpdateStyleSheet(aOldDocument, nsnull, ¬ify, &alternate,
|
|
|
|
aForceUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsStyleLinkElement::DoUpdateStyleSheet(nsIDocument *aOldDocument,
|
|
|
|
nsICSSLoaderObserver* aObserver,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool* aWillNotify,
|
|
|
|
bool* aIsAlternate,
|
|
|
|
bool aForceUpdate)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
*aWillNotify = false;
|
2007-04-20 15:59:18 -07:00
|
|
|
|
2007-03-22 10:30:00 -07: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!
|
|
|
|
aOldDocument->BeginUpdate(UPDATE_STYLE);
|
|
|
|
aOldDocument->RemoveStyleSheet(mStyleSheet);
|
|
|
|
aOldDocument->EndUpdate(UPDATE_STYLE);
|
2009-01-29 12:39:21 -08:00
|
|
|
nsStyleLinkElement::SetStyleSheet(nsnull);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mDontLoadStyle || !mUpdatesEnabled) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> thisContent;
|
|
|
|
QueryInterface(NS_GET_IID(nsIContent), getter_AddRefs(thisContent));
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(thisContent, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc = thisContent->GetDocument();
|
|
|
|
|
2010-03-02 13:00:53 -08:00
|
|
|
if (!doc || !doc->CSSLoader()->GetEnabled()) {
|
2007-11-20 10:50:12 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isInline;
|
2009-07-28 09:07:31 -07:00
|
|
|
nsCOMPtr<nsIURI> uri = GetStyleSheetURL(&isInline);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!aForceUpdate && mStyleSheet && !isInline && uri) {
|
2010-05-17 21:00:40 -07:00
|
|
|
nsIURI* oldURI = mStyleSheet->GetSheetURI();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (oldURI) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool equal;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = oldURI->Equals(uri, &equal);
|
|
|
|
if (NS_SUCCEEDED(rv) && equal) {
|
|
|
|
return NS_OK; // We already loaded this stylesheet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mStyleSheet) {
|
|
|
|
doc->BeginUpdate(UPDATE_STYLE);
|
|
|
|
doc->RemoveStyleSheet(mStyleSheet);
|
|
|
|
doc->EndUpdate(UPDATE_STYLE);
|
2009-01-29 12:39:21 -08:00
|
|
|
nsStyleLinkElement::SetStyleSheet(nsnull);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!uri && !isInline) {
|
|
|
|
return NS_OK; // If href is empty and this is not inline style then just bail
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString title, type, media;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isAlternate;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
GetStyleSheetInfo(title, type, media, &isAlternate);
|
|
|
|
|
|
|
|
if (!type.LowerCaseEqualsLiteral("text/css")) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool doneLoading = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (isInline) {
|
2011-05-18 18:22:59 -07:00
|
|
|
nsAutoString text;
|
2011-10-17 07:59:28 -07:00
|
|
|
nsContentUtils::GetNodeTextContent(thisContent, false, text);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-18 18:22:59 -07:00
|
|
|
// Parse the style sheet.
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = doc->CSSLoader()->
|
2011-05-18 18:22:59 -07:00
|
|
|
LoadInlineStyle(thisContent, text, mLineNumber, title, media,
|
2007-04-20 15:59:18 -07:00
|
|
|
aObserver, &doneLoading, &isAlternate);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
2011-09-26 15:03:16 -07: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);
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = doc->CSSLoader()->
|
2011-09-26 15:03:16 -07:00
|
|
|
LoadStyleLink(thisContent, clonedURI, title, media, isAlternate, aObserver,
|
2007-04-20 15:59:18 -07:00
|
|
|
&isAlternate);
|
2008-09-28 12:18:04 -07: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 07:59:28 -07:00
|
|
|
doneLoading = true;
|
|
|
|
isAlternate = false;
|
2008-04-02 20:56:38 -07:00
|
|
|
rv = NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-04-20 15:59:18 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
*aWillNotify = !doneLoading;
|
|
|
|
*aIsAlternate = isAlternate;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-04-20 15:59:18 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|