From 5c606a8dbbaaeadf0bf837ee1682e23392a14562 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Wed, 31 Oct 2012 16:44:24 -0700 Subject: [PATCH] Move node style getter/setter to css::node_util --- src/servo/css/matching.rs | 20 ++----------------- src/servo/css/node_util.rs | 39 ++++++++++++++++++++++++++++++++++++++ src/servo/css/styles.rs | 21 -------------------- src/servo/layout/box.rs | 1 - src/servo/servo.rc | 1 + 5 files changed, 42 insertions(+), 40 deletions(-) create mode 100644 src/servo/css/node_util.rs diff --git a/src/servo/css/matching.rs b/src/servo/css/matching.rs index bc92ed8bd65..a8d2177111b 100644 --- a/src/servo/css/matching.rs +++ b/src/servo/css/matching.rs @@ -9,7 +9,7 @@ use newcss::values::*; use newcss::{SelectCtx, SelectResults}; use styles::{SpecifiedStyle}; use select_handler::NodeSelectHandler; -use std::cell::Cell; +use node_util::NodeUtil; /** Check if a CSS attribute matches the attribute of an HTML element. @@ -168,22 +168,6 @@ impl Node : PrivMatchingMethods { } } -trait PrivStyleMethods { - fn update_style(decl : SelectResults); -} - -impl Node : PrivStyleMethods { - /** - Update the computed style of an HTML element with a style specified by CSS. - */ - fn update_style(decl : SelectResults) { - let decl = Cell(move decl); - do self.aux |data| { - data.style = Some(decl.take()) - } - } -} - trait MatchingMethods { fn match_css_style(select_ctx : &SelectCtx); } @@ -204,7 +188,7 @@ impl Node : MatchingMethods { node: self }; let style = select_ctx.select_style(&self, &select_handler); - self.update_style(move style); + self.set_style(move style); } } diff --git a/src/servo/css/node_util.rs b/src/servo/css/node_util.rs new file mode 100644 index 00000000000..7e20f12e742 --- /dev/null +++ b/src/servo/css/node_util.rs @@ -0,0 +1,39 @@ +use dom::node::Node; +use newcss::SelectResults; +use std::cell::Cell; + +trait NodeUtil { + fn get_style() -> &self/SelectResults; + fn set_style(decl : SelectResults); +} + +impl Node: NodeUtil { + /** + * Provides the computed style for the given node. If CSS selector + * Returns the style results for the given node. If CSS selector + * matching has not yet been performed, fails. + * FIXME: This isn't completely memory safe since the style is + * stored in a box that can be overwritten + */ + fn get_style() -> &self/SelectResults { + if !self.has_aux() { + fail ~"style() called on a node without aux data!"; + } + unsafe { &*self.aux( |x| { + match x.style { + Some(ref style) => ptr::to_unsafe_ptr(style), + None => fail ~"style() called on node without a style!" + } + })} + } + + /** + Update the computed style of an HTML element with a style specified by CSS. + */ + fn set_style(decl : SelectResults) { + let decl = Cell(move decl); + do self.aux |data| { + data.style = Some(decl.take()) + } + } +} diff --git a/src/servo/css/styles.rs b/src/servo/css/styles.rs index 574a4d80b2e..1aa85eb9d72 100644 --- a/src/servo/css/styles.rs +++ b/src/servo/css/styles.rs @@ -104,8 +104,6 @@ fn empty_style_for_node_kind(kind: &NodeKind) -> SpecifiedStyle { trait StyleMethods { fn initialize_layout_data() -> Option<@LayoutData>; - - fn style() -> &self/SelectResults; fn initialize_style_for_subtree(ctx: &LayoutContext, refs: &DVec<@LayoutData>); fn recompute_style_for_subtree(ctx: &LayoutContext, styles : &SelectCtx); } @@ -126,25 +124,6 @@ impl Node : StyleMethods { true => None } } - - /** - * Provides the computed style for the given node. If CSS selector - * Returns the style results for the given node. If CSS selector - * matching has not yet been performed, fails. - * FIXME: This isn't completely memory safe since the style is - * stored in a box that can be overwritten - */ - fn style() -> &self/SelectResults { - if !self.has_aux() { - fail ~"style() called on a node without aux data!"; - } - unsafe { &*self.aux( |x| { - match x.style { - Some(ref style) => ptr::to_unsafe_ptr(style), - None => fail ~"style() called on node without a style!" - } - })} - } /** * Initializes layout data and styles for a Node tree, if any nodes do not have diff --git a/src/servo/layout/box.rs b/src/servo/layout/box.rs index d243fb77ea6..f9ea2e79fe2 100644 --- a/src/servo/layout/box.rs +++ b/src/servo/layout/box.rs @@ -385,7 +385,6 @@ impl RenderBox : RenderBoxMethods { fn build_display_list(@self, builder: &DisplayListBuilder, dirty: &Rect, offset: &Point2D, list: &mut DisplayList) { - let style = self.d().node.style(); let box_bounds = self.d().position; let abs_box_bounds = box_bounds.translate(offset); diff --git a/src/servo/servo.rc b/src/servo/servo.rc index f9ecf42900a..d7447ffbe57 100755 --- a/src/servo/servo.rc +++ b/src/servo/servo.rc @@ -50,6 +50,7 @@ pub mod css { mod matching; priv mod select_handler; pub mod compute; + priv mod node_util; } pub mod layout {