Add CSSOM support for CSS layers (#31481)

Instead of just crashing.
This commit is contained in:
Oriol Brufau 2024-03-03 13:47:39 +01:00 committed by GitHub
parent 845f503c34
commit 06aeeeb1f3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 298 additions and 77 deletions

View file

@ -5,8 +5,11 @@
use dom_struct::dom_struct;
use servo_arc::Arc;
use style::shared_lock::{Locked, ToCssWithGuard};
use style::stylesheets::import_rule::ImportLayer;
use style::stylesheets::ImportRule;
use style_traits::ToCss;
use crate::dom::bindings::codegen::Bindings::CSSImportRuleBinding::CSSImportRuleMethods;
use crate::dom::bindings::reflector::reflect_dom_object;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
@ -60,3 +63,15 @@ impl SpecificCSSRule for CSSImportRule {
.into()
}
}
impl CSSImportRuleMethods for CSSImportRule {
/// <https://drafts.csswg.org/cssom-1/#dom-cssimportrule-layername>
fn GetLayerName(&self) -> Option<DOMString> {
let guard = self.cssrule.shared_lock().read();
match &self.import_rule.read_with(&guard).layer {
ImportLayer::None => None,
ImportLayer::Anonymous => Some(DOMString::new()),
ImportLayer::Named(name) => Some(DOMString::from_string(name.to_css_string())),
}
}
}

View file

@ -0,0 +1,78 @@
/* 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 https://mozilla.org/MPL/2.0/. */
use dom_struct::dom_struct;
use servo_arc::Arc;
use style::shared_lock::ToCssWithGuard;
use style::stylesheets::LayerBlockRule;
use style_traits::ToCss;
use crate::dom::bindings::codegen::Bindings::CSSLayerBlockRuleBinding::CSSLayerBlockRuleMethods;
use crate::dom::bindings::reflector::reflect_dom_object;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::cssgroupingrule::CSSGroupingRule;
use crate::dom::cssrule::SpecificCSSRule;
use crate::dom::cssstylesheet::CSSStyleSheet;
use crate::dom::window::Window;
#[dom_struct]
pub struct CSSLayerBlockRule {
cssgroupingrule: CSSGroupingRule,
#[ignore_malloc_size_of = "Arc"]
#[no_trace]
layerblockrule: Arc<LayerBlockRule>,
}
impl CSSLayerBlockRule {
pub fn new_inherited(
parent_stylesheet: &CSSStyleSheet,
layerblockrule: Arc<LayerBlockRule>,
) -> CSSLayerBlockRule {
CSSLayerBlockRule {
cssgroupingrule: CSSGroupingRule::new_inherited(
parent_stylesheet,
layerblockrule.rules.clone(),
),
layerblockrule,
}
}
#[allow(crown::unrooted_must_root)]
pub fn new(
window: &Window,
parent_stylesheet: &CSSStyleSheet,
layerblockrule: Arc<LayerBlockRule>,
) -> DomRoot<CSSLayerBlockRule> {
reflect_dom_object(
Box::new(CSSLayerBlockRule::new_inherited(
parent_stylesheet,
layerblockrule,
)),
window,
)
}
}
impl SpecificCSSRule for CSSLayerBlockRule {
fn ty(&self) -> u16 {
0
}
fn get_css(&self) -> DOMString {
let guard = self.cssgroupingrule.shared_lock().read();
self.layerblockrule.to_css_string(&guard).into()
}
}
impl CSSLayerBlockRuleMethods for CSSLayerBlockRule {
/// <https://drafts.csswg.org/css-cascade-5/#dom-csslayerblockrule-name>
fn Name(&self) -> DOMString {
if let Some(name) = &self.layerblockrule.name {
DOMString::from_string(name.to_css_string())
} else {
DOMString::new()
}
}
}

View file

