|
| 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> |
0 commit comments