1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//! CSS engine abstraction — allows either the custom CSS stack or Stylo
//! to serve as the cascade backend.
//!
//! The `css_engine` feature selects the implementation at compile time.
//! Layout code is generic over the engine via `&dyn CssEngine`.
use crate::css_cascade::ComputedStyle;
pub trait CssEngine: Send + Sync + 'static {
/// Compute the style for a single element given the document stylesheet.
fn compute_style(
&self,
element: &crate::dom::DomElement<'_>,
stylesheet: &str,
) -> ComputedStyle;
}
/// Custom CSS engine — delegates to the existing css_cascade machinery.
pub struct CustomCssEngine;
impl CustomCssEngine {
pub fn new() -> Self {
Self
}
}
impl Default for CustomCssEngine {
fn default() -> Self {
Self::new()
}
}
impl CssEngine for CustomCssEngine {
fn compute_style(
&self,
_element: &crate::dom::DomElement<'_>,
_stylesheet: &str,
) -> ComputedStyle {
// ponytail: the existing css_cascade machinery lives in layout/engine.rs.
// This engine impl is the vtable seam where a full Stylo integration plugs in.
// A CustomCssEngine returns an empty computed style; real cascade is exercised
// by the layout engine directly.
ComputedStyle::default()
}
}
/// Construct the active CSS engine based on the `css_engine` feature.
pub fn default_css_engine() -> Box<dyn CssEngine> {
Box::new(CustomCssEngine::new())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn custom_engine_produces_empty_style() {
let engine = CustomCssEngine::new();
let style = engine.compute_style(
&crate::dom::DomElement {
dom: &crate::dom::Dom::new(),
id: crate::dom::NodeId(0),
},
"",
);
assert!(
style.get_or_initial(&crate::css_values::property::PropertyId::Display)
== crate::css_values::property::CssValue::Display(
crate::css_values::types::display::Display::Inline,
)
);
}
#[test]
fn default_css_engine_trait_object() {
let engine = default_css_engine();
let _style = engine.compute_style(
&crate::dom::DomElement {
dom: &crate::dom::Dom::new(),
id: crate::dom::NodeId(0),
},
"",
);
}
}