2016-02-26 01:51:01 +00: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
|
|
|
|
* 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/. */
|
|
|
|
|
2016-03-01 22:38:13 +00:00
|
|
|
#include "mozilla/StyleSheet.h"
|
2019-12-05 04:44:32 +00:00
|
|
|
#include "mozilla/BasePrincipal.h"
|
2018-03-22 18:20:41 +00:00
|
|
|
#include "mozilla/ComputedStyleInlines.h"
|
2018-04-30 17:23:53 +00:00
|
|
|
#include "mozilla/css/ErrorReporter.h"
|
2018-02-01 04:04:04 +00:00
|
|
|
#include "mozilla/css/GroupRule.h"
|
2017-05-30 01:10:25 +00:00
|
|
|
#include "mozilla/dom/CSSImportRule.h"
|
2016-10-14 11:25:38 +00:00
|
|
|
#include "mozilla/dom/CSSRuleList.h"
|
2018-02-13 00:53:44 +00:00
|
|
|
#include "mozilla/dom/Element.h"
|
2016-11-09 06:28:24 +00:00
|
|
|
#include "mozilla/dom/MediaList.h"
|
2019-12-31 21:58:40 +00:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2016-03-01 23:10:45 +00:00
|
|
|
#include "mozilla/dom/ShadowRoot.h"
|
2018-02-13 00:53:44 +00:00
|
|
|
#include "mozilla/dom/ShadowRootBinding.h"
|
2018-07-17 19:37:48 +00:00
|
|
|
#include "mozilla/NullPrincipal.h"
|
2018-06-04 12:14:47 +00:00
|
|
|
#include "mozilla/ServoBindings.h"
|
2017-06-19 05:45:43 +00:00
|
|
|
#include "mozilla/ServoCSSRuleList.h"
|
2018-02-13 00:53:44 +00:00
|
|
|
#include "mozilla/ServoStyleSet.h"
|
2019-07-26 01:10:23 +00:00
|
|
|
#include "mozilla/StaticPrefs_layout.h"
|
2016-03-01 22:38:13 +00:00
|
|
|
#include "mozilla/StyleSheetInlines.h"
|
2020-02-22 20:41:30 +00:00
|
|
|
#include "mozilla/css/SheetLoadData.h"
|
2016-02-26 01:51:01 +00:00
|
|
|
|
2016-11-22 23:26:20 +00:00
|
|
|
#include "mozAutoDocUpdate.h"
|
2019-05-22 06:01:51 +00:00
|
|
|
#include "SheetLoadData.h"
|
2016-09-29 06:19:06 +00:00
|
|
|
|
2016-02-26 01:51:01 +00:00
|
|
|
namespace mozilla {
|
|
|
|
|
2018-06-04 10:09:11 +00:00
|
|
|
using namespace dom;
|
|
|
|
|
2018-04-30 15:50:03 +00:00
|
|
|
StyleSheet::StyleSheet(css::SheetParsingMode aParsingMode, CORSMode aCORSMode,
|
|
|
|
const dom::SRIMetadata& aIntegrity)
|
2020-04-13 20:49:32 +00:00
|
|
|
: mParentSheet(nullptr),
|
2019-12-31 21:58:40 +00:00
|
|
|
mConstructorDocument(nullptr),
|
2018-05-11 10:57:38 +00:00
|
|
|
mDocumentOrShadowRoot(nullptr),
|
2016-03-01 20:39:29 +00:00
|
|
|
mOwningNode(nullptr),
|
2017-05-30 01:10:25 +00:00
|
|
|
mOwnerRule(nullptr),
|
2016-08-02 20:12:27 +00:00
|
|
|
mParsingMode(aParsingMode),
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 08:55:10 +00:00
|
|
|
mState(static_cast<State>(0)),
|
2019-07-16 11:43:39 +00:00
|
|
|
mInner(new StyleSheetInfo(aCORSMode, aIntegrity, aParsingMode)) {
|
2018-04-30 15:50:03 +00:00
|
|
|
mInner->AddSheet(this);
|
2016-02-26 01:51:01 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 16:34:38 +00:00
|
|
|
StyleSheet::StyleSheet(const StyleSheet& aCopy, StyleSheet* aParentSheetToUse,
|
2017-05-30 01:10:25 +00:00
|
|
|
dom::CSSImportRule* aOwnerRuleToUse,
|
2020-04-17 16:34:38 +00:00
|
|
|
dom::DocumentOrShadowRoot* aDocOrShadowRootToUse,
|
|
|
|
dom::Document* aConstructorDocToUse,
|
2016-02-26 01:51:01 +00:00
|
|
|
nsINode* aOwningNodeToUse)
|
2020-04-17 16:34:38 +00:00
|
|
|
: mParentSheet(aParentSheetToUse),
|
|
|
|
mConstructorDocument(aConstructorDocToUse),
|
2017-01-24 21:09:33 +00:00
|
|
|
mTitle(aCopy.mTitle),
|
2020-04-17 16:34:38 +00:00
|
|
|
mDocumentOrShadowRoot(aDocOrShadowRootToUse),
|
2016-03-01 20:39:29 +00:00
|
|
|
mOwningNode(aOwningNodeToUse),
|
2017-05-30 01:10:25 +00:00
|
|
|
mOwnerRule(aOwnerRuleToUse),
|
2016-02-26 01:51:01 +00:00
|
|
|
mParsingMode(aCopy.mParsingMode),
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 08:55:10 +00:00
|
|
|
mState(aCopy.mState),
|
2017-02-14 17:41:33 +00:00
|
|
|
// Shallow copy, but concrete subclasses will fix up.
|
|
|
|
mInner(aCopy.mInner) {
|
2020-04-17 16:34:38 +00:00
|
|
|
MOZ_ASSERT(!aConstructorDocToUse || aCopy.IsConstructed());
|
|
|
|
MOZ_ASSERT(!aConstructorDocToUse || !aDocOrShadowRootToUse,
|
|
|
|
"Should never have both of these together.");
|
2017-02-17 23:48:35 +00:00
|
|
|
MOZ_ASSERT(mInner, "Should only copy StyleSheets with an mInner.");
|
|
|
|
mInner->AddSheet(this);
|
2019-12-03 11:04:03 +00:00
|
|
|
// CSSOM's been there, force full copy now.
|
|
|
|
if (HasForcedUniqueInner()) {
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 08:55:10 +00:00
|
|
|
MOZ_ASSERT(IsComplete(),
|
|
|
|
"Why have rules been accessed on an incomplete sheet?");
|
2018-04-30 15:50:03 +00:00
|
|
|
EnsureUniqueInner();
|
2019-12-03 11:04:03 +00:00
|
|
|
// But CSSOM hasn't been on _this_ stylesheet yet, so no need to clone
|
|
|
|
// ourselves.
|
|
|
|
mState &= ~(State::ForcedUniqueInner | State::ModifiedRules |
|
|
|
|
State::ModifiedRulesForDevtools);
|
2018-04-30 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 07:05:24 +00:00
|
|
|
if (aCopy.mMedia) {
|
|
|
|
// XXX This is wrong; we should be keeping @import rules and
|
|
|
|
// sheets in sync!
|
|
|
|
mMedia = aCopy.mMedia->Clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 21:58:40 +00:00
|
|
|
/* static */
|
|
|
|
// https://wicg.github.io/construct-stylesheets/#dom-cssstylesheet-cssstylesheet
|
|
|
|
already_AddRefed<StyleSheet> StyleSheet::Constructor(
|
|
|
|
const dom::GlobalObject& aGlobal, const dom::CSSStyleSheetInit& aOptions,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
|
|
|
|
if (!window) {
|
2020-02-06 21:16:10 +00:00
|
|
|
aRv.ThrowNotSupportedError("Not supported when there is no document");
|
2019-12-31 21:58:40 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Document* constructorDocument = window->GetExtantDoc();
|
|
|
|
if (!constructorDocument) {
|
2020-02-06 21:16:10 +00:00
|
|
|
aRv.ThrowNotSupportedError("Not supported when there is no document");
|
2019-12-31 21:58:40 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-01-07 00:19:53 +00:00
|
|
|
// 1. Construct a sheet and set its properties (see spec).
|
|
|
|
auto sheet =
|
|
|
|
MakeRefPtr<StyleSheet>(css::SheetParsingMode::eAuthorSheetFeatures,
|
|
|
|
CORSMode::CORS_NONE, dom::SRIMetadata());
|
|
|
|
|
2020-03-04 23:39:33 +00:00
|
|
|
// baseURL not yet in the spec. Implemented based on the following discussion:
|
|
|
|
// https://github.com/WICG/construct-stylesheets/issues/95#issuecomment-594217180
|
|
|
|
RefPtr<nsIURI> baseURI;
|
2020-03-04 18:34:35 +00:00
|
|
|
if (!aOptions.mBaseURL.WasPassed()) {
|
|
|
|
baseURI = constructorDocument->GetBaseURI();
|
2020-03-04 23:39:33 +00:00
|
|
|
} else {
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(baseURI), aOptions.mBaseURL.Value(),
|
|
|
|
nullptr, constructorDocument->GetBaseURI());
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.ThrowNotAllowedError(
|
|
|
|
"Constructed style sheets must have a valid base URL");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-03-04 18:34:35 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 00:19:53 +00:00
|
|
|
nsIURI* sheetURI = constructorDocument->GetDocumentURI();
|
|
|
|
nsIURI* originalURI = nullptr;
|
|
|
|
sheet->SetURIs(sheetURI, originalURI, baseURI);
|
|
|
|
|
|
|
|
sheet->SetPrincipal(constructorDocument->NodePrincipal());
|
|
|
|
sheet->SetReferrerInfo(constructorDocument->GetReferrerInfo());
|
|
|
|
sheet->mConstructorDocument = constructorDocument;
|
|
|
|
|
|
|
|
// 2. Set the sheet's media according to aOptions.
|
|
|
|
if (aOptions.mMedia.IsString()) {
|
|
|
|
sheet->SetMedia(MediaList::Create(aOptions.mMedia.GetAsString()));
|
|
|
|
} else {
|
|
|
|
sheet->SetMedia(aOptions.mMedia.GetAsMediaList()->Clone());
|
|
|
|
}
|
2019-12-31 21:58:40 +00:00
|
|
|
|
2020-01-07 00:19:53 +00:00
|
|
|
// 3. Set the sheet's disabled flag according to aOptions.
|
|
|
|
sheet->SetDisabled(aOptions.mDisabled);
|
|
|
|
sheet->SetComplete();
|
2019-12-31 21:58:40 +00:00
|
|
|
|
2020-01-07 00:19:53 +00:00
|
|
|
// 4. Return sheet.
|
|
|
|
return sheet.forget();
|
2019-12-31 21:58:40 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 07:05:24 +00:00
|
|
|
StyleSheet::~StyleSheet() {
|
2017-06-19 00:15:49 +00:00
|
|
|
MOZ_ASSERT(!mInner, "Inner should have been dropped in LastRelease");
|
|
|
|
}
|
|
|
|
|
2018-04-30 15:50:03 +00:00
|
|
|
bool StyleSheet::HasRules() const {
|
2018-04-30 17:47:12 +00:00
|
|
|
return Servo_StyleSheet_HasRules(Inner().mContents);
|
2018-04-30 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
2019-01-02 13:05:23 +00:00
|
|
|
Document* StyleSheet::GetAssociatedDocument() const {
|
2020-02-15 01:16:46 +00:00
|
|
|
auto* associated = GetAssociatedDocumentOrShadowRoot();
|
|
|
|
return associated ? associated->AsNode().OwnerDoc() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
dom::DocumentOrShadowRoot* StyleSheet::GetAssociatedDocumentOrShadowRoot()
|
|
|
|
const {
|
2020-04-17 22:55:03 +00:00
|
|
|
const StyleSheet& outer = OutermostSheet();
|
|
|
|
if (outer.mDocumentOrShadowRoot) {
|
|
|
|
return outer.mDocumentOrShadowRoot;
|
|
|
|
}
|
|
|
|
if (outer.IsConstructed()) {
|
|
|
|
return outer.mConstructorDocument;
|
2020-03-02 17:46:24 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
2018-05-11 10:57:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 21:12:11 +00:00
|
|
|
Document* StyleSheet::GetKeptAliveByDocument() const {
|
2020-04-17 22:55:03 +00:00
|
|
|
const StyleSheet& outer = OutermostSheet();
|
|
|
|
if (outer.mDocumentOrShadowRoot) {
|
|
|
|
return outer.mDocumentOrShadowRoot->AsNode().GetComposedDoc();
|
|
|
|
}
|
|
|
|
if (outer.IsConstructed()) {
|
|
|
|
for (DocumentOrShadowRoot* adopter : outer.mAdopters) {
|
|
|
|
MOZ_ASSERT(adopter->AsNode().OwnerDoc() == outer.mConstructorDocument);
|
|
|
|
if (adopter->AsNode().IsInComposedDoc()) {
|
|
|
|
return outer.mConstructorDocument.get();
|
2020-04-17 21:12:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
2018-05-11 10:57:38 +00:00
|
|
|
}
|
|
|
|
|
2017-06-19 00:15:49 +00:00
|
|
|
void StyleSheet::LastRelease() {
|
2017-02-17 23:48:35 +00:00
|
|
|
MOZ_ASSERT(mInner, "Should have an mInner at time of destruction.");
|
|
|
|
MOZ_ASSERT(mInner->mSheets.Contains(this), "Our mInner should include us.");
|
2020-02-15 01:16:46 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mAdopters.IsEmpty(),
|
|
|
|
"Should have no adopters at time of destruction.");
|
2017-06-19 00:15:49 +00:00
|
|
|
|
|
|
|
UnparentChildren();
|
|
|
|
|
2017-02-17 23:48:35 +00:00
|
|
|
mInner->RemoveSheet(this);
|
|
|
|
mInner = nullptr;
|
|
|
|
|
2017-01-06 07:05:24 +00:00
|
|
|
DropMedia();
|
2018-04-30 15:50:03 +00:00
|
|
|
DropRuleList();
|
2016-02-26 01:51:01 +00:00
|
|
|
}
|
|
|
|
|
2017-02-16 00:19:33 +00:00
|
|
|
void StyleSheet::UnlinkInner() {
|
|
|
|
// We can only have a cycle through our inner if we have a unique inner,
|
|
|
|
// because otherwise there are no JS wrappers for anything in the inner.
|
|
|
|
if (mInner->mSheets.Length() != 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-03 10:49:23 +00:00
|
|
|
for (StyleSheet* child : ChildSheets()) {
|
2020-04-13 20:49:32 +00:00
|
|
|
MOZ_ASSERT(child->mParentSheet == this, "We have a unique inner!");
|
|
|
|
child->mParentSheet = nullptr;
|
2017-02-16 00:19:33 +00:00
|
|
|
}
|
2019-12-03 10:49:23 +00:00
|
|
|
Inner().mChildren.Clear();
|
2017-02-16 00:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::TraverseInner(nsCycleCollectionTraversalCallback& cb) {
|
|
|
|
// We can only have a cycle through our inner if we have a unique inner,
|
|
|
|
// because otherwise there are no JS wrappers for anything in the inner.
|
|
|
|
if (mInner->mSheets.Length() != 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-03 10:49:23 +00:00
|
|
|
for (StyleSheet* child : ChildSheets()) {
|
2017-02-16 00:19:33 +00:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "child sheet");
|
2019-12-03 10:49:23 +00:00
|
|
|
cb.NoteXPCOMChild(child);
|
2017-02-16 00:19:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 11:25:38 +00:00
|
|
|
// QueryInterface implementation for StyleSheet
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(StyleSheet)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2017-04-03 09:55:06 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsICSSLoaderObserver)
|
2018-01-11 08:17:57 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2016-10-14 11:25:38 +00:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(StyleSheet)
|
2017-06-19 00:15:49 +00:00
|
|
|
// We want to disconnect from our inner as soon as our refcount drops to zero,
|
|
|
|
// without waiting for async deletion by the cycle collector. Otherwise we
|
|
|
|
// might end up cloning the inner if someone mutates another sheet that shares
|
|
|
|
// it with us, even though there is only one such sheet and we're about to go
|
|
|
|
// away. This situation arises easily with sheet preloading.
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_LAST_RELEASE(StyleSheet, LastRelease())
|
2016-10-14 11:25:38 +00:00
|
|
|
|
2017-01-06 07:05:24 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(StyleSheet)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(StyleSheet)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMedia)
|
2018-04-30 15:50:03 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRuleList)
|
2019-12-31 21:58:40 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mConstructorDocument)
|
2020-02-22 20:41:30 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mReplacePromise)
|
2017-02-16 00:19:33 +00:00
|
|
|
tmp->TraverseInner(cb);
|
2017-01-06 07:05:24 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(StyleSheet)
|
|
|
|
tmp->DropMedia();
|
2017-02-16 00:19:33 +00:00
|
|
|
tmp->UnlinkInner();
|
2018-04-30 15:50:03 +00:00
|
|
|
tmp->DropRuleList();
|
2019-12-31 21:58:40 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mConstructorDocument)
|
2020-02-22 20:41:30 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mReplacePromise)
|
2017-01-06 07:05:24 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(StyleSheet)
|
2016-10-14 11:25:38 +00:00
|
|
|
|
2019-11-30 11:57:10 +00:00
|
|
|
dom::CSSStyleSheetParsingMode StyleSheet::ParsingModeDOM() {
|
2018-12-24 01:34:49 +00:00
|
|
|
#define CHECK_MODE(X, Y) \
|
2016-08-02 20:17:06 +00:00
|
|
|
static_assert( \
|
|
|
|
static_cast<int>(X) == static_cast<int>(Y), \
|
|
|
|
"mozilla::dom::CSSStyleSheetParsingMode and " \
|
|
|
|
"mozilla::css::SheetParsingMode should have identical values");
|
|
|
|
|
2019-11-30 11:57:10 +00:00
|
|
|
CHECK_MODE(dom::CSSStyleSheetParsingMode::Agent, css::eAgentSheetFeatures);
|
|
|
|
CHECK_MODE(dom::CSSStyleSheetParsingMode::User, css::eUserSheetFeatures);
|
|
|
|
CHECK_MODE(dom::CSSStyleSheetParsingMode::Author, css::eAuthorSheetFeatures);
|
2016-08-02 20:17:06 +00:00
|
|
|
|
2018-12-24 01:34:49 +00:00
|
|
|
#undef CHECK_MODE
|
2016-08-02 20:17:06 +00:00
|
|
|
|
2019-11-30 11:57:10 +00:00
|
|
|
return static_cast<dom::CSSStyleSheetParsingMode>(mParsingMode);
|
2016-08-02 20:17:06 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 23:10:45 +00:00
|
|
|
void StyleSheet::SetComplete() {
|
2020-02-22 20:41:30 +00:00
|
|
|
// HasForcedUniqueInner() is okay if the sheet is constructed, because
|
|
|
|
// constructed sheets are always unique and they may be set to complete
|
|
|
|
// multiple times if their rules are replaced via Replace()
|
|
|
|
MOZ_ASSERT(IsConstructed() || !HasForcedUniqueInner(),
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 08:55:10 +00:00
|
|
|
"Can't complete a sheet that's already been forced unique.");
|
|
|
|
MOZ_ASSERT(!IsComplete(), "Already complete?");
|
|
|
|
mState |= State::Complete;
|
|
|
|
if (!Disabled()) {
|
2018-03-16 21:56:05 +00:00
|
|
|
ApplicableStateChanged(true);
|
|
|
|
}
|
2020-02-22 20:41:30 +00:00
|
|
|
MaybeResolveReplacePromise();
|
2018-03-16 21:56:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::ApplicableStateChanged(bool aApplicable) {
|
2020-02-22 20:41:30 +00:00
|
|
|
MOZ_ASSERT(aApplicable == IsApplicable());
|
2020-02-15 01:16:46 +00:00
|
|
|
auto Notify = [this](DocumentOrShadowRoot& target) {
|
|
|
|
nsINode& node = target.AsNode();
|
|
|
|
if (ShadowRoot* shadow = ShadowRoot::FromNode(node)) {
|
|
|
|
shadow->StyleSheetApplicableStateChanged(*this);
|
|
|
|
} else {
|
|
|
|
node.AsDocument()->StyleSheetApplicableStateChanged(*this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-17 22:55:03 +00:00
|
|
|
const StyleSheet& sheet = OutermostSheet();
|
|
|
|
if (sheet.mDocumentOrShadowRoot) {
|
|
|
|
Notify(*sheet.mDocumentOrShadowRoot);
|
|
|
|
}
|
2016-03-01 23:10:45 +00:00
|
|
|
|
2020-04-17 22:55:03 +00:00
|
|
|
for (DocumentOrShadowRoot* adopter : sheet.mAdopters) {
|
|
|
|
MOZ_ASSERT(adopter, "adopters should never be null");
|
|
|
|
Notify(*adopter);
|
2016-03-01 23:10:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 08:55:10 +00:00
|
|
|
void StyleSheet::SetDisabled(bool aDisabled) {
|
2019-04-29 05:34:06 +00:00
|
|
|
if (IsReadOnly()) {
|
2019-03-15 12:54:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 08:55:10 +00:00
|
|
|
if (aDisabled == Disabled()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-06 07:05:24 +00:00
|
|
|
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 08:55:10 +00:00
|
|
|
if (aDisabled) {
|
|
|
|
mState |= State::Disabled;
|
|
|
|
} else {
|
|
|
|
mState &= ~State::Disabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsComplete()) {
|
|
|
|
ApplicableStateChanged(!aDisabled);
|
2017-01-06 07:05:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-30 00:15:43 +00:00
|
|
|
void StyleSheet::SetURLExtraData() {
|
2019-07-16 11:43:39 +00:00
|
|
|
Inner().mURLData =
|
|
|
|
new URLExtraData(GetBaseURI(), GetReferrerInfo(), Principal());
|
2018-09-17 05:36:45 +00:00
|
|
|
}
|
2019-03-30 00:15:43 +00:00
|
|
|
|
2016-09-29 06:19:06 +00:00
|
|
|
StyleSheetInfo::StyleSheetInfo(CORSMode aCORSMode,
|
2018-04-30 15:50:03 +00:00
|
|
|
const SRIMetadata& aIntegrity,
|
|
|
|
css::SheetParsingMode aParsingMode)
|
2018-03-22 18:36:20 +00:00
|
|
|
: mPrincipal(NullPrincipal::CreateWithoutOriginAttributes()),
|
2016-09-29 06:19:06 +00:00
|
|
|
mCORSMode(aCORSMode),
|
2019-07-16 11:43:39 +00:00
|
|
|
mReferrerInfo(new ReferrerInfo(nullptr)),
|
2016-09-29 06:19:06 +00:00
|
|
|
mIntegrity(aIntegrity),
|
2018-04-30 15:50:03 +00:00
|
|
|
mContents(Servo_StyleSheet_Empty(aParsingMode).Consume()),
|
|
|
|
mURLData(URLExtraData::Dummy())
|
2016-09-29 06:19:06 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
,
|
|
|
|
mPrincipalSet(false)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (!mPrincipal) {
|
2017-10-25 06:30:31 +00:00
|
|
|
MOZ_CRASH("NullPrincipal::Init failed");
|
2016-09-29 06:19:06 +00:00
|
|
|
}
|
2018-04-30 15:50:03 +00:00
|
|
|
MOZ_COUNT_CTOR(StyleSheetInfo);
|
2016-09-29 06:19:06 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 15:50:03 +00:00
|
|
|
StyleSheetInfo::StyleSheetInfo(StyleSheetInfo& aCopy, StyleSheet* aPrimarySheet)
|
2017-02-17 23:48:35 +00:00
|
|
|
: mSheetURI(aCopy.mSheetURI),
|
|
|
|
mOriginalSheetURI(aCopy.mOriginalSheetURI),
|
|
|
|
mBaseURI(aCopy.mBaseURI),
|
|
|
|
mPrincipal(aCopy.mPrincipal),
|
|
|
|
mCORSMode(aCopy.mCORSMode),
|
2019-07-16 11:43:39 +00:00
|
|
|
mReferrerInfo(aCopy.mReferrerInfo),
|
2017-02-17 23:48:35 +00:00
|
|
|
mIntegrity(aCopy.mIntegrity),
|
2019-12-03 10:49:23 +00:00
|
|
|
// We don't rebuild the child because we're making a copy without
|
|
|
|
// children.
|
2017-06-28 23:51:46 +00:00
|
|
|
mSourceMapURL(aCopy.mSourceMapURL),
|
2017-08-09 19:33:24 +00:00
|
|
|
mSourceMapURLFromComment(aCopy.mSourceMapURLFromComment),
|
2017-09-14 20:59:32 +00:00
|
|
|
mSourceURL(aCopy.mSourceURL),
|
2018-04-30 15:50:03 +00:00
|
|
|
mContents(Servo_StyleSheet_Clone(aCopy.mContents.get(), aPrimarySheet)
|
|
|
|
.Consume()),
|
|
|
|
mURLData(aCopy.mURLData)
|
2017-02-17 23:48:35 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
,
|
|
|
|
mPrincipalSet(aCopy.mPrincipalSet)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
AddSheet(aPrimarySheet);
|
2018-04-30 15:50:03 +00:00
|
|
|
|
|
|
|
// Our child list is fixed up by our parent.
|
|
|
|
MOZ_COUNT_CTOR(StyleSheetInfo);
|
2017-02-17 23:48:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-30 00:23:49 +00:00
|
|
|
StyleSheetInfo::~StyleSheetInfo() { MOZ_COUNT_DTOR(StyleSheetInfo); }
|
2018-04-30 15:50:03 +00:00
|
|
|
|
|
|
|
StyleSheetInfo* StyleSheetInfo::CloneFor(StyleSheet* aPrimarySheet) {
|
|
|
|
return new StyleSheetInfo(*this, aPrimarySheet);
|
|
|
|
}
|
|
|
|
|
2018-04-30 17:09:30 +00:00
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(ServoStyleSheetMallocSizeOf)
|
|
|
|
MOZ_DEFINE_MALLOC_ENCLOSING_SIZE_OF(ServoStyleSheetMallocEnclosingSizeOf)
|
|
|
|
|
2018-04-30 15:50:03 +00:00
|
|
|
size_t StyleSheetInfo::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
|
|
|
|
size_t n = aMallocSizeOf(this);
|
2019-03-30 00:23:49 +00:00
|
|
|
|
2019-11-07 11:19:23 +00:00
|
|
|
n += Servo_StyleSheet_SizeOfIncludingThis(
|
|
|
|
ServoStyleSheetMallocSizeOf, ServoStyleSheetMallocEnclosingSizeOf,
|
|
|
|
mContents);
|
2019-03-30 00:23:49 +00:00
|
|
|
|
2018-04-30 15:50:03 +00:00
|
|
|
return n;
|
2017-05-15 23:19:17 +00:00
|
|
|
}
|
|
|
|
|
2017-02-17 23:48:35 +00:00
|
|
|
void StyleSheetInfo::AddSheet(StyleSheet* aSheet) {
|
|
|
|
mSheets.AppendElement(aSheet);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheetInfo::RemoveSheet(StyleSheet* aSheet) {
|
2019-12-03 10:49:23 +00:00
|
|
|
if (aSheet == mSheets[0] && mSheets.Length() > 1) {
|
|
|
|
StyleSheet* newParent = mSheets[1];
|
|
|
|
for (StyleSheet* child : mChildren) {
|
2020-04-13 20:49:32 +00:00
|
|
|
child->mParentSheet = newParent;
|
2019-12-03 10:49:23 +00:00
|
|
|
}
|
2017-03-07 00:27:32 +00:00
|
|
|
}
|
|
|
|
|
2017-02-17 23:48:35 +00:00
|
|
|
if (1 == mSheets.Length()) {
|
|
|
|
NS_ASSERTION(aSheet == mSheets.ElementAt(0), "bad parent");
|
|
|
|
delete this;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSheets.RemoveElement(aSheet);
|
|
|
|
}
|
|
|
|
|
2016-10-14 11:25:38 +00:00
|
|
|
void StyleSheet::GetType(nsAString& aType) { aType.AssignLiteral("text/css"); }
|
|
|
|
|
2018-01-11 08:17:57 +00:00
|
|
|
void StyleSheet::GetHref(nsAString& aHref, ErrorResult& aRv) {
|
2018-04-30 17:47:12 +00:00
|
|
|
if (nsIURI* sheetURI = Inner().mOriginalSheetURI) {
|
2016-10-14 11:25:38 +00:00
|
|
|
nsAutoCString str;
|
|
|
|
nsresult rv = sheetURI->GetSpec(str);
|
2018-01-11 08:17:57 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
2016-10-14 11:25:38 +00:00
|
|
|
CopyUTF8toUTF16(str, aHref);
|
|
|
|
} else {
|
|
|
|
SetDOMStringToNull(aHref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::GetTitle(nsAString& aTitle) {
|
2018-05-08 16:53:51 +00:00
|
|
|
// From https://drafts.csswg.org/cssom/#dom-stylesheet-title:
|
|
|
|
//
|
|
|
|
// The title attribute must return the title or null if title is the empty
|
|
|
|
// string.
|
|
|
|
//
|
|
|
|
if (!mTitle.IsEmpty()) {
|
|
|
|
aTitle.Assign(mTitle);
|
|
|
|
} else {
|
|
|
|
SetDOMStringToNull(aTitle);
|
|
|
|
}
|
2016-10-14 11:25:38 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 23:19:17 +00:00
|
|
|
void StyleSheet::WillDirty() {
|
2019-04-29 05:34:06 +00:00
|
|
|
MOZ_ASSERT(!IsReadOnly());
|
|
|
|
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 08:55:10 +00:00
|
|
|
if (IsComplete()) {
|
2017-05-15 23:19:17 +00:00
|
|
|
EnsureUniqueInner();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 11:15:46 +00:00
|
|
|
void StyleSheet::AddStyleSet(ServoStyleSet* aStyleSet) {
|
2019-04-19 04:20:31 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mStyleSets.Contains(aStyleSet),
|
|
|
|
"style set already registered");
|
2017-05-15 23:27:15 +00:00
|
|
|
mStyleSets.AppendElement(aStyleSet);
|
|
|
|
}
|
|
|
|
|
2018-03-29 11:15:46 +00:00
|
|
|
void StyleSheet::DropStyleSet(ServoStyleSet* aStyleSet) {
|
2019-04-19 04:20:31 +00:00
|
|
|
bool found = mStyleSets.RemoveElement(aStyleSet);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(found, "didn't find style set");
|
2019-04-19 13:59:53 +00:00
|
|
|
#ifndef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
|
|
|
Unused << found;
|
|
|
|
#endif
|
2017-05-15 23:27:15 +00:00
|
|
|
}
|
|
|
|
|
2019-11-30 12:24:24 +00:00
|
|
|
// NOTE(emilio): Composed doc and containing shadow root are set in child sheets
|
|
|
|
// too, so no need to do it for each ancestor.
|
2020-04-17 21:12:11 +00:00
|
|
|
#define NOTIFY(function_, args_) \
|
|
|
|
do { \
|
|
|
|
StyleSheet* current = this; \
|
|
|
|
do { \
|
|
|
|
for (ServoStyleSet * set : current->mStyleSets) { \
|
|
|
|
set->function_ args_; \
|
|
|
|
} \
|
2020-04-17 22:54:35 +00:00
|
|
|
if (auto* docOrShadow = current->mDocumentOrShadowRoot) { \
|
|
|
|
if (auto* shadow = ShadowRoot::FromNode(docOrShadow->AsNode())) { \
|
|
|
|
shadow->function_ args_; \
|
|
|
|
} else { \
|
|
|
|
docOrShadow->AsNode().AsDocument()->function_ args_; \
|
|
|
|
} \
|
|
|
|
} \
|
2020-04-17 21:12:11 +00:00
|
|
|
for (auto* adopter : mAdopters) { \
|
|
|
|
if (auto* shadow = ShadowRoot::FromNode(adopter->AsNode())) { \
|
|
|
|
shadow->function_ args_; \
|
|
|
|
} else { \
|
|
|
|
adopter->AsNode().AsDocument()->function_ args_; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
current = current->mParentSheet; \
|
|
|
|
} while (current); \
|
2019-05-22 06:01:51 +00:00
|
|
|
} while (0)
|
|
|
|
|
2017-05-15 23:19:17 +00:00
|
|
|
void StyleSheet::EnsureUniqueInner() {
|
|
|
|
MOZ_ASSERT(mInner->mSheets.Length() != 0, "unexpected number of outers");
|
2019-04-29 05:34:06 +00:00
|
|
|
|
|
|
|
if (IsReadOnly()) {
|
|
|
|
// Sheets that can't be modified don't need a unique inner.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 08:55:10 +00:00
|
|
|
mState |= State::ForcedUniqueInner;
|
2017-05-15 23:19:17 +00:00
|
|
|
|
2017-10-12 07:56:02 +00:00
|
|
|
if (HasUniqueInner()) {
|
2017-05-15 23:19:17 +00:00
|
|
|
// already unique
|
|
|
|
return;
|
|
|
|
}
|
2017-11-28 00:50:45 +00:00
|
|
|
|
2017-05-15 23:19:17 +00:00
|
|
|
StyleSheetInfo* clone = mInner->CloneFor(this);
|
|
|
|
MOZ_ASSERT(clone);
|
|
|
|
mInner->RemoveSheet(this);
|
|
|
|
mInner = clone;
|
2017-05-18 21:21:11 +00:00
|
|
|
|
2018-03-23 16:01:34 +00:00
|
|
|
// Fixup the child lists and parent links in the Servo sheet. This is done
|
|
|
|
// here instead of in StyleSheetInner::CloneFor, because it's just more
|
|
|
|
// convenient to do so instead.
|
2018-04-30 15:50:03 +00:00
|
|
|
BuildChildListAfterInnerClone();
|
2017-05-15 23:30:10 +00:00
|
|
|
|
|
|
|
// let our containing style sets know that if we call
|
|
|
|
// nsPresContext::EnsureSafeToHandOutCSSRules we will need to restyle the
|
|
|
|
// document
|
2019-11-30 12:24:24 +00:00
|
|
|
NOTIFY(SheetCloned, (*this));
|
2017-05-15 23:19:17 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 11:25:38 +00:00
|
|
|
// WebIDL CSSStyleSheet API
|
|
|
|
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 08:55:10 +00:00
|
|
|
dom::CSSRuleList* StyleSheet::GetCssRules(nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
2016-10-14 11:25:38 +00:00
|
|
|
if (!AreRulesAvailable(aSubjectPrincipal, aRv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-04-30 16:08:40 +00:00
|
|
|
return GetCssRulesInternal();
|
2016-10-14 11:25:38 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 23:51:46 +00:00
|
|
|
void StyleSheet::GetSourceMapURL(nsAString& aSourceMapURL) {
|
2017-08-09 19:33:24 +00:00
|
|
|
if (mInner->mSourceMapURL.IsEmpty()) {
|
|
|
|
aSourceMapURL = mInner->mSourceMapURLFromComment;
|
|
|
|
} else {
|
|
|
|
aSourceMapURL = mInner->mSourceMapURL;
|
|
|
|
}
|
2017-06-28 23:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::SetSourceMapURL(const nsAString& aSourceMapURL) {
|
|
|
|
mInner->mSourceMapURL = aSourceMapURL;
|
|
|
|
}
|
|
|
|
|
2017-08-09 19:33:24 +00:00
|
|
|
void StyleSheet::SetSourceMapURLFromComment(
|
|
|
|
const nsAString& aSourceMapURLFromComment) {
|
|
|
|
mInner->mSourceMapURLFromComment = aSourceMapURLFromComment;
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:59:32 +00:00
|
|
|
void StyleSheet::GetSourceURL(nsAString& aSourceURL) {
|
|
|
|
aSourceURL = mInner->mSourceURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::SetSourceURL(const nsAString& aSourceURL) {
|
|
|
|
mInner->mSourceURL = aSourceURL;
|
|
|
|
}
|
|
|
|
|
2017-05-30 01:10:25 +00:00
|
|
|
css::Rule* StyleSheet::GetDOMOwnerRule() const { return mOwnerRule; }
|
|
|
|
|
2020-03-12 18:11:09 +00:00
|
|
|
// https://drafts.csswg.org/cssom/#dom-cssstylesheet-insertrule
|
|
|
|
// https://wicg.github.io/construct-stylesheets/#dom-cssstylesheet-insertrule
|
2016-10-14 11:25:38 +00:00
|
|
|
uint32_t StyleSheet::InsertRule(const nsAString& aRule, uint32_t aIndex,
|
|
|
|
nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
2019-05-09 12:32:52 +00:00
|
|
|
if (IsReadOnly() || !AreRulesAvailable(aSubjectPrincipal, aRv)) {
|
2016-10-14 11:25:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2020-03-12 18:11:09 +00:00
|
|
|
|
|
|
|
if (ModificationDisallowed()) {
|
|
|
|
aRv.ThrowNotAllowedError(
|
|
|
|
"This method can only be called on "
|
|
|
|
"modifiable style sheets");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-30 16:08:40 +00:00
|
|
|
return InsertRuleInternal(aRule, aIndex, aRv);
|
2016-10-14 11:25:38 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 18:11:09 +00:00
|
|
|
// https://drafts.csswg.org/cssom/#dom-cssstylesheet-deleterule
|
|
|
|
// https://wicg.github.io/construct-stylesheets/#dom-cssstylesheet-deleterule
|
2016-10-14 11:25:38 +00:00
|
|
|
void StyleSheet::DeleteRule(uint32_t aIndex, nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
2019-05-09 12:32:52 +00:00
|
|
|
if (IsReadOnly() || !AreRulesAvailable(aSubjectPrincipal, aRv)) {
|
2016-10-14 11:25:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-03-12 18:11:09 +00:00
|
|
|
|
|
|
|
if (ModificationDisallowed()) {
|
|
|
|
return aRv.ThrowNotAllowedError(
|
|
|
|
"This method can only be called on "
|
|
|
|
"modifiable style sheets");
|
|
|
|
}
|
|
|
|
|
2018-04-30 16:08:40 +00:00
|
|
|
return DeleteRuleInternal(aIndex, aRv);
|
2016-10-14 11:25:38 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 12:32:52 +00:00
|
|
|
int32_t StyleSheet::AddRule(const nsAString& aSelector, const nsAString& aBlock,
|
|
|
|
const Optional<uint32_t>& aIndex,
|
|
|
|
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
|
|
|
|
if (IsReadOnly() || !AreRulesAvailable(aSubjectPrincipal, aRv)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString rule;
|
|
|
|
rule.Append(aSelector);
|
|
|
|
rule.AppendLiteral(" { ");
|
|
|
|
if (!aBlock.IsEmpty()) {
|
|
|
|
rule.Append(aBlock);
|
|
|
|
rule.Append(' ');
|
|
|
|
}
|
|
|
|
rule.Append('}');
|
|
|
|
|
|
|
|
auto index =
|
|
|
|
aIndex.WasPassed() ? aIndex.Value() : GetCssRulesInternal()->Length();
|
|
|
|
|
|
|
|
InsertRuleInternal(rule, index, aRv);
|
|
|
|
// Always return -1.
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-02-22 20:41:30 +00:00
|
|
|
void StyleSheet::MaybeResolveReplacePromise() {
|
|
|
|
MOZ_ASSERT(!!mReplacePromise == ModificationDisallowed());
|
|
|
|
if (!mReplacePromise) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetModificationDisallowed(false);
|
|
|
|
mReplacePromise->MaybeResolve(this);
|
|
|
|
mReplacePromise = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::MaybeRejectReplacePromise() {
|
|
|
|
MOZ_ASSERT(!!mReplacePromise == ModificationDisallowed());
|
|
|
|
if (!mReplacePromise) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetModificationDisallowed(false);
|
|
|
|
mReplacePromise->MaybeRejectWithNetworkError(
|
|
|
|
"@import style sheet load failed");
|
|
|
|
mReplacePromise = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-12-31 21:58:40 +00:00
|
|
|
// https://wicg.github.io/construct-stylesheets/#dom-cssstylesheet-replace
|
2020-02-19 01:00:15 +00:00
|
|
|
already_AddRefed<dom::Promise> StyleSheet::Replace(const nsACString& aText,
|
2019-12-31 21:58:40 +00:00
|
|
|
ErrorResult& aRv) {
|
2020-04-24 17:42:22 +00:00
|
|
|
nsIGlobalObject* globalObject = nullptr;
|
|
|
|
if (Document* doc = GetAssociatedDocument()) {
|
|
|
|
globalObject = doc->GetScopeObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(nordzilla, bug1632686) We need to find a way to get the relevant
|
|
|
|
// global object if a non-constructed sheet has been disassocited from
|
|
|
|
// its document.
|
|
|
|
|
2020-02-22 20:41:30 +00:00
|
|
|
RefPtr<dom::Promise> promise = dom::Promise::Create(globalObject, aRv);
|
|
|
|
if (!promise) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-12-31 21:58:40 +00:00
|
|
|
|
|
|
|
// Step 1 and 4 are variable declarations
|
|
|
|
|
2020-02-22 20:41:30 +00:00
|
|
|
// 2.1 Check if sheet is constructed, else reject promise.
|
2020-04-17 16:34:38 +00:00
|
|
|
if (!IsConstructed()) {
|
2020-02-22 20:41:30 +00:00
|
|
|
promise->MaybeRejectWithNotAllowedError(
|
|
|
|
"This method can only be called on "
|
|
|
|
"constructed style sheets");
|
|
|
|
return promise.forget();
|
2019-12-31 21:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2.2 Check if sheet is modifiable, else throw.
|
2020-02-22 20:41:30 +00:00
|
|
|
if (ModificationDisallowed()) {
|
|
|
|
promise->MaybeRejectWithNotAllowedError(
|
|
|
|
"This method can only be called on "
|
|
|
|
"modifiable style sheets");
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2019-12-31 21:58:40 +00:00
|
|
|
// 3. Disallow modifications until finished.
|
2020-02-22 20:41:30 +00:00
|
|
|
SetModificationDisallowed(true);
|
2019-12-31 21:58:40 +00:00
|
|
|
|
2020-02-22 20:41:30 +00:00
|
|
|
auto* loader = mConstructorDocument->CSSLoader();
|
|
|
|
auto loadData = MakeRefPtr<css::SheetLoadData>(
|
|
|
|
loader, GetBaseURI(), this, /* aSyncLoad */ false,
|
|
|
|
/* aUseSystemPrincipal */ false, /* aPreloadEncoding */ nullptr,
|
|
|
|
/* aObserver */ nullptr, /* aLoaderPrincipal */ nullptr,
|
|
|
|
GetReferrerInfo(),
|
|
|
|
/* aRequestingNode */ nullptr);
|
2019-12-31 21:58:40 +00:00
|
|
|
|
|
|
|
// In parallel
|
|
|
|
// 5.1 Parse aText into rules.
|
|
|
|
// 5.2 Load import rules, throw NetworkError if failed.
|
|
|
|
// 5.3 Set sheet's rules to new rules.
|
2020-02-22 20:41:30 +00:00
|
|
|
nsCOMPtr<nsISerialEventTarget> target =
|
|
|
|
mConstructorDocument->EventTargetFor(TaskCategory::Other);
|
|
|
|
loadData->mIsBeingParsed = true;
|
|
|
|
MOZ_ASSERT(!mReplacePromise);
|
|
|
|
mReplacePromise = promise;
|
|
|
|
ParseSheet(*loader, aText, *loadData)
|
|
|
|
->Then(
|
|
|
|
target, __func__,
|
|
|
|
[loadData] { loadData->SheetFinishedParsingAsync(); },
|
|
|
|
[] { MOZ_CRASH("This MozPromise should never be rejected."); });
|
2019-12-31 21:58:40 +00:00
|
|
|
|
|
|
|
// 6. Return the promise
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://wicg.github.io/construct-stylesheets/#dom-cssstylesheet-replacesync
|
2020-01-14 22:34:40 +00:00
|
|
|
void StyleSheet::ReplaceSync(const nsACString& aText, ErrorResult& aRv) {
|
2019-12-31 21:58:40 +00:00
|
|
|
// Step 1 is a variable declaration
|
|
|
|
|
|
|
|
// 2.1 Check if sheet is constructed, else throw.
|
2020-04-17 16:34:38 +00:00
|
|
|
if (!IsConstructed()) {
|
2020-02-03 20:19:11 +00:00
|
|
|
return aRv.ThrowNotAllowedError(
|
2020-02-06 21:16:10 +00:00
|
|
|
"Can only be called on constructed style sheets");
|
2019-12-31 21:58:40 +00:00
|
|
|
}
|
2020-01-14 22:34:40 +00:00
|
|
|
|
2019-12-31 21:58:40 +00:00
|
|
|
// 2.2 Check if sheet is modifiable, else throw.
|
2020-01-14 22:34:40 +00:00
|
|
|
if (ModificationDisallowed()) {
|
2020-02-03 20:19:11 +00:00
|
|
|
return aRv.ThrowNotAllowedError(
|
2020-02-06 21:16:10 +00:00
|
|
|
"Can only be called on modifiable style sheets");
|
2020-01-14 22:34:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-31 21:58:40 +00:00
|
|
|
// 3. Parse aText into rules.
|
2020-03-12 18:11:09 +00:00
|
|
|
// 4. If rules contain @imports, skip them and continue parsing.
|
|
|
|
auto* loader = mConstructorDocument->CSSLoader();
|
2020-01-14 22:34:40 +00:00
|
|
|
SetURLExtraData();
|
|
|
|
RefPtr<const RawServoStyleSheetContents> rawContent =
|
|
|
|
Servo_StyleSheet_FromUTF8Bytes(
|
2020-03-12 18:11:09 +00:00
|
|
|
loader, this,
|
2020-01-14 22:34:40 +00:00
|
|
|
/* load_data = */ nullptr, &aText, mParsingMode, Inner().mURLData,
|
|
|
|
/* line_number_offset = */ 0,
|
|
|
|
mConstructorDocument->GetCompatibilityMode(),
|
|
|
|
/* reusable_sheets = */ nullptr,
|
|
|
|
mConstructorDocument->GetStyleUseCounters(),
|
2020-03-12 18:11:09 +00:00
|
|
|
StyleAllowImportRules::No, StyleSanitizationKind::None,
|
2020-01-14 22:34:40 +00:00
|
|
|
/* sanitized_output = */ nullptr)
|
|
|
|
.Consume();
|
|
|
|
|
|
|
|
// 5. Set sheet's rules to the new rules.
|
|
|
|
DropRuleList();
|
2020-02-13 14:38:48 +00:00
|
|
|
Inner().mContents = std::move(rawContent);
|
2020-01-14 22:34:40 +00:00
|
|
|
FinishParse();
|
2020-02-15 01:16:46 +00:00
|
|
|
RuleChanged(nullptr);
|
2019-12-31 21:58:40 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 07:04:15 +00:00
|
|
|
nsresult StyleSheet::DeleteRuleFromGroup(css::GroupRule* aGroup,
|
|
|
|
uint32_t aIndex) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aGroup);
|
|
|
|
NS_ASSERTION(IsComplete(), "No deleting from an incomplete sheet!");
|
|
|
|
RefPtr<css::Rule> rule = aGroup->GetStyleRuleAt(aIndex);
|
|
|
|
NS_ENSURE_TRUE(rule, NS_ERROR_ILLEGAL_VALUE);
|
|
|
|
|
|
|
|
// check that the rule actually belongs to this sheet!
|
|
|
|
if (this != rule->GetStyleSheet()) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2019-04-29 05:34:06 +00:00
|
|
|
if (IsReadOnly()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-08 07:04:15 +00:00
|
|
|
WillDirty();
|
|
|
|
|
|
|
|
nsresult result = aGroup->DeleteStyleRuleAt(aIndex);
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
|
|
|
|
2018-06-29 02:56:09 +00:00
|
|
|
rule->DropReferences();
|
|
|
|
|
2017-11-28 00:50:45 +00:00
|
|
|
RuleRemoved(*rule);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::RuleAdded(css::Rule& aRule) {
|
2019-12-03 11:04:03 +00:00
|
|
|
SetModifiedRules();
|
2018-04-30 16:19:44 +00:00
|
|
|
NOTIFY(RuleAdded, (*this, aRule));
|
2017-11-28 00:50:45 +00:00
|
|
|
}
|
2017-03-08 07:04:15 +00:00
|
|
|
|
2017-11-28 00:50:45 +00:00
|
|
|
void StyleSheet::RuleRemoved(css::Rule& aRule) {
|
2019-12-03 11:04:03 +00:00
|
|
|
SetModifiedRules();
|
2018-04-30 16:19:44 +00:00
|
|
|
NOTIFY(RuleRemoved, (*this, aRule));
|
2017-11-28 00:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::RuleChanged(css::Rule* aRule) {
|
2019-12-03 11:04:03 +00:00
|
|
|
SetModifiedRules();
|
2018-04-30 16:19:44 +00:00
|
|
|
NOTIFY(RuleChanged, (*this, aRule));
|
2019-11-30 12:24:24 +00:00
|
|
|
}
|
2017-11-28 00:50:45 +00:00
|
|
|
|
2019-11-30 12:24:24 +00:00
|
|
|
// nsICSSLoaderObserver implementation
|
|
|
|
NS_IMETHODIMP
|
|
|
|
StyleSheet::StyleSheetLoaded(StyleSheet* aSheet, bool aWasDeferred,
|
|
|
|
nsresult aStatus) {
|
|
|
|
if (!aSheet->GetParentSheet()) {
|
|
|
|
return NS_OK; // ignore if sheet has been detached already
|
2017-11-28 00:50:45 +00:00
|
|
|
}
|
2019-11-30 12:24:24 +00:00
|
|
|
MOZ_ASSERT(this == aSheet->GetParentSheet(),
|
|
|
|
"We are being notified of a sheet load for a sheet that is not "
|
|
|
|
"our child!");
|
|
|
|
if (NS_FAILED(aStatus)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aSheet->GetOwnerRule());
|
|
|
|
NOTIFY(ImportRuleLoaded, (*aSheet->GetOwnerRule(), *aSheet));
|
|
|
|
return NS_OK;
|
2017-03-08 07:04:15 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 05:31:23 +00:00
|
|
|
#undef NOTIFY
|
|
|
|
|
2017-03-08 07:04:15 +00:00
|
|
|
nsresult StyleSheet::InsertRuleIntoGroup(const nsAString& aRule,
|
|
|
|
css::GroupRule* aGroup,
|
|
|
|
uint32_t aIndex) {
|
|
|
|
NS_ASSERTION(IsComplete(), "No inserting into an incomplete sheet!");
|
|
|
|
// check that the group actually belongs to this sheet!
|
|
|
|
if (this != aGroup->GetStyleSheet()) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2019-04-29 05:34:06 +00:00
|
|
|
if (IsReadOnly()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-03-12 18:11:09 +00:00
|
|
|
if (ModificationDisallowed()) {
|
|
|
|
return NS_ERROR_DOM_NOT_ALLOWED_ERR;
|
|
|
|
}
|
|
|
|
|
2017-03-08 07:04:15 +00:00
|
|
|
WillDirty();
|
|
|
|
|
2018-04-30 16:19:44 +00:00
|
|
|
nsresult result = InsertRuleIntoGroupInternal(aRule, aGroup, aIndex);
|
2017-03-08 07:04:15 +00:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2017-11-28 00:50:45 +00:00
|
|
|
RuleAdded(*aGroup->GetStyleRuleAt(aIndex));
|
2017-03-08 07:04:15 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-10 21:52:00 +00:00
|
|
|
uint64_t StyleSheet::FindOwningWindowInnerID() const {
|
|
|
|
uint64_t windowID = 0;
|
2019-01-02 13:05:23 +00:00
|
|
|
if (Document* doc = GetAssociatedDocument()) {
|
2018-05-11 10:57:38 +00:00
|
|
|
windowID = doc->InnerWindowID();
|
2017-07-10 21:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (windowID == 0 && mOwningNode) {
|
|
|
|
windowID = mOwningNode->OwnerDoc()->InnerWindowID();
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<css::Rule> ownerRule;
|
|
|
|
if (windowID == 0 && (ownerRule = GetDOMOwnerRule())) {
|
|
|
|
RefPtr<StyleSheet> sheet = ownerRule->GetStyleSheet();
|
|
|
|
if (sheet) {
|
|
|
|
windowID = sheet->FindOwningWindowInnerID();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 20:49:32 +00:00
|
|
|
if (windowID == 0 && mParentSheet) {
|
|
|
|
windowID = mParentSheet->FindOwningWindowInnerID();
|
2017-07-10 21:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return windowID;
|
|
|
|
}
|
|
|
|
|
2019-12-03 10:43:57 +00:00
|
|
|
void StyleSheet::RemoveFromParent() {
|
2020-04-13 20:49:32 +00:00
|
|
|
if (!mParentSheet) {
|
2019-12-03 10:43:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-13 20:49:32 +00:00
|
|
|
MOZ_ASSERT(mParentSheet->ChildSheets().Contains(this));
|
|
|
|
mParentSheet->Inner().mChildren.RemoveElement(this);
|
|
|
|
mParentSheet = nullptr;
|
2019-12-03 10:43:57 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 00:37:59 +00:00
|
|
|
void StyleSheet::UnparentChildren() {
|
2020-04-17 22:54:55 +00:00
|
|
|
MOZ_ASSERT(!mDocumentOrShadowRoot,
|
2020-04-17 22:54:35 +00:00
|
|
|
"How did we get to the destructor, exactly, if we're owned "
|
|
|
|
"by a document?");
|
2020-04-17 16:34:38 +00:00
|
|
|
// XXXbz this is a little bogus; see the comment where we
|
|
|
|
// declare mChildren in StyleSheetInfo.
|
2019-12-03 10:49:23 +00:00
|
|
|
for (StyleSheet* child : ChildSheets()) {
|
2020-04-13 20:49:32 +00:00
|
|
|
if (child->mParentSheet == this) {
|
|
|
|
child->mParentSheet = nullptr;
|
2017-01-24 00:37:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 11:25:38 +00:00
|
|
|
void StyleSheet::SubjectSubsumesInnerPrincipal(nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
2018-04-30 17:47:12 +00:00
|
|
|
StyleSheetInfo& info = Inner();
|
2016-10-14 11:25:38 +00:00
|
|
|
|
|
|
|
if (aSubjectPrincipal.Subsumes(info.mPrincipal)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-29 07:03:01 +00:00
|
|
|
// Allow access only if CORS mode is not NONE and the security flag
|
|
|
|
// is not turned off.
|
|
|
|
if (GetCORSMode() == CORS_NONE && !nsContentUtils::BypassCSSOMOriginCheck()) {
|
2020-02-03 20:35:54 +00:00
|
|
|
aRv.ThrowSecurityError("Not allowed to access cross-origin stylesheet");
|
2016-10-14 11:25:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now make sure we set the principal of our inner to the subjectPrincipal.
|
|
|
|
// We do this because we're in a situation where the caller would not normally
|
|
|
|
// be able to access the sheet, but the sheet has opted in to being read.
|
|
|
|
// Unfortunately, that means it's also opted in to being _edited_, and if the
|
|
|
|
// caller now makes edits to the sheet we want the resulting resource loads,
|
|
|
|
// if any, to look as if they are coming from the caller's principal, not the
|
|
|
|
// original sheet principal.
|
|
|
|
//
|
|
|
|
// That means we need a unique inner, of course. But we don't want to do that
|
|
|
|
// if we're not complete yet. Luckily, all the callers of this method throw
|
|
|
|
// anyway if not complete, so we can just do that here too.
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 08:55:10 +00:00
|
|
|
if (!IsComplete()) {
|
2020-02-03 20:35:54 +00:00
|
|
|
aRv.ThrowInvalidAccessError(
|
|
|
|
"Not allowed to access still-loading stylesheet");
|
2016-10-14 11:25:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WillDirty();
|
|
|
|
|
|
|
|
info.mPrincipal = &aSubjectPrincipal;
|
|
|
|
}
|
|
|
|
|
2016-10-14 11:25:38 +00:00
|
|
|
bool StyleSheet::AreRulesAvailable(nsIPrincipal& aSubjectPrincipal,
|
2016-10-14 11:25:38 +00:00
|
|
|
ErrorResult& aRv) {
|
|
|
|
// Rules are not available on incomplete sheets.
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 08:55:10 +00:00
|
|
|
if (!IsComplete()) {
|
2020-02-03 20:35:54 +00:00
|
|
|
aRv.ThrowInvalidAccessError(
|
|
|
|
"Can't access rules of still-loading stylsheet");
|
2016-10-14 11:25:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//-- Security check: Only scripts whose principal subsumes that of the
|
|
|
|
// style sheet can access rule collections.
|
|
|
|
SubjectSubsumesInnerPrincipal(aSubjectPrincipal, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-11 10:57:38 +00:00
|
|
|
void StyleSheet::SetAssociatedDocumentOrShadowRoot(
|
2020-04-17 22:54:55 +00:00
|
|
|
DocumentOrShadowRoot* aDocOrShadowRoot) {
|
2020-04-17 16:34:38 +00:00
|
|
|
MOZ_ASSERT(!IsConstructed());
|
2020-04-17 22:54:35 +00:00
|
|
|
MOZ_ASSERT(!mParentSheet || !aDocOrShadowRoot,
|
|
|
|
"Shouldn't be set on child sheets");
|
2017-01-24 21:12:40 +00:00
|
|
|
// not ref counted
|
2018-05-11 10:57:38 +00:00
|
|
|
mDocumentOrShadowRoot = aDocOrShadowRoot;
|
2017-01-24 21:12:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 10:49:23 +00:00
|
|
|
void StyleSheet::AppendStyleSheet(StyleSheet& aSheet) {
|
2017-01-24 00:23:40 +00:00
|
|
|
WillDirty();
|
2019-12-03 10:49:23 +00:00
|
|
|
AppendStyleSheetSilently(aSheet);
|
2017-06-29 17:09:56 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 10:49:23 +00:00
|
|
|
void StyleSheet::AppendStyleSheetSilently(StyleSheet& aSheet) {
|
2019-04-29 05:34:06 +00:00
|
|
|
MOZ_ASSERT(!IsReadOnly());
|
2017-06-29 17:09:56 +00:00
|
|
|
|
2019-12-03 10:49:23 +00:00
|
|
|
Inner().mChildren.AppendElement(&aSheet);
|
2017-01-24 00:23:40 +00:00
|
|
|
|
|
|
|
// This is not reference counted. Our parent tells us when
|
|
|
|
// it's going away.
|
2020-04-13 20:49:32 +00:00
|
|
|
aSheet.mParentSheet = this;
|
2017-01-24 00:23:40 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 00:42:31 +00:00
|
|
|
size_t StyleSheet::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
|
|
|
|
size_t n = 0;
|
2019-12-03 10:49:23 +00:00
|
|
|
n += aMallocSizeOf(this);
|
2017-01-24 00:42:31 +00:00
|
|
|
|
2019-12-03 10:49:23 +00:00
|
|
|
// We want to measure the inner with only one of the children, and it makes
|
|
|
|
// sense for it to be the latest as it is the most likely to be reachable.
|
|
|
|
if (Inner().mSheets.LastElement() == this) {
|
|
|
|
n += Inner().SizeOfIncludingThis(aMallocSizeOf);
|
2017-01-24 00:42:31 +00:00
|
|
|
}
|
2019-12-03 10:49:23 +00:00
|
|
|
|
|
|
|
// Measurement of the following members may be added later if DMD finds it
|
|
|
|
// is worthwhile:
|
|
|
|
// - mTitle
|
|
|
|
// - mMedia
|
|
|
|
// - mStyleSets
|
|
|
|
// - mRuleList
|
|
|
|
|
2017-01-24 00:42:31 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2017-01-24 00:40:28 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
void StyleSheet::List(FILE* out, int32_t aIndent) const {
|
|
|
|
int32_t index;
|
|
|
|
|
|
|
|
// Indent
|
|
|
|
nsAutoCString str;
|
|
|
|
for (index = aIndent; --index >= 0;) {
|
|
|
|
str.AppendLiteral(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
str.AppendLiteral("CSS Style Sheet: ");
|
|
|
|
nsAutoCString urlSpec;
|
|
|
|
nsresult rv = GetSheetURI()->GetSpec(urlSpec);
|
|
|
|
if (NS_SUCCEEDED(rv) && !urlSpec.IsEmpty()) {
|
|
|
|
str.Append(urlSpec);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mMedia) {
|
|
|
|
str.AppendLiteral(" media: ");
|
|
|
|
nsAutoString buffer;
|
|
|
|
mMedia->GetText(buffer);
|
|
|
|
AppendUTF16toUTF8(buffer, str);
|
|
|
|
}
|
|
|
|
str.Append('\n');
|
|
|
|
fprintf_stderr(out, "%s", str.get());
|
|
|
|
|
2019-12-03 10:49:23 +00:00
|
|
|
for (const StyleSheet* child : ChildSheets()) {
|
2017-01-24 00:40:28 +00:00
|
|
|
child->List(out, aIndent + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-01-07 00:19:53 +00:00
|
|
|
void StyleSheet::SetMedia(already_AddRefed<dom::MediaList> aMedia) {
|
2017-01-06 07:05:24 +00:00
|
|
|
mMedia = aMedia;
|
2020-01-07 00:19:53 +00:00
|
|
|
if (mMedia) {
|
|
|
|
mMedia->SetStyleSheet(this);
|
|
|
|
}
|
2017-01-06 07:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::DropMedia() {
|
|
|
|
if (mMedia) {
|
|
|
|
mMedia->SetStyleSheet(nullptr);
|
|
|
|
mMedia = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dom::MediaList* StyleSheet::Media() {
|
|
|
|
if (!mMedia) {
|
2018-03-28 15:31:46 +00:00
|
|
|
mMedia = dom::MediaList::Create(nsString());
|
2017-01-06 07:05:24 +00:00
|
|
|
mMedia->SetStyleSheet(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mMedia;
|
|
|
|
}
|
|
|
|
|
2016-10-14 11:25:38 +00:00
|
|
|
// nsWrapperCache
|
|
|
|
|
|
|
|
JSObject* StyleSheet::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-25 21:20:54 +00:00
|
|
|
return dom::CSSStyleSheet_Binding::Wrap(aCx, this, aGivenProto);
|
2016-10-14 11:25:38 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 17:09:30 +00:00
|
|
|
void StyleSheet::BuildChildListAfterInnerClone() {
|
2018-04-30 17:47:12 +00:00
|
|
|
MOZ_ASSERT(Inner().mSheets.Length() == 1, "Should've just cloned");
|
|
|
|
MOZ_ASSERT(Inner().mSheets[0] == this);
|
2019-12-03 10:49:23 +00:00
|
|
|
MOZ_ASSERT(Inner().mChildren.IsEmpty());
|
2018-04-30 17:09:30 +00:00
|
|
|
|
2018-04-30 17:47:12 +00:00
|
|
|
auto* contents = Inner().mContents.get();
|
2018-04-30 17:09:30 +00:00
|
|
|
RefPtr<ServoCssRules> rules = Servo_StyleSheet_GetRules(contents).Consume();
|
|
|
|
|
|
|
|
uint32_t index = 0;
|
|
|
|
while (true) {
|
|
|
|
uint32_t line, column; // Actually unused.
|
|
|
|
RefPtr<RawServoImportRule> import =
|
|
|
|
Servo_CssRules_GetImportRuleAt(rules, index, &line, &column).Consume();
|
|
|
|
if (!import) {
|
|
|
|
// Note that only @charset rules come before @import rules, and @charset
|
|
|
|
// rules are parsed but skipped, so we can stop iterating as soon as we
|
|
|
|
// find something that isn't an @import rule.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto* sheet = const_cast<StyleSheet*>(Servo_ImportRule_GetSheet(import));
|
|
|
|
MOZ_ASSERT(sheet);
|
2019-12-03 10:49:23 +00:00
|
|
|
AppendStyleSheetSilently(*sheet);
|
2018-04-30 17:09:30 +00:00
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<StyleSheet> StyleSheet::CreateEmptyChildSheet(
|
|
|
|
already_AddRefed<dom::MediaList> aMediaList) const {
|
2019-07-16 11:43:39 +00:00
|
|
|
RefPtr<StyleSheet> child =
|
|
|
|
new StyleSheet(ParsingMode(), CORSMode::CORS_NONE, SRIMetadata());
|
2018-04-30 17:09:30 +00:00
|
|
|
|
|
|
|
child->mMedia = aMediaList;
|
|
|
|
return child.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We disable parallel stylesheet parsing if any of the following three
|
|
|
|
// conditions hold:
|
|
|
|
//
|
|
|
|
// (1) The pref is off.
|
2018-11-27 22:08:11 +00:00
|
|
|
// (2) The browser is recording CSS errors (which parallel parsing can't
|
|
|
|
// handle).
|
|
|
|
// (3) The stylesheet is a chrome stylesheet, since those can use
|
|
|
|
// -moz-bool-pref, which needs to access the pref service, which is not
|
|
|
|
// threadsafe.
|
2019-08-16 10:56:09 +00:00
|
|
|
static bool AllowParallelParse(css::Loader& aLoader, nsIURI* aSheetURI) {
|
2018-04-30 17:09:30 +00:00
|
|
|
// If the browser is recording CSS errors, we need to use the sequential path
|
|
|
|
// because the parallel path doesn't support that.
|
2019-08-16 10:56:09 +00:00
|
|
|
Document* doc = aLoader.GetDocument();
|
2018-04-30 17:09:30 +00:00
|
|
|
if (doc && css::ErrorReporter::ShouldReportErrors(*doc)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a chrome stylesheet, it might use -moz-bool-pref, which needs to
|
|
|
|
// access the pref service, which is not thread-safe. We could probably expose
|
|
|
|
// the relevant booleans as thread-safe var caches if we needed to, but
|
|
|
|
// parsing chrome stylesheets in parallel is unlikely to be a win anyway.
|
|
|
|
//
|
|
|
|
// Note that UA stylesheets can also use -moz-bool-pref, but those are always
|
|
|
|
// parsed sync.
|
|
|
|
if (dom::IsChromeURI(aSheetURI)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<StyleSheetParsePromise> StyleSheet::ParseSheet(
|
2019-08-16 10:56:09 +00:00
|
|
|
css::Loader& aLoader, const nsACString& aBytes,
|
|
|
|
css::SheetLoadData& aLoadData) {
|
2018-04-30 17:09:30 +00:00
|
|
|
MOZ_ASSERT(mParsePromise.IsEmpty());
|
|
|
|
RefPtr<StyleSheetParsePromise> p = mParsePromise.Ensure(__func__);
|
2019-03-30 00:15:43 +00:00
|
|
|
SetURLExtraData();
|
2018-04-30 17:09:30 +00:00
|
|
|
|
2018-08-20 17:03:11 +00:00
|
|
|
const StyleUseCounters* useCounters =
|
2019-08-16 10:56:09 +00:00
|
|
|
aLoader.GetDocument() ? aLoader.GetDocument()->GetStyleUseCounters()
|
|
|
|
: nullptr;
|
2020-03-12 18:11:09 +00:00
|
|
|
// @import rules are disallowed due to this decision:
|
|
|
|
// https://github.com/WICG/construct-stylesheets/issues/119#issuecomment-588352418
|
|
|
|
// We may allow @import rules again in the future.
|
|
|
|
auto allowImportRules = SelfOrAncestorIsConstructed()
|
|
|
|
? StyleAllowImportRules::No
|
|
|
|
: StyleAllowImportRules::Yes;
|
2018-04-30 17:09:30 +00:00
|
|
|
if (!AllowParallelParse(aLoader, GetSheetURI())) {
|
|
|
|
RefPtr<RawServoStyleSheetContents> contents =
|
|
|
|
Servo_StyleSheet_FromUTF8Bytes(
|
2019-08-16 10:56:09 +00:00
|
|
|
&aLoader, this, &aLoadData, &aBytes, mParsingMode, Inner().mURLData,
|
|
|
|
aLoadData.mLineNumber, aLoader.GetCompatibilityMode(),
|
2020-03-12 18:11:09 +00:00
|
|
|
/* reusable_sheets = */ nullptr, useCounters, allowImportRules,
|
2019-12-12 13:57:54 +00:00
|
|
|
StyleSanitizationKind::None,
|
|
|
|
/* sanitized_output = */ nullptr)
|
2018-04-30 17:09:30 +00:00
|
|
|
.Consume();
|
|
|
|
FinishAsyncParse(contents.forget());
|
|
|
|
} else {
|
2019-08-16 10:56:09 +00:00
|
|
|
auto holder = MakeRefPtr<css::SheetLoadDataHolder>(__func__, &aLoadData);
|
2018-04-30 17:09:30 +00:00
|
|
|
Servo_StyleSheet_FromUTF8BytesAsync(
|
2019-08-16 10:56:09 +00:00
|
|
|
holder, Inner().mURLData, &aBytes, mParsingMode, aLoadData.mLineNumber,
|
|
|
|
aLoader.GetCompatibilityMode(),
|
2020-03-12 18:11:09 +00:00
|
|
|
/* should_record_use_counters = */ !!useCounters, allowImportRules);
|
2018-04-30 17:09:30 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 15:59:07 +00:00
|
|
|
return p;
|
2018-04-30 17:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::FinishAsyncParse(
|
|
|
|
already_AddRefed<RawServoStyleSheetContents> aSheetContents) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!mParsePromise.IsEmpty());
|
2018-04-30 17:47:12 +00:00
|
|
|
Inner().mContents = aSheetContents;
|
2018-04-30 17:09:30 +00:00
|
|
|
FinishParse();
|
|
|
|
mParsePromise.Resolve(true, __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::ParseSheetSync(
|
|
|
|
css::Loader* aLoader, const nsACString& aBytes,
|
|
|
|
css::SheetLoadData* aLoadData, uint32_t aLineNumber,
|
|
|
|
css::LoaderReusableStyleSheets* aReusableSheets) {
|
|
|
|
nsCompatibility compatMode =
|
|
|
|
aLoader ? aLoader->GetCompatibilityMode() : eCompatibility_FullStandards;
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2018-08-20 17:03:11 +00:00
|
|
|
const StyleUseCounters* useCounters =
|
|
|
|
aLoader && aLoader->GetDocument()
|
|
|
|
? aLoader->GetDocument()->GetStyleUseCounters()
|
|
|
|
: nullptr;
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2020-03-12 18:11:09 +00:00
|
|
|
auto allowImportRules = SelfOrAncestorIsConstructed()
|
|
|
|
? StyleAllowImportRules::No
|
|
|
|
: StyleAllowImportRules::Yes;
|
|
|
|
|
2019-03-30 00:15:43 +00:00
|
|
|
SetURLExtraData();
|
2018-04-30 17:47:12 +00:00
|
|
|
Inner().mContents =
|
2020-03-12 18:11:09 +00:00
|
|
|
Servo_StyleSheet_FromUTF8Bytes(
|
|
|
|
aLoader, this, aLoadData, &aBytes, mParsingMode, Inner().mURLData,
|
|
|
|
aLineNumber, compatMode, aReusableSheets, useCounters,
|
|
|
|
allowImportRules, StyleSanitizationKind::None,
|
|
|
|
/* sanitized_output = */ nullptr)
|
2018-04-30 17:09:30 +00:00
|
|
|
.Consume();
|
|
|
|
|
|
|
|
FinishParse();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::FinishParse() {
|
|
|
|
nsString sourceMapURL;
|
2018-04-30 17:47:12 +00:00
|
|
|
Servo_StyleSheet_GetSourceMapURL(Inner().mContents, &sourceMapURL);
|
2018-04-30 17:09:30 +00:00
|
|
|
SetSourceMapURLFromComment(sourceMapURL);
|
|
|
|
|
|
|
|
nsString sourceURL;
|
2018-04-30 17:47:12 +00:00
|
|
|
Servo_StyleSheet_GetSourceURL(Inner().mContents, &sourceURL);
|
2018-04-30 17:09:30 +00:00
|
|
|
SetSourceURL(sourceURL);
|
|
|
|
}
|
|
|
|
|
2020-02-03 20:35:54 +00:00
|
|
|
void StyleSheet::ReparseSheet(const nsACString& aInput, ErrorResult& aRv) {
|
Bug 1484690 - Move the enabled state to the sheet instead of sharing it. r=heycam
We share the inner object across sheets from the same URL, so what happens here
is that, once the sheet parses and loads, we call SetEnabled() on the first
sheet, which sets the inner bit, then calls ApplicableStateChanged.
That set actually turned the second sheet complete, so when inserting the sheet,
we think that the second sheet is already enabled, and thus in the author data,
and try to insert before it. Of course there's nothing there, so we panic.
We rely on calling SetEnabled() on all the sheets already to insert them in the
styleset / author data, so this makes it clearer and fixes the bug by moving the
state to each individual sheet.
Differential Revision: https://phabricator.services.mozilla.com/D3798
--HG--
extra : moz-landing-system : lando
2018-08-21 08:55:10 +00:00
|
|
|
if (!IsComplete()) {
|
2020-02-03 20:35:54 +00:00
|
|
|
return aRv.ThrowInvalidAccessError("Cannot reparse still-loading sheet");
|
2018-04-30 17:09:30 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 15:17:54 +00:00
|
|
|
// Allowing to modify UA sheets is dangerous (in the sense that C++ code
|
|
|
|
// relies on rules in those sheets), plus they're probably going to be shared
|
|
|
|
// across processes in which case this is directly a no-go.
|
2019-04-29 05:34:06 +00:00
|
|
|
if (IsReadOnly()) {
|
2020-02-03 20:35:54 +00:00
|
|
|
return;
|
2019-03-12 15:17:54 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 17:09:30 +00:00
|
|
|
// Hold strong ref to the CSSLoader in case the document update
|
|
|
|
// kills the document
|
|
|
|
RefPtr<css::Loader> loader;
|
2019-01-02 13:05:23 +00:00
|
|
|
if (Document* doc = GetAssociatedDocument()) {
|
2018-05-11 10:57:38 +00:00
|
|
|
loader = doc->CSSLoader();
|
2018-04-30 17:09:30 +00:00
|
|
|
NS_ASSERTION(loader, "Document with no CSS loader!");
|
|
|
|
} else {
|
|
|
|
loader = new css::Loader;
|
|
|
|
}
|
|
|
|
|
|
|
|
WillDirty();
|
|
|
|
|
|
|
|
// cache child sheets to reuse
|
|
|
|
css::LoaderReusableStyleSheets reusableSheets;
|
2019-12-03 10:49:23 +00:00
|
|
|
for (StyleSheet* child : ChildSheets()) {
|
2018-04-30 17:09:30 +00:00
|
|
|
if (child->GetOriginalURI()) {
|
|
|
|
reusableSheets.AddReusableSheet(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 10:49:23 +00:00
|
|
|
// Clean up child sheets list.
|
|
|
|
for (StyleSheet* child : ChildSheets()) {
|
2020-04-13 20:49:32 +00:00
|
|
|
child->mParentSheet = nullptr;
|
2018-04-30 17:09:30 +00:00
|
|
|
}
|
2019-12-03 10:49:23 +00:00
|
|
|
Inner().mChildren.Clear();
|
2018-04-30 17:09:30 +00:00
|
|
|
|
|
|
|
uint32_t lineNumber = 1;
|
|
|
|
if (mOwningNode) {
|
|
|
|
nsCOMPtr<nsIStyleSheetLinkingElement> link = do_QueryInterface(mOwningNode);
|
|
|
|
if (link) {
|
|
|
|
lineNumber = link->GetLineNumber();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify to the stylesets about the old rules going away.
|
|
|
|
{
|
|
|
|
ServoCSSRuleList* ruleList = GetCssRulesInternal();
|
|
|
|
MOZ_ASSERT(ruleList);
|
|
|
|
|
|
|
|
uint32_t ruleCount = ruleList->Length();
|
|
|
|
for (uint32_t i = 0; i < ruleCount; ++i) {
|
|
|
|
css::Rule* rule = ruleList->GetRule(i);
|
|
|
|
MOZ_ASSERT(rule);
|
|
|
|
RuleRemoved(*rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DropRuleList();
|
|
|
|
|
2020-01-08 01:21:30 +00:00
|
|
|
ParseSheetSync(loader, aInput, /* aLoadData = */ nullptr, lineNumber,
|
|
|
|
&reusableSheets);
|
2018-04-30 17:09:30 +00:00
|
|
|
|
|
|
|
// Notify the stylesets about the new rules.
|
|
|
|
{
|
|
|
|
// Get the rule list (which will need to be regenerated after ParseSheet).
|
|
|
|
ServoCSSRuleList* ruleList = GetCssRulesInternal();
|
|
|
|
MOZ_ASSERT(ruleList);
|
|
|
|
|
|
|
|
uint32_t ruleCount = ruleList->Length();
|
|
|
|
for (uint32_t i = 0; i < ruleCount; ++i) {
|
|
|
|
css::Rule* rule = ruleList->GetRule(i);
|
|
|
|
MOZ_ASSERT(rule);
|
|
|
|
RuleAdded(*rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 11:04:03 +00:00
|
|
|
// Our rules are no longer considered modified for devtools.
|
|
|
|
mState &= ~State::ModifiedRulesForDevtools;
|
2018-04-30 17:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::DropRuleList() {
|
|
|
|
if (mRuleList) {
|
2018-06-29 02:56:09 +00:00
|
|
|
mRuleList->DropReferences();
|
2018-04-30 17:09:30 +00:00
|
|
|
mRuleList = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<StyleSheet> StyleSheet::Clone(
|
|
|
|
StyleSheet* aCloneParent, dom::CSSImportRule* aCloneOwnerRule,
|
2018-05-11 10:57:38 +00:00
|
|
|
dom::DocumentOrShadowRoot* aCloneDocumentOrShadowRoot,
|
2018-04-30 17:09:30 +00:00
|
|
|
nsINode* aCloneOwningNode) const {
|
2020-03-24 22:54:18 +00:00
|
|
|
MOZ_ASSERT(!IsConstructed(),
|
|
|
|
"Cannot create a non-constructed sheet from a constructed sheet");
|
2020-04-17 16:34:38 +00:00
|
|
|
RefPtr<StyleSheet> clone = new StyleSheet(
|
|
|
|
*this, aCloneParent, aCloneOwnerRule, aCloneDocumentOrShadowRoot,
|
|
|
|
/* aConstructorDocToUse */ nullptr, aCloneOwningNode);
|
2018-04-30 17:09:30 +00:00
|
|
|
return clone.forget();
|
|
|
|
}
|
|
|
|
|
2020-03-24 22:54:18 +00:00
|
|
|
already_AddRefed<StyleSheet> StyleSheet::CloneAdoptedSheet(
|
|
|
|
Document& aConstructorDocument) const {
|
|
|
|
MOZ_ASSERT(IsConstructed(),
|
|
|
|
"Cannot create a constructed sheet from a non-constructed sheet");
|
|
|
|
MOZ_ASSERT(aConstructorDocument.IsStaticDocument(),
|
|
|
|
"Should never clone adopted sheets for a non-static document");
|
2020-04-17 16:34:38 +00:00
|
|
|
RefPtr<StyleSheet> clone =
|
|
|
|
new StyleSheet(*this,
|
|
|
|
/* aParentSheetToUse */ nullptr,
|
|
|
|
/* aOwnerRuleToUse */ nullptr,
|
|
|
|
/* aDocOrShadowRootToUse */ nullptr, &aConstructorDocument,
|
|
|
|
/* aOwningNodeToUse */ nullptr);
|
2020-03-24 22:54:18 +00:00
|
|
|
return clone.forget();
|
|
|
|
}
|
|
|
|
|
2018-04-30 17:09:30 +00:00
|
|
|
ServoCSSRuleList* StyleSheet::GetCssRulesInternal() {
|
|
|
|
if (!mRuleList) {
|
|
|
|
EnsureUniqueInner();
|
|
|
|
|
|
|
|
RefPtr<ServoCssRules> rawRules =
|
2018-04-30 17:47:12 +00:00
|
|
|
Servo_StyleSheet_GetRules(Inner().mContents).Consume();
|
2018-04-30 17:09:30 +00:00
|
|
|
MOZ_ASSERT(rawRules);
|
2018-06-29 02:56:09 +00:00
|
|
|
mRuleList = new ServoCSSRuleList(rawRules.forget(), this, nullptr);
|
2018-04-30 17:09:30 +00:00
|
|
|
}
|
|
|
|
return mRuleList;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t StyleSheet::InsertRuleInternal(const nsAString& aRule, uint32_t aIndex,
|
|
|
|
ErrorResult& aRv) {
|
2019-04-29 05:34:06 +00:00
|
|
|
MOZ_ASSERT(!IsReadOnly());
|
2020-03-12 18:11:09 +00:00
|
|
|
MOZ_ASSERT(!ModificationDisallowed());
|
2019-04-29 05:34:06 +00:00
|
|
|
|
2018-04-30 17:09:30 +00:00
|
|
|
// Ensure mRuleList is constructed.
|
|
|
|
GetCssRulesInternal();
|
|
|
|
|
|
|
|
aRv = mRuleList->InsertRule(aRule, aIndex);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX We may not want to get the rule when stylesheet change event
|
|
|
|
// is not enabled.
|
|
|
|
css::Rule* rule = mRuleList->GetRule(aIndex);
|
2019-11-30 12:24:24 +00:00
|
|
|
RuleAdded(*rule);
|
2018-04-30 17:09:30 +00:00
|
|
|
|
|
|
|
return aIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StyleSheet::DeleteRuleInternal(uint32_t aIndex, ErrorResult& aRv) {
|
2019-04-29 05:34:06 +00:00
|
|
|
MOZ_ASSERT(!IsReadOnly());
|
2020-03-12 18:11:09 +00:00
|
|
|
MOZ_ASSERT(!ModificationDisallowed());
|
2019-04-29 05:34:06 +00:00
|
|
|
|
2018-04-30 17:09:30 +00:00
|
|
|
// Ensure mRuleList is constructed.
|
|
|
|
GetCssRulesInternal();
|
|
|
|
if (aIndex >= mRuleList->Length()) {
|
2020-02-03 20:19:11 +00:00
|
|
|
aRv.ThrowIndexSizeError(
|
2019-07-18 17:51:39 +00:00
|
|
|
nsPrintfCString("Cannot delete rule at index %u"
|
|
|
|
" because the number of rules is only %u",
|
|
|
|
aIndex, mRuleList->Length()));
|
2018-04-30 17:09:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hold a strong ref to the rule so it doesn't die when we remove it
|
|
|
|
// from the list. XXX We may not want to hold it if stylesheet change
|
|
|
|
// event is not enabled.
|
|
|
|
RefPtr<css::Rule> rule = mRuleList->GetRule(aIndex);
|
|
|
|
aRv = mRuleList->DeleteRule(aIndex);
|
|
|
|
if (!aRv.Failed()) {
|
|
|
|
RuleRemoved(*rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult StyleSheet::InsertRuleIntoGroupInternal(const nsAString& aRule,
|
|
|
|
css::GroupRule* aGroup,
|
|
|
|
uint32_t aIndex) {
|
2019-04-29 05:34:06 +00:00
|
|
|
MOZ_ASSERT(!IsReadOnly());
|
|
|
|
|
2018-04-30 17:09:30 +00:00
|
|
|
auto rules = static_cast<ServoCSSRuleList*>(aGroup->CssRules());
|
|
|
|
MOZ_ASSERT(rules->GetParentRule() == aGroup);
|
|
|
|
return rules->InsertRule(aRule, aIndex);
|
|
|
|
}
|
|
|
|
|
2019-04-19 04:20:31 +00:00
|
|
|
StyleOrigin StyleSheet::GetOrigin() const {
|
|
|
|
return Servo_StyleSheet_GetOrigin(Inner().mContents);
|
2018-04-30 17:09:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 11:19:23 +00:00
|
|
|
void StyleSheet::SetSharedContents(const ServoCssRules* aSharedRules) {
|
2019-03-30 00:23:49 +00:00
|
|
|
MOZ_ASSERT(!IsComplete());
|
|
|
|
|
|
|
|
SetURLExtraData();
|
|
|
|
|
|
|
|
Inner().mContents =
|
|
|
|
Servo_StyleSheet_FromSharedData(Inner().mURLData, aSharedRules).Consume();
|
|
|
|
|
|
|
|
// Don't call FinishParse(), since that tries to set source map URLs,
|
|
|
|
// which we don't have.
|
|
|
|
}
|
|
|
|
|
|
|
|
const ServoCssRules* StyleSheet::ToShared(
|
|
|
|
RawServoSharedMemoryBuilder* aBuilder) {
|
|
|
|
// Assert some things we assume when creating a StyleSheet using shared
|
|
|
|
// memory.
|
2019-08-21 13:24:45 +00:00
|
|
|
MOZ_ASSERT(GetReferrerInfo()->ReferrerPolicy() == ReferrerPolicy::_empty);
|
2019-07-16 11:43:39 +00:00
|
|
|
MOZ_ASSERT(GetReferrerInfo()->GetSendReferrer());
|
|
|
|
MOZ_ASSERT(!nsCOMPtr<nsIURI>(GetReferrerInfo()->GetComputedReferrer()));
|
2019-03-30 00:23:49 +00:00
|
|
|
MOZ_ASSERT(GetCORSMode() == CORS_NONE);
|
|
|
|
MOZ_ASSERT(Inner().mIntegrity.IsEmpty());
|
2019-12-05 04:44:32 +00:00
|
|
|
MOZ_ASSERT(Principal()->IsSystemPrincipal());
|
2019-03-30 00:23:49 +00:00
|
|
|
|
|
|
|
return Servo_SharedMemoryBuilder_AddStylesheet(aBuilder, Inner().mContents);
|
|
|
|
}
|
|
|
|
|
2019-04-29 05:34:06 +00:00
|
|
|
bool StyleSheet::IsReadOnly() const {
|
|
|
|
return IsComplete() && GetOrigin() == StyleOrigin::UserAgent;
|
|
|
|
}
|
|
|
|
|
2016-02-26 01:51:01 +00:00
|
|
|
} // namespace mozilla
|