Skip to content

Commit 90bae7f

Browse files
committed
Derive HasViewportPercentage 🍷
1 parent d1e31f7 commit 90bae7f

27 files changed

+202
-452
lines changed

Cargo.lock

Lines changed: 15 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/style/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ serde_derive = {version = "0.9", optional = true}
5858
servo_atoms = {path = "../atoms", optional = true}
5959
servo_config = {path = "../config", optional = true}
6060
smallvec = "0.3.3"
61+
style_derive = {path = "../style_derive"}
6162
style_traits = {path = "../style_traits"}
6263
servo_url = {path = "../url", optional = true}
6364
time = "0.1"

components/style/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ extern crate selectors;
7878
#[cfg(feature = "servo")] extern crate servo_url;
7979
extern crate smallvec;
8080
#[macro_use]
81+
extern crate style_derive;
82+
#[macro_use]
8183
extern crate style_traits;
8284
extern crate time;
8385
#[allow(unused_extern_crates)]

components/style/macros.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,6 @@ macro_rules! define_numbered_css_keyword_enum {
3838
}
3939
}
4040

41-
/// A macro used to implement HasViewportPercentage trait
42-
/// for a given type that may never contain viewport units.
43-
macro_rules! no_viewport_percentage {
44-
($name: ident) => {
45-
impl $crate::values::HasViewportPercentage for $name {
46-
#[inline]
47-
fn has_viewport_percentage(&self) -> bool {
48-
false
49-
}
50-
}
51-
};
52-
}
53-
5441
/// A macro for implementing `ComputedValueAsSpecified`, `Parse`
5542
/// and `HasViewportPercentage` traits for the enums defined
5643
/// using `define_css_keyword_enum` macro.

components/style/properties/helpers.mako.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,6 @@
8181
use values::HasViewportPercentage;
8282
use style_traits::ToCss;
8383

84-
impl HasViewportPercentage for SpecifiedValue {
85-
fn has_viewport_percentage(&self) -> bool {
86-
let &SpecifiedValue(ref vec) = self;
87-
vec.iter().any(|ref x| x.has_viewport_percentage())
88-
}
89-
}
90-
9184
pub mod single_value {
9285
use cssparser::Parser;
9386
use parser::{Parse, ParserContext};
@@ -168,7 +161,7 @@
168161
}
169162

170163
/// The specified value of ${name}.
171-
#[derive(Debug, Clone, PartialEq)]
164+
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
172165
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
173166
pub struct SpecifiedValue(pub Vec<single_value::SpecifiedValue>);
174167

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

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,7 @@ ${helpers.single_keyword("background-origin",
236236
}
237237
}
238238

