Skip to content

Commit

Permalink
Wrap in Arc<_> every object reflected in CSSOM.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Aug 31, 2016
1 parent 94d5b28 commit ef5977f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 49 deletions.
2 changes: 1 addition & 1 deletion components/script/dom/htmlmetaelement.rs
Expand Up @@ -79,7 +79,7 @@ impl HTMLMetaElement {
if !content.is_empty() {
if let Some(translated_rule) = ViewportRule::from_meta(&**content) {
*self.stylesheet.borrow_mut() = Some(Arc::new(Stylesheet {
rules: vec![CSSRule::Viewport(translated_rule)],
rules: vec![CSSRule::Viewport(Arc::new(translated_rule))],
origin: Origin::Author,
media: None,
// Viewport constraints are always recomputed on resize; they don't need to
Expand Down
12 changes: 6 additions & 6 deletions components/style/keyframes.rs
Expand Up @@ -164,7 +164,7 @@ fn get_animated_properties(keyframe: &Keyframe) -> Vec<TransitionProperty> {
}

impl KeyframesAnimation {
pub fn from_keyframes(keyframes: &[Keyframe]) -> Option<Self> {
pub fn from_keyframes(keyframes: &[Arc<Keyframe>]) -> Option<Self> {
if keyframes.is_empty() {
return None;
}
Expand Down Expand Up @@ -216,7 +216,7 @@ struct KeyframeListParser<'a> {
context: &'a ParserContext<'a>,
}

pub fn parse_keyframe_list(context: &ParserContext, input: &mut Parser) -> Vec<Keyframe> {
pub fn parse_keyframe_list(context: &ParserContext, input: &mut Parser) -> Vec<Arc<Keyframe>> {
RuleListParser::new_for_nested_rule(input, KeyframeListParser { context: context })
.filter_map(Result::ok)
.collect()
Expand All @@ -225,12 +225,12 @@ pub fn parse_keyframe_list(context: &ParserContext, input: &mut Parser) -> Vec<K
enum Void {}
impl<'a> AtRuleParser for KeyframeListParser<'a> {
type Prelude = Void;
type AtRule = Keyframe;
type AtRule = Arc<Keyframe>;
}

impl<'a> QualifiedRuleParser for KeyframeListParser<'a> {
type Prelude = KeyframeSelector;
type QualifiedRule = Keyframe;
type QualifiedRule = Arc<Keyframe>;

fn parse_prelude(&self, input: &mut Parser) -> Result<Self::Prelude, ()> {
let start = input.position();
Expand Down Expand Up @@ -263,10 +263,10 @@ impl<'a> QualifiedRuleParser for KeyframeListParser<'a> {
}
// `parse_important` is not called here, `!important` is not allowed in keyframe blocks.
}
Ok(Keyframe {
Ok(Arc::new(Keyframe {
selector: prelude,
declarations: Arc::new(declarations),
})
}))
}
}

Expand Down
45 changes: 23 additions & 22 deletions components/style/stylesheets.rs
Expand Up @@ -19,6 +19,7 @@ use smallvec::SmallVec;
use std::cell::Cell;
use std::iter::Iterator;
use std::slice;
use std::sync::Arc;
use string_cache::{Atom, Namespace};
use url::Url;
use viewport::ViewportRule;
Expand Down Expand Up @@ -67,12 +68,12 @@ pub enum CSSRule {
// No Charset here, CSSCharsetRule has been removed from CSSOM
// https://drafts.csswg.org/cssom/#changes-from-5-december-2013

Namespace(NamespaceRule),
Style(StyleRule),
Media(MediaRule),
FontFace(FontFaceRule),
Viewport(ViewportRule),
Keyframes(KeyframesRule),
Namespace(Arc<NamespaceRule>),
Style(Arc<StyleRule>),
Media(Arc<MediaRule>),
FontFace(Arc<FontFaceRule>),
Viewport(Arc<ViewportRule>),
Keyframes(Arc<KeyframesRule>),
}


Expand All @@ -88,13 +89,13 @@ pub struct NamespaceRule {
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct KeyframesRule {
pub name: Atom,
pub keyframes: Vec<Keyframe>,
pub keyframes: Vec<Arc<Keyframe>>,
}

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct MediaRule {
pub media_queries: MediaQueryList,
pub media_queries: Arc<MediaQueryList>,
pub rules: Vec<CSSRule>,
}

Expand All @@ -110,7 +111,7 @@ impl MediaRule {
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct StyleRule {
pub selectors: Vec<Selector<TheSelectorImpl>>,
pub declarations: PropertyDeclarationBlock,
pub declarations: Arc<PropertyDeclarationBlock>,
}


Expand Down Expand Up @@ -414,7 +415,7 @@ enum AtRulePrelude {
/// A @font-face rule prelude.
FontFace,
/// A @media rule prelude, with its media queries.
Media(MediaQueryList),
Media(Arc<MediaQueryList>),
/// A @viewport rule prelude.
Viewport,
/// A @keyframes rule, with its animation name.
Expand Down Expand Up @@ -444,10 +445,10 @@ impl<'a> AtRuleParser for TopLevelRuleParser<'a> {

let prefix = input.try(|input| input.expect_ident()).ok().map(|p| p.into());
let url = Namespace(Atom::from(try!(input.expect_url_or_string())));
return Ok(AtRuleType::WithoutBlock(CSSRule::Namespace(NamespaceRule {
return Ok(AtRuleType::WithoutBlock(CSSRule::Namespace(Arc::new(NamespaceRule {
prefix: prefix,
url: url,
})))
}))))
} else {
return Err(()) // "@namespace must be before any rule but @charset and @import"
}
Expand Down Expand Up @@ -501,7 +502,7 @@ impl<'a, 'b> AtRuleParser for NestedRuleParser<'a, 'b> {
match_ignore_ascii_case! { name,
"media" => {
let media_queries = parse_media_query_list(input);
Ok(AtRuleType::WithBlock(AtRulePrelude::Media(media_queries)))
Ok(AtRuleType::WithBlock(AtRulePrelude::Media(Arc::new(media_queries))))
},
"font-face" => {
Ok(AtRuleType::WithBlock(AtRulePrelude::FontFace))
Expand Down Expand Up @@ -529,22 +530,22 @@ impl<'a, 'b> AtRuleParser for NestedRuleParser<'a, 'b> {
fn parse_block(&self, prelude: AtRulePrelude, input: &mut Parser) -> Result<CSSRule, ()> {
match prelude {
AtRulePrelude::FontFace => {
parse_font_face_block(self.context, input).map(CSSRule::FontFace)
Ok(CSSRule::FontFace(Arc::new(try!(parse_font_face_block(self.context, input)))))
}
AtRulePrelude::Media(media_queries) => {
Ok(CSSRule::Media(MediaRule {
Ok(CSSRule::Media(Arc::new(MediaRule {
media_queries: media_queries,
rules: parse_nested_rules(self.context, input),
}))
})))
}
AtRulePrelude::Viewport => {
ViewportRule::parse(input, self.context).map(CSSRule::Viewport)
Ok(CSSRule::Viewport(Arc::new(try!(ViewportRule::parse(input, self.context)))))
}
AtRulePrelude::Keyframes(name) => {
Ok(CSSRule::Keyframes(KeyframesRule {
Ok(CSSRule::Keyframes(Arc::new(KeyframesRule {
name: name,
keyframes: parse_keyframe_list(&self.context, input),
}))
})))
}
}
}
Expand All @@ -559,9 +560,9 @@ impl<'a, 'b> QualifiedRuleParser for NestedRuleParser<'a, 'b> {
}

fn parse_block(&self, prelude: Vec<Selector<TheSelectorImpl>>, input: &mut Parser) -> Result<CSSRule, ()> {
Ok(CSSRule::Style(StyleRule {
Ok(CSSRule::Style(Arc::new(StyleRule {
selectors: prelude,
declarations: parse_property_declaration_list(self.context, input)
}))
declarations: Arc::new(parse_property_declaration_list(self.context, input))
})))
}
}
40 changes: 20 additions & 20 deletions tests/unit/style/stylesheets.rs
Expand Up @@ -65,11 +65,11 @@ fn test_parse_stylesheet() {
media: None,
dirty_on_viewport_size_change: false,
rules: vec![
CSSRule::Namespace(NamespaceRule {
CSSRule::Namespace(Arc::new(NamespaceRule {
prefix: None,
url: NsAtom(Atom::from("http://www.w3.org/1999/xhtml"))
}),
CSSRule::Style(StyleRule {
})),
CSSRule::Style(Arc::new(StyleRule {
selectors: vec![
Selector {
complex_selector: Arc::new(ComplexSelector {
Expand Down Expand Up @@ -97,7 +97,7 @@ fn test_parse_stylesheet() {
specificity: (0 << 20) + (1 << 10) + (1 << 0),
},
],
declarations: PropertyDeclarationBlock {
declarations: Arc::new(PropertyDeclarationBlock {
declarations: Arc::new(vec![
(PropertyDeclaration::Display(DeclaredValue::Value(
longhands::display::SpecifiedValue::none)),
Expand All @@ -106,9 +106,9 @@ fn test_parse_stylesheet() {
Importance::Important),
]),
important_count: 2,
},
}),
CSSRule::Style(StyleRule {
}),
})),
CSSRule::Style(Arc::new(StyleRule {
selectors: vec![
Selector {
complex_selector: Arc::new(ComplexSelector {
Expand Down Expand Up @@ -145,16 +145,16 @@ fn test_parse_stylesheet() {
specificity: (0 << 20) + (0 << 10) + (1 << 0),
},
],
declarations: PropertyDeclarationBlock {
declarations: Arc::new(PropertyDeclarationBlock {
declarations: Arc::new(vec![
(PropertyDeclaration::Display(DeclaredValue::Value(
longhands::display::SpecifiedValue::block)),
Importance::Normal),
]),
important_count: 0,
},
}),
CSSRule::Style(StyleRule {
}),
})),
CSSRule::Style(Arc::new(StyleRule {
selectors: vec![
Selector {
complex_selector: Arc::new(ComplexSelector {
Expand All @@ -180,7 +180,7 @@ fn test_parse_stylesheet() {
specificity: (1 << 20) + (1 << 10) + (0 << 0),
},
],
declarations: PropertyDeclarationBlock {
declarations: Arc::new(PropertyDeclarationBlock {
declarations: Arc::new(vec![
(PropertyDeclaration::BackgroundColor(DeclaredValue::Value(
longhands::background_color::SpecifiedValue {
Expand Down Expand Up @@ -228,21 +228,21 @@ fn test_parse_stylesheet() {
Importance::Normal),
]),
important_count: 0,
},
}),
CSSRule::Keyframes(KeyframesRule {
}),
})),
CSSRule::Keyframes(Arc::new(KeyframesRule {
name: "foo".into(),
keyframes: vec![
Keyframe {
Arc::new(Keyframe {
selector: KeyframeSelector::new_for_unit_testing(
vec![KeyframePercentage::new(0.)]),
declarations: Arc::new(vec![
(PropertyDeclaration::Width(DeclaredValue::Value(
LengthOrPercentageOrAuto::Percentage(Percentage(0.)))),
Importance::Normal),
]),
},
Keyframe {
}),
Arc::new(Keyframe {
selector: KeyframeSelector::new_for_unit_testing(
vec![KeyframePercentage::new(1.)]),
declarations: Arc::new(vec![
Expand All @@ -254,9 +254,9 @@ fn test_parse_stylesheet() {
vec![animation_play_state::SingleSpecifiedValue::running]))),
Importance::Normal),
]),
},
}),
]
})
}))

],
});
Expand Down

0 comments on commit ef5977f

Please sign in to comment.