script/layout_dom/
servo_dangerous_style_shadow_root.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5#![deny(missing_docs)]
6
7use std::fmt;
8
9use style::dom::TShadowRoot;
10use style::stylist::CascadeData;
11
12use crate::dom::bindings::root::LayoutDom;
13use crate::dom::shadowroot::ShadowRoot;
14use crate::layout_dom::{ServoDangerousStyleElement, ServoDangerousStyleNode};
15
16/// A wrapper around [`LayoutDom<_, ShadowRoot>`] to be used with `stylo` and `selectors`.
17///
18/// Note: This should only be used for `stylo` or `selectors interaction.
19#[derive(Clone, Copy, PartialEq)]
20pub struct ServoDangerousStyleShadowRoot<'dom> {
21    /// The wrapped private DOM ShadowRoot.
22    shadow_root: LayoutDom<'dom, ShadowRoot>,
23}
24
25impl fmt::Debug for ServoDangerousStyleShadowRoot<'_> {
26    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27        self.as_node().fmt(f)
28    }
29}
30
31impl<'dom> From<LayoutDom<'dom, ShadowRoot>> for ServoDangerousStyleShadowRoot<'dom> {
32    fn from(shadow_root: LayoutDom<'dom, ShadowRoot>) -> Self {
33        Self { shadow_root }
34    }
35}
36
37impl<'dom> TShadowRoot for ServoDangerousStyleShadowRoot<'dom> {
38    type ConcreteNode = ServoDangerousStyleNode<'dom>;
39
40    fn as_node(&self) -> ServoDangerousStyleNode<'dom> {
41        self.shadow_root.upcast().into()
42    }
43
44    fn host(&self) -> ServoDangerousStyleElement<'dom> {
45        ServoDangerousStyleElement {
46            element: self.shadow_root.get_host_for_layout(),
47        }
48    }
49
50    fn style_data<'a>(&self) -> Option<&'a CascadeData>
51    where
52        Self: 'a,
53    {
54        Some(self.shadow_root.get_style_data_for_layout())
55    }
56}