Skip to content

Commit

Permalink
Bug 1882581: Implement @scope parsing. r=firefox-style-system-revie…
Browse files Browse the repository at this point in the history
…wers,saschanaz,emilio

Differential Revision: https://phabricator.services.mozilla.com/D203153
  • Loading branch information
dshin-moz committed Mar 19, 2024
1 parent 51479c8 commit 1e31a04
Show file tree
Hide file tree
Showing 39 changed files with 448 additions and 208 deletions.
14 changes: 14 additions & 0 deletions dom/webidl/CSSScopeRule.webidl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/.
*
* The origin of this IDL file is
* https://drafts.csswg.org/css-cascade-6/#the-cssscoperule-interface
*/

[Exposed=Window, Pref="layout.css.at-scope.enabled"]
interface CSSScopeRule : CSSGroupingRule {
readonly attribute UTF8String? start;
readonly attribute UTF8String? end;
};
1 change: 1 addition & 0 deletions dom/webidl/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ WEBIDL_FILES = [
"CSSPseudoElement.webidl",
"CSSRule.webidl",
"CSSRuleList.webidl",
"CSSScopeRule.webidl",
"CSSStyleDeclaration.webidl",
"CSSStyleRule.webidl",
"CSSStyleSheet.webidl",
Expand Down
1 change: 1 addition & 0 deletions layout/inspector/InspectorUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ static uint32_t CollectAtRules(ServoCSSRuleList& aRuleList,
case StyleCssRuleType::CounterStyle:
case StyleCssRuleType::FontFeatureValues:
case StyleCssRuleType::FontPaletteValues:
case StyleCssRuleType::Scope:
break;
}

Expand Down
6 changes: 4 additions & 2 deletions layout/inspector/ServoStyleRuleMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ void ServoStyleRuleMap::RuleRemoved(StyleSheet& aStyleSheet,
case StyleCssRuleType::Supports:
case StyleCssRuleType::LayerBlock:
case StyleCssRuleType::Container:
case StyleCssRuleType::Document: {
case StyleCssRuleType::Document:
case StyleCssRuleType::Scope: {
// See the comment in SheetRemoved.
mTable.Clear();
break;
Expand Down Expand Up @@ -124,7 +125,8 @@ void ServoStyleRuleMap::FillTableFromRule(css::Rule& aRule) {
case StyleCssRuleType::Media:
case StyleCssRuleType::Supports:
case StyleCssRuleType::Container:
case StyleCssRuleType::Document: {
case StyleCssRuleType::Document:
case StyleCssRuleType::Scope: {
auto& rule = static_cast<css::GroupRule&>(aRule);
FillTableFromRuleList(*rule.CssRules());
break;
Expand Down
66 changes: 66 additions & 0 deletions layout/style/CSSScopeRule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* -*- 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/CSSScopeRule.h"
#include "mozilla/dom/CSSScopeRuleBinding.h"
#include "mozilla/ServoBindings.h"

namespace mozilla::dom {

CSSScopeRule::CSSScopeRule(RefPtr<StyleScopeRule> aRawRule, StyleSheet* aSheet,
css::Rule* aParentRule, uint32_t aLine,
uint32_t aColumn)
: css::GroupRule(aSheet, aParentRule, aLine, aColumn),
mRawRule(std::move(aRawRule)) {}

NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(CSSScopeRule, css::GroupRule)

// QueryInterface implementation for SupportsRule

#ifdef DEBUG
void CSSScopeRule::List(FILE* out, int32_t aIndent) const {
nsAutoCString str;
for (int32_t i = 0; i < aIndent; i++) {
str.AppendLiteral(" ");
}
Servo_ScopeRule_Debug(mRawRule, &str);
fprintf_stderr(out, "%s\n", str.get());
}
#endif

StyleCssRuleType CSSScopeRule::Type() const { return StyleCssRuleType::Scope; }

already_AddRefed<StyleLockedCssRules> CSSScopeRule::GetOrCreateRawRules() {
return Servo_ScopeRule_GetRules(mRawRule).Consume();
}

void CSSScopeRule::SetRawAfterClone(RefPtr<StyleScopeRule> aRaw) {
mRawRule = std::move(aRaw);
css::GroupRule::DidSetRawAfterClone();
}

void CSSScopeRule::GetCssText(nsACString& aCssText) const {
Servo_ScopeRule_GetCssText(mRawRule.get(), &aCssText);
}

void CSSScopeRule::GetStart(nsACString& aStart) const {
Servo_ScopeRule_GetStart(mRawRule.get(), &aStart);
}

void CSSScopeRule::GetEnd(nsACString& aEnd) const {
Servo_ScopeRule_GetEnd(mRawRule.get(), &aEnd);
}

size_t CSSScopeRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
return aMallocSizeOf(this);
}

JSObject* CSSScopeRule::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return CSSScopeRule_Binding::Wrap(aCx, this, aGivenProto);
}

} // namespace mozilla::dom
49 changes: 49 additions & 0 deletions layout/style/CSSScopeRule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* -*- 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/. */

#ifndef CSSScopeRule_h___
#define CSSScopeRule_h___

#include "mozilla/css/GroupRule.h"
#include "mozilla/ServoBindingTypes.h"

namespace mozilla::dom {

class CSSScopeRule final : public css::GroupRule {
public:
CSSScopeRule(RefPtr<StyleScopeRule> aRawRule, StyleSheet* aSheet,
css::Rule* aParentRule, uint32_t aLine, uint32_t aColumn);

NS_DECL_ISUPPORTS_INHERITED

#ifdef DEBUG
void List(FILE* out = stdout, int32_t aIndent = 0) const final;
#endif

StyleScopeRule* Raw() const { return mRawRule; }
void SetRawAfterClone(RefPtr<StyleScopeRule>);

already_AddRefed<StyleLockedCssRules> GetOrCreateRawRules() final;

// WebIDL interface
StyleCssRuleType Type() const final;
void GetCssText(nsACString& aCssText) const final;

void GetStart(nsACString& aStart) const;
void GetEnd(nsACString& aEnd) const;

size_t SizeOfIncludingThis(MallocSizeOf) const override;
JSObject* WrapObject(JSContext*, JS::Handle<JSObject*>) override;

private:
~CSSScopeRule() = default;

RefPtr<StyleScopeRule> mRawRule;
};

} // namespace mozilla::dom

#endif
14 changes: 7 additions & 7 deletions layout/style/Rule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ void Rule::AssertParentRuleType() {
// this->Type() here since it's virtual.
if (mParentRule) {
auto type = mParentRule->Type();
MOZ_ASSERT(type == StyleCssRuleType::Media ||
type == StyleCssRuleType::Style ||
type == StyleCssRuleType::Document ||
type == StyleCssRuleType::Supports ||
type == StyleCssRuleType::Keyframes ||
type == StyleCssRuleType::LayerBlock ||
type == StyleCssRuleType::Container);
MOZ_ASSERT(
type == StyleCssRuleType::Media || type == StyleCssRuleType::Style ||
type == StyleCssRuleType::Document ||
type == StyleCssRuleType::Supports ||
type == StyleCssRuleType::Keyframes ||
type == StyleCssRuleType::LayerBlock ||
type == StyleCssRuleType::Container || type == StyleCssRuleType::Scope);
}
}
#endif
Expand Down
1 change: 1 addition & 0 deletions layout/style/ServoBindingTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ UNLOCKED_RULE_TYPE(Supports)
UNLOCKED_RULE_TYPE(Document)
UNLOCKED_RULE_TYPE(FontFeatureValues)
UNLOCKED_RULE_TYPE(FontPaletteValues)
UNLOCKED_RULE_TYPE(Scope)

SERVO_ARC_TYPE(AnimationValue, mozilla::StyleAnimationValue)
SERVO_ARC_TYPE(ComputedStyle, mozilla::ComputedStyle)
Expand Down
1 change: 1 addition & 0 deletions layout/style/ServoBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ BASIC_RULE_FUNCS_UNLOCKED(FontPaletteValues)
BASIC_RULE_FUNCS_LOCKED(FontFace)
BASIC_RULE_FUNCS_LOCKED(CounterStyle)
GROUP_RULE_FUNCS_UNLOCKED(Container)
GROUP_RULE_FUNCS_UNLOCKED(Scope)

#undef GROUP_RULE_FUNCS_LOCKED
#undef GROUP_RULE_FUNCS_UNLOCKED
Expand Down
3 changes: 3 additions & 0 deletions layout/style/ServoCSSRuleList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "mozilla/dom/CSSPropertyRule.h"
#include "mozilla/dom/CSSStyleRule.h"
#include "mozilla/dom/CSSSupportsRule.h"
#include "mozilla/dom/CSSScopeRule.h"
#include "mozilla/IntegerRange.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/StyleSheet.h"
Expand Down Expand Up @@ -98,6 +99,7 @@ css::Rule* ServoCSSRuleList::GetRule(uint32_t aIndex) {
CASE_RULE_UNLOCKED(LayerBlock, LayerBlock)
CASE_RULE_UNLOCKED(LayerStatement, LayerStatement)
CASE_RULE_UNLOCKED(Container, Container)
CASE_RULE_UNLOCKED(Scope, Scope)
#undef CASE_RULE_LOCKED
#undef CASE_RULE_UNLOCKED
#undef CASE_RULE_WITH_PREFIX
Expand Down Expand Up @@ -276,6 +278,7 @@ void ServoCSSRuleList::SetRawContents(RefPtr<StyleLockedCssRules> aNewRules,
RULE_CASE_UNLOCKED(LayerBlock, LayerBlock)
RULE_CASE_UNLOCKED(LayerStatement, LayerStatement)
RULE_CASE_UNLOCKED(Container, Container)
RULE_CASE_UNLOCKED(Scope, Scope)
case StyleCssRuleType::Keyframe:
MOZ_ASSERT_UNREACHABLE("keyframe rule cannot be here");
break;
Expand Down
1 change: 1 addition & 0 deletions layout/style/ServoStyleConstsInlines.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ template struct StyleStrong<StyleFontPaletteValuesRule>;
template struct StyleStrong<StyleLockedFontFaceRule>;
template struct StyleStrong<StyleLockedCounterStyleRule>;
template struct StyleStrong<StyleContainerRule>;
template struct StyleStrong<StyleScopeRule>;

template <typename T>
inline void StyleOwnedSlice<T>::Clear() {
Expand Down
2 changes: 2 additions & 0 deletions layout/style/ServoStyleSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "mozilla/dom/CSSNamespaceRule.h"
#include "mozilla/dom/CSSPageRule.h"
#include "mozilla/dom/CSSPropertyRule.h"
#include "mozilla/dom/CSSScopeRule.h"
#include "mozilla/dom/CSSSupportsRule.h"
#include "mozilla/dom/FontFaceSet.h"
#include "mozilla/dom/Element.h"
Expand Down Expand Up @@ -1002,6 +1003,7 @@ void ServoStyleSet::RuleChangedInternal(StyleSheet& aSheet, css::Rule& aRule,
CASE_FOR(LayerBlock, LayerBlock)
CASE_FOR(LayerStatement, LayerStatement)
CASE_FOR(Container, Container)
CASE_FOR(Scope, Scope)
// @namespace can only be inserted / removed when there are only other
// @namespace and @import rules, and can't be mutated.
case StyleCssRuleType::Namespace:
Expand Down
2 changes: 2 additions & 0 deletions layout/style/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ EXPORTS.mozilla.dom += [
"CSSPageRule.h",
"CSSPropertyRule.h",
"CSSRuleList.h",
"CSSScopeRule.h",
"CSSStyleRule.h",
"CSSSupportsRule.h",
"CSSValue.h",
Expand Down Expand Up @@ -194,6 +195,7 @@ UNIFIED_SOURCES += [
"CSSPageRule.cpp",
"CSSPropertyRule.cpp",
"CSSRuleList.cpp",
"CSSScopeRule.cpp",
"CSSStyleRule.cpp",
"CSSSupportsRule.cpp",
"DeclarationBlock.cpp",
Expand Down
7 changes: 7 additions & 0 deletions modules/libpref/init/StaticPrefList.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8752,6 +8752,13 @@
mirror: always
rust: true

# Whether @scope rule is enabled
- name: layout.css.at-scope.enabled
type: RelaxedAtomicBool
value: false
mirror: always
rust: true

# Dictates whether or not the prefers contrast media query will be
# usable.
# true: prefers-contrast will toggle based on OS and browser settings.
Expand Down
17 changes: 17 additions & 0 deletions servo/components/selectors/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,23 @@ impl<Impl: SelectorImpl> SelectorList<Impl> {
)
}

pub fn parse_forgiving<'i, 't, P>(
parser: &P,
input: &mut CssParser<'i, 't>,
parse_relative: ParseRelative,
) -> Result<Self, ParseError<'i, P::Error>>
where
P: Parser<'i, Impl = Impl>,
{
Self::parse_with_state(
parser,
input,
SelectorParsingState::empty(),
ForgivingParsing::Yes,
parse_relative,
)
}

#[inline]
fn parse_with_state<'i, 't, P>(
parser: &P,
Expand Down
7 changes: 6 additions & 1 deletion servo/components/style/gecko/arc_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::stylesheets::keyframes_rule::Keyframe;
use crate::stylesheets::{
ContainerRule, CounterStyleRule, CssRules, DocumentRule, FontFaceRule, FontFeatureValuesRule,
FontPaletteValuesRule, ImportRule, KeyframesRule, LayerBlockRule, LayerStatementRule,
MediaRule, NamespaceRule, PageRule, PropertyRule, StyleRule, StylesheetContents, SupportsRule,
MediaRule, NamespaceRule, PageRule, PropertyRule, ScopeRule, StyleRule, StylesheetContents, SupportsRule,
};
use servo_arc::Arc;

Expand Down Expand Up @@ -169,3 +169,8 @@ impl_simple_arc_ffi!(
Servo_AnimationValue_AddRef,
Servo_AnimationValue_Release
);
impl_simple_arc_ffi!(
ScopeRule,
Servo_ScopeRule_AddRef,
Servo_ScopeRule_Release
);
5 changes: 5 additions & 0 deletions servo/components/style/invalidation/stylesheets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,11 @@ impl StylesheetInvalidationSet {
debug!(" > Found unsupported rule, marking the whole subtree invalid.");
self.invalidate_fully();
},
Scope(..) => {
// Addition/removal of @scope requires re-evaluation of scope proximity to properly
// figure out the styling order.
self.invalidate_fully();
},
}
}
}
Loading

0 comments on commit 1e31a04

Please sign in to comment.