1
#[allow(unused_imports)]
2
pub use super::*;
3
#[cfg(test)]
4
mod extract_tests {
5
    use super::*;
6
    use crate::{
7
        dom::{Dom, NodeData, NodeType},
8
        styled_dom::StyledDom,
9
    };
10

            
11
    /// The extracted DOM holds exactly the popup's content, re-rooted as a
12
    /// plain block, and nothing from outside the subtree.
13
    #[test]
14
1
    fn extracts_exactly_the_subtree_re_rooted_as_a_div() {
15
1
        let popup = Dom::create_from_data(NodeData::create_node(NodeType::TransientWindow(
16
1
            TransientWindowConfig::opened(),
17
1
        )))
18
1
        .with_child(Dom::create_p_with_text("inside"));
19
1
        let full = Dom::create_body()
20
1
            .with_child(Dom::create_p_with_text("outside"))
21
1
            .with_child(Dom::create_div().with_child(popup));
22
1
        let styled = StyledDom::create_from_dom(full);
23

            
24
        // body=0, p=1, text=2, div=3, transient=4, p=5, text=6
25
1
        let nodes = styled.node_data.as_container();
26
1
        let tw = nodes
27
1
            .linear_iter()
28
5
            .find(|n| {
29
4
                matches!(
30
5
                    nodes.get(*n).map(NodeData::get_node_type),
31
                    Some(NodeType::TransientWindow(_))
32
                )
33
5
            })
34
1
            .expect("the transient node");
35

            
36
1
        let out = extract_subtree_as_dom(&styled, tw).expect("extracts");
37
1
        assert!(
38
1
            matches!(out.root.get_node_type(), NodeType::Div),
39
            "the popup's own root must be a plain container in its window"
40
        );
41
        // root + p + text = 3 nodes; "outside" must not be among them.
42
1
        let out_styled = StyledDom::create_from_dom(out);
43
1
        let texts: Vec<String> = out_styled
44
1
            .node_data
45
1
            .as_ref()
46
1
            .iter()
47
3
            .filter_map(|n| match n.get_node_type() {
48
1
                NodeType::Text(t) => Some(t.as_str().to_string()),
49
2
                _ => None,
50
3
            })
51
1
            .collect();
52
1
        assert_eq!(texts, vec!["inside".to_string()]);
53
1
    }
54

            
55
    /// Only a TransientWindow can be extracted - asking for a div is a caller
56
    /// bug and must not quietly open a window onto arbitrary content.
57
    #[test]
58
1
    fn refuses_a_non_transient_root() {
59
1
        let styled = StyledDom::create_from_dom(Dom::create_body().with_child(Dom::create_div()));
60
1
        assert!(extract_subtree_as_dom(&styled, crate::id::NodeId::new(1)).is_none());
61
1
    }
62

            
63
    /// Author CSS attached with `Dom::with_css` is SCOPED - it is consumed
64
    /// into the property cache when the tree is styled - so cloning node data
65
    /// alone would drop it. The extracted copy must carry the resolved style,
66
    /// including what the root inherited from ancestors it leaves behind.
67
    #[test]
68
1
    fn resolved_style_travels_with_the_extracted_subtree() {
69
        use azul_css::props::{
70
            basic::{ColorU, PixelValue},
71
            layout::LayoutWidth,
72
            property::{CssProperty, CssPropertyType},
73
            style::StyleTextColor,
74
        };
75

            
76
1
        let popup = Dom::create_from_data(NodeData::create_node(NodeType::TransientWindow(
77
1
            TransientWindowConfig::opened(),
78
1
        )))
79
1
        .with_child(Dom::create_div().with_css("width: 240px;"));
80
1
        let full = Dom::create_body()
81
1
            .with_css("color: #123456;") // inherited by the popup root from OUTSIDE the subtree
82
1
            .with_child(Dom::create_div().with_child(popup));
83
1
        let styled = StyledDom::create_from_dom(full);
84

            
85
1
        let nodes = styled.node_data.as_container();
86
1
        let tw = nodes
87
1
            .linear_iter()
88
3
            .find(|n| {
89
2
                matches!(
90
3
                    nodes.get(*n).map(NodeData::get_node_type),
91
                    Some(NodeType::TransientWindow(_))
92
                )
93
3
            })
94
1
            .expect("the transient node");
95
1
        let out = extract_subtree_as_dom(&styled, tw).expect("extracts");
96

            
97
        // The popup's root inherited the body's colour.
98
1
        let root_color = out
99
1
            .root
100
1
            .style
101
1
            .iter_inline_properties()
102
1
            .find_map(|(p, _)| match p {
103
1
                CssProperty::TextColor(c) => c.get_property().copied(),
104
                _ => None,
105
1
            });
106
1
        assert_eq!(
107
            root_color,
108
            Some(StyleTextColor {
109
                inner: ColorU {
110
                    r: 0x12,
111
                    g: 0x34,
112
                    b: 0x56,
113
                    a: 255
114
                }
115
            }),
116
            "the root must carry what it inherited from outside the subtree"
117
        );
118

            
119
        // The child's own `width: 240px` came along as a matched author rule.
120
1
        let child = out.children.as_ref().first().expect("the sized child");
121
1
        let width = child
122
1
            .root
123
1
            .style
124
1
            .iter_inline_properties()
125
1
            .find_map(|(p, _)| match p {
126
1
                CssProperty::Width(w) => w.get_property().cloned(),
127
                _ => None,
128
1
            });
129
1
        assert_eq!(
130
            width,
131
1
            Some(LayoutWidth::Px(PixelValue::px(240.0))),
132
            "a scoped `with_css` rule must survive extraction"
133
        );
134
1
        assert!(
135
1
            child
136
1
                .root
137
1
                .style
138
1
                .iter_inline_properties()
139
1
                .all(|(p, _)| p.get_type() != CssPropertyType::TextColor),
140
            "a non-root node carries only its OWN matched rules; inheritance is re-derived"
141
        );
142
1
    }
143
}