Skip to content

Commit 4643737

Browse files
committed
Applied a bunch of suggestions from Simon.
1 parent d0489f1 commit 4643737

11 files changed

Lines changed: 385 additions & 390 deletions

File tree

components/style/properties/build.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,7 @@ def abort(message):
4444

4545
def render(filename, **context):
4646
try:
47-
# Workaround the fact that we can have a different working directory when called, and Mako includes will fail
48-
# miserably if we don't give it proper instructions in the TemplateLookup.
49-
if os.getcwd().endswith("components/style"):
50-
properties_path = "properties"
51-
else:
52-
properties_path = "components/style/properties"
53-
54-
lookup = TemplateLookup(directories=[properties_path])
47+
lookup = TemplateLookup(directories=[BASE])
5548
template = Template(open(filename, "rb").read(),
5649
filename=filename,
5750
input_encoding="utf8",

components/style/properties/data.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,6 @@ def switch_to_style_struct(self, name):
134134
return
135135
raise Exception("Failed to find the struct named " + name)
136136

137-
def new_method(self, name, return_type):
138-
return Method(name, return_type)
139-
140137
def declare_longhand(self, name, products="gecko servo", **kwargs):
141138
products = products.split()
142139
if self.product not in products:
@@ -157,7 +154,3 @@ def declare_shorthand(self, name, sub_properties, *args, **kwargs):
157154
shorthand = Shorthand(name, sub_properties, *args, **kwargs)
158155
self.shorthands.append(shorthand)
159156
return shorthand
160-
161-
# Wrapper for the module-level method, since some scopes can only access the stuff in the PropertiesData class.
162-
def to_rust_ident(self, name):
163-
return to_rust_ident(name)
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
<%! from data import Keyword, to_rust_ident %>
6+
7+
<%def name="longhand(name, **kwargs)">
8+
<%call expr="raw_longhand(name, **kwargs)">
9+
${caller.body()}
10+
% if not data.longhands_by_name[name].derived_from:
11+
pub fn parse_specified(context: &ParserContext, input: &mut Parser)
12+
-> Result<DeclaredValue<SpecifiedValue>, ()> {
13+
parse(context, input).map(DeclaredValue::Value)
14+
}
15+
% endif
16+
</%call>
17+
</%def>
18+
19+
<%def name="predefined_type(name, type, initial_value, parse_method='parse', products='gecko servo')">
20+
<%self:longhand name="${name}" products="${products}">
21+
#[allow(unused_imports)]
22+
use app_units::Au;
23+
pub type SpecifiedValue = specified::${type};
24+
pub mod computed_value {
25+
pub use values::computed::${type} as T;
26+
}
27+
#[inline] pub fn get_initial_value() -> computed_value::T { ${initial_value} }
28+
#[inline] pub fn parse(_context: &ParserContext, input: &mut Parser)
29+
-> Result<SpecifiedValue, ()> {
30+
specified::${type}::${parse_method}(input)
31+
}
32+
</%self:longhand>
33+
</%def>
34+
35+
<%def name="raw_longhand(*args, **kwargs)">
36+
<%
37+
property = data.declare_longhand(*args, **kwargs)
38+
if property is None:
39+
return ""
40+
%>
41+
pub mod ${property.ident} {
42+
#![allow(unused_imports)]
43+
% if not property.derived_from:
44+
use cssparser::Parser;
45+
use parser::ParserContext;
46+
use properties::{CSSWideKeyword, DeclaredValue, Shorthand};
47+
% endif
48+
use error_reporting::ParseErrorReporter;
49+
use properties::longhands;
50+
use properties::property_bit_field::PropertyBitField;
51+
use properties::{ComputedValues, ServoComputedValues, PropertyDeclaration};
52+
use properties::style_struct_traits::${data.current_style_struct.trait_name};
53+
use properties::style_structs;
54+
use std::boxed::Box as StdBox;
55+
use std::collections::HashMap;
56+
use std::sync::Arc;
57+
use values::computed::{TContext, ToComputedValue};
58+
use values::{computed, specified};
59+
use string_cache::Atom;
60+
${caller.body()}
61+
#[allow(unused_variables)]
62+
pub fn cascade_property<C: ComputedValues>(
63+
declaration: &PropertyDeclaration,
64+
inherited_style: &C,
65+
context: &mut computed::Context<C>,
66+
seen: &mut PropertyBitField,
67+
cacheable: &mut bool,
68+
error_reporter: &mut StdBox<ParseErrorReporter + Send>) {
69+
let declared_value = match *declaration {
70+
PropertyDeclaration::${property.camel_case}(ref declared_value) => {
71+
declared_value
72+
}
73+
_ => panic!("entered the wrong cascade_property() implementation"),
74+
};
75+
% if not property.derived_from:
76+
if seen.get_${property.ident}() {
77+
return
78+
}
79+
seen.set_${property.ident}();
80+
{
81+
let custom_props = context.style().custom_properties();
82+
::properties::substitute_variables_${property.ident}(
83+
declared_value, &custom_props, |value| match *value {
84+
DeclaredValue::Value(ref specified_value) => {
85+
let computed = specified_value.to_computed_value(context);
86+
context.mutate_style().mutate_${data.current_style_struct.trait_name_lower}()
87+
.set_${property.ident}(computed);
88+
}
89+
DeclaredValue::WithVariables { .. } => unreachable!(),
90+
DeclaredValue::Initial => {
91+
// We assume that it's faster to use copy_*_from rather than
92+
// set_*(get_initial_value());
93+
let initial_struct = C::initial_values()
94+
.get_${data.current_style_struct.trait_name_lower}();
95+
context.mutate_style().mutate_${data.current_style_struct.trait_name_lower}()
96+
.copy_${property.ident}_from(initial_struct);
97+
},
98+
DeclaredValue::Inherit => {
99+
// This is a bit slow, but this is rare so it shouldn't
100+
// matter.
101+
//
102+
// FIXME: is it still?
103+
*cacheable = false;
104+
let inherited_struct =
105+
inherited_style.get_${data.current_style_struct.trait_name_lower}();
106+
context.mutate_style().mutate_${data.current_style_struct.trait_name_lower}()
107+
.copy_${property.ident}_from(inherited_struct);
108+
}
109+
}, error_reporter
110+
);
111+
}
112+
113+
% if property.custom_cascade:
114+
cascade_property_custom(declaration,
115+
inherited_style,
116+
context,
117+
seen,
118+
cacheable,
119+
error_reporter);
120+
% endif
121+
% else:
122+
// Do not allow stylesheets to set derived properties.
123+
% endif
124+
}
125+
% if not property.derived_from:
126+
pub fn parse_declared(context: &ParserContext, input: &mut Parser)
127+
-> Result<DeclaredValue<SpecifiedValue>, ()> {
128+
match input.try(CSSWideKeyword::parse) {
129+
Ok(CSSWideKeyword::InheritKeyword) => Ok(DeclaredValue::Inherit),
130+
Ok(CSSWideKeyword::InitialKeyword) => Ok(DeclaredValue::Initial),
131+
Ok(CSSWideKeyword::UnsetKeyword) => Ok(DeclaredValue::${
132+
"Inherit" if data.current_style_struct.inherited else "Initial"}),
133+
Err(()) => {
134+
input.look_for_var_functions();
135+
let start = input.position();
136+
let specified = parse_specified(context, input);
137+
if specified.is_err() {
138+
while let Ok(_) = input.next() {} // Look for var() after the error.
139+
}
140+
let var = input.seen_var_functions();
141+
if specified.is_err() && var {
142+
input.reset(start);
143+
let (first_token_type, css) = try!(
144+
::custom_properties::parse_non_custom_with_var(input));
145+
return Ok(DeclaredValue::WithVariables {
146+
css: css.into_owned(),
147+
first_token_type: first_token_type,
148+
base_url: context.base_url.clone(),
149+
from_shorthand: None,
150+
})
151+
}
152+
specified
153+
}
154+
}
155+
}
156+
% endif
157+
}
158+
</%def>
159+
160+
<%def name="single_keyword(name, values, **kwargs)">
161+
<%call expr="single_keyword_computed(name, values, **kwargs)">
162+
use values::computed::ComputedValueAsSpecified;
163+
impl ComputedValueAsSpecified for SpecifiedValue {}
164+
</%call>
165+
</%def>
166+
167+
<%def name="single_keyword_computed(name, values, **kwargs)">
168+
<%
169+
keyword_kwargs = {a: kwargs.pop(a, None) for a in [
170+
'gecko_constant_prefix', 'extra_gecko_values', 'extra_servo_values'
171+
]}
172+
%>
173+
<%call expr="longhand(name, keyword=Keyword(name, values.split(), **keyword_kwargs), **kwargs)">
174+
pub use self::computed_value::T as SpecifiedValue;
175+
${caller.body()}
176+
pub mod computed_value {
177+
define_css_keyword_enum! { T:
178+
% for value in data.longhands_by_name[name].keyword.values_for(product):
179+
"${value}" => ${to_rust_ident(value)},
180+
% endfor
181+
}
182+
}
183+
#[inline] pub fn get_initial_value() -> computed_value::T {
184+
computed_value::T::${to_rust_ident(values.split()[0])}
185+
}
186+
pub fn parse(_context: &ParserContext, input: &mut Parser)
187+
-> Result<SpecifiedValue, ()> {
188+
computed_value::T::parse(input)
189+
}
190+
</%call>
191+
</%def>