@ -0,0 +1,79 @@
/* 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 https://mozilla.org/MPL/2.0/. */
use dom_struct::dom_struct;
use js::jsval::JSVal;
use servo_arc::Arc;
use style::shared_lock::ToCssWithGuard;
use style::stylesheets::LayerStatementRule;
use style_traits::ToCss;
use crate::dom::bindings::codegen::Bindings::CSSLayerStatementRuleBinding::CSSLayerStatementRuleMethods;
use crate::dom::bindings::reflector::reflect_dom_object;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::utils::to_frozen_array;
use crate::dom::cssrule::{CSSRule, SpecificCSSRule};
use crate::dom::cssstylesheet::CSSStyleSheet;
use crate::dom::window::Window;
use crate::script_runtime::JSContext as SafeJSContext;
#[dom_struct]
pub struct CSSLayerStatementRule {
cssrule: CSSRule,
#[ignore_malloc_size_of = "Arc"]
#[no_trace]
layerstatementrule: Arc<LayerStatementRule>,
}
impl CSSLayerStatementRule {
pub fn new_inherited(
parent_stylesheet: &CSSStyleSheet,
layerstatementrule: Arc<LayerStatementRule>,
) -> CSSLayerStatementRule {
CSSLayerStatementRule {
cssrule: CSSRule::new_inherited(parent_stylesheet),
layerstatementrule,
}
}
#[allow(crown::unrooted_must_root)]
pub fn new(
window: &Window,
parent_stylesheet: &CSSStyleSheet,
layerstatementrule: Arc<LayerStatementRule>,
) -> DomRoot<CSSLayerStatementRule> {
reflect_dom_object(
Box::new(CSSLayerStatementRule::new_inherited(
parent_stylesheet,
layerstatementrule,
)),
window,
)
}
}
impl SpecificCSSRule for CSSLayerStatementRule {
fn ty(&self) -> u16 {
0
}
fn get_css(&self) -> DOMString {
let guard = self.cssrule.shared_lock().read();
self.layerstatementrule.to_css_string(&guard).into()
}
}
impl CSSLayerStatementRuleMethods for CSSLayerStatementRule {
/// <https://drafts.csswg.org/css-cascade-5/#dom-csslayerstatementrule-namelist>
fn NameList(&self, cx: SafeJSContext) -> JSVal {
let names: Vec<DOMString> = self
.layerstatementrule
.names
.iter()
.map(|name| DOMString::from_string(name.to_css_string()))
.collect();
to_frozen_array(names.as_slice(), cx)
}
}

View file

@ -17,6 +17,8 @@ use crate::dom::cssfontfacerule::CSSFontFaceRule;
use crate::dom::cssimportrule::CSSImportRule;
use crate::dom::csskeyframerule::CSSKeyframeRule;
use crate::dom::csskeyframesrule::CSSKeyframesRule;
use crate::dom::csslayerblockrule::CSSLayerBlockRule;
use crate::dom::csslayerstatementrule::CSSLayerStatementRule;
use crate::dom::cssmediarule::CSSMediaRule;
use crate::dom::cssnamespacerule::CSSNamespaceRule;
use crate::dom::cssstylerule::CSSStyleRule;
@ -62,6 +64,10 @@ impl CSSRule {
rule as &dyn SpecificCSSRule
} else if let Some(rule) = self.downcast::<CSSSupportsRule>() {
rule as &dyn SpecificCSSRule
} else if let Some(rule) = self.downcast::<CSSLayerBlockRule>() {
rule as &dyn SpecificCSSRule
} else if let Some(rule) = self.downcast::<CSSLayerStatementRule>() {
rule as &dyn SpecificCSSRule
} else {
unreachable!()
}
@ -102,10 +108,14 @@ impl CSSRule {
StyleCssRule::Page(_) => unreachable!(),
StyleCssRule::Container(_) => unimplemented!(), // TODO
StyleCssRule::Document(_) => unimplemented!(), // TODO
StyleCssRule::LayerBlock(_) => unimplemented!(), // TODO
StyleCssRule::LayerStatement(_) => unimplemented!(), // TODO
StyleCssRule::LayerBlock(s) => {
DomRoot::upcast(CSSLayerBlockRule::new(window, parent_stylesheet, s))
},
StyleCssRule::LayerStatement(s) => {
DomRoot::upcast(CSSLayerStatementRule::new(window, parent_stylesheet, s))
},
StyleCssRule::FontPaletteValues(_) => unimplemented!(), // TODO
StyleCssRule::Property(_) => unimplemented!(), // TODO
StyleCssRule::Property(_) => unimplemented!(), // TODO
}
}

View file

@ -263,6 +263,8 @@ pub mod cssgroupingrule;
pub mod cssimportrule;
pub mod csskeyframerule;
pub mod csskeyframesrule;
pub mod csslayerblockrule;
pub mod csslayerstatementrule;
pub mod cssmediarule;
pub mod cssnamespacerule;
pub mod cssrule;

View file

@ -8,4 +8,5 @@ interface CSSImportRule : CSSRule {
// readonly attribute DOMString href;
// [SameObject, PutForwards=mediaText] readonly attribute MediaList media;
// [SameObject] readonly attribute CSSStyleSheet styleSheet;
readonly attribute DOMString? layerName;
};

View file

@ -0,0 +1,9 @@
/* 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 https://mozilla.org/MPL/2.0/. */
// https://drafts.csswg.org/css-cascade-5/#the-csslayerblockrule-interface
[Exposed=Window]
interface CSSLayerBlockRule : CSSGroupingRule {
readonly attribute DOMString name;
};

View file

@ -0,0 +1,9 @@
/* 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 https://mozilla.org/MPL/2.0/. */
// https://drafts.csswg.org/css-cascade-5/#the-csslayerstatementrule-interface
[Exposed=Window]
interface CSSLayerStatementRule : CSSRule {
readonly attribute /*FrozenArray<ResizeObserverSize>*/any nameList;
};