2016-11-23 02:26:20 +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/. */
|
|
|
|
|
|
|
|
/* representation of CSSRuleList for stylo */
|
|
|
|
|
|
|
|
#include "mozilla/ServoCSSRuleList.h"
|
|
|
|
|
|
|
|
#include "mozilla/ServoBindings.h"
|
2016-11-23 02:26:20 +03:00
|
|
|
#include "mozilla/ServoStyleRule.h"
|
2016-11-23 02:26:20 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
ServoCSSRuleList::ServoCSSRuleList(ServoStyleSheet* aStyleSheet,
|
|
|
|
already_AddRefed<ServoCssRules> aRawRules)
|
|
|
|
: mStyleSheet(aStyleSheet)
|
|
|
|
, mRawRules(aRawRules)
|
|
|
|
{
|
|
|
|
Servo_CssRules_ListTypes(mRawRules, &mRules);
|
|
|
|
// XXX We may want to eagerly create object for import rule, so that
|
|
|
|
// we don't lose the reference to child stylesheet when our own
|
|
|
|
// stylesheet goes away.
|
|
|
|
}
|
|
|
|
|
2017-01-06 07:14:28 +03:00
|
|
|
// QueryInterface implementation for ServoCSSRuleList
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ServoCSSRuleList)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(dom::CSSRuleList)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(ServoCSSRuleList, dom::CSSRuleList)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ServoCSSRuleList, dom::CSSRuleList)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(ServoCSSRuleList)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ServoCSSRuleList)
|
|
|
|
for (uintptr_t& rule : tmp->mRules) {
|
|
|
|
if (rule > kMaxRuleType) {
|
|
|
|
CastToPtr(rule)->Release();
|
|
|
|
// Safest to set it to zero, in case someone else pokes at it
|
|
|
|
// during their own unlinking process.
|
|
|
|
rule = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(dom::CSSRuleList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ServoCSSRuleList,
|
|
|
|
dom::CSSRuleList)
|
|
|
|
tmp->EnumerateInstantiatedRules([&](css::Rule* aRule) {
|
2016-12-01 02:18:14 +03:00
|
|
|
if (!aRule->IsCCLeaf()) {
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mRules[i]");
|
|
|
|
cb.NoteXPCOMChild(aRule);
|
|
|
|
}
|
2017-01-06 07:14:28 +03:00
|
|
|
});
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2016-11-29 14:59:55 +03:00
|
|
|
css::Rule*
|
|
|
|
ServoCSSRuleList::GetRule(uint32_t aIndex)
|
2016-11-23 02:26:20 +03:00
|
|
|
{
|
|
|
|
uintptr_t rule = mRules[aIndex];
|
|
|
|
if (rule <= kMaxRuleType) {
|
|
|
|
RefPtr<css::Rule> ruleObj = nullptr;
|
|
|
|
switch (rule) {
|
2016-11-23 02:26:20 +03:00
|
|
|
case nsIDOMCSSRule::STYLE_RULE: {
|
|
|
|
ruleObj = new ServoStyleRule(
|
|
|
|
Servo_CssRules_GetStyleRuleAt(mRawRules, aIndex).Consume());
|
|
|
|
break;
|
|
|
|
}
|
2016-11-23 02:26:20 +03:00
|
|
|
case nsIDOMCSSRule::MEDIA_RULE:
|
|
|
|
case nsIDOMCSSRule::FONT_FACE_RULE:
|
|
|
|
case nsIDOMCSSRule::KEYFRAMES_RULE:
|
|
|
|
case nsIDOMCSSRule::NAMESPACE_RULE:
|
|
|
|
// XXX create corresponding rules
|
|
|
|
default:
|
2017-02-14 04:19:54 +03:00
|
|
|
NS_WARNING("stylo: not implemented yet");
|
2016-11-24 08:28:28 +03:00
|
|
|
return nullptr;
|
2016-11-23 02:26:20 +03:00
|
|
|
}
|
|
|
|
ruleObj->SetStyleSheet(mStyleSheet);
|
|
|
|
rule = CastToUint(ruleObj.forget().take());
|
|
|
|
mRules[aIndex] = rule;
|
|
|
|
}
|
2016-11-29 14:59:55 +03:00
|
|
|
return CastToPtr(rule);
|
|
|
|
}
|
|
|
|
|
2017-01-13 18:41:03 +03:00
|
|
|
css::Rule*
|
2016-11-29 14:59:55 +03:00
|
|
|
ServoCSSRuleList::IndexedGetter(uint32_t aIndex, bool& aFound)
|
|
|
|
{
|
|
|
|
if (aIndex >= mRules.Length()) {
|
|
|
|
aFound = false;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
aFound = true;
|
2017-01-13 18:41:03 +03:00
|
|
|
return GetRule(aIndex);
|
2016-11-23 02:26:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Func>
|
|
|
|
void
|
|
|
|
ServoCSSRuleList::EnumerateInstantiatedRules(Func aCallback)
|
|
|
|
{
|
|
|
|
for (uintptr_t rule : mRules) {
|
|
|
|
if (rule > kMaxRuleType) {
|
|
|
|
aCallback(CastToPtr(rule));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ServoCSSRuleList::DropReference()
|
|
|
|
{
|
|
|
|
mStyleSheet = nullptr;
|
|
|
|
EnumerateInstantiatedRules([](css::Rule* rule) {
|
|
|
|
rule->SetStyleSheet(nullptr);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-29 14:59:55 +03:00
|
|
|
nsresult
|
|
|
|
ServoCSSRuleList::InsertRule(const nsAString& aRule, uint32_t aIndex)
|
|
|
|
{
|
|
|
|
NS_ConvertUTF16toUTF8 rule(aRule);
|
|
|
|
// XXX This needs to actually reflect whether it is nested when we
|
|
|
|
// support using CSSRuleList in CSSGroupingRules.
|
|
|
|
bool nested = false;
|
|
|
|
uint16_t type;
|
|
|
|
nsresult rv = Servo_CssRules_InsertRule(mRawRules, mStyleSheet->RawSheet(),
|
|
|
|
&rule, aIndex, nested, &type);
|
|
|
|
if (!NS_FAILED(rv)) {
|
2016-11-30 06:36:00 +03:00
|
|
|
mRules.InsertElementAt(aIndex, type);
|
2016-11-29 14:59:55 +03:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ServoCSSRuleList::DeleteRule(uint32_t aIndex)
|
|
|
|
{
|
|
|
|
nsresult rv = Servo_CssRules_DeleteRule(mRawRules, aIndex);
|
|
|
|
if (!NS_FAILED(rv)) {
|
2017-02-08 06:19:08 +03:00
|
|
|
uintptr_t rule = mRules[aIndex];
|
|
|
|
if (rule > kMaxRuleType) {
|
|
|
|
CastToPtr(rule)->Release();
|
|
|
|
}
|
2016-11-29 14:59:55 +03:00
|
|
|
mRules.RemoveElementAt(aIndex);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-11-23 02:26:20 +03:00
|
|
|
ServoCSSRuleList::~ServoCSSRuleList()
|
|
|
|
{
|
|
|
|
EnumerateInstantiatedRules([](css::Rule* rule) { rule->Release(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|