aboutsummaryrefslogtreecommitdiffstats
path: root/components/layout/css/node_style.rs
blob: 946ac11a5e75e22184bfcdd3666c384130f5d214 (plain) (blame)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! Style retrieval from DOM elements.

use wrapper::{PseudoElementType, ThreadSafeLayoutNode};

use std::mem;
use style::properties::ComputedValues;
use std::sync::Arc;

/// Node mixin providing `style` method that returns a `NodeStyle`
pub trait StyledNode {
    /// Returns the style results for the given node. If CSS selector matching has not yet been
    /// performed, fails.
    fn style<'a>(&'a self) -> &'a Arc<ComputedValues>;
    /// Does this node have a computed style yet?
    fn has_style(&self) -> bool;
    /// Removes the style from this node.
    fn unstyle(self);
}

impl<'ln> StyledNode for ThreadSafeLayoutNode<'ln> {
    #[inline]
    #[allow(unsafe_blocks)]
    fn style<'a>(&'a self) -> &'a Arc<ComputedValues> {
        unsafe {
            let layout_data_ref = self.borrow_layout_data();
            match self.get_pseudo_element_type() {
                PseudoElementType::Before(_) => {
                     mem::transmute(layout_data_ref.as_ref()
                                                   .unwrap()
                                                   .data
                                                   .before_style
                                                   .as_ref()
                                                   .unwrap())
                }
                PseudoElementType::After(_) => {
                    mem::transmute(layout_data_ref.as_ref()
                                                  .unwrap()
                                                  .data
                                                  .after_style
                                                  .as_ref()
                                                  .unwrap())
                }
                PseudoElementType::Normal => {
                    mem::transmute(layout_data_ref.as_ref()
                                                  .unwrap()
                                                  .shared_data
                                                  .style
                                                  .as_ref()
                                                  .unwrap())
                }
            }
        }
    }

    fn has_style(&self) -> bool {
        let layout_data_ref = self.borrow_layout_data();
        layout_data_ref.as_ref().unwrap().shared_data.style.is_some()
    }

    fn unstyle(self) {
        let mut layout_data_ref = self.mutate_layout_data();
        let layout_data = layout_data_ref.as_mut().expect("no layout data");

        let style =
            match self.get_pseudo_element_type() {
                PseudoElementType::Before(_) => &mut layout_data.data.before_style,
                PseudoElementType::After (_) => &mut layout_data.data.after_style,
                PseudoElementType::Normal    => &mut layout_data.shared_data.style,
            };

        *style = None;
    }
}