script/layout_dom/
document.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 selectors::matching::QuirksMode;
6use style::dom::{TDocument, TNode};
7use style::shared_lock::{
8    SharedRwLock as StyleSharedRwLock, SharedRwLockReadGuard as StyleSharedRwLockReadGuard,
9};
10use style::stylist::Stylist;
11
12use crate::dom::bindings::root::LayoutDom;
13use crate::dom::document::{Document, LayoutDocumentHelpers};
14use crate::dom::node::{LayoutNodeHelpers, Node, NodeFlags};
15use crate::layout_dom::{ServoLayoutElement, ServoLayoutNode, ServoShadowRoot};
16
17// A wrapper around documents that ensures ayout can only ever access safe properties.
18#[derive(Clone, Copy)]
19pub struct ServoLayoutDocument<'dom> {
20    /// The wrapped private DOM Document
21    document: LayoutDom<'dom, Document>,
22}
23
24impl<'ld> ::style::dom::TDocument for ServoLayoutDocument<'ld> {
25    type ConcreteNode = ServoLayoutNode<'ld>;
26
27    fn as_node(&self) -> Self::ConcreteNode {
28        ServoLayoutNode::from_layout_js(self.document.upcast())
29    }
30
31    fn quirks_mode(&self) -> QuirksMode {
32        self.document.quirks_mode()
33    }
34
35    fn is_html_document(&self) -> bool {
36        self.document.is_html_document_for_layout()
37    }
38
39    fn shared_lock(&self) -> &StyleSharedRwLock {
40        self.document.style_shared_lock()
41    }
42}
43
44impl<'ld> ServoLayoutDocument<'ld> {
45    pub fn root_element(&self) -> Option<ServoLayoutElement<'ld>> {
46        self.as_node()
47            .dom_children()
48            .flat_map(|n| n.as_element())
49            .next()
50    }
51
52    pub fn style_shared_lock(&self) -> &StyleSharedRwLock {
53        self.document.style_shared_lock()
54    }
55
56    pub fn shadow_roots(&self) -> Vec<ServoShadowRoot<'_>> {
57        unsafe {
58            self.document
59                .shadow_roots()
60                .iter()
61                .map(|sr| {
62                    debug_assert!(sr.upcast::<Node>().get_flag(NodeFlags::IS_CONNECTED));
63                    ServoShadowRoot::from_layout_js(*sr)
64                })
65                .collect()
66        }
67    }
68
69    pub fn flush_shadow_roots_stylesheets(
70        &self,
71        stylist: &mut Stylist,
72        guard: &StyleSharedRwLockReadGuard,
73    ) {
74        unsafe {
75            if !self.document.shadow_roots_styles_changed() {
76                return;
77            }
78            self.document.flush_shadow_roots_stylesheets();
79            for shadow_root in self.shadow_roots() {
80                shadow_root.flush_stylesheets(stylist, guard);
81            }
82        }
83    }
84
85    pub(crate) fn from_layout_js(document: LayoutDom<'ld, Document>) -> Self {
86        ServoLayoutDocument { document }
87    }
88}