components/style/properties/longhand/border.mako.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
<%page args="data, helpers" />
5+
<%namespace name="helpers" file="/helpers.mako.rs" />
6+
7+
<% from data import Method %>
68

79
<% data.new_style_struct("Border", inherited=False, gecko_ffi_name="nsStyleBorder",
8-
additional_methods=[data.new_method("border_" + side + "_is_none_or_hidden_and_has_nonzero_width",
9-
"bool") for side in ["top", "right", "bottom", "left"]]) %>
10+
additional_methods=[Method("border_" + side + "_is_none_or_hidden_and_has_nonzero_width",
11+
"bool") for side in ["top", "right", "bottom", "left"]]) %>
1012

1113
% for side in ["top", "right", "bottom", "left"]:
1214
${helpers.predefined_type("border-%s-color" % side, "CSSColor", "::cssparser::Color::CurrentColor")}

components/style/properties/longhand/box.mako.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
<%page args="data, helpers" />
6-
7-
<% data.new_style_struct("Box", inherited=False, gecko_ffi_name="nsStyleDisplay",
8-
additional_methods=[data.new_method("clone_display",
9-
"longhands::display::computed_value::T"),
10-
data.new_method("clone_position",
11-
"longhands::position::computed_value::T"),
12-
data.new_method("is_floated", "bool"),
13-
data.new_method("overflow_x_is_visible", "bool"),
14-
data.new_method("overflow_y_is_visible", "bool"),
15-
data.new_method("transition_count", "usize")]) %>
5+
<%namespace name="helpers" file="/helpers.mako.rs" />
6+
7+
<% from data import Method, to_rust_ident %>
8+
9+
<% data.new_style_struct("Box",
10+
inherited=False,
11+
gecko_ffi_name="nsStyleDisplay",
12+
additional_methods=[Method("clone_display", "longhands::display::computed_value::T"),
13+
Method("clone_position", "longhands::position::computed_value::T"),
14+
Method("is_floated", "bool"),
15+
Method("overflow_x_is_visible", "bool"),
16+
Method("overflow_y_is_visible", "bool"),
17+
Method("transition_count", "usize")]) %>
1618

