#![expect(unsafe_code)]
use std::borrow::Cow;
use std::fmt::Debug;
use std::ops::Range;
use atomic_refcell::AtomicRefCell;
use fonts::TextByteRange;
use html5ever::{LocalName, Namespace};
use malloc_size_of_derive::MallocSizeOf;
use net_traits::image_cache::Image;
use pixels::ImageMetadata;
use servo_arc::Arc;
use servo_base::id::{BrowsingContextId, PipelineId};
use servo_url::ServoUrl;
use style::attr::AttrValue;
use style::context::SharedStyleContext;
use style::data::ElementDataRef;
use style::dom::{LayoutIterator, NodeInfo, OpaqueNode, TElement, TNode};
use style::properties::ComputedValues;
use style::selector_parser::{PseudoElement, PseudoElementCascadeType, SelectorImpl};
use style::stylist::RuleInclusion;
use crate::{
GenericLayoutData, GenericLayoutDataTrait, HTMLCanvasData, HTMLMediaData, LayoutNodeType,
SVGElementData, StyleData,
};
pub trait LayoutDataTrait: GenericLayoutDataTrait + Default + Send + Sync + 'static {}
pub trait LayoutNode<'dom>: Copy + Debug + TNode + Send + Sync {
type ConcreteThreadSafeLayoutNode: ThreadSafeLayoutNode<'dom>;
fn to_threadsafe(&self) -> Self::ConcreteThreadSafeLayoutNode;
fn type_id(&self) -> LayoutNodeType;
unsafe fn initialize_style_and_layout_data<RequestedLayoutDataType: LayoutDataTrait>(&self);
fn style_data(&self) -> Option<&'dom StyleData>;
fn layout_data(&self) -> Option<&'dom GenericLayoutData>;
fn rev_children(self) -> LayoutIterator<ReverseChildrenIterator<Self>> {
LayoutIterator(ReverseChildrenIterator {
current: self.last_child(),
})
}
fn traverse_preorder(self) -> TreeIterator<Self> {
TreeIterator::new(self)
}
fn is_connected(&self) -> bool;
}
pub struct ReverseChildrenIterator<ConcreteNode> {
current: Option<ConcreteNode>,
}
impl<'dom, ConcreteNode> Iterator for ReverseChildrenIterator<ConcreteNode>
where
ConcreteNode: LayoutNode<'dom>,
{
type Item = ConcreteNode;
fn next(&mut self) -> Option<ConcreteNode> {
let node = self.current;
self.current = node.and_then(|node| node.prev_sibling());
node
}
}
pub struct TreeIterator<ConcreteNode> {
stack: Vec<ConcreteNode>,
}
impl<'dom, ConcreteNode> TreeIterator<ConcreteNode>
where
ConcreteNode: LayoutNode<'dom>,
{
fn new(root: ConcreteNode) -> TreeIterator<ConcreteNode> {
let stack = vec![root];
TreeIterator { stack }
}
pub fn next_skipping_children(&mut self) -> Option<ConcreteNode> {
self.stack.pop()
}
}
impl<'dom, ConcreteNode> Iterator for TreeIterator<ConcreteNode>
where
ConcreteNode: LayoutNode<'dom>,
{
type Item = ConcreteNode;
fn next(&mut self) -> Option<ConcreteNode> {
let ret = self.stack.pop();
if let Some(node) = ret {
self.stack.extend(node.rev_children())
}
ret
}
}
pub trait ThreadSafeLayoutNode<'dom>: Clone + Copy + Debug + NodeInfo + PartialEq + Sized {
type ConcreteNode: LayoutNode<'dom, ConcreteThreadSafeLayoutNode = Self>;
type ConcreteElement: TElement;
type ConcreteThreadSafeLayoutElement: ThreadSafeLayoutElement<'dom, ConcreteThreadSafeLayoutNode = Self>
+ ::selectors::Element<Impl = SelectorImpl>;
type ChildrenIterator: Iterator<Item = Self> + Sized;
fn opaque(&self) -> OpaqueNode;
fn type_id(&self) -> Option<LayoutNodeType>;
fn parent_style(&self, context: &SharedStyleContext) -> Arc<ComputedValues>;
fn initialize_layout_data<RequestedLayoutDataType: LayoutDataTrait>(&self);
fn debug_id(self) -> usize;
fn children(&self) -> LayoutIterator<Self::ChildrenIterator>;
fn as_element(&self) -> Option<Self::ConcreteThreadSafeLayoutElement>;
fn as_html_element(&self) -> Option<Self::ConcreteThreadSafeLayoutElement>;
fn style_data(&self) -> Option<&'dom StyleData>;
fn layout_data(&self) -> Option<&'dom GenericLayoutData>;
fn style(&self, context: &SharedStyleContext) -> Arc<ComputedValues> {
if let Some(el) = self.as_element() {
el.style(context)
} else {
debug_assert!(self.is_text_node());
self.parent_style(context)
}
}
fn is_content(&self) -> bool {
self.type_id().is_some()
}
fn unsafe_get(self) -> Self::ConcreteNode;
fn text_content(self) -> Cow<'dom, str>;
fn selection(&self) -> Option<SharedSelection>;
fn image_url(&self) -> Option<ServoUrl>;
fn image_density(&self) -> Option<f64>;
fn image_data(&self) -> Option<(Option<Image>, Option<ImageMetadata>)>;
fn showing_broken_image_icon(&self) -> bool;
fn canvas_data(&self) -> Option<HTMLCanvasData>;
fn svg_data(&self) -> Option<SVGElementData<'dom>>;
fn media_data(&self) -> Option<HTMLMediaData>;
fn iframe_browsing_context_id(&self) -> Option<BrowsingContextId>;
fn iframe_pipeline_id(&self) -> Option<PipelineId>;
fn get_span(&self) -> Option<u32>;
fn get_colspan(&self) -> Option<u32>;
fn get_rowspan(&self) -> Option<u32>;
fn pseudo_element_chain(&self) -> PseudoElementChain;
fn with_pseudo(&self, pseudo_element_type: PseudoElement) -> Option<Self> {
self.as_element()
.and_then(|element| element.with_pseudo(pseudo_element_type))
.as_ref()
.map(ThreadSafeLayoutElement::as_node)
}
fn with_pseudo_element_chain(&self, pseudo_element_chain: PseudoElementChain) -> Self;
fn set_uses_content_attribute_with_attr(&self, _uses_content_attribute_with_attr: bool);
}
pub trait ThreadSafeLayoutElement<'dom>:
Clone + Copy + Sized + Debug + ::selectors::Element<Impl = SelectorImpl>
{
type ConcreteThreadSafeLayoutNode: ThreadSafeLayoutNode<'dom, ConcreteThreadSafeLayoutElement = Self>;
type ConcreteElement: TElement;
fn as_node(&self) -> Self::ConcreteThreadSafeLayoutNode;
fn with_pseudo(&self, pseudo: PseudoElement) -> Option<Self>;
fn type_id(&self) -> Option<LayoutNodeType>;
fn unsafe_get(self) -> Self::ConcreteElement;
fn get_local_name(&self) -> &LocalName;
fn get_attr(&self, namespace: &Namespace, name: &LocalName) -> Option<&str>;
fn get_attr_enum(&self, namespace: &Namespace, name: &LocalName) -> Option<&AttrValue>;
fn style_data(&self) -> ElementDataRef<'_>;
fn pseudo_element_chain(&self) -> PseudoElementChain;
#[inline]
fn style(&self, context: &SharedStyleContext) -> Arc<ComputedValues> {
let get_style_for_pseudo_element =
|base_style: &Arc<ComputedValues>, pseudo_element: PseudoElement| {
match pseudo_element.cascade_type() {
PseudoElementCascadeType::Eager => self
.style_data()
.styles
.pseudos
.get(&pseudo_element)
.unwrap()
.clone(),
PseudoElementCascadeType::Precomputed => context
.stylist
.precomputed_values_for_pseudo::<Self::ConcreteElement>(
&context.guards,
&pseudo_element,
Some(base_style),
),
PseudoElementCascadeType::Lazy => {
context
.stylist
.lazily_compute_pseudo_element_style(
&context.guards,
self.unsafe_get(),
&pseudo_element,
RuleInclusion::All,
base_style,
false,
None,
)
.unwrap()
},
}
};
let data = self.style_data();
let element_style = data.styles.primary();
let pseudo_element_chain = self.pseudo_element_chain();
let primary_pseudo_style = match pseudo_element_chain.primary {
Some(pseudo_element) => get_style_for_pseudo_element(element_style, pseudo_element),
None => return element_style.clone(),
};
match pseudo_element_chain.secondary {
Some(pseudo_element) => {
get_style_for_pseudo_element(&primary_pseudo_style, pseudo_element)
},
None => primary_pseudo_style,
}
}
fn is_shadow_host(&self) -> bool;
fn is_body_element_of_html_element_root(&self) -> bool;
fn is_root(&self) -> bool;
}
#[derive(Clone, Copy, Debug, Default, Eq, Hash, MallocSizeOf, PartialEq)]
pub struct PseudoElementChain {
pub primary: Option<PseudoElement>,
pub secondary: Option<PseudoElement>,
}
impl PseudoElementChain {
pub fn unnested(pseudo_element: PseudoElement) -> Self {
Self {
primary: Some(pseudo_element),
secondary: None,
}
}
pub fn innermost(&self) -> Option<PseudoElement> {
self.secondary.or(self.primary)
}
pub fn with_pseudo(&self, pseudo_element: PseudoElement) -> Self {
match self.primary {
Some(primary) if primary.is_before_or_after() => Self {
primary: self.primary,
secondary: Some(pseudo_element),
},
_ => {
assert!(self.secondary.is_none());
Self::unnested(pseudo_element)
},
}
}
pub fn without_innermost(&self) -> Option<Self> {
let primary = self.primary?;
Some(
self.secondary
.map_or_else(Self::default, |_| Self::unnested(primary)),
)
}
pub fn is_empty(&self) -> bool {
self.primary.is_none()
}
}
#[derive(Clone, Debug, Default, MallocSizeOf, PartialEq)]
pub struct ScriptSelection {
pub range: TextByteRange,
pub character_range: Range<usize>,
pub enabled: bool,
}
pub type SharedSelection = std::sync::Arc<AtomicRefCell<ScriptSelection>>;