use std::collections::HashSet;
use std::ffi::c_void;
use std::fmt;
use embedder_traits::UntrustedNodeAddress;
use js::rust::HandleValue;
use layout_api::ElementsFromPointFlags;
use rustc_hash::FxBuildHasher;
use script_bindings::codegen::GenericBindings::DocumentBinding::DocumentMethods;
use script_bindings::codegen::GenericBindings::WindowBinding::WindowMethods;
use script_bindings::error::{Error, ErrorResult};
use script_bindings::script_runtime::{CanGc, JSContext};
use servo_arc::Arc;
use servo_config::pref;
use style::media_queries::MediaList;
use style::shared_lock::{SharedRwLock as StyleSharedRwLock, SharedRwLockReadGuard};
use style::stylesheets::scope_rule::ImplicitScopeRoot;
use style::stylesheets::{Stylesheet, StylesheetContents};
use stylo_atoms::Atom;
use webrender_api::units::LayoutPoint;
use crate::dom::Document;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::NodeBinding::GetRootNodeOptions;
use crate::dom::bindings::codegen::Bindings::NodeBinding::Node_Binding::NodeMethods;
use crate::dom::bindings::codegen::Bindings::ShadowRootBinding::ShadowRootMethods;
use crate::dom::bindings::conversions::{ConversionResult, SafeFromJSValConvertible};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::num::Finite;
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::trace::HashMapTracedValues;
use crate::dom::css::stylesheetlist::StyleSheetListOwner;
use crate::dom::element::Element;
use crate::dom::node::{self, Node, VecPreOrderInsertionHelper};
use crate::dom::shadowroot::ShadowRoot;
use crate::dom::types::{CSSStyleSheet, EventTarget};
use crate::dom::window::Window;
use crate::stylesheet_set::StylesheetSetRef;
#[derive(Clone, JSTraceable, MallocSizeOf)]
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
pub(crate) enum StylesheetSource {
Element(Dom<Element>),
Constructed(Dom<CSSStyleSheet>),
}
impl StylesheetSource {
pub(crate) fn get_cssom_object(&self) -> Option<DomRoot<CSSStyleSheet>> {
match self {
StylesheetSource::Element(el) => el.upcast::<Node>().get_cssom_stylesheet(),
StylesheetSource::Constructed(ss) => Some(ss.as_rooted()),
}
}
pub(crate) fn is_a_valid_owner(&self) -> bool {
match self {
StylesheetSource::Element(el) => el.as_stylesheet_owner().is_some(),
StylesheetSource::Constructed(ss) => ss.is_constructed(),
}
}
pub(crate) fn is_constructed(&self) -> bool {
matches!(self, StylesheetSource::Constructed(_))
}
}
#[derive(Clone, JSTraceable, MallocSizeOf)]
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
pub(crate) struct ServoStylesheetInDocument {
#[ignore_malloc_size_of = "Stylo"]
#[no_trace]
pub(crate) sheet: Arc<Stylesheet>,
pub(crate) owner: StylesheetSource,
}
impl stylo_malloc_size_of::MallocSizeOf for ServoStylesheetInDocument {
fn size_of(&self, ops: &mut stylo_malloc_size_of::MallocSizeOfOps) -> usize {
<ServoStylesheetInDocument as malloc_size_of::MallocSizeOf>::size_of(self, ops)
}
}
impl fmt::Debug for ServoStylesheetInDocument {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
self.sheet.fmt(formatter)
}
}
impl PartialEq for ServoStylesheetInDocument {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.sheet, &other.sheet)
}
}
impl ::style::stylesheets::StylesheetInDocument for ServoStylesheetInDocument {
fn enabled(&self) -> bool {
self.sheet.enabled()
}
fn media<'a>(&'a self, guard: &'a SharedRwLockReadGuard) -> Option<&'a MediaList> {
self.sheet.media(guard)
}
fn contents<'a>(&'a self, guard: &'a SharedRwLockReadGuard) -> &'a StylesheetContents {
self.sheet.contents(guard)
}
fn implicit_scope_root(&self) -> Option<ImplicitScopeRoot> {
None
}
}
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
#[derive(Clone, JSTraceable, MallocSizeOf)]
pub(crate) struct DocumentOrShadowRoot {
window: Dom<Window>,
}
impl DocumentOrShadowRoot {
pub(crate) fn new(window: &Window) -> Self {
Self {
window: Dom::from_ref(window),
}
}
#[expect(unsafe_code)]
pub(crate) fn element_from_point(
&self,
x: Finite<f64>,
y: Finite<f64>,
document_element: Option<DomRoot<Element>>,
has_browsing_context: bool,
) -> Option<DomRoot<Element>> {
let x = *x as f32;
let y = *y as f32;
let viewport = self.window.viewport_details().size;
if !has_browsing_context {
return None;
}
if x < 0.0 || y < 0.0 || x > viewport.width || y > viewport.height {
return None;
}
let results = self
.window
.elements_from_point_query(LayoutPoint::new(x, y), ElementsFromPointFlags::empty());
let Some(result) = results.first() else {
return document_element;
};
let address = UntrustedNodeAddress(result.node.0 as *const c_void);
let node = unsafe { node::from_untrusted_node_address(address) };
DomRoot::downcast::<Element>(node.clone()).or_else(|| {
let parent_node = node.GetParentNode()?;
if let Some(shadow_root) = parent_node.downcast::<ShadowRoot>() {
Some(shadow_root.Host())
} else {
node.GetParentElement()
}
})
}
#[expect(unsafe_code)]
pub(crate) fn elements_from_point(
&self,
x: Finite<f64>,
y: Finite<f64>,
document_element: Option<DomRoot<Element>>,
has_browsing_context: bool,
) -> Vec<DomRoot<Element>> {
let x = *x as f32;
let y = *y as f32;
let viewport = self.window.viewport_details().size;
if !has_browsing_context {
return vec![];
}
if x < 0.0 || y < 0.0 || x > viewport.width || y > viewport.height {
return vec![];
}
let nodes = self
.window
.elements_from_point_query(LayoutPoint::new(x, y), ElementsFromPointFlags::FindAll);
let mut elements: Vec<DomRoot<Element>> = nodes
.iter()
.flat_map(|result| {
let address = UntrustedNodeAddress(result.node.0 as *const c_void);
let node = unsafe { node::from_untrusted_node_address(address) };
DomRoot::downcast::<Element>(node)
})
.collect();
if let Some(root_element) = document_element {
if elements.last() != Some(&root_element) {
elements.push(root_element);
}
}
elements
}
pub(crate) fn active_element(&self, this: &Node) -> Option<DomRoot<Element>> {
let document = self.window.Document();
let candidate = document
.focus_handler()
.focused_area()
.dom_anchor(&document);
let candidate =
DomRoot::downcast::<Node>(candidate.upcast::<EventTarget>().retarget(this.upcast()))?;
if this != &*candidate.GetRootNode(&GetRootNodeOptions::empty()) {
return None;
}
if let Some(candidate) = DomRoot::downcast::<Element>(candidate.clone()) {
return Some(candidate);
}
assert!(candidate.is::<Document>());
if let Some(body) = document.GetBody() {
return Some(DomRoot::upcast(body));
}
if let Some(document_element) = document.GetDocumentElement() {
return Some(document_element);
}
None
}
#[cfg_attr(crown, expect(crown::unrooted_must_root))] pub(crate) fn remove_stylesheet(
owner: StylesheetSource,
s: &Arc<Stylesheet>,
mut stylesheets: StylesheetSetRef<ServoStylesheetInDocument>,
) {
let guard = s.shared_lock.read();
stylesheets.remove_stylesheet(
None,
ServoStylesheetInDocument {
sheet: s.clone(),
owner,
},
&guard,
);
}
#[cfg_attr(crown, expect(crown::unrooted_must_root))] pub(crate) fn add_stylesheet(
owner: StylesheetSource,
mut stylesheets: StylesheetSetRef<ServoStylesheetInDocument>,
sheet: Arc<Stylesheet>,
insertion_point: Option<ServoStylesheetInDocument>,
style_shared_lock: &StyleSharedRwLock,
) {
debug_assert!(owner.is_a_valid_owner(), "Wat");
if owner.is_constructed() && !pref!(dom_adoptedstylesheet_enabled) {
return;
}
let sheet = ServoStylesheetInDocument { sheet, owner };
let guard = style_shared_lock.read();
match insertion_point {
Some(ip) => {
stylesheets.insert_stylesheet_before(None, sheet, ip, &guard);
},
None => {
stylesheets.append_stylesheet(None, sheet, &guard);
},
}
}
pub(crate) fn unregister_named_element(
&self,
id_map: &DomRefCell<HashMapTracedValues<Atom, Vec<Dom<Element>>, FxBuildHasher>>,
to_unregister: &Element,
id: &Atom,
) {
debug!(
"Removing named element {:p}: {:p} id={}",
self, to_unregister, id
);
let mut id_map = id_map.borrow_mut();
let is_empty = match id_map.get_mut(id) {
None => false,
Some(elements) => {
let position = elements
.iter()
.position(|element| &**element == to_unregister)
.expect("This element should be in registered.");
elements.remove(position);
elements.is_empty()
},
};
if is_empty {
id_map.remove(id);
}
}
pub(crate) fn register_named_element(
&self,
id_map: &DomRefCell<HashMapTracedValues<Atom, Vec<Dom<Element>>, FxBuildHasher>>,
element: &Element,
id: &Atom,
root: DomRoot<Node>,
) {
debug!("Adding named element {:p}: {:p} id={}", self, element, id);
assert!(
element.upcast::<Node>().is_in_a_document_tree() ||
element.upcast::<Node>().is_in_a_shadow_tree()
);
assert!(!id.is_empty());
let mut id_map = id_map.borrow_mut();
let elements = id_map.entry(id.clone()).or_default();
elements.insert_pre_order(element, &root);
}
fn set_adopted_stylesheet(
adopted_stylesheets: &mut Vec<Dom<CSSStyleSheet>>,
incoming_stylesheets: &[Dom<CSSStyleSheet>],
owner: &StyleSheetListOwner,
) -> ErrorResult {
if !pref!(dom_adoptedstylesheet_enabled) {
return Ok(());
}
let owner_doc = match owner {
StyleSheetListOwner::Document(doc) => doc,
StyleSheetListOwner::ShadowRoot(root) => root.owner_doc(),
};
for sheet in incoming_stylesheets.iter() {
if !sheet.constructor_document_matches(owner_doc) {
return Err(Error::NotAllowed(None));
}
}
let mut stylesheet_remove_set = HashSet::with_capacity(adopted_stylesheets.len());
for sheet_to_remove in adopted_stylesheets.iter() {
if stylesheet_remove_set.insert(sheet_to_remove) {
owner.remove_stylesheet(
StylesheetSource::Constructed(sheet_to_remove.clone()),
&sheet_to_remove.style_stylesheet(),
);
sheet_to_remove.remove_adopter(owner);
}
}
let mut stylesheet_add_set = HashSet::with_capacity(incoming_stylesheets.len());
for sheet in incoming_stylesheets.iter() {
if !stylesheet_add_set.insert(sheet) {
owner.remove_stylesheet(
StylesheetSource::Constructed(sheet.clone()),
&sheet.style_stylesheet(),
);
} else {
sheet.add_adopter(owner.clone());
}
owner.append_constructed_stylesheet(sheet);
}
*adopted_stylesheets = incoming_stylesheets.to_vec();
Ok(())
}
pub(crate) fn set_adopted_stylesheet_from_jsval(
context: JSContext,
adopted_stylesheets: &mut Vec<Dom<CSSStyleSheet>>,
incoming_value: HandleValue,
owner: &StyleSheetListOwner,
can_gc: CanGc,
) -> ErrorResult {
let maybe_stylesheets =
Vec::<DomRoot<CSSStyleSheet>>::safe_from_jsval(context, incoming_value, (), can_gc);
match maybe_stylesheets {
Ok(ConversionResult::Success(stylesheets)) => {
rooted_vec!(let stylesheets <- stylesheets.iter().map(|s| s.as_traced()));
DocumentOrShadowRoot::set_adopted_stylesheet(
adopted_stylesheets,
&stylesheets,
owner,
)
},
Ok(ConversionResult::Failure(msg)) => Err(Error::Type(msg.into_owned())),
Err(_) => Err(Error::Type(
c"The provided value is not a sequence of 'CSSStylesheet'.".to_owned(),
)),
}
}
}