1719
// TODO(SimonSapin): don't parse `inline-table`, since we don't support it
1820
<%helpers:longhand name="display" custom_cascade="${product == 'servo'}">
@@ -34,7 +36,7 @@
3436
#[derive(Deserialize, Serialize)]
3537
pub enum T {
3638
% for value in values:
37-
${data.to_rust_ident(value)},
39+
${to_rust_ident(value)},
3840
% endfor
3941
}
4042

@@ -43,14 +45,14 @@
4345
where W: ::std::fmt::Write {
4446
match *self {
4547
% for value in values:
46-
T::${data.to_rust_ident(value)} => dest.write_str("${value}"),
48+
T::${to_rust_ident(value)} => dest.write_str("${value}"),
4749
% endfor
4850
}
4951
}
5052
}
5153
}
5254
#[inline] pub fn get_initial_value() -> computed_value::T {
53-
computed_value::T::${data.to_rust_ident(values[0])}
55+
computed_value::T::${to_rust_ident(values[0])}
5456
}
5557
pub fn parse(_context: &ParserContext, input: &mut Parser)
5658
-> Result<SpecifiedValue, ()> {
@@ -63,7 +65,7 @@
6365
return Err(())
6466
}
6567
% endif
66-
Ok(computed_value::T::${data.to_rust_ident(value)})
68+
Ok(computed_value::T::${to_rust_ident(value)})
6769
},
6870
% endfor
6971
_ => Err(())

components/style/properties/longhand/margin.mako.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
<%page args="data, helpers" />
5+
<%namespace name="helpers" file="/helpers.mako.rs" />
66
77
<% data.new_style_struct("Margin", inherited=False, gecko_ffi_name="nsStyleMargin") %>
88

components/style/properties/longhand/outline.mako.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
<%page args="data, helpers" />
5+
<%namespace name="helpers" file="/helpers.mako.rs" />
66

7-
<% data.new_style_struct("Outline", inherited=False, gecko_ffi_name="nsStyleOutline",
8-
additional_methods=[data.new_method("outline_is_none_or_hidden_and_has_nonzero_width", "bool")]) %>
7+
<% from data import Method %>
8+
9+
<% data.new_style_struct("Outline",
10+
inherited=False,
11+
gecko_ffi_name="nsStyleOutline",
12+
additional_methods=[Method("outline_is_none_or_hidden_and_has_nonzero_width", "bool")]) %>
913

1014
// TODO(pcwalton): `invert`
1115
${helpers.predefined_type("outline-color", "CSSColor", "::cssparser::Color::CurrentColor")}

components/style/properties/longhand/padding.mako.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
<%page args="data, helpers" />
5+
<%namespace name="helpers" file="/helpers.mako.rs" />
66
77
<% data.new_style_struct("Padding", inherited=False, gecko_ffi_name="nsStylePadding") %>
88

components/style/properties/longhand/position.mako.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
<%page args="data, helpers" />
5+
<%namespace name="helpers" file="/helpers.mako.rs" />
66

77
<% data.new_style_struct("Position", inherited=False, gecko_ffi_name="nsStylePosition") %>
88

0 commit comments

Comments
 (0)