aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/main/css/node_util.rs
blob: 5c88632eb55bc8aa0ee818e49cf9fd64b8ec29d1 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* 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/. */

use layout::incremental::RestyleDamage;
use layout::util::LayoutDataAccess;
use layout::wrapper::{TLayoutNode, ThreadSafeLayoutNode};
use layout::wrapper::{After, AfterBlock, Before, BeforeBlock, Normal};
use std::cast;
use style::ComputedValues;
use sync::Arc;

pub trait NodeUtil {
    fn get_css_select_results<'a>(&'a self) -> &'a Arc<ComputedValues>;
    fn have_css_select_results(&self) -> bool;

    fn get_restyle_damage(&self) -> RestyleDamage;
    fn set_restyle_damage(&self, damage: RestyleDamage);
}

impl<'ln> NodeUtil for ThreadSafeLayoutNode<'ln> {
    /// Returns the style results for the given node. If CSS selector
    /// matching has not yet been performed, fails.
    #[inline]
    fn get_css_select_results<'a>(&'a self) -> &'a Arc<ComputedValues> {
        unsafe {
            let layout_data_ref = self.borrow_layout_data();
            match self.get_pseudo_element_type() {
                Before | BeforeBlock => {
                     cast::transmute_region(layout_data_ref.as_ref()
                                                           .unwrap()
                                                           .data
                                                           .before_style
                                                           .as_ref()
                                                           .unwrap())
                }
                After | AfterBlock => {
                    cast::transmute_region(layout_data_ref.as_ref()
                                                          .unwrap()
                                                          .data
                                                          .after_style
                                                          .as_ref()
                                                          .unwrap())
                }
                Normal => {
                    cast::transmute_region(layout_data_ref.as_ref()
                                                          .unwrap()
                                                          .data
                                                          .style
                                                          .as_ref()
                                                          .unwrap())
                }
            }
        }
    }

    /// Does this node have a computed style yet?
    fn have_css_select_results(&self) -> bool {
        let layout_data_ref = self.borrow_layout_data();
        layout_data_ref.get_ref().data.style.is_some()
    }

    /// Get the description of how to account for recent style changes.
    /// This is a simple bitfield and fine to copy by value.
    fn get_restyle_damage(&self) -> RestyleDamage {
        // For DOM elements, if we haven't computed damage yet, assume the worst.
        // Other nodes don't have styles.
        let default = if self.node_is_element() {
            RestyleDamage::all()
        } else {
            RestyleDamage::none()
        };

        let layout_data_ref = self.borrow_layout_data();
        layout_data_ref
            .get_ref()
            .data
            .restyle_damage
            .map(|x| RestyleDamage::from_int(x))
            .unwrap_or(default)
    }

    /// Set the restyle damage field.
    fn set_restyle_damage(&self, damage: RestyleDamage) {
        let mut layout_data_ref = self.mutate_layout_data();
        match &mut *layout_data_ref {
            &Some(ref mut layout_data) => layout_data.data.restyle_damage = Some(damage.to_int()),
            _ => fail!("no layout data for this node"),
        }
    }
}