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
24use style::values::AtomIdent;
25impl<'ld> ::style::dom::TDocument for ServoLayoutDocument<'ld> {
26    type ConcreteNode = ServoLayoutNode<'ld>;
27
28    fn as_node(&self) -> Self::ConcreteNode {
29        ServoLayoutNode::from_layout_dom(self.document.upcast())
30    }
31
32    fn quirks_mode(&self) -> QuirksMode {
33        self.document.quirks_mode()
34    }
35
36    fn is_html_document(&self) -> bool {
37        self.document.is_html_document_for_layout()
38    }
39
40    fn shared_lock(&self) -> &StyleSharedRwLock {
41        self.document.style_shared_lock()
42    }
43
44    fn elements_with_id<'a>(&self, id: &AtomIdent) -> Result<&'a [ServoLayoutElement<'ld>], ()>
45    where
46        Self: 'a,
47    {
48        let elements_with_id = self.document.elements_with_id(&id.0);
49
50        // SAFETY: ServoLayoutElement is known to have the same layout and alignment as LayoutDom<Element>
51        let result = unsafe {
52            std::slice::from_raw_parts(
53                elements_with_id.as_ptr() as *const ServoLayoutElement<'ld>,
54                elements_with_id.len(),
55            )
56        };
57        Ok(result)
58    }
59}
60
61impl<'ld> ServoLayoutDocument<'ld> {
62    pub fn root_element(&self) -> Option<ServoLayoutElement<'ld>> {
63        self.as_node()
64            .dom_children()
65            .flat_map(|n| n.as_element())
66            .next()
67    }
68
69    pub fn style_shared_lock(&self) -> &StyleSharedRwLock {
70        self.document.style_shared_lock()
71    }
72
73    pub fn shadow_roots(&self) -> Vec<ServoShadowRoot<'_>> {
74        unsafe {
75            self.document
76                .shadow_roots()
77                .iter()
78                .map(|sr| {
79                    debug_assert!(sr.upcast::<Node>().get_flag(NodeFlags::IS_CONNECTED));
80                    ServoShadowRoot::from_layout_dom(*sr)
81                })
82                .collect()
83        }
84    }
85
86    pub fn flush_shadow_roots_stylesheets(
87        &self,
88        stylist: &mut Stylist,
89        guard: &StyleSharedRwLockReadGuard,
90    ) {
91        unsafe {
92            if !self.document.shadow_roots_styles_changed() {
93                return;
94            }
95            self.document.flush_shadow_roots_stylesheets();
96            for shadow_root in self.shadow_roots() {
97                shadow_root.flush_stylesheets(stylist, guard);
98            }
99        }
100    }
101
102    pub(crate) fn from_layout_dom(document: LayoutDom<'ld, Document>) -> Self {
103        ServoLayoutDocument { document }
104    }
105}