From 35b8b95263d4f93dd791a44a7f570e2240655fd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sun, 3 Mar 2019 11:31:06 +0000 Subject: [PATCH] style: Implement a version of #[css(skip_if)] that takes more context. I called it contextual_skip_if, though better names welcome. Differential Revision: https://phabricator.services.mozilla.com/D21858 --- components/style_derive/to_css.rs | 22 ++++++++++++++++++++-- components/style_traits/values.rs | 4 ++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/components/style_derive/to_css.rs b/components/style_derive/to_css.rs index 4eabdcb8357..3a1bc9f4c6c 100644 --- a/components/style_derive/to_css.rs +++ b/components/style_derive/to_css.rs @@ -148,12 +148,20 @@ fn derive_variant_fields_expr( } } } + + if let Some(condition) = attrs.contextual_skip_if { + expr = quote! { + if !#condition(#(#bindings),*) { + #expr + } + } + } return expr; } - let mut expr = derive_single_field_expr(first, attrs, where_clause); + let mut expr = derive_single_field_expr(first, attrs, where_clause, bindings); for (binding, attrs) in iter { - derive_single_field_expr(binding, attrs, where_clause).to_tokens(&mut expr) + derive_single_field_expr(binding, attrs, where_clause, bindings).to_tokens(&mut expr) } quote! {{ @@ -167,6 +175,7 @@ fn derive_single_field_expr( field: &BindingInfo, attrs: CssFieldAttrs, where_clause: &mut Option, + bindings: &[BindingInfo], ) -> TokenStream { let mut expr = if attrs.iterable { if let Some(if_empty) = attrs.if_empty { @@ -216,6 +225,14 @@ fn derive_single_field_expr( } } + if let Some(condition) = attrs.contextual_skip_if { + expr = quote! { + if !#condition(#(#bindings),*) { + #expr + } + } + } + expr } @@ -249,5 +266,6 @@ pub struct CssFieldAttrs { pub iterable: bool, pub skip: bool, pub represents_keyword: bool, + pub contextual_skip_if: Option, pub skip_if: Option, } diff --git a/components/style_traits/values.rs b/components/style_traits/values.rs index 582e34eb3db..46751541319 100644 --- a/components/style_traits/values.rs +++ b/components/style_traits/values.rs @@ -37,6 +37,10 @@ use std::fmt::{self, Write}; /// * if `#[css(skip_if = "function")]` is found on a field, the `ToCss` call /// for that field is skipped if `function` returns true. This function is /// provided the field as an argument; +/// * if `#[css(contextual_skip_if = "function")]` is found on a field, the +/// `ToCss` call for that field is skipped if `function` returns true. This +/// function is given all the fields in the current struct or variant as an +/// argument; /// * `#[css(represents_keyword)]` can be used on bool fields in order to /// serialize the field name if the field is true, or nothing otherwise. It /// also collects those keywords for `SpecifiedValueInfo`.