2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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 04:21:26 +04:00
|
|
|
|
2006-03-31 12:00:42 +04: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 04:21:26 +04:00
|
|
|
#include "nsStyleLinkElement.h"
|
|
|
|
|
2014-06-20 14:32:49 +04:00
|
|
|
#include "mozilla/CSSStyleSheet.h"
|
2010-06-29 02:49:35 +04:00
|
|
|
#include "mozilla/css/Loader.h"
|
2013-01-09 03:25:47 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2014-06-06 09:23:40 +04:00
|
|
|
#include "mozilla/dom/FragmentOrElement.h"
|
2013-12-02 14:26:12 +04:00
|
|
|
#include "mozilla/dom/ShadowRoot.h"
|
2014-06-05 16:46:56 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-01-09 03:25:47 +04:00
|
|
|
#include "nsIContent.h"
|
2001-05-19 04:21:26 +04:00
|
|
|
#include "nsIDocument.h"
|
2002-04-05 15:29:40 +04:00
|
|
|
#include "nsIDOMComment.h"
|
2001-05-19 04:21:26 +04:00
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIDOMStyleSheet.h"
|
|
|
|
#include "nsNetUtil.h"
|
2002-04-05 15:29:40 +04:00
|
|
|
#include "nsUnicharUtils.h"
|
2002-05-15 22:55:21 +04:00
|
|
|
#include "nsCRT.h"
|
2006-07-21 20:28:51 +04:00
|
|
|
#include "nsXPCOMCIDInternal.h"
|
|
|
|
#include "nsUnicharInputStream.h"
|
2006-09-15 23:54:45 +04:00
|
|
|
#include "nsContentUtils.h"
|
2012-08-30 21:58:24 +04:00
|
|
|
#include "nsStyleUtil.h"
|
2015-04-15 19:47:03 +03:00
|
|
|
#include "nsQueryObject.h"
|
2001-05-19 04:21:26 +04:00
|
|
|
|
2013-01-09 03:25:47 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2004-09-09 21:32:35 +04:00
|
|
|
nsStyleLinkElement::nsStyleLinkElement()
|
2011-10-17 18:59:28 +04:00
|
|
|
: mDontLoadStyle(false)
|
|
|
|
, mUpdatesEnabled(true)
|
2004-09-09 21:32:35 +04:00
|
|
|
, mLineNumber(1)
|
2001-05-19 04:21:26 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsStyleLinkElement::~nsStyleLinkElement()
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
nsStyleLinkElement::SetStyleSheet(nullptr);
|
2001-05-19 04:21:26 +04:00
|
|
|
}
|
|
|
|
|
2012-10-08 06:39:09 +04:00
|
|
|
void
|
|
|
|
nsStyleLinkElement::Unlink()
|
|
|
|
{
|
2015-05-25 15:00:00 +03:00
|
|
|
nsStyleLinkElement::SetStyleSheet(nullptr);
|
2012-10-08 06:39:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsStyleLinkElement::Traverse(nsCycleCollectionTraversalCallback &cb)
|
|
|
|
{
|
|
|
|
nsStyleLinkElement* tmp = this;
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mStyleSheet);
|
2012-10-08 06:39:09 +04:00
|
|
|
}
|
|
|
|
|
2001-05-19 04:21:26 +04:00
|
|
|
NS_IMETHODIMP
|
2014-06-20 14:32:49 +04:00
|
|
|
nsStyleLinkElement::SetStyleSheet(CSSStyleSheet* aStyleSheet)
|
2001-05-19 04:21:26 +04:00
|
|
|
{
|
2013-03-17 18:42:59 +04:00
|
|
|
if (mStyleSheet) {
|
|
|
|
mStyleSheet->SetOwningNode(nullptr);
|
2003-02-04 07:45:58 +03:00
|
|
|
}
|
|
|
|
|
2001-05-19 04:21:26 +04:00
|
|
|
mStyleSheet = aStyleSheet;
|
2013-03-17 18:42:59 +04:00
|
|
|
if (mStyleSheet) {
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryObject(this);
|
2002-12-03 08:48:14 +03:00
|
|
|
if (node) {
|
2013-03-17 18:42:59 +04:00
|
|
|
mStyleSheet->SetOwningNode(node);
|
2002-12-03 08:48:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-19 04:21:26 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-02 14:53:40 +04:00
|
|
|
NS_IMETHODIMP_(CSSStyleSheet*)
|
|
|
|
nsStyleLinkElement::GetStyleSheet()
|
2001-05-19 04:21:26 +04:00
|
|
|
{
|
2014-07-02 14:53:40 +04:00
|
|
|
return mStyleSheet;
|
2001-05-19 04:21:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsStyleLinkElement::InitStyleLinkElement(bool aDontLoadStyle)
|
2001-05-19 04:21:26 +04:00
|
|
|
{
|
|
|
|
mDontLoadStyle = aDontLoadStyle;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsStyleLinkElement::SetEnableUpdates(bool aEnableUpdates)
|
2001-05-19 04:21:26 +04:00
|
|
|
{
|
|
|
|
mUpdatesEnabled = aEnableUpdates;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-10-25 04:34:20 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStyleLinkElement::GetCharset(nsAString& aCharset)
|
|
|
|
{
|
|
|
|
// descendants have to implement this themselves
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2006-12-18 06:59:46 +03:00
|
|
|
/* virtual */ void
|
|
|
|
nsStyleLinkElement::OverrideBaseURI(nsIURI* aNewBaseURI)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("Base URI can't be overriden in this implementation "
|
|
|
|
"of nsIStyleSheetLinkingElement.");
|
|
|
|
}
|
|
|
|
|
2004-09-09 21:32:35 +04:00
|
|
|
/* virtual */ void
|
2012-08-22 19:56:38 +04:00
|
|
|
nsStyleLinkElement::SetLineNumber(uint32_t aLineNumber)
|
2004-09-09 21:32:35 +04:00
|
|
|
{
|
|
|
|
mLineNumber = aLineNumber;
|
|
|
|
}
|
|
|
|
|
2014-06-05 16:46:56 +04:00
|
|
|
/* static */ bool
|
2015-02-23 15:06:28 +03:00
|
|
|
nsStyleLinkElement::IsImportEnabled()
|
2014-06-05 16:46:56 +04:00
|
|
|
{
|
|
|
|
static bool sAdded = false;
|
2015-02-23 15:06:28 +03:00
|
|
|
static bool sImportsEnabled;
|
2014-06-05 16:46:56 +04:00
|
|
|
if (!sAdded) {
|
|
|
|
// This part runs only once because of the static flag.
|
2015-02-23 15:06:28 +03:00
|
|
|
Preferences::AddBoolVarCache(&sImportsEnabled,
|
|
|
|
"dom.htmlimports.enabled",
|
2014-06-05 16:46:56 +04:00
|
|
|
false);
|
|
|
|
sAdded = true;
|
|
|
|
}
|
2014-07-08 06:02:03 +04:00
|
|
|
|
2015-02-23 15:06:28 +03:00
|
|
|
return sImportsEnabled;
|
2014-06-05 16:46:56 +04:00
|
|
|
}
|
|
|
|
|
2014-07-08 06:02:03 +04:00
|
|
|
static uint32_t ToLinkMask(const nsAString& aLink, nsIPrincipal* aPrincipal)
|
2011-11-20 15:13:40 +04:00
|
|
|
{
|
|
|
|
if (aLink.EqualsLiteral("prefetch"))
|
2014-02-27 17:44:52 +04:00
|
|
|
return nsStyleLinkElement::ePREFETCH;
|
2011-11-20 15:13:40 +04:00
|
|
|
else if (aLink.EqualsLiteral("dns-prefetch"))
|
2014-02-27 17:44:52 +04:00
|
|
|
return nsStyleLinkElement::eDNS_PREFETCH;
|
2011-11-20 15:13:40 +04:00
|
|
|
else if (aLink.EqualsLiteral("stylesheet"))
|
2014-02-27 17:44:52 +04:00
|
|
|
return nsStyleLinkElement::eSTYLESHEET;
|
2011-11-20 15:13:40 +04:00
|
|
|
else if (aLink.EqualsLiteral("next"))
|
2014-02-27 17:44:52 +04:00
|
|
|
return nsStyleLinkElement::eNEXT;
|
2011-11-20 15:13:40 +04:00
|
|
|
else if (aLink.EqualsLiteral("alternate"))
|
2014-02-27 17:44:52 +04:00
|
|
|
return nsStyleLinkElement::eALTERNATE;
|
2015-02-23 15:06:28 +03:00
|
|
|
else if (aLink.EqualsLiteral("import") &&
|
|
|
|
nsStyleLinkElement::IsImportEnabled())
|
2014-05-21 21:08:12 +04:00
|
|
|
return nsStyleLinkElement::eHTMLIMPORT;
|
2015-02-21 01:01:36 +03:00
|
|
|
else if (aLink.EqualsLiteral("preconnect"))
|
|
|
|
return nsStyleLinkElement::ePRECONNECT;
|
2011-11-20 15:13:40 +04:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-08 06:02:03 +04:00
|
|
|
uint32_t nsStyleLinkElement::ParseLinkTypes(const nsAString& aTypes, nsIPrincipal* aPrincipal)
|
2001-05-19 04:21:26 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t linkMask = 0;
|
2003-07-14 11:37:39 +04:00
|
|
|
nsAString::const_iterator start, done;
|
|
|
|
aTypes.BeginReading(start);
|
2001-05-19 04:21:26 +04:00
|
|
|
aTypes.EndReading(done);
|
2003-07-14 11:37:39 +04:00
|
|
|
if (start == done)
|
2011-11-20 15:13:40 +04:00
|
|
|
return linkMask;
|
2001-05-19 04:21:26 +04:00
|
|
|
|
2003-07-14 11:37:39 +04:00
|
|
|
nsAString::const_iterator current(start);
|
2011-12-04 01:50:15 +04:00
|
|
|
bool inString = !nsContentUtils::IsHTMLWhitespace(*current);
|
2001-05-21 12:24:35 +04:00
|
|
|
nsAutoString subString;
|
2011-11-20 15:13:40 +04:00
|
|
|
|
2001-05-19 04:21:26 +04:00
|
|
|
while (current != done) {
|
2011-12-04 01:50:15 +04:00
|
|
|
if (nsContentUtils::IsHTMLWhitespace(*current)) {
|
2001-05-19 04:21:26 +04:00
|
|
|
if (inString) {
|
2012-03-10 09:50:34 +04:00
|
|
|
nsContentUtils::ASCIIToLower(Substring(start, current), subString);
|
2014-07-08 06:02:03 +04:00
|
|
|
linkMask |= ToLinkMask(subString, aPrincipal);
|
2011-10-17 18:59:28 +04:00
|
|
|
inString = false;
|
2001-05-19 04:21:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!inString) {
|
|
|
|
start = current;
|
2011-10-17 18:59:28 +04:00
|
|
|
inString = true;
|
2001-05-19 04:21:26 +04:00
|
|
|
}
|
|
|
|
}
|
2001-06-20 02:38:45 +04:00
|
|
|
++current;
|
2001-05-19 04:21:26 +04:00
|
|
|
}
|
|
|
|
if (inString) {
|
2012-03-10 09:50:34 +04:00
|
|
|
nsContentUtils::ASCIIToLower(Substring(start, current), subString);
|
2014-07-08 06:02:03 +04:00
|
|
|
linkMask |= ToLinkMask(subString, aPrincipal);
|
2001-05-19 04:21:26 +04:00
|
|
|
}
|
2011-11-20 15:13:40 +04:00
|
|
|
return linkMask;
|
2001-05-19 04:21:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-04-21 02:59:18 +04:00
|
|
|
nsStyleLinkElement::UpdateStyleSheet(nsICSSLoaderObserver* aObserver,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool* aWillNotify,
|
2014-08-29 04:20:27 +04:00
|
|
|
bool* aIsAlternate,
|
|
|
|
bool aForceReload)
|
2007-04-21 02:59:18 +04:00
|
|
|
{
|
2014-08-29 04:20:27 +04:00
|
|
|
if (aForceReload) {
|
|
|
|
// We remove this stylesheet from the cache to load a new version.
|
|
|
|
nsCOMPtr<nsIContent> thisContent;
|
|
|
|
CallQueryInterface(this, getter_AddRefs(thisContent));
|
2014-09-27 02:07:40 +04:00
|
|
|
nsCOMPtr<nsIDocument> doc = thisContent->IsInShadowTree() ?
|
|
|
|
thisContent->OwnerDoc() : thisContent->GetUncomposedDoc();
|
2014-08-29 04:20:27 +04:00
|
|
|
if (doc && doc->CSSLoader()->GetEnabled() &&
|
|
|
|
mStyleSheet && mStyleSheet->GetOriginalURI()) {
|
|
|
|
doc->CSSLoader()->ObsoleteSheet(mStyleSheet->GetOriginalURI());
|
|
|
|
}
|
|
|
|
}
|
2013-12-02 14:26:12 +04:00
|
|
|
return DoUpdateStyleSheet(nullptr, nullptr, aObserver, aWillNotify,
|
2014-08-29 04:20:27 +04:00
|
|
|
aIsAlternate, aForceReload);
|
2007-04-21 02:59:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsStyleLinkElement::UpdateStyleSheetInternal(nsIDocument *aOldDocument,
|
2013-12-02 14:26:12 +04:00
|
|
|
ShadowRoot *aOldShadowRoot,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aForceUpdate)
|
2007-04-21 02:59:18 +04:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool notify, alternate;
|
2013-12-02 14:26:12 +04:00
|
|
|
return DoUpdateStyleSheet(aOldDocument, aOldShadowRoot, nullptr, ¬ify,
|
|
|
|
&alternate, aForceUpdate);
|
2007-04-21 02:59:18 +04:00
|
|
|
}
|
|
|
|
|
2013-01-09 03:25:47 +04:00
|
|
|
static bool
|
|
|
|
IsScopedStyleElement(nsIContent* aContent)
|
|
|
|
{
|
|
|
|
// This is quicker than, say, QIing aContent to nsStyleLinkElement
|
|
|
|
// and then calling its virtual GetStyleSheetInfo method to find out
|
|
|
|
// if it is scoped.
|
2015-03-03 14:08:59 +03:00
|
|
|
return (aContent->IsHTMLElement(nsGkAtoms::style) ||
|
|
|
|
aContent->IsSVGElement(nsGkAtoms::style)) &&
|
2013-01-09 03:25:47 +04:00
|
|
|
aContent->HasAttr(kNameSpaceID_None, nsGkAtoms::scoped);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
HasScopedStyleSheetChild(nsIContent* aContent)
|
|
|
|
{
|
|
|
|
for (nsIContent* n = aContent->GetFirstChild(); n; n = n->GetNextSibling()) {
|
|
|
|
if (IsScopedStyleElement(n)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called when aElement has had a <style scoped> child removed.
|
|
|
|
static void
|
|
|
|
UpdateIsElementInStyleScopeFlagOnSubtree(Element* aElement)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aElement->IsElementInStyleScope(),
|
|
|
|
"only call UpdateIsElementInStyleScopeFlagOnSubtree on a "
|
|
|
|
"subtree that has IsElementInStyleScope boolean flag set");
|
|
|
|
|
|
|
|
if (HasScopedStyleSheetChild(aElement)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aElement->ClearIsElementInStyleScope();
|
|
|
|
|
|
|
|
nsIContent* n = aElement->GetNextNode(aElement);
|
|
|
|
while (n) {
|
|
|
|
if (HasScopedStyleSheetChild(n)) {
|
|
|
|
n = n->GetNextNonChildNode(aElement);
|
|
|
|
} else {
|
|
|
|
if (n->IsElement()) {
|
|
|
|
n->ClearIsElementInStyleScope();
|
|
|
|
}
|
|
|
|
n = n->GetNextNode(aElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Element*
|
|
|
|
GetScopeElement(nsIStyleSheet* aSheet)
|
|
|
|
{
|
2014-06-20 14:32:49 +04:00
|
|
|
nsRefPtr<CSSStyleSheet> cssStyleSheet = do_QueryObject(aSheet);
|
2013-01-09 03:25:47 +04:00
|
|
|
if (!cssStyleSheet) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cssStyleSheet->GetScopeElement();
|
|
|
|
}
|
|
|
|
|
2007-04-21 02:59:18 +04:00
|
|
|
nsresult
|
2013-12-02 14:26:12 +04:00
|
|
|
nsStyleLinkElement::DoUpdateStyleSheet(nsIDocument* aOldDocument,
|
|
|
|
ShadowRoot* aOldShadowRoot,
|
2007-04-21 02:59:18 +04:00
|
|
|
nsICSSLoaderObserver* aObserver,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool* aWillNotify,
|
|
|
|
bool* aIsAlternate,
|
|
|
|
bool aForceUpdate)
|
2001-05-19 04:21:26 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
*aWillNotify = false;
|
2007-04-21 02:59:18 +04:00
|
|
|
|
2013-01-09 03:25:47 +04:00
|
|
|
nsCOMPtr<nsIContent> thisContent;
|
|
|
|
CallQueryInterface(this, getter_AddRefs(thisContent));
|
|
|
|
|
2013-12-02 14:26:12 +04:00
|
|
|
// All instances of nsStyleLinkElement should implement nsIContent.
|
|
|
|
NS_ENSURE_TRUE(thisContent, NS_ERROR_FAILURE);
|
|
|
|
|
2014-12-22 06:09:32 +03:00
|
|
|
if (thisContent->IsInAnonymousSubtree() &&
|
|
|
|
thisContent->IsAnonymousContentInSVGUseSubtree()) {
|
|
|
|
// Stylesheets in <use>-cloned subtrees are disabled until we figure out
|
|
|
|
// how they should behave.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-12-02 14:26:12 +04:00
|
|
|
// Check for a ShadowRoot because link elements are inert in a
|
|
|
|
// ShadowRoot.
|
|
|
|
ShadowRoot* containingShadow = thisContent->GetContainingShadow();
|
2015-03-03 14:08:59 +03:00
|
|
|
if (thisContent->IsHTMLElement(nsGkAtoms::link) &&
|
2013-12-02 14:26:12 +04:00
|
|
|
(aOldShadowRoot || containingShadow)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-09 03:25:47 +04:00
|
|
|
Element* oldScopeElement = GetScopeElement(mStyleSheet);
|
|
|
|
|
2014-06-07 12:42:54 +04:00
|
|
|
if (mStyleSheet && (aOldDocument || aOldShadowRoot)) {
|
|
|
|
MOZ_ASSERT(!(aOldDocument && aOldShadowRoot),
|
|
|
|
"ShadowRoot content is never in document, thus "
|
|
|
|
"there should not be a old document and old "
|
|
|
|
"ShadowRoot simultaneously.");
|
|
|
|
|
|
|
|
// We're removing the link element from the document or shadow tree,
|
|
|
|
// 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!
|
2013-12-02 14:26:12 +04:00
|
|
|
if (aOldShadowRoot) {
|
|
|
|
aOldShadowRoot->RemoveSheet(mStyleSheet);
|
|
|
|
} else {
|
|
|
|
aOldDocument->BeginUpdate(UPDATE_STYLE);
|
|
|
|
aOldDocument->RemoveStyleSheet(mStyleSheet);
|
|
|
|
aOldDocument->EndUpdate(UPDATE_STYLE);
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsStyleLinkElement::SetStyleSheet(nullptr);
|
2013-01-09 03:25:47 +04:00
|
|
|
if (oldScopeElement) {
|
|
|
|
UpdateIsElementInStyleScopeFlagOnSubtree(oldScopeElement);
|
|
|
|
}
|
2003-02-04 07:45:58 +03:00
|
|
|
}
|
|
|
|
|
2013-02-05 14:10:40 +04:00
|
|
|
// When static documents are created, stylesheets are cloned manually.
|
|
|
|
if (mDontLoadStyle || !mUpdatesEnabled ||
|
|
|
|
thisContent->OwnerDoc()->IsStaticDocument()) {
|
2001-05-19 04:21:26 +04:00
|
|
|
return NS_OK;
|
2001-06-14 16:33:38 +04:00
|
|
|
}
|
2001-05-19 04:21:26 +04:00
|
|
|
|
2014-09-27 02:07:40 +04:00
|
|
|
nsCOMPtr<nsIDocument> doc = thisContent->IsInShadowTree() ?
|
|
|
|
thisContent->OwnerDoc() : thisContent->GetUncomposedDoc();
|
2010-03-03 00:00:53 +03:00
|
|
|
if (!doc || !doc->CSSLoader()->GetEnabled()) {
|
2007-11-20 21:50:12 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isInline;
|
2009-07-28 20:07:31 +04:00
|
|
|
nsCOMPtr<nsIURI> uri = GetStyleSheetURL(&isInline);
|
2001-05-19 04:21:26 +04:00
|
|
|
|
2005-12-09 22:20:42 +03:00
|
|
|
if (!aForceUpdate && mStyleSheet && !isInline && uri) {
|
2010-05-18 08:00:40 +04:00
|
|
|
nsIURI* oldURI = mStyleSheet->GetSheetURI();
|
2003-07-01 06:59:54 +04:00
|
|
|
if (oldURI) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool equal;
|
2003-07-01 06:59:54 +04:00
|
|
|
nsresult rv = oldURI->Equals(uri, &equal);
|
|
|
|
if (NS_SUCCEEDED(rv) && equal) {
|
|
|
|
return NS_OK; // We already loaded this stylesheet
|
|
|
|
}
|
2001-08-16 15:32:11 +04:00
|
|
|
}
|
2001-05-19 04:21:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mStyleSheet) {
|
2014-07-03 21:02:07 +04:00
|
|
|
if (thisContent->IsInShadowTree()) {
|
2013-12-02 14:26:12 +04:00
|
|
|
ShadowRoot* containingShadow = thisContent->GetContainingShadow();
|
|
|
|
containingShadow->RemoveSheet(mStyleSheet);
|
|
|
|
} else {
|
|
|
|
doc->BeginUpdate(UPDATE_STYLE);
|
|
|
|
doc->RemoveStyleSheet(mStyleSheet);
|
|
|
|
doc->EndUpdate(UPDATE_STYLE);
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsStyleLinkElement::SetStyleSheet(nullptr);
|
2001-05-19 04:21:26 +04:00
|
|
|
}
|
|
|
|
|
2003-07-01 06:59:54 +04:00
|
|
|
if (!uri && !isInline) {
|
2002-04-05 15:29:40 +04:00
|
|
|
return NS_OK; // If href is empty and this is not inline style then just bail
|
2001-05-19 04:21:26 +04:00
|
|
|
}
|
|
|
|
|
2002-04-05 15:29:40 +04:00
|
|
|
nsAutoString title, type, media;
|
2013-01-09 03:25:47 +04:00
|
|
|
bool isScoped;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isAlternate;
|
2002-04-05 15:29:40 +04:00
|
|
|
|
2013-01-09 03:25:47 +04:00
|
|
|
GetStyleSheetInfo(title, type, media, &isScoped, &isAlternate);
|
2002-04-05 15:29:40 +04:00
|
|
|
|
2004-06-17 04:13:25 +04:00
|
|
|
if (!type.LowerCaseEqualsLiteral("text/css")) {
|
2001-05-19 04:21:26 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-09 03:25:47 +04:00
|
|
|
Element* scopeElement = isScoped ? thisContent->GetParentElement() : nullptr;
|
|
|
|
if (scopeElement) {
|
|
|
|
NS_ASSERTION(isInline, "non-inline style must not have scope element");
|
2014-06-06 09:23:40 +04:00
|
|
|
scopeElement->SetIsElementInStyleScopeFlagOnSubtree(true);
|
2013-01-09 03:25:47 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool doneLoading = false;
|
2003-07-01 06:59:54 +04:00
|
|
|
nsresult rv = NS_OK;
|
2002-04-05 15:29:40 +04:00
|
|
|
if (isInline) {
|
2011-05-19 05:22:59 +04:00
|
|
|
nsAutoString text;
|
2015-05-22 21:16:20 +03:00
|
|
|
if (!nsContentUtils::GetNodeTextContent(thisContent, false, text, fallible)) {
|
2014-03-20 23:51:16 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2002-04-05 15:29:40 +04:00
|
|
|
|
2015-03-03 14:09:00 +03:00
|
|
|
MOZ_ASSERT(thisContent->NodeInfo()->NameAtom() != nsGkAtoms::link,
|
2013-11-09 03:44:39 +04:00
|
|
|
"<link> is not 'inline', and needs different CSP checks");
|
|
|
|
if (!nsStyleUtil::CSPAllowsInlineStyle(thisContent,
|
|
|
|
thisContent->NodePrincipal(),
|
2012-08-30 21:58:24 +04:00
|
|
|
doc->GetDocumentURI(),
|
|
|
|
mLineNumber, text, &rv))
|
|
|
|
return rv;
|
|
|
|
|
2011-05-19 05:22:59 +04:00
|
|
|
// Parse the style sheet.
|
2005-04-19 05:27:11 +04:00
|
|
|
rv = doc->CSSLoader()->
|
2011-05-19 05:22:59 +04:00
|
|
|
LoadInlineStyle(thisContent, text, mLineNumber, title, media,
|
2013-01-09 03:25:47 +04:00
|
|
|
scopeElement, aObserver, &doneLoading, &isAlternate);
|
2002-04-05 15:29:40 +04:00
|
|
|
}
|
|
|
|
else {
|
2011-09-27 02:03:16 +04: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 05:27:11 +04:00
|
|
|
rv = doc->CSSLoader()->
|
2012-08-28 21:10:07 +04:00
|
|
|
LoadStyleLink(thisContent, clonedURI, title, media, isAlternate,
|
2014-11-18 16:46:47 +03:00
|
|
|
GetCORSMode(), doc->GetReferrerPolicy(),
|
|
|
|
aObserver, &isAlternate);
|
2008-09-28 23:18:04 +04: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 18:59:28 +04:00
|
|
|
doneLoading = true;
|
|
|
|
isAlternate = false;
|
2008-04-03 07:56:38 +04:00
|
|
|
rv = NS_OK;
|
|
|
|
}
|
2002-04-05 15:29:40 +04:00
|
|
|
}
|
2001-05-19 04:21:26 +04:00
|
|
|
|
2007-04-21 02:59:18 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
*aWillNotify = !doneLoading;
|
|
|
|
*aIsAlternate = isAlternate;
|
2001-05-19 04:21:26 +04:00
|
|
|
|
2007-04-21 02:59:18 +04:00
|
|
|
return NS_OK;
|
2001-05-19 04:21:26 +04:00
|
|
|
}
|
2013-01-09 03:25:48 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
nsStyleLinkElement::UpdateStyleSheetScopedness(bool aIsNowScoped)
|
|
|
|
{
|
|
|
|
if (!mStyleSheet) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> thisContent;
|
|
|
|
CallQueryInterface(this, getter_AddRefs(thisContent));
|
|
|
|
|
2013-03-17 18:42:59 +04:00
|
|
|
Element* oldScopeElement = mStyleSheet->GetScopeElement();
|
2013-01-09 03:25:48 +04:00
|
|
|
Element* newScopeElement = aIsNowScoped ?
|
|
|
|
thisContent->GetParentElement() :
|
|
|
|
nullptr;
|
|
|
|
|
|
|
|
if (oldScopeElement == newScopeElement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDocument* document = thisContent->GetOwnerDocument();
|
|
|
|
|
2014-07-03 21:02:07 +04:00
|
|
|
if (thisContent->IsInShadowTree()) {
|
2013-12-02 14:26:12 +04:00
|
|
|
ShadowRoot* containingShadow = thisContent->GetContainingShadow();
|
|
|
|
containingShadow->RemoveSheet(mStyleSheet);
|
|
|
|
|
|
|
|
mStyleSheet->SetScopeElement(newScopeElement);
|
2013-01-09 03:25:48 +04:00
|
|
|
|
2013-12-02 14:26:12 +04:00
|
|
|
containingShadow->InsertSheet(mStyleSheet, thisContent);
|
|
|
|
} else {
|
|
|
|
document->BeginUpdate(UPDATE_STYLE);
|
|
|
|
document->RemoveStyleSheet(mStyleSheet);
|
2013-01-09 03:25:48 +04:00
|
|
|
|
2013-12-02 14:26:12 +04:00
|
|
|
mStyleSheet->SetScopeElement(newScopeElement);
|
|
|
|
|
|
|
|
document->AddStyleSheet(mStyleSheet);
|
|
|
|
document->EndUpdate(UPDATE_STYLE);
|
|
|
|
}
|
2013-01-09 03:25:48 +04:00
|
|
|
|
|
|
|
if (oldScopeElement) {
|
|
|
|
UpdateIsElementInStyleScopeFlagOnSubtree(oldScopeElement);
|
|
|
|
}
|
|
|
|
if (newScopeElement) {
|
2014-06-06 09:23:40 +04:00
|
|
|
newScopeElement->SetIsElementInStyleScopeFlagOnSubtree(true);
|
2013-01-09 03:25:48 +04:00
|
|
|
}
|
|
|
|
}
|