script/layout_dom/
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
5use std::fmt;
6
7use style::dom::TShadowRoot;
8use style::shared_lock::SharedRwLockReadGuard as StyleSharedRwLockReadGuard;
9use style::stylist::{CascadeData, Stylist};
10
11use crate::dom::bindings::root::LayoutDom;
12use crate::dom::shadowroot::{LayoutShadowRootHelpers, ShadowRoot};
13use crate::layout_dom::{ServoLayoutElement, ServoLayoutNode};
14
15#[derive(Clone, Copy, PartialEq)]
16pub struct ServoShadowRoot<'dom> {
17    /// The wrapped private DOM ShadowRoot.
18    shadow_root: LayoutDom<'dom, ShadowRoot>,
19}
20
21impl fmt::Debug for ServoShadowRoot<'_> {
22    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23        self.as_node().fmt(f)
24    }
25}
26
27impl<'dom> TShadowRoot for ServoShadowRoot<'dom> {
28    type ConcreteNode = ServoLayoutNode<'dom>;
29
30    fn as_node(&self) -> Self::ConcreteNode {
31        ServoLayoutNode::from_layout_js(self.shadow_root.upcast())
32    }
33
34    fn host(&self) -> ServoLayoutElement<'dom> {
35        ServoLayoutElement::from_layout_js(self.shadow_root.get_host_for_layout())
36    }
37
38    fn style_data<'a>(&self) -> Option<&'a CascadeData>
39    where
40        Self: 'a,
41    {
42        Some(self.shadow_root.get_style_data_for_layout())
43    }
44}
45
46impl<'dom> ServoShadowRoot<'dom> {
47    pub(super) fn from_layout_js(shadow_root: LayoutDom<'dom, ShadowRoot>) -> Self {
48        ServoShadowRoot { shadow_root }
49    }
50
51    /// Flush the stylesheets for the underlying shadow root.
52    ///
53    /// # Safety
54    ///
55    /// This modifies a DOM object, so should care should be taken that only one
56    /// thread has a reference to this object.
57    pub unsafe fn flush_stylesheets(
58        &self,
59        stylist: &mut Stylist,
60        guard: &StyleSharedRwLockReadGuard,
61    ) {
62        unsafe {
63            self.shadow_root
64                .flush_stylesheets::<ServoLayoutElement>(stylist, guard)
65        }
66    }
67}