2017-05-19 09:12:34 +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/dom/CSSKeyframesRule.h"
|
|
|
|
|
|
|
|
#include "mozilla/dom/CSSKeyframesRuleBinding.h"
|
2018-06-04 13:09:11 +03:00
|
|
|
#include "mozilla/dom/CSSRuleList.h"
|
|
|
|
#include "mozilla/ServoBindings.h"
|
2018-11-13 01:20:52 +03:00
|
|
|
#include "nsCOMArray.h"
|
2018-06-04 13:09:11 +03:00
|
|
|
|
|
|
|
#include <limits>
|
2017-05-19 09:12:34 +03:00
|
|
|
|
2021-09-08 14:18:32 +03:00
|
|
|
namespace mozilla::dom {
|
2017-05-19 09:12:34 +03:00
|
|
|
|
2018-06-04 13:09:11 +03:00
|
|
|
// -------------------------------------------
|
|
|
|
// CSSKeyframeList
|
|
|
|
//
|
|
|
|
|
|
|
|
class CSSKeyframeList : public dom::CSSRuleList {
|
|
|
|
public:
|
2018-06-29 05:56:09 +03:00
|
|
|
CSSKeyframeList(already_AddRefed<RawServoKeyframesRule> aRawRule,
|
|
|
|
StyleSheet* aSheet, CSSKeyframesRule* aParentRule)
|
|
|
|
: mStyleSheet(aSheet), mParentRule(aParentRule), mRawRule(aRawRule) {
|
2018-06-04 13:09:11 +03:00
|
|
|
mRules.SetCount(Servo_KeyframesRule_GetCount(mRawRule));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(CSSKeyframeList, dom::CSSRuleList)
|
|
|
|
|
2021-07-20 16:17:02 +03:00
|
|
|
void SetRawAfterClone(RefPtr<RawServoKeyframesRule> aRaw) {
|
|
|
|
mRawRule = std::move(aRaw);
|
|
|
|
uint32_t index = 0;
|
|
|
|
for (css::Rule* rule : mRules) {
|
|
|
|
if (rule) {
|
|
|
|
uint32_t line = 0, column = 0;
|
|
|
|
RefPtr<RawServoKeyframe> keyframe =
|
|
|
|
Servo_KeyframesRule_GetKeyframeAt(mRawRule, index, &line, &column)
|
|
|
|
.Consume();
|
|
|
|
static_cast<CSSKeyframeRule*>(rule)->SetRawAfterClone(
|
|
|
|
std::move(keyframe));
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-29 05:56:09 +03:00
|
|
|
void DropSheetReference() {
|
|
|
|
if (!mStyleSheet) {
|
|
|
|
return;
|
2018-06-04 13:09:11 +03:00
|
|
|
}
|
2018-06-29 05:56:09 +03:00
|
|
|
mStyleSheet = nullptr;
|
2018-06-04 13:09:11 +03:00
|
|
|
for (css::Rule* rule : mRules) {
|
|
|
|
if (rule) {
|
2018-06-29 05:56:09 +03:00
|
|
|
rule->DropSheetReference();
|
2018-06-04 13:09:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StyleSheet* GetParentObject() final { return mStyleSheet; }
|
|
|
|
|
|
|
|
CSSKeyframeRule* GetRule(uint32_t aIndex) {
|
|
|
|
if (!mRules[aIndex]) {
|
|
|
|
uint32_t line = 0, column = 0;
|
|
|
|
RefPtr<RawServoKeyframe> rule =
|
|
|
|
Servo_KeyframesRule_GetKeyframeAt(mRawRule, aIndex, &line, &column)
|
|
|
|
.Consume();
|
2018-06-29 05:56:09 +03:00
|
|
|
CSSKeyframeRule* ruleObj = new CSSKeyframeRule(rule.forget(), mStyleSheet,
|
|
|
|
mParentRule, line, column);
|
2018-06-04 13:09:11 +03:00
|
|
|
mRules.ReplaceObjectAt(ruleObj, aIndex);
|
|
|
|
}
|
|
|
|
return static_cast<CSSKeyframeRule*>(mRules[aIndex]);
|
|
|
|
}
|
|
|
|
|
|
|
|
CSSKeyframeRule* IndexedGetter(uint32_t aIndex, bool& aFound) final {
|
|
|
|
if (aIndex >= mRules.Length()) {
|
|
|
|
aFound = false;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
aFound = true;
|
|
|
|
return GetRule(aIndex);
|
|
|
|
}
|
|
|
|
|
2019-04-29 08:34:06 +03:00
|
|
|
void AppendRule() {
|
|
|
|
MOZ_ASSERT(!mParentRule->IsReadOnly());
|
|
|
|
mRules.AppendObject(nullptr);
|
|
|
|
}
|
2018-06-04 13:09:11 +03:00
|
|
|
|
2019-03-20 12:50:51 +03:00
|
|
|
void RemoveRule(uint32_t aIndex) {
|
2019-04-29 08:34:06 +03:00
|
|
|
MOZ_ASSERT(!mParentRule->IsReadOnly());
|
|
|
|
|
2019-03-20 12:50:51 +03:00
|
|
|
if (aIndex >= mRules.Length()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (css::Rule* child = mRules[aIndex]) {
|
|
|
|
child->DropReferences();
|
|
|
|
}
|
|
|
|
mRules.RemoveObjectAt(aIndex);
|
|
|
|
}
|
2018-06-04 13:09:11 +03:00
|
|
|
|
|
|
|
uint32_t Length() final { return mRules.Length(); }
|
|
|
|
|
2018-06-29 05:56:09 +03:00
|
|
|
void DropReferences() {
|
|
|
|
if (!mStyleSheet && !mParentRule) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-04 13:09:11 +03:00
|
|
|
mStyleSheet = nullptr;
|
|
|
|
mParentRule = nullptr;
|
|
|
|
for (css::Rule* rule : mRules) {
|
|
|
|
if (rule) {
|
2018-06-29 05:56:09 +03:00
|
|
|
rule->DropParentRuleReference();
|
|
|
|
rule->DropSheetReference();
|
2018-06-04 13:09:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
|
|
|
|
size_t n = aMallocSizeOf(this);
|
|
|
|
for (const css::Rule* rule : mRules) {
|
|
|
|
n += rule ? rule->SizeOfIncludingThis(aMallocSizeOf) : 0;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~CSSKeyframeList() {
|
|
|
|
MOZ_ASSERT(!mParentRule, "Backpointer should have been cleared");
|
|
|
|
MOZ_ASSERT(!mStyleSheet, "Backpointer should have been cleared");
|
|
|
|
DropAllRules();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DropAllRules() {
|
2018-06-29 05:56:09 +03:00
|
|
|
DropReferences();
|
2018-06-04 13:09:11 +03:00
|
|
|
mRules.Clear();
|
|
|
|
mRawRule = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// may be nullptr when the style sheet drops the reference to us.
|
|
|
|
StyleSheet* mStyleSheet = nullptr;
|
|
|
|
CSSKeyframesRule* mParentRule = nullptr;
|
|
|
|
RefPtr<RawServoKeyframesRule> mRawRule;
|
|
|
|
nsCOMArray<css::Rule> mRules;
|
|
|
|
};
|
|
|
|
|
|
|
|
// QueryInterface implementation for CSSKeyframeList
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CSSKeyframeList)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(dom::CSSRuleList)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(CSSKeyframeList, dom::CSSRuleList)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(CSSKeyframeList, dom::CSSRuleList)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(CSSKeyframeList)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(CSSKeyframeList)
|
|
|
|
tmp->DropAllRules();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(dom::CSSRuleList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(CSSKeyframeList,
|
|
|
|
dom::CSSRuleList)
|
|
|
|
for (css::Rule* rule : tmp->mRules) {
|
|
|
|
if (rule) {
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mRules[i]");
|
|
|
|
cb.NoteXPCOMChild(rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
// -------------------------------------------
|
|
|
|
// CSSKeyframesRule
|
|
|
|
//
|
|
|
|
|
|
|
|
CSSKeyframesRule::CSSKeyframesRule(RefPtr<RawServoKeyframesRule> aRawRule,
|
2018-06-29 05:56:09 +03:00
|
|
|
StyleSheet* aSheet, css::Rule* aParentRule,
|
|
|
|
uint32_t aLine, uint32_t aColumn)
|
|
|
|
: css::Rule(aSheet, aParentRule, aLine, aColumn),
|
2018-06-04 13:09:11 +03:00
|
|
|
mRawRule(std::move(aRawRule)) {}
|
|
|
|
|
|
|
|
CSSKeyframesRule::~CSSKeyframesRule() {
|
|
|
|
if (mKeyframeList) {
|
2018-06-29 05:56:09 +03:00
|
|
|
mKeyframeList->DropReferences();
|
2018-06-04 13:09:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(CSSKeyframesRule, css::Rule)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(CSSKeyframesRule, css::Rule)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CSSKeyframesRule)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(css::Rule)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(CSSKeyframesRule)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(CSSKeyframesRule, css::Rule)
|
|
|
|
if (tmp->mKeyframeList) {
|
2018-06-29 05:56:09 +03:00
|
|
|
tmp->mKeyframeList->DropReferences();
|
2018-06-04 13:09:11 +03:00
|
|
|
tmp->mKeyframeList = nullptr;
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(CSSKeyframesRule, Rule)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mKeyframeList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2019-02-26 01:09:24 +03:00
|
|
|
/* virtual */
|
|
|
|
bool CSSKeyframesRule::IsCCLeaf() const {
|
2018-06-04 13:09:11 +03:00
|
|
|
// If we don't have rule list constructed, we are a leaf.
|
|
|
|
return Rule::IsCCLeaf() && !mKeyframeList;
|
|
|
|
}
|
|
|
|
|
2021-09-08 14:18:32 +03:00
|
|
|
StyleCssRuleType CSSKeyframesRule::Type() const {
|
|
|
|
return StyleCssRuleType::Keyframes;
|
|
|
|
}
|
|
|
|
|
2021-07-20 16:17:02 +03:00
|
|
|
void CSSKeyframesRule::SetRawAfterClone(RefPtr<RawServoKeyframesRule> aRaw) {
|
|
|
|
mRawRule = std::move(aRaw);
|
|
|
|
if (mKeyframeList) {
|
|
|
|
mKeyframeList->SetRawAfterClone(mRawRule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-04 13:09:11 +03:00
|
|
|
#ifdef DEBUG
|
2019-02-26 01:09:24 +03:00
|
|
|
/* virtual */
|
|
|
|
void CSSKeyframesRule::List(FILE* out, int32_t aIndent) const {
|
2018-06-04 13:09:11 +03:00
|
|
|
nsAutoCString str;
|
|
|
|
for (int32_t i = 0; i < aIndent; i++) {
|
|
|
|
str.AppendLiteral(" ");
|
|
|
|
}
|
|
|
|
Servo_KeyframesRule_Debug(mRawRule, &str);
|
|
|
|
fprintf_stderr(out, "%s\n", str.get());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-02-26 01:09:24 +03:00
|
|
|
/* virtual */
|
|
|
|
void CSSKeyframesRule::DropSheetReference() {
|
2018-06-04 13:09:11 +03:00
|
|
|
if (mKeyframeList) {
|
2018-06-29 05:56:09 +03:00
|
|
|
mKeyframeList->DropSheetReference();
|
2018-06-04 13:09:11 +03:00
|
|
|
}
|
2018-06-29 05:56:09 +03:00
|
|
|
css::Rule::DropSheetReference();
|
2018-06-04 13:09:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static const uint32_t kRuleNotFound = std::numeric_limits<uint32_t>::max();
|
|
|
|
|
|
|
|
uint32_t CSSKeyframesRule::FindRuleIndexForKey(const nsAString& aKey) {
|
|
|
|
NS_ConvertUTF16toUTF8 key(aKey);
|
|
|
|
return Servo_KeyframesRule_FindRule(mRawRule, &key);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Func>
|
2019-04-29 08:34:06 +03:00
|
|
|
nsresult CSSKeyframesRule::UpdateRule(Func aCallback) {
|
|
|
|
if (IsReadOnly()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2021-07-20 16:17:02 +03:00
|
|
|
StyleSheet* sheet = GetStyleSheet();
|
|
|
|
if (sheet) {
|
|
|
|
sheet->WillDirty();
|
|
|
|
}
|
|
|
|
|
2018-06-04 13:09:11 +03:00
|
|
|
aCallback();
|
2021-07-20 16:17:02 +03:00
|
|
|
|
|
|
|
if (sheet) {
|
2020-08-10 21:00:44 +03:00
|
|
|
sheet->RuleChanged(this, StyleRuleChangeKind::Generic);
|
2018-06-04 13:09:11 +03:00
|
|
|
}
|
2019-04-29 08:34:06 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2018-06-04 13:09:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSSKeyframesRule::GetName(nsAString& aName) const {
|
|
|
|
nsAtom* name = Servo_KeyframesRule_GetName(mRawRule);
|
|
|
|
aName = nsDependentAtomString(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSSKeyframesRule::SetName(const nsAString& aName) {
|
|
|
|
RefPtr<nsAtom> name = NS_Atomize(aName);
|
|
|
|
nsAtom* oldName = Servo_KeyframesRule_GetName(mRawRule);
|
|
|
|
if (name == oldName) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateRule([this, &name]() {
|
|
|
|
Servo_KeyframesRule_SetName(mRawRule, name.forget().take());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSSKeyframesRule::AppendRule(const nsAString& aRule) {
|
|
|
|
StyleSheet* sheet = GetStyleSheet();
|
|
|
|
if (!sheet) {
|
|
|
|
// We cannot parse the rule if we don't have a stylesheet.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 rule(aRule);
|
|
|
|
UpdateRule([this, sheet, &rule]() {
|
|
|
|
bool parsedOk =
|
|
|
|
Servo_KeyframesRule_AppendRule(mRawRule, sheet->RawContents(), &rule);
|
|
|
|
if (parsedOk && mKeyframeList) {
|
|
|
|
mKeyframeList->AppendRule();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSSKeyframesRule::DeleteRule(const nsAString& aKey) {
|
|
|
|
auto index = FindRuleIndexForKey(aKey);
|
|
|
|
if (index == kRuleNotFound) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateRule([this, index]() {
|
|
|
|
Servo_KeyframesRule_DeleteRule(mRawRule, index);
|
|
|
|
if (mKeyframeList) {
|
|
|
|
mKeyframeList->RemoveRule(index);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:09:24 +03:00
|
|
|
/* virtual */
|
2020-12-17 17:04:35 +03:00
|
|
|
void CSSKeyframesRule::GetCssText(nsACString& aCssText) const {
|
2018-06-04 13:09:11 +03:00
|
|
|
Servo_KeyframesRule_GetCssText(mRawRule, &aCssText);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ dom::CSSRuleList* CSSKeyframesRule::CssRules() {
|
|
|
|
if (!mKeyframeList) {
|
2018-06-29 05:56:09 +03:00
|
|
|
mKeyframeList = new CSSKeyframeList(do_AddRef(mRawRule), mSheet, this);
|
2018-06-04 13:09:11 +03:00
|
|
|
}
|
|
|
|
return mKeyframeList;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ dom::CSSKeyframeRule* CSSKeyframesRule::FindRule(
|
|
|
|
const nsAString& aKey) {
|
|
|
|
auto index = FindRuleIndexForKey(aKey);
|
|
|
|
if (index != kRuleNotFound) {
|
|
|
|
// Construct mKeyframeList but ignore the result.
|
|
|
|
CssRules();
|
|
|
|
return mKeyframeList->GetRule(index);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:09:24 +03:00
|
|
|
/* virtual */
|
|
|
|
size_t CSSKeyframesRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
|
2018-06-04 13:09:11 +03:00
|
|
|
size_t n = aMallocSizeOf(this);
|
|
|
|
if (mKeyframeList) {
|
|
|
|
n += mKeyframeList->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
2017-05-19 09:12:34 +03:00
|
|
|
|
2019-02-26 01:09:24 +03:00
|
|
|
/* virtual */
|
|
|
|
JSObject* CSSKeyframesRule::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return CSSKeyframesRule_Binding::Wrap(aCx, this, aGivenProto);
|
2017-05-19 09:12:34 +03:00
|
|
|
}
|
|
|
|
|
2021-09-08 14:18:32 +03:00
|
|
|
} // namespace mozilla::dom
|