2016-02-24 10:01:11 +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
|
|
|
|
* 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/. */
|
|
|
|
|
|
|
|
#include "mozilla/ServoStyleSheet.h"
|
2016-12-01 21:40:04 +03:00
|
|
|
|
|
|
|
#include "mozilla/css/Rule.h"
|
2016-03-02 01:38:13 +03:00
|
|
|
#include "mozilla/StyleBackendType.h"
|
2016-11-23 02:26:20 +03:00
|
|
|
#include "mozilla/ServoBindings.h"
|
|
|
|
#include "mozilla/ServoCSSRuleList.h"
|
|
|
|
#include "mozilla/dom/CSSRuleList.h"
|
2016-02-24 10:01:11 +03:00
|
|
|
|
2016-11-29 14:59:55 +03:00
|
|
|
#include "mozAutoDocUpdate.h"
|
|
|
|
|
2016-12-01 21:37:44 +03:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2016-02-24 10:01:11 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
2016-08-02 23:12:27 +03:00
|
|
|
ServoStyleSheet::ServoStyleSheet(css::SheetParsingMode aParsingMode,
|
|
|
|
CORSMode aCORSMode,
|
2016-02-26 04:51:01 +03:00
|
|
|
net::ReferrerPolicy aReferrerPolicy,
|
|
|
|
const dom::SRIMetadata& aIntegrity)
|
2016-08-02 23:12:27 +03:00
|
|
|
: StyleSheet(StyleBackendType::Servo, aParsingMode)
|
2016-09-29 09:11:52 +03:00
|
|
|
, mSheetInfo(aCORSMode, aReferrerPolicy, aIntegrity)
|
2016-02-24 10:01:11 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-02-26 04:51:01 +03:00
|
|
|
ServoStyleSheet::~ServoStyleSheet()
|
2016-02-24 10:01:11 +03:00
|
|
|
{
|
2016-02-26 04:51:01 +03:00
|
|
|
DropSheet();
|
2016-02-24 10:01:11 +03:00
|
|
|
}
|
|
|
|
|
2017-01-06 07:14:28 +03:00
|
|
|
// QueryInterface implementation for ServoStyleSheet
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ServoStyleSheet)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(StyleSheet)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(ServoStyleSheet, StyleSheet)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ServoStyleSheet, StyleSheet)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(ServoStyleSheet)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ServoStyleSheet)
|
|
|
|
tmp->DropRuleList();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(StyleSheet)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ServoStyleSheet, StyleSheet)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRuleList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2016-02-24 10:01:11 +03:00
|
|
|
bool
|
|
|
|
ServoStyleSheet::HasRules() const
|
|
|
|
{
|
2016-09-26 15:03:25 +03:00
|
|
|
return mSheet && Servo_StyleSheet_HasRules(mSheet);
|
2016-02-24 10:01:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-20 07:49:44 +03:00
|
|
|
ServoStyleSheet::SetAssociatedDocument(nsIDocument* aDocument,
|
|
|
|
DocumentAssociationMode aAssociationMode)
|
2016-02-24 10:01:11 +03:00
|
|
|
{
|
2017-01-20 07:49:44 +03:00
|
|
|
MOZ_ASSERT_IF(!aDocument, aAssociationMode == NotOwnedByDocument);
|
|
|
|
|
2016-04-27 05:34:49 +03:00
|
|
|
// XXXheycam: Traverse to child ServoStyleSheets to set this, like
|
2017-01-20 07:49:44 +03:00
|
|
|
// CSSStyleSheet::SetAssociatedDocument does.
|
2016-04-27 05:34:49 +03:00
|
|
|
|
|
|
|
mDocument = aDocument;
|
2017-01-20 07:49:44 +03:00
|
|
|
mDocumentAssociationMode = aAssociationMode;
|
2016-02-24 10:01:11 +03:00
|
|
|
}
|
|
|
|
|
2016-09-26 15:03:25 +03:00
|
|
|
ServoStyleSheet*
|
2016-02-24 10:01:11 +03:00
|
|
|
ServoStyleSheet::GetParentSheet() const
|
|
|
|
{
|
2016-04-27 05:34:49 +03:00
|
|
|
// XXXheycam: When we implement support for child sheets, we'll have
|
2017-01-20 07:49:44 +03:00
|
|
|
// to fix SetAssociatedDocument to propagate the associated document down
|
2016-04-27 05:34:49 +03:00
|
|
|
// to the children.
|
2016-02-24 10:01:11 +03:00
|
|
|
MOZ_CRASH("stylo: not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-26 15:03:25 +03:00
|
|
|
ServoStyleSheet::AppendStyleSheet(ServoStyleSheet* aSheet)
|
2016-02-24 10:01:11 +03:00
|
|
|
{
|
2016-12-17 12:58:56 +03:00
|
|
|
aSheet->mDocument = mDocument;
|
2016-02-24 10:01:11 +03:00
|
|
|
}
|
|
|
|
|
2016-08-31 13:10:10 +03:00
|
|
|
nsresult
|
2016-12-17 12:58:56 +03:00
|
|
|
ServoStyleSheet::ParseSheet(css::Loader* aLoader,
|
|
|
|
const nsAString& aInput,
|
2016-02-24 10:01:11 +03:00
|
|
|
nsIURI* aSheetURI,
|
|
|
|
nsIURI* aBaseURI,
|
|
|
|
nsIPrincipal* aSheetPrincipal,
|
2016-08-02 23:12:27 +03:00
|
|
|
uint32_t aLineNumber)
|
2016-02-24 10:01:11 +03:00
|
|
|
{
|
2016-05-21 03:02:54 +03:00
|
|
|
RefPtr<ThreadSafeURIHolder> base = new ThreadSafeURIHolder(aBaseURI);
|
|
|
|
RefPtr<ThreadSafeURIHolder> referrer = new ThreadSafeURIHolder(aSheetURI);
|
|
|
|
RefPtr<ThreadSafePrincipalHolder> principal =
|
|
|
|
new ThreadSafePrincipalHolder(aSheetPrincipal);
|
|
|
|
|
2016-08-02 12:05:22 +03:00
|
|
|
nsCString baseString;
|
2016-08-31 13:10:10 +03:00
|
|
|
nsresult rv = aBaseURI->GetSpec(baseString);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2016-08-02 12:05:22 +03:00
|
|
|
|
2016-02-26 04:51:01 +03:00
|
|
|
NS_ConvertUTF16toUTF8 input(aInput);
|
2016-12-17 12:58:56 +03:00
|
|
|
if (!mSheet) {
|
|
|
|
mSheet =
|
|
|
|
Servo_StyleSheet_FromUTF8Bytes(aLoader, this, &input, mParsingMode,
|
|
|
|
&baseString, base, referrer,
|
|
|
|
principal).Consume();
|
|
|
|
} else {
|
|
|
|
Servo_StyleSheet_ClearAndUpdate(mSheet, aLoader, this, &input, base,
|
|
|
|
referrer, principal);
|
|
|
|
}
|
2016-08-31 13:10:10 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2016-02-26 04:51:01 +03:00
|
|
|
}
|
|
|
|
|
2016-10-25 20:15:38 +03:00
|
|
|
void
|
|
|
|
ServoStyleSheet::LoadFailed()
|
|
|
|
{
|
|
|
|
mSheet = Servo_StyleSheet_Empty(mParsingMode).Consume();
|
|
|
|
}
|
|
|
|
|
2016-02-26 04:51:01 +03:00
|
|
|
void
|
|
|
|
ServoStyleSheet::DropSheet()
|
|
|
|
{
|
|
|
|
mSheet = nullptr;
|
2017-01-06 07:14:28 +03:00
|
|
|
DropRuleList();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServoStyleSheet::DropRuleList()
|
|
|
|
{
|
|
|
|
if (mRuleList) {
|
|
|
|
mRuleList->DropReference();
|
|
|
|
mRuleList = nullptr;
|
|
|
|
}
|
2016-02-24 10:01:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ServoStyleSheet::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
MOZ_CRASH("stylo: not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
|
|
|
ServoStyleSheet::List(FILE* aOut, int32_t aIndex) const
|
|
|
|
{
|
|
|
|
MOZ_CRASH("stylo: not implemented");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-01-20 21:30:28 +03:00
|
|
|
nsIDOMCSSRule*
|
2016-10-14 14:25:38 +03:00
|
|
|
ServoStyleSheet::GetDOMOwnerRule() const
|
2016-10-14 14:25:38 +03:00
|
|
|
{
|
2016-10-14 14:25:38 +03:00
|
|
|
return nullptr;
|
2016-10-14 14:25:38 +03:00
|
|
|
}
|
|
|
|
|
2016-10-14 14:25:38 +03:00
|
|
|
CSSRuleList*
|
|
|
|
ServoStyleSheet::GetCssRulesInternal(ErrorResult& aRv)
|
|
|
|
{
|
2016-11-23 02:26:20 +03:00
|
|
|
if (!mRuleList) {
|
|
|
|
RefPtr<ServoCssRules> rawRules = Servo_StyleSheet_GetRules(mSheet).Consume();
|
|
|
|
mRuleList = new ServoCSSRuleList(this, rawRules.forget());
|
|
|
|
}
|
|
|
|
return mRuleList;
|
2016-10-14 14:25:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
ServoStyleSheet::InsertRuleInternal(const nsAString& aRule,
|
|
|
|
uint32_t aIndex, ErrorResult& aRv)
|
|
|
|
{
|
2016-11-29 14:59:55 +03:00
|
|
|
// Ensure mRuleList is constructed.
|
|
|
|
GetCssRulesInternal(aRv);
|
|
|
|
|
|
|
|
mozAutoDocUpdate updateBatch(mDocument, UPDATE_STYLE, true);
|
|
|
|
aRv = mRuleList->InsertRule(aRule, aIndex);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (mDocument) {
|
|
|
|
// XXX When we support @import rules, we should not notify here,
|
|
|
|
// but rather when the sheet the rule is importing is loaded.
|
|
|
|
// XXX We may not want to get the rule when stylesheet change event
|
|
|
|
// is not enabled.
|
|
|
|
mDocument->StyleRuleAdded(this, mRuleList->GetRule(aIndex));
|
|
|
|
}
|
|
|
|
return aIndex;
|
2016-10-14 14:25:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServoStyleSheet::DeleteRuleInternal(uint32_t aIndex, ErrorResult& aRv)
|
|
|
|
{
|
2016-11-29 14:59:55 +03:00
|
|
|
// Ensure mRuleList is constructed.
|
|
|
|
GetCssRulesInternal(aRv);
|
|
|
|
if (aIndex > mRuleList->Length()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mozAutoDocUpdate updateBatch(mDocument, UPDATE_STYLE, true);
|
|
|
|
// 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);
|
|
|
|
MOZ_ASSERT(!aRv.ErrorCodeIs(NS_ERROR_DOM_INDEX_SIZE_ERR),
|
|
|
|
"IndexSizeError should have been handled earlier");
|
|
|
|
if (!aRv.Failed() && mDocument) {
|
|
|
|
mDocument->StyleRuleRemoved(this, rule);
|
|
|
|
}
|
2016-10-14 14:25:38 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 10:01:11 +03:00
|
|
|
} // namespace mozilla
|