239-
impl HasViewportPercentage for ExplicitSize {
240-
fn has_viewport_percentage(&self) -> bool {
241-
return self.width.has_viewport_percentage() || self.height.has_viewport_percentage();
242-
}
243-
}
244-
245-
#[derive(Clone, PartialEq, Debug)]
239+
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
246240
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
247241
#[allow(missing_docs)]
248242
pub struct ExplicitSize {
@@ -266,16 +260,7 @@ ${helpers.single_keyword("background-origin",
266260
}
267261
}
268262

269-
impl HasViewportPercentage for SpecifiedValue {
270-
fn has_viewport_percentage(&self) -> bool {
271-
match *self {
272-
SpecifiedValue::Explicit(ref explicit_size) => explicit_size.has_viewport_percentage(),
273-
_ => false
274-
}
275-
}
276-
}
277-
278-
#[derive(Clone, PartialEq, Debug)]
263+
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
279264
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
280265
pub enum SpecifiedValue {
281266
Explicit(ExplicitSize),

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

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -208,17 +208,6 @@ ${helpers.predefined_type("border-image-source", "ImageLayer",
208208
use values::HasViewportPercentage;
209209
use values::specified::{LengthOrNumber, Number};
210210

211-
impl HasViewportPercentage for SpecifiedValue {
212-
fn has_viewport_percentage(&self) -> bool {
213-
let mut viewport_percentage = false;
214-
for value in self.0.iter() {
215-
let vp = value.has_viewport_percentage();
216-
viewport_percentage = vp || viewport_percentage;
217-
}
218-
viewport_percentage
219-
}
220-
}
221-
222211
pub mod computed_value {
223212
use values::computed::LengthOrNumber;
224213
#[derive(Debug, Clone, PartialEq)]
@@ -227,7 +216,7 @@ ${helpers.predefined_type("border-image-source", "ImageLayer",
227216
pub LengthOrNumber, pub LengthOrNumber);
228217
}
229218

230-
#[derive(Debug, Clone, PartialEq)]
219+
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
231220
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
232221
pub struct SpecifiedValue(pub Vec<LengthOrNumber>);
233222

@@ -402,20 +391,6 @@ ${helpers.predefined_type("border-image-source", "ImageLayer",
402391
use values::HasViewportPercentage;
403392
use values::specified::{LengthOrPercentage, Number};
404393

405-
impl HasViewportPercentage for SpecifiedValue {
406-
fn has_viewport_percentage(&self) -> bool {
407-
let mut viewport_percentage = false;
408-
for value in self.0.clone() {
409-
let vp = match value {
410-
SingleSpecifiedValue::LengthOrPercentage(len) => len.has_viewport_percentage(),
411-
_ => false,
412-
};
413-
viewport_percentage = vp || viewport_percentage;
414-
}
415-
viewport_percentage
416-
}
417-
}
418-
419394
pub mod computed_value {
420395
use values::computed::{LengthOrPercentage, Number};
421396
#[derive(Debug, Clone, PartialEq)]
@@ -432,7 +407,7 @@ ${helpers.predefined_type("border-image-source", "ImageLayer",
432407
}
433408
}
434409

435-
#[derive(Debug, Clone, PartialEq)]
410+
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
436411
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
437412
pub struct SpecifiedValue(pub Vec<SingleSpecifiedValue>);
438413

@@ -458,7 +433,7 @@ ${helpers.predefined_type("border-image-source", "ImageLayer",
458433
}
459434
}
460435

461-
#[derive(Debug, Clone, PartialEq)]
436+
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
462437
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
463438
pub enum SingleSpecifiedValue {
464439
LengthOrPercentage(LengthOrPercentage),

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

Lines changed: 5 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -274,18 +274,9 @@ ${helpers.single_keyword("position", "static absolute relative fixed",
274274

275275
${helpers.gecko_keyword_conversion(vertical_align.keyword)}
276276

277-
impl HasViewportPercentage for SpecifiedValue {
278-
fn has_viewport_percentage(&self) -> bool {
279-
match *self {
280-
SpecifiedValue::LengthOrPercentage(ref length) => length.has_viewport_percentage(),
281-
_ => false
282-
}
283-
}
284-
}
285-
286277
/// The `vertical-align` value.
287278
#[allow(non_camel_case_types)]
288-
#[derive(Debug, Clone, PartialEq)]
279+
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
289280
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
290281
pub enum SpecifiedValue {
291282
% for keyword in vertical_align_keywords:
@@ -1048,15 +1039,6 @@ ${helpers.single_keyword("animation-fill-mode",
10481039
use values::HasViewportPercentage;
10491040
use values::specified::LengthOrPercentage;
10501041

1051-
impl HasViewportPercentage for SpecifiedValue {
1052-
fn has_viewport_percentage(&self) -> bool {
1053-
match *self {
1054-
SpecifiedValue::Repeat(ref length) => length.has_viewport_percentage(),
1055-
_ => false
1056-
}
1057-
}
1058-
}
1059-
10601042
pub mod computed_value {
10611043
use values::computed::LengthOrPercentage;
10621044

@@ -1065,7 +1047,7 @@ ${helpers.single_keyword("animation-fill-mode",
10651047
pub struct T(pub Option<LengthOrPercentage>);
10661048
}
10671049

1068-
#[derive(Debug, Clone, PartialEq)]
1050+
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
10691051
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
10701052
pub enum SpecifiedValue {
10711053
None,
@@ -1253,7 +1235,7 @@ ${helpers.predefined_type("scroll-snap-coordinate",
12531235
/// Multiple transform functions compose a transformation.
12541236
///
12551237
/// Some transformations can be expressed by other more general functions.
1256-
#[derive(Clone, Debug, PartialEq)]
1238+
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
12571239
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
12581240
pub enum SpecifiedOperation {
12591241
/// Represents a 2D 2x3 matrix.
@@ -1328,41 +1310,6 @@ ${helpers.predefined_type("scroll-snap-coordinate",
13281310
}
13291311
}
13301312

1331-
impl HasViewportPercentage for SpecifiedOperation {
1332-
fn has_viewport_percentage(&self) -> bool {
1333-
match *self {
1334-
SpecifiedOperation::Translate(ref l1, None) |
1335-
SpecifiedOperation::TranslateX(ref l1) |
1336-
SpecifiedOperation::TranslateY(ref l1) => {
1337-
l1.has_viewport_percentage()
1338-
}
1339-
SpecifiedOperation::TranslateZ(ref l1) => {
1340-
l1.has_viewport_percentage()
1341-
}
1342-
SpecifiedOperation::Translate(ref l1, Some(ref l2)) => {
1343-
l1.has_viewport_percentage() ||
1344-
l2.has_viewport_percentage()
1345-
}
1346-
SpecifiedOperation::Translate3D(ref l1, ref l2, ref l3) => {
1347-
l1.has_viewport_percentage() ||
1348-
l2.has_viewport_percentage() ||
1349-
l3.has_viewport_percentage()
1350-
},
1351-
SpecifiedOperation::Perspective(ref length) => length.has_viewport_percentage(),
1352-
SpecifiedOperation::PrefixedMatrix{ ref e, ref f, .. } => {
1353-
e.has_viewport_percentage() ||
1354-
f.has_viewport_percentage()
1355-
},
1356-
SpecifiedOperation::PrefixedMatrix3D{ ref m41, ref m42, ref m43, .. } => {
1357-
m41.has_viewport_percentage() ||
1358-
m42.has_viewport_percentage() ||
1359-
m43.has_viewport_percentage()
1360-
},
1361-
_ => false
1362-
}
1363-
}
1364-
}
1365-
13661313
impl ToCss for SpecifiedOperation {
13671314
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
13681315
use self::SpecifiedOperation::*;
@@ -1422,14 +1369,7 @@ ${helpers.predefined_type("scroll-snap-coordinate",
14221369
}
14231370
}
14241371

1425-
impl HasViewportPercentage for SpecifiedValue {
1426-
fn has_viewport_percentage(&self) -> bool {
1427-
let &SpecifiedValue(ref specified_ops) = self;
1428-
specified_ops.iter().any(|ref x| x.has_viewport_percentage())
1429-
}
1430-
}
1431-
1432-
#[derive(Clone, Debug, PartialEq)]
1372+
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
14331373
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
14341374
pub struct SpecifiedValue(Vec<SpecifiedOperation>);
14351375

@@ -2223,15 +2163,7 @@ ${helpers.single_keyword("transform-style",
22232163
}
22242164
}
22252165

2226-
impl HasViewportPercentage for SpecifiedValue {
2227-
fn has_viewport_percentage(&self) -> bool {
2228-
self.horizontal.has_viewport_percentage() ||
2229-
self.vertical.has_viewport_percentage() ||
2230-
self.depth.has_viewport_percentage()
2231-
}
2232-
}
2233-
2234-
#[derive(Clone, Debug, PartialEq)]
2166+
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
22352167
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
22362168
pub struct SpecifiedValue {
22372169
horizontal: LengthOrPercentage,

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,7 @@ ${helpers.predefined_type("clip",
9898
use values::specified::{Angle, CSSColor, Length, Shadow};
9999
use values::specified::url::SpecifiedUrl;
100100

101-
impl HasViewportPercentage for SpecifiedValue {
102-
fn has_viewport_percentage(&self) -> bool {
103-
self.0.iter().any(|ref x| x.has_viewport_percentage())
104-
}
105-
}
106-
107-
#[derive(Debug, Clone, PartialEq)]
101+
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
108102
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
109103
pub struct SpecifiedValue(pub Vec<SpecifiedFilter>);
110104

0 commit comments

Comments
 (0)