1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 * internal interface representing CSS style rules that contain other
9 * rules, such as @media rules
12 #include "mozilla/css/GroupRule.h"
14 #include "mozilla/dom/CSSRuleList.h"
16 #include "nsPrintfCString.h"
18 using namespace mozilla::dom
;
20 namespace mozilla::css
{
22 GroupRule::GroupRule(StyleSheet
* aSheet
, Rule
* aParentRule
,
23 uint32_t aLineNumber
, uint32_t aColumnNumber
)
24 : Rule(aSheet
, aParentRule
, aLineNumber
, aColumnNumber
) {}
26 GroupRule::~GroupRule() {
27 MOZ_ASSERT(!mSheet
, "SetStyleSheet should have been called");
29 mRuleList
->DropReferences();
33 NS_IMPL_ADDREF_INHERITED(GroupRule
, Rule
)
34 NS_IMPL_RELEASE_INHERITED(GroupRule
, Rule
)
36 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(GroupRule
)
37 NS_INTERFACE_MAP_END_INHERITING(Rule
)
39 bool GroupRule::IsCCLeaf() const {
40 if (!Rule::IsCCLeaf()) {
46 ServoCSSRuleList
* GroupRule::CssRules() {
48 // Lazily create the rule list since most style rules won't have child
51 new ServoCSSRuleList(GetOrCreateRawRules(), GetStyleSheet(), this);
56 NS_IMPL_CYCLE_COLLECTION_CLASS(GroupRule
)
58 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(GroupRule
, Rule
)
60 // If tmp has a style sheet (which can happen if it gets unlinked
61 // earlier than its owning style sheet), then we need to null out the
62 // style sheet pointer on descendants now, before we clear mRuleList.
63 tmp
->mRuleList
->DropReferences();
64 tmp
->mRuleList
= nullptr;
66 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
68 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(GroupRule
, Rule
)
69 ImplCycleCollectionTraverse(cb
, tmp
->mRuleList
, "mRuleList");
70 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
73 void GroupRule::List(FILE* out
, int32_t aIndent
) const {
74 // TODO list something reasonable?
79 void GroupRule::DropSheetReference() {
81 mRuleList
->DropSheetReference();
83 Rule::DropSheetReference();
86 uint32_t GroupRule::InsertRule(const nsACString
& aRule
, uint32_t aIndex
,
92 StyleSheet
* sheet
= GetStyleSheet();
93 if (NS_WARN_IF(!sheet
)) {
94 aRv
.Throw(NS_ERROR_FAILURE
);
98 uint32_t count
= StyleRuleCount();
100 aRv
.ThrowIndexSizeError(nsPrintfCString(
101 "Can't insert rule at index %u because rule list length is %u", aIndex
,
106 NS_ASSERTION(count
<= INT32_MAX
, "Too many style rules!");
108 nsresult rv
= sheet
->InsertRuleIntoGroup(aRule
, this, aIndex
);
116 void GroupRule::DeleteRule(uint32_t aIndex
, ErrorResult
& aRv
) {
121 StyleSheet
* sheet
= GetStyleSheet();
122 if (NS_WARN_IF(!sheet
)) {
123 aRv
.Throw(NS_ERROR_FAILURE
);
127 uint32_t count
= StyleRuleCount();
128 if (aIndex
>= count
) {
129 aRv
.ThrowIndexSizeError(nsPrintfCString(
130 "Index %u is too large for list of length %u", aIndex
, count
));
134 NS_ASSERTION(count
<= INT32_MAX
, "Too many style rules!");
136 nsresult rv
= sheet
->DeleteRuleFromGroup(this, aIndex
);
142 size_t GroupRule::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf
) const {
143 // TODO how to implement?
147 } // namespace mozilla::css