2001-05-19 00:21:26 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2004-04-17 21:52:36 +00: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.
|
|
|
|
*
|
2001-05-19 00:21:26 +00:00
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
2004-04-17 21:52:36 +00:00
|
|
|
*
|
|
|
|
* 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 ***** */
|
2001-05-19 00:21:26 +00:00
|
|
|
|
|
|
|
#include "nsStyleLinkElement.h"
|
|
|
|
|
2002-04-05 11:29:40 +00:00
|
|
|
#include "nsHTMLAtoms.h"
|
2001-05-19 00:21:26 +00:00
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsICSSLoader.h"
|
2002-12-03 05:48:14 +00:00
|
|
|
#include "nsICSSStyleSheet.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"
|
2002-04-05 11:29:40 +00:00
|
|
|
#include "nsIDOMText.h"
|
|
|
|
#include "nsIUnicharInputStream.h"
|
2001-05-19 00:21:26 +00:00
|
|
|
#include "nsNetUtil.h"
|
2002-04-05 11:29:40 +00:00
|
|
|
#include "nsUnicharUtils.h"
|
2001-05-19 00:21:26 +00:00
|
|
|
#include "nsVoidArray.h"
|
2002-05-15 18:55:21 +00:00
|
|
|
#include "nsCRT.h"
|
2001-05-19 00:21:26 +00:00
|
|
|
|
2004-09-09 17:32:35 +00:00
|
|
|
nsStyleLinkElement::nsStyleLinkElement()
|
|
|
|
: mDontLoadStyle(PR_FALSE)
|
|
|
|
, mUpdatesEnabled(PR_TRUE)
|
|
|
|
, mLineNumber(1)
|
2001-05-19 00:21:26 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsStyleLinkElement::~nsStyleLinkElement()
|
|
|
|
{
|
2003-02-04 04:45:58 +00:00
|
|
|
nsCOMPtr<nsICSSStyleSheet> cssSheet = do_QueryInterface(mStyleSheet);
|
|
|
|
if (cssSheet) {
|
|
|
|
cssSheet->SetOwningNode(nsnull);
|
|
|
|
}
|
2001-05-19 00:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStyleLinkElement::SetStyleSheet(nsIStyleSheet* aStyleSheet)
|
|
|
|
{
|
2003-02-04 04:45:58 +00:00
|
|
|
nsCOMPtr<nsICSSStyleSheet> cssSheet = do_QueryInterface(mStyleSheet);
|
|
|
|
if (cssSheet) {
|
|
|
|
cssSheet->SetOwningNode(nsnull);
|
|
|
|
}
|
|
|
|
|
2001-05-19 00:21:26 +00:00
|
|
|
mStyleSheet = aStyleSheet;
|
2003-02-04 04:45:58 +00:00
|
|
|
cssSheet = do_QueryInterface(mStyleSheet);
|
2002-12-03 05:48:14 +00:00
|
|
|
if (cssSheet) {
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
CallQueryInterface(this,
|
|
|
|
NS_STATIC_CAST(nsIDOMNode**, getter_AddRefs(node)));
|
|
|
|
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
|
2001-10-15 22:57:17 +00:00
|
|
|
nsStyleLinkElement::InitStyleLinkElement(nsIParser* aParser,
|
|
|
|
PRBool aDontLoadStyle)
|
2001-05-19 00:21:26 +00:00
|
|
|
{
|
|
|
|
mParser = aParser;
|
|
|
|
mDontLoadStyle = aDontLoadStyle;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStyleLinkElement::GetSheet(nsIDOMStyleSheet** aSheet)
|
|
|
|
{
|
2001-10-15 22:57:17 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aSheet);
|
|
|
|
*aSheet = nsnull;
|
|
|
|
|
|
|
|
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
|
|
|
|
nsStyleLinkElement::SetEnableUpdates(PRBool aEnableUpdates)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-09-09 17:32:35 +00:00
|
|
|
/* virtual */ void
|
|
|
|
nsStyleLinkElement::SetLineNumber(PRUint32 aLineNumber)
|
|
|
|
{
|
|
|
|
mLineNumber = aLineNumber;
|
|
|
|
}
|
|
|
|
|
2002-03-23 23:54:46 +00:00
|
|
|
void nsStyleLinkElement::ParseLinkTypes(const nsAString& aTypes,
|
2001-05-19 00:21:26 +00:00
|
|
|
nsStringArray& aResult)
|
|
|
|
{
|
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)
|
2001-05-19 00:21:26 +00:00
|
|
|
return;
|
|
|
|
|
2003-07-14 07:37:39 +00:00
|
|
|
nsAString::const_iterator current(start);
|
2001-05-19 00:21:26 +00:00
|
|
|
PRBool inString = !nsCRT::IsAsciiSpace(*current);
|
2001-05-21 08:24:35 +00:00
|
|
|
nsAutoString subString;
|
2001-05-19 00:21:26 +00:00
|
|
|
|
|
|
|
while (current != done) {
|
2001-05-21 08:24:35 +00:00
|
|
|
if (nsCRT::IsAsciiSpace(*current)) {
|
2001-05-19 00:21:26 +00:00
|
|
|
if (inString) {
|
2003-07-14 07:37:39 +00:00
|
|
|
ToLowerCase(Substring(start, current), subString);
|
2001-05-21 08:24:35 +00:00
|
|
|
aResult.AppendString(subString);
|
2001-05-19 05:10:31 +00:00
|
|
|
inString = PR_FALSE;
|
2001-05-19 00:21:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!inString) {
|
|
|
|
start = current;
|
2001-05-19 05:10:31 +00:00
|
|
|
inString = PR_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) {
|
2003-07-14 07:37:39 +00:00
|
|
|
ToLowerCase(Substring(start, current), subString);
|
2001-05-21 08:24:35 +00:00
|
|
|
aResult.AppendString(subString);
|
2001-05-19 00:21:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ALLOW_ASYNCH_STYLE_SHEETS
|
|
|
|
const PRBool kBlockByDefault=PR_FALSE;
|
|
|
|
#else
|
|
|
|
const PRBool kBlockByDefault=PR_TRUE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2003-01-06 14:55:13 +00:00
|
|
|
nsStyleLinkElement::UpdateStyleSheet(nsIDocument *aOldDocument,
|
|
|
|
nsICSSLoaderObserver* aObserver)
|
2001-05-19 00:21:26 +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);
|
2003-02-04 04:45:58 +00:00
|
|
|
mStyleSheet = nsnull;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2001-05-30 19:17:42 +00:00
|
|
|
// Keep a strong ref to the parser so it's still around when we pass it
|
|
|
|
// to the CSS loader. Release strong ref in mParser so we don't hang on
|
|
|
|
// to the parser once we start the load or if we fail to load the
|
|
|
|
// stylesheet.
|
|
|
|
nsCOMPtr<nsIParser> parser = mParser;
|
|
|
|
mParser = nsnull;
|
|
|
|
|
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
|
|
|
|
2001-06-14 12:33:38 +00:00
|
|
|
if (!doc) {
|
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
|
|
|
|
2003-07-01 02:59:54 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2002-04-05 11:29:40 +00:00
|
|
|
PRBool isInline;
|
2003-07-01 02:59:54 +00:00
|
|
|
GetStyleSheetURL(&isInline, getter_AddRefs(uri));
|
2001-05-19 00:21:26 +00:00
|
|
|
|
2003-07-01 02:59:54 +00:00
|
|
|
if (mStyleSheet && !isInline && uri) {
|
|
|
|
nsCOMPtr<nsIURI> oldURI;
|
2001-05-19 00:21:26 +00:00
|
|
|
|
2004-09-10 19:01:04 +00:00
|
|
|
mStyleSheet->GetSheetURI(getter_AddRefs(oldURI));
|
2003-07-01 02:59:54 +00:00
|
|
|
if (oldURI) {
|
|
|
|
PRBool equal;
|
|
|
|
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);
|
2001-05-19 00:21:26 +00:00
|
|
|
mStyleSheet = nsnull;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
PRBool isAlternate;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-09-12 18:41:15 +00:00
|
|
|
if (!kBlockByDefault) {
|
|
|
|
parser = nsnull;
|
2001-05-19 00:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool doneLoading;
|
2003-07-01 02:59:54 +00:00
|
|
|
nsresult rv = NS_OK;
|
2002-04-05 11:29:40 +00:00
|
|
|
if (isInline) {
|
2003-09-27 04:18:26 +00:00
|
|
|
PRUint32 count = thisContent->GetChildCount();
|
2002-04-05 11:29:40 +00:00
|
|
|
|
2004-10-27 02:19:43 +00:00
|
|
|
nsAutoString content;
|
2002-04-05 11:29:40 +00:00
|
|
|
|
2003-09-27 04:18:26 +00:00
|
|
|
PRUint32 i;
|
2002-04-05 11:29:40 +00:00
|
|
|
for (i = 0; i < count; ++i) {
|
2003-09-27 04:18:26 +00:00
|
|
|
nsIContent *node = thisContent->GetChildAt(i);
|
2002-04-05 11:29:40 +00:00
|
|
|
nsCOMPtr<nsIDOMText> tc = do_QueryInterface(node);
|
|
|
|
// Ignore nodes that are not DOMText.
|
|
|
|
if (!tc) {
|
|
|
|
nsCOMPtr<nsIDOMComment> comment = do_QueryInterface(node);
|
|
|
|
if (comment)
|
|
|
|
// Skip a comment
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString tcString;
|
|
|
|
tc->GetData(tcString);
|
2004-10-27 02:19:43 +00:00
|
|
|
content.Append(tcString);
|
2002-04-05 11:29:40 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 02:19:43 +00:00
|
|
|
// Use of the stream will be done before parsing returns. So it will go
|
|
|
|
// out of scope before |content| does.
|
2002-04-05 11:29:40 +00:00
|
|
|
nsCOMPtr<nsIUnicharInputStream> uin;
|
2004-10-27 02:19:43 +00:00
|
|
|
rv = NS_NewStringUnicharInputStream(getter_AddRefs(uin), &content,
|
|
|
|
PR_FALSE);
|
2002-04-05 11:29:40 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have a url and a unicode input stream, parse the
|
|
|
|
// style sheet.
|
2005-04-19 01:27:11 +00:00
|
|
|
rv = doc->CSSLoader()->
|
|
|
|
LoadInlineStyle(thisContent, uin, mLineNumber, title, media,
|
2005-09-12 18:41:15 +00:00
|
|
|
parser, aObserver, &doneLoading, &isAlternate);
|
2002-04-05 11:29:40 +00:00
|
|
|
}
|
|
|
|
else {
|
2005-09-12 18:41:15 +00:00
|
|
|
doneLoading = PR_FALSE; // If rv is success, we won't be done loading; if
|
|
|
|
// it's not, this value doesn't matter.
|
2005-04-19 01:27:11 +00:00
|
|
|
rv = doc->CSSLoader()->
|
2005-09-12 18:41:15 +00:00
|
|
|
LoadStyleLink(thisContent, uri, title, media, isAlternate,
|
|
|
|
parser, aObserver, &isAlternate);
|
2002-04-05 11:29:40 +00:00
|
|
|
}
|
2001-05-19 00:21:26 +00:00
|
|
|
|
2005-09-12 18:41:15 +00:00
|
|
|
if (NS_SUCCEEDED(rv) && parser && !doneLoading && !isAlternate) {
|
2001-05-19 00:21:26 +00:00
|
|
|
rv = NS_ERROR_HTMLPARSER_BLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|