pub struct Stylesheet { /* private fields */ }Expand description
A parsed stylesheet containing styles keyed by element ID
Implementations§
Source§impl Stylesheet
impl Stylesheet
Sourcepub fn new() -> Stylesheet
pub fn new() -> Stylesheet
Create an empty stylesheet
Sourcepub fn parse_with_errors(css: &str) -> CssParseResult
pub fn parse_with_errors(css: &str) -> CssParseResult
Parse CSS text into a stylesheet with full error collection
This is the recommended method for parsing CSS as it collects all errors and warnings during parsing, allowing you to report them to users in a human-readable format.
§Example
let css = "#card { opacity: 0.5; unknown: value; }";
let result = Stylesheet::parse_with_errors(css);
// Print any warnings to stderr
result.print_diagnostics();
// Use the stylesheet (partial results are still available)
let stylesheet = result.stylesheet;Sourcepub fn parse_with_variables(
css: &str,
external_vars: &HashMap<String, String>,
) -> Result<Stylesheet, ParseError>
pub fn parse_with_variables( css: &str, external_vars: &HashMap<String, String>, ) -> Result<Stylesheet, ParseError>
Parse CSS with pre-seeded external variables (e.g. from theme or prior stylesheets).
var(--name) references in the CSS will resolve against both :root variables
defined in this CSS and the provided external variables. CSS-defined variables
take precedence over external ones.
Sourcepub fn parse_with_errors_and_variables(
css: &str,
external_vars: &HashMap<String, String>,
) -> CssParseResult
pub fn parse_with_errors_and_variables( css: &str, external_vars: &HashMap<String, String>, ) -> CssParseResult
Parse CSS with external variables and full error collection.
Sourcepub fn parse(css: &str) -> Result<Stylesheet, ParseError>
pub fn parse(css: &str) -> Result<Stylesheet, ParseError>
Parse CSS text into a stylesheet
Parse errors are logged via tracing at DEBUG level with full context. When parsing fails, an error is returned but the application can fall back to built-in theme styles.
For full error collection, use parse_with_errors() instead.
§Example
let css = "#card { opacity: 0.5; }";
let stylesheet = Stylesheet::parse(css)?;Sourcepub fn parse_or_empty(css: &str) -> Stylesheet
pub fn parse_or_empty(css: &str) -> Stylesheet
Parse CSS text, logging errors and returning an empty stylesheet on failure
This is a convenience method for cases where you want to gracefully fall back to an empty stylesheet rather than handle errors explicitly.
Sourcepub fn insert(&mut self, id: impl Into<String>, style: ElementStyle)
pub fn insert(&mut self, id: impl Into<String>, style: ElementStyle)
Insert a style for an element ID (without the # prefix)
This is the programmatic equivalent of parsing #id { ... } in CSS.
If a style already exists for this ID, it is replaced.
Sourcepub fn insert_with_state(
&mut self,
id: impl Into<String>,
state: ElementState,
style: ElementStyle,
)
pub fn insert_with_state( &mut self, id: impl Into<String>, state: ElementState, style: ElementStyle, )
Insert a state-specific style for an element ID
This is the programmatic equivalent of parsing #id:hover { ... } in CSS.
Sourcepub fn get(&self, id: &str) -> Option<&ElementStyle>
pub fn get(&self, id: &str) -> Option<&ElementStyle>
Get a style by element ID (without the # prefix)
Returns None if no style is defined for the given ID.
Sourcepub fn get_with_state(
&self,
id: &str,
state: ElementState,
) -> Option<&ElementStyle>
pub fn get_with_state( &self, id: &str, state: ElementState, ) -> Option<&ElementStyle>
Sourcepub fn get_placeholder_style(&self, id: &str) -> Option<&ElementStyle>
pub fn get_placeholder_style(&self, id: &str) -> Option<&ElementStyle>
Get the ::placeholder pseudo-element style for an element ID
Looks up #id::placeholder in the stylesheet. The color property
in a ::placeholder block maps to text_color on the returned style,
and placeholder-color maps directly.
Sourcepub fn get_all_states(
&self,
id: &str,
) -> (Option<&ElementStyle>, Vec<(ElementState, &ElementStyle)>)
pub fn get_all_states( &self, id: &str, ) -> (Option<&ElementStyle>, Vec<(ElementState, &ElementStyle)>)
Get all styles for an element, including state variants
Returns a tuple of (base_style, state_styles) where state_styles is a Vec of (ElementState, &ElementStyle) pairs.
§Example
let css = r#"
#button { background: blue; }
#button:hover { background: lightblue; }
#button:active { background: darkblue; }
"#;
let stylesheet = Stylesheet::parse(css)?;
let (base, states) = stylesheet.get_all_states("button");Sourcepub fn contains_with_state(&self, id: &str, state: ElementState) -> bool
pub fn contains_with_state(&self, id: &str, state: ElementState) -> bool
Check if a style exists for the given ID and state
Sourcepub fn complex_rules(&self) -> &[(ComplexSelector, ElementStyle)]
pub fn complex_rules(&self) -> &[(ComplexSelector, ElementStyle)]
Get all complex selector rules
Sourcepub fn has_complex_state_rules(&self) -> bool
pub fn has_complex_state_rules(&self) -> bool
Check if there are any complex rules that involve state changes
Sourcepub fn svg_tag_rules(
&self,
) -> Vec<(&str, &[(CompoundSelector, Option<Combinator>)], &ElementStyle)>
pub fn svg_tag_rules( &self, ) -> Vec<(&str, &[(CompoundSelector, Option<Combinator>)], &ElementStyle)>
Returns complex rules whose rightmost compound selector targets an SVG tag name.
Each entry returns: (tag_name, ancestor_segments if any, style).
For a bare path { fill: red; }, ancestor_segments is empty.
For #my-svg path { fill: red; }, ancestor_segments contains the #my-svg part.
Sourcepub fn merge(&mut self, other: Stylesheet)
pub fn merge(&mut self, other: Stylesheet)
Merge another stylesheet into this one (cascade — later rules override earlier)
This follows CSS cascade rules: styles from other override matching
styles in self. Variables and keyframes are also merged.
Sourcepub fn from_file(path: impl AsRef<Path>) -> Result<Stylesheet, ParseError>
pub fn from_file(path: impl AsRef<Path>) -> Result<Stylesheet, ParseError>
Load and parse a .css file from disk
Sourcepub fn get_variable(&self, name: &str) -> Option<&str>
pub fn get_variable(&self, name: &str) -> Option<&str>
Sourcepub fn variable_names(&self) -> impl Iterator<Item = &str>
pub fn variable_names(&self) -> impl Iterator<Item = &str>
Get all variable names
Sourcepub fn variable_count(&self) -> usize
pub fn variable_count(&self) -> usize
Get the number of variables defined
Sourcepub fn variables(&self) -> &HashMap<String, String>
pub fn variables(&self) -> &HashMap<String, String>
Get all CSS variables as a reference to the internal map
Sourcepub fn get_keyframes(&self, name: &str) -> Option<&CssKeyframes>
pub fn get_keyframes(&self, name: &str) -> Option<&CssKeyframes>
Get a keyframe animation by name
§Example
let css = r#"
@keyframes fade-in {
from { opacity: 0; }
to { opacity: 1; }
}
"#;
let stylesheet = Stylesheet::parse_with_errors(css).stylesheet;
if let Some(keyframes) = stylesheet.get_keyframes("fade-in") {
let animation = keyframes.to_enter_animation(300);
}Sourcepub fn contains_keyframes(&self, name: &str) -> bool
pub fn contains_keyframes(&self, name: &str) -> bool
Check if keyframes exist with the given name
Sourcepub fn keyframe_names(&self) -> impl Iterator<Item = &str>
pub fn keyframe_names(&self) -> impl Iterator<Item = &str>
Get all keyframe animation names
Sourcepub fn keyframe_count(&self) -> usize
pub fn keyframe_count(&self) -> usize
Get the number of keyframe animations defined
Sourcepub fn add_keyframes(&mut self, keyframes: CssKeyframes)
pub fn add_keyframes(&mut self, keyframes: CssKeyframes)
Add a keyframe animation to the stylesheet
Sourcepub fn contains_flow(&self, name: &str) -> bool
pub fn contains_flow(&self, name: &str) -> bool
Check if a flow exists with the given name
Sourcepub fn flow_names(&self) -> impl Iterator<Item = &str>
pub fn flow_names(&self) -> impl Iterator<Item = &str>
Get all flow names
Sourcepub fn flow_count(&self) -> usize
pub fn flow_count(&self) -> usize
Get the number of flows defined
Sourcepub fn resolve_animation(&self, id: &str) -> Option<MotionAnimation>
pub fn resolve_animation(&self, id: &str) -> Option<MotionAnimation>
Resolve a full motion animation for an element by its ID
This combines:
- The element’s
animation:property (from its style) - The referenced
@keyframesdefinition
Returns Some(MotionAnimation) if the element has an animation configured
and the keyframes exist.
§Example
let css = r#"
@keyframes fade-in {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
#card {
animation: fade-in 300ms ease-out;
}
"#;
let stylesheet = Stylesheet::parse_with_errors(css).stylesheet;
if let Some(motion) = stylesheet.resolve_animation("card") {
// Apply motion animation to the element
}Sourcepub fn resolve_animation_with_state(
&self,
id: &str,
state: ElementState,
) -> Option<MotionAnimation>
pub fn resolve_animation_with_state( &self, id: &str, state: ElementState, ) -> Option<MotionAnimation>
Resolve animation for an element considering its current state
This checks both the base style and state-specific styles for animations.
Sourcepub fn resolve_keyframe_animation(
&self,
id: &str,
) -> Option<MultiKeyframeAnimation>
pub fn resolve_keyframe_animation( &self, id: &str, ) -> Option<MultiKeyframeAnimation>
Resolve CSS animation to full MultiKeyframeAnimation with all keyframes preserved
Unlike resolve_animation() which only captures first/last keyframes for simple
enter/exit animations, this method preserves ALL keyframes for complex multi-step
animations like pulse, bounce, etc.
§Example
let css = r#"
@keyframes pulse {
0%, 100% { opacity: 1; transform: scale(1); }
50% { opacity: 0.8; transform: scale(1.05); }
}
#button { animation: pulse 1000ms ease-in-out infinite; }
"#;
let stylesheet = Stylesheet::parse_with_errors(css).stylesheet;
if let Some(mut anim) = stylesheet.resolve_keyframe_animation("button") {
anim.start();
// Animation will interpolate through all 3 keyframes
}Sourcepub fn resolve_keyframe_animation_with_state(
&self,
id: &str,
state: ElementState,
) -> Option<MultiKeyframeAnimation>
pub fn resolve_keyframe_animation_with_state( &self, id: &str, state: ElementState, ) -> Option<MultiKeyframeAnimation>
Resolve keyframe animation for an element considering its current state
This checks both the base style and state-specific styles for animations, returning a full MultiKeyframeAnimation with all keyframes preserved.
Trait Implementations§
Source§impl Clone for Stylesheet
impl Clone for Stylesheet
Source§fn clone(&self) -> Stylesheet
fn clone(&self) -> Stylesheet
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Stylesheet
impl Debug for Stylesheet
Source§impl Default for Stylesheet
impl Default for Stylesheet
Source§fn default() -> Stylesheet
fn default() -> Stylesheet
Auto Trait Implementations§
impl Freeze for Stylesheet
impl RefUnwindSafe for Stylesheet
impl Send for Stylesheet
impl Sync for Stylesheet
impl Unpin for Stylesheet
impl UnsafeUnpin for Stylesheet
impl UnwindSafe for Stylesheet
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.