1
//! Utility functions for color parsing and XML DOM construction.
2

            
3
use azul_core::dom::Dom;
4
use azul_css::props::basic::color::ColorU;
5

            
6
/// Parses a CSS color string (e.g. `"#ff0000"`, `"red"`) into a `ColorU`.
7
/// Returns `ColorU::BLACK` if the string cannot be parsed.
8
pub fn coloru_from_str(s: &str) -> ColorU {
9
    azul_css::props::basic::color::parse_css_color(s)
10
        .ok()
11
        .unwrap_or(ColorU::BLACK)
12
}
13

            
14
/// Create a Dom (with CSS attached but not applied) from an already-parsed Xml structure.
15
///
16
/// Returns an unstyled `Dom` suitable for use in layout callbacks (which return `Dom`,
17
/// not `StyledDom`). The CSS from `<style>` tags is attached to the `Dom.css` field
18
/// and will be applied during the cascade pass.
19
#[cfg(not(feature = "xml"))]
20
pub fn dom_from_parsed_xml(_xml: azul_core::xml::Xml) -> Dom {
21
    Dom::create_body()
22
        .with_children(
23
            vec![Dom::create_text(format!(
24
                "library was not compiled with --feature=\"xml\""
25
            ))]
26
            .into(),
27
        )
28
}
29

            
30
/// Create a Dom (with CSS attached but not applied) from an already-parsed Xml structure.
31
#[cfg(feature = "xml")]
32
pub fn dom_from_parsed_xml(xml: azul_core::xml::Xml) -> Dom {
33
    use azul_core::xml::{str_to_dom_unstyled, ComponentMap};
34

            
35
    let component_map = ComponentMap::with_builtin();
36
    match str_to_dom_unstyled(xml.root.as_ref(), &component_map) {
37
        Ok(dom) => dom,
38
        Err(e) => {
39
            Dom::create_body()
40
                .with_children(vec![Dom::create_text(format!("{}", e))].into())
41
        }
42
    }
